diff --git a/api/migrations/versions/2025_09_02_2012-4f02b6704509_remove_unused_is_deleted_field_from_.py b/api/migrations/versions/2025_09_02_2012-4f02b6704509_remove_unused_is_deleted_field_from_.py index e39cdf50d4..55d7a1ed5e 100644 --- a/api/migrations/versions/2025_09_02_2012-4f02b6704509_remove_unused_is_deleted_field_from_.py +++ b/api/migrations/versions/2025_09_02_2012-4f02b6704509_remove_unused_is_deleted_field_from_.py @@ -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)