mirror of https://github.com/langgenius/dify.git
Simplify is_deleted migration: drop column without backup
This commit is contained in:
parent
5efe443d85
commit
4ad79538e2
|
|
@ -4,13 +4,11 @@ Revision ID: 4f02b6704509
|
|||
Revises: 7bb281b7a422
|
||||
Create Date: 2025-09-02 20:12:37.311318
|
||||
|
||||
This migration runs on both PostgreSQL and MySQL. It uses SQLAlchemy
|
||||
expressions instead of raw SQL so dialects handle boolean literals.
|
||||
This migration runs on both PostgreSQL and MySQL.
|
||||
"""
|
||||
from alembic import op
|
||||
import models as models
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy import inspect
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
|
|
@ -19,35 +17,8 @@ down_revision = '7bb281b7a422'
|
|||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
# Backup table name for soft-deleted conversations
|
||||
backup_table_name = 'conversations_4f02b6704509_bak'
|
||||
|
||||
|
||||
def upgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
bind = op.get_bind()
|
||||
metadata = sa.MetaData()
|
||||
conversations = sa.Table('conversations', metadata, autoload_with=bind)
|
||||
|
||||
# Drop leftover backup table if it exists (idempotent reruns)
|
||||
inspector = inspect(bind)
|
||||
if backup_table_name in inspector.get_table_names():
|
||||
op.drop_table(backup_table_name)
|
||||
|
||||
# Create backup table with identical schema
|
||||
op.create_table(backup_table_name, *[col.copy() for col in conversations.columns])
|
||||
backup_table = sa.Table(backup_table_name, metadata, autoload_with=bind)
|
||||
|
||||
# Copy soft-deleted rows into backup
|
||||
insert_backup = sa.insert(backup_table).from_select(
|
||||
conversations.columns.keys(),
|
||||
sa.select(*conversations.c).where(conversations.c.is_deleted.is_(sa.true())),
|
||||
)
|
||||
bind.execute(insert_backup)
|
||||
|
||||
# Delete soft-deleted rows from main table
|
||||
bind.execute(sa.delete(conversations).where(conversations.c.is_deleted.is_(sa.true())))
|
||||
|
||||
with op.batch_alter_table('conversations', schema=None) as batch_op:
|
||||
batch_op.drop_column('is_deleted')
|
||||
|
||||
|
|
@ -59,21 +30,4 @@ def downgrade():
|
|||
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
|
||||
bind = op.get_bind()
|
||||
metadata = sa.MetaData()
|
||||
inspector = inspect(bind)
|
||||
|
||||
if backup_table_name in inspector.get_table_names():
|
||||
conversations = sa.Table('conversations', metadata, autoload_with=bind)
|
||||
backup_table = sa.Table(backup_table_name, metadata, autoload_with=bind)
|
||||
|
||||
restore_stmt = sa.insert(conversations).from_select(
|
||||
conversations.columns.keys(),
|
||||
sa.select(*backup_table.c),
|
||||
)
|
||||
bind.execute(restore_stmt)
|
||||
|
||||
op.drop_table(backup_table_name)
|
||||
|
||||
# ### end Alembic commands ###
|
||||
|
|
|
|||
Loading…
Reference in New Issue