Handle offline migrations and MySQL/PostgreSQL booleans

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

View File

@ -26,20 +26,15 @@ backup_table_name = 'conversations_4f02b6704509_bak'
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
# Create backup table for soft-deleted conversations
bind = op.get_bind()
dialect = bind.dialect.name
context = op.get_context()
dialect = context.dialect.name if hasattr(context, "dialect") else op.get_bind().dialect.name
true_literal = "1" if dialect == "mysql" else "TRUE"
# Check if there are any soft-deleted conversations
result = bind.execute(sa.text(f"SELECT COUNT(*) FROM conversations WHERE is_deleted = {true_literal}"))
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_literal}
"""))
# Create backup table with all soft-deleted conversations (works even if zero rows)
op.execute(sa.text(f"""
CREATE TABLE {backup_table_name} AS
SELECT * FROM conversations WHERE is_deleted = {true_literal}
"""))
# Delete soft-deleted conversations from main table
op.execute(sa.text(f"DELETE FROM conversations WHERE is_deleted = {true_literal}"))
@ -59,6 +54,9 @@ def downgrade():
# Check if backup table exists using inspector (works for all database types)
bind = op.get_bind()
if bind is None:
# Offline migration generation; skip data restoration logic
return
dialect = bind.dialect.name
true_literal = "1" if dialect == "mysql" else "TRUE"
inspector = inspect(bind)