Fix migration down_revision after merge

This commit is contained in:
-LAN- 2025-11-27 17:12:18 +08:00
parent 62f2288460
commit ab142a302e
No known key found for this signature in database
GPG Key ID: 6BA0D108DED011FF
1 changed files with 10 additions and 10 deletions

View File

@ -13,7 +13,7 @@ from sqlalchemy import inspect
# revision identifiers, used by Alembic.
revision = '4f02b6704509'
down_revision = 'b95962a3885c'
down_revision = '7bb281b7a422'
branch_labels = None
depends_on = None
@ -24,21 +24,21 @@ backup_table_name = 'conversations_4f02b6704509_bak'
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
# Create backup table for soft-deleted conversations
# Check if there are any soft-deleted conversations
result = op.get_bind().execute(sa.text("SELECT COUNT(*) FROM conversations WHERE is_deleted = true"))
count = result.scalar()
if count > 0:
# Create backup table with all columns from conversations
op.execute(sa.text(f"""
CREATE TABLE {backup_table_name} AS
SELECT * FROM conversations WHERE is_deleted = true
"""))
# Delete soft-deleted conversations from main table
op.execute("DELETE FROM conversations WHERE is_deleted = true")
with op.batch_alter_table('conversations', schema=None) as batch_op:
batch_op.drop_column('is_deleted')
@ -49,21 +49,21 @@ def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('conversations', schema=None) as batch_op:
batch_op.add_column(sa.Column('is_deleted', sa.BOOLEAN(), server_default=sa.text('false'), autoincrement=False, nullable=False))
# Restore soft-deleted conversations from backup table if it exists
# Check if backup table exists using inspector (works for all database types)
bind = op.get_bind()
inspector = inspect(bind)
existing_tables = inspector.get_table_names()
if backup_table_name in existing_tables:
# Restore the soft-deleted conversations
op.execute(sa.text(f"""
INSERT INTO conversations
INSERT INTO conversations
SELECT * FROM {backup_table_name}
"""))
# Drop the backup table after restoration
op.execute(sa.text(f"DROP TABLE {backup_table_name}"))