添加意见反馈
This commit is contained in:
1 parent
2bd27ed70d
commit
6b2ab65436
16 files changed
+853
-37
No files matched your search
@@ -0,0 +1,128 @@
|
||||
"""add chat_log_id to general_feedbacks
|
||||
|
||||
Revision ID: 1c2d3e4f5a6b
|
||||
Revises: f2b3c4d5e6f7
|
||||
Create Date: 2026-04-23 16:25:00.000000
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = '1c2d3e4f5a6b'
|
||||
down_revision = 'f2b3c4d5e6f7'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def _normalize_chat_logs_id_postgresql(bind):
|
||||
"""
|
||||
Ensure chat_logs.id is unique before adding PK.
|
||||
Strategy:
|
||||
- Keep the first row for each duplicated id unchanged.
|
||||
- Reassign subsequent duplicated rows to fresh ids > current max(id).
|
||||
"""
|
||||
# fast path: already unique
|
||||
dup_cnt = bind.execute(sa.text(
|
||||
"SELECT COUNT(*) FROM (SELECT id FROM chat_logs GROUP BY id HAVING COUNT(*) > 1) t"
|
||||
)).scalar() or 0
|
||||
if int(dup_cnt) == 0:
|
||||
return
|
||||
|
||||
# Reassign duplicated rows (rn > 1) with new ids.
|
||||
bind.execute(sa.text("""
|
||||
WITH ordered AS (
|
||||
SELECT
|
||||
ctid,
|
||||
id,
|
||||
ROW_NUMBER() OVER (
|
||||
PARTITION BY id
|
||||
ORDER BY created_at NULLS LAST, ctid
|
||||
) AS rn
|
||||
FROM chat_logs
|
||||
),
|
||||
dups AS (
|
||||
SELECT
|
||||
ctid,
|
||||
id,
|
||||
ROW_NUMBER() OVER (ORDER BY id, ctid) AS dup_seq
|
||||
FROM ordered
|
||||
WHERE rn > 1
|
||||
),
|
||||
max_id AS (
|
||||
SELECT COALESCE(MAX(id), 0) AS m FROM chat_logs
|
||||
)
|
||||
UPDATE chat_logs t
|
||||
SET id = (SELECT m FROM max_id) + dups.dup_seq
|
||||
FROM dups
|
||||
WHERE t.ctid = dups.ctid
|
||||
"""))
|
||||
|
||||
|
||||
def _ensure_chat_logs_id_unique(bind):
|
||||
inspector = sa.inspect(bind)
|
||||
tables = inspector.get_table_names()
|
||||
if 'chat_logs' not in tables:
|
||||
return
|
||||
|
||||
# 1) If id is already the PK, we're good.
|
||||
pk = inspector.get_pk_constraint('chat_logs') or {}
|
||||
constrained = pk.get('constrained_columns') or []
|
||||
if 'id' in constrained:
|
||||
return
|
||||
|
||||
# 2) If id has a UNIQUE constraint, that's also enough for FK target.
|
||||
for uq in inspector.get_unique_constraints('chat_logs') or []:
|
||||
cols = uq.get('column_names') or []
|
||||
if cols == ['id'] or 'id' in cols:
|
||||
return
|
||||
|
||||
# 3) Fallback: add primary key on id.
|
||||
if bind.dialect.name == 'sqlite':
|
||||
# SQLite cannot easily add PK via ALTER TABLE.
|
||||
return
|
||||
|
||||
if bind.dialect.name == 'postgresql':
|
||||
_normalize_chat_logs_id_postgresql(bind)
|
||||
op.execute('ALTER TABLE chat_logs ADD PRIMARY KEY (id)')
|
||||
|
||||
# If chat_logs.id uses a sequence default, align sequence to MAX(id).
|
||||
op.execute("""
|
||||
DO $$
|
||||
DECLARE
|
||||
seq_name text;
|
||||
BEGIN
|
||||
SELECT pg_get_serial_sequence('chat_logs', 'id') INTO seq_name;
|
||||
IF seq_name IS NOT NULL THEN
|
||||
EXECUTE format(
|
||||
'SELECT setval(%L, COALESCE((SELECT MAX(id) FROM chat_logs), 0), true)',
|
||||
seq_name
|
||||
);
|
||||
END IF;
|
||||
END $$;
|
||||
""")
|
||||
return
|
||||
|
||||
op.create_primary_key('pk_chat_logs', 'chat_logs', ['id'])
|
||||
|
||||
|
||||
def upgrade():
|
||||
bind = op.get_bind()
|
||||
_ensure_chat_logs_id_unique(bind)
|
||||
|
||||
op.add_column('general_feedbacks', sa.Column('chat_log_id', sa.Integer(), nullable=True))
|
||||
op.create_index('ix_general_feedbacks_chat_log_id', 'general_feedbacks', ['chat_log_id'])
|
||||
op.create_foreign_key(
|
||||
'fk_general_feedbacks_chat_log_id_chat_logs',
|
||||
'general_feedbacks',
|
||||
'chat_logs',
|
||||
['chat_log_id'],
|
||||
['id'],
|
||||
)
|
||||
|
||||
|
||||
def downgrade():
|
||||
op.drop_constraint('fk_general_feedbacks_chat_log_id_chat_logs', 'general_feedbacks', type_='foreignkey')
|
||||
op.drop_index('ix_general_feedbacks_chat_log_id', table_name='general_feedbacks')
|
||||
op.drop_column('general_feedbacks', 'chat_log_id')
|
||||
@@ -0,0 +1,66 @@
|
||||
"""add general feedbacks table
|
||||
|
||||
Revision ID: ab12cd34ef56
|
||||
Revises: 9a2d6b4e8c1f
|
||||
Create Date: 2026-04-22 15:30:00.000000
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = 'ab12cd34ef56'
|
||||
down_revision = '9a2d6b4e8c1f'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def _ensure_users_primary_key(bind):
|
||||
inspector = sa.inspect(bind)
|
||||
tables = inspector.get_table_names()
|
||||
if 'users' not in tables:
|
||||
return
|
||||
|
||||
pk = inspector.get_pk_constraint('users') or {}
|
||||
constrained = pk.get('constrained_columns') or []
|
||||
if 'id' in constrained:
|
||||
return
|
||||
|
||||
if bind.dialect.name == 'sqlite':
|
||||
# SQLite alter table limitations: assume existing schema already has users.id PK.
|
||||
return
|
||||
|
||||
if bind.dialect.name == 'postgresql':
|
||||
op.execute('ALTER TABLE users ADD PRIMARY KEY (id)')
|
||||
return
|
||||
|
||||
# Generic fallback for other dialects.
|
||||
op.create_primary_key('pk_users', 'users', ['id'])
|
||||
|
||||
|
||||
def upgrade():
|
||||
bind = op.get_bind()
|
||||
_ensure_users_primary_key(bind)
|
||||
|
||||
op.create_table(
|
||||
'general_feedbacks',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('user_id', sa.Integer(), nullable=False),
|
||||
sa.Column('username', sa.String(length=50), nullable=False),
|
||||
sa.Column('feedback_type', sa.String(length=32), nullable=False),
|
||||
sa.Column('feedback_module', sa.String(length=32), nullable=False),
|
||||
sa.Column('content', sa.Text(), nullable=False),
|
||||
sa.Column('source_page', sa.String(length=32), nullable=True),
|
||||
sa.Column('created_at', sa.DateTime(), nullable=False),
|
||||
sa.ForeignKeyConstraint(['user_id'], ['users.id']),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
op.create_index('ix_general_feedbacks_user_id', 'general_feedbacks', ['user_id'])
|
||||
op.create_index('ix_general_feedbacks_created_at', 'general_feedbacks', ['created_at'])
|
||||
|
||||
|
||||
def downgrade():
|
||||
op.drop_index('ix_general_feedbacks_created_at', table_name='general_feedbacks')
|
||||
op.drop_index('ix_general_feedbacks_user_id', table_name='general_feedbacks')
|
||||
op.drop_table('general_feedbacks')
|
||||
@@ -0,0 +1,23 @@
|
||||
"""legacy revision bridge
|
||||
|
||||
Revision ID: d4e5f6a7b8c9
|
||||
Revises: c7d2a4b9e1f0
|
||||
Create Date: 2026-04-22 23:30:00.000000
|
||||
|
||||
"""
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = 'd4e5f6a7b8c9'
|
||||
down_revision = 'c7d2a4b9e1f0'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
# Compatibility bridge for environments whose alembic_version
|
||||
# was stamped to a historical, now-missing revision id.
|
||||
pass
|
||||
|
||||
|
||||
def downgrade():
|
||||
pass
|
||||
@@ -0,0 +1,22 @@
|
||||
"""merge heads after legacy bridge
|
||||
|
||||
Revision ID: f2b3c4d5e6f7
|
||||
Revises: ab12cd34ef56, d4e5f6a7b8c9
|
||||
Create Date: 2026-04-22 23:50:00.000000
|
||||
|
||||
"""
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = 'f2b3c4d5e6f7'
|
||||
down_revision = ('ab12cd34ef56', 'd4e5f6a7b8c9')
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
# Merge-only revision to unify multiple heads.
|
||||
pass
|
||||
|
||||
|
||||
def downgrade():
|
||||
pass
|
||||
Reference in new issue
Block a user