mirror of https://github.com/langgenius/dify.git
Make conversation is_deleted migration cross-database safe
This commit is contained in:
parent
ab142a302e
commit
b31b0446dd
|
|
@ -1,9 +1,11 @@
|
|||
"""remove unused is_deleted field from conversations
|
||||
|
||||
Revision ID: 4f02b6704509
|
||||
Revises: b95962a3885c
|
||||
Revises: 7bb281b7a422
|
||||
Create Date: 2025-09-02 20:12:37.311318
|
||||
|
||||
This migration runs on both PostgreSQL and MySQL. Avoid hard‑coding boolean
|
||||
literals so the SQL renders correctly for each dialect.
|
||||
"""
|
||||
from alembic import op
|
||||
import models as models
|
||||
|
|
@ -24,20 +26,23 @@ 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
|
||||
true_literal = "1" if dialect == "mysql" else "TRUE"
|
||||
|
||||
# Check if there are any soft-deleted conversations
|
||||
result = op.get_bind().execute(sa.text("SELECT COUNT(*) FROM conversations WHERE is_deleted = true"))
|
||||
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
|
||||
SELECT * FROM conversations WHERE is_deleted = {true_literal}
|
||||
"""))
|
||||
|
||||
# Delete soft-deleted conversations from main table
|
||||
op.execute("DELETE FROM conversations WHERE is_deleted = true")
|
||||
op.execute(sa.text(f"DELETE FROM conversations WHERE is_deleted = {true_literal}"))
|
||||
|
||||
with op.batch_alter_table('conversations', schema=None) as batch_op:
|
||||
batch_op.drop_column('is_deleted')
|
||||
|
|
@ -54,6 +59,8 @@ def downgrade():
|
|||
|
||||
# Check if backup table exists using inspector (works for all database types)
|
||||
bind = op.get_bind()
|
||||
dialect = bind.dialect.name
|
||||
true_literal = "1" if dialect == "mysql" else "TRUE"
|
||||
inspector = inspect(bind)
|
||||
existing_tables = inspector.get_table_names()
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue