refac
This commit is contained in:
1 parent
6f8221df58
commit
655afbe90b
1 file changed
+13
-9
@@ -49,7 +49,7 @@ def upgrade():
|
|||||||
|
|
||||||
# Step 3: Migrate data from 'old_chat' to 'chat' (only if old_chat exists)
|
# Step 3: Migrate data from 'old_chat' to 'chat' (only if old_chat exists)
|
||||||
# Re-check columns after potential rename above
|
# Re-check columns after potential rename above
|
||||||
current_cols = {c['name'] for c in inspector.get_columns('chat')}
|
current_cols = {c['name'] for c in sa.inspect(conn).get_columns('chat')}
|
||||||
if 'old_chat' in current_cols:
|
if 'old_chat' in current_cols:
|
||||||
chat_table = table(
|
chat_table = table(
|
||||||
'chat',
|
'chat',
|
||||||
@@ -76,8 +76,12 @@ def upgrade():
|
|||||||
|
|
||||||
|
|
||||||
def downgrade():
|
def downgrade():
|
||||||
|
conn = op.get_bind()
|
||||||
|
columns = {col['name'] for col in sa.inspect(conn).get_columns('chat')}
|
||||||
|
|
||||||
# Step 1: Add 'old_chat' column back as Text
|
# Step 1: Add 'old_chat' column back as Text
|
||||||
op.add_column('chat', sa.Column('old_chat', sa.Text(), nullable=True))
|
if 'old_chat' not in columns:
|
||||||
|
op.add_column('chat', sa.Column('old_chat', sa.Text(), nullable=True))
|
||||||
|
|
||||||
# Step 2: Convert 'chat' JSON data back to text and store in 'old_chat'
|
# Step 2: Convert 'chat' JSON data back to text and store in 'old_chat'
|
||||||
chat_table = table(
|
chat_table = table(
|
||||||
@@ -87,14 +91,14 @@ def downgrade():
|
|||||||
sa.Column('old_chat', sa.Text()),
|
sa.Column('old_chat', sa.Text()),
|
||||||
)
|
)
|
||||||
|
|
||||||
connection = op.get_bind()
|
if 'chat' in columns:
|
||||||
results = connection.execute(select(chat_table.c.id, chat_table.c.chat))
|
results = conn.execute(select(chat_table.c.id, chat_table.c.chat))
|
||||||
for row in results:
|
for row in results:
|
||||||
text_data = json.dumps(row.chat) if row.chat is not None else None
|
text_data = json.dumps(row.chat) if row.chat is not None else None
|
||||||
connection.execute(sa.update(chat_table).where(chat_table.c.id == row.id).values(old_chat=text_data))
|
conn.execute(sa.update(chat_table).where(chat_table.c.id == row.id).values(old_chat=text_data))
|
||||||
|
|
||||||
# Step 3: Remove the new 'chat' JSON column
|
# Step 3: Remove the new 'chat' JSON column
|
||||||
op.drop_column('chat', 'chat')
|
op.drop_column('chat', 'chat')
|
||||||
|
|
||||||
# Step 4: Rename 'old_chat' back to 'chat'
|
# Step 4: Rename 'old_chat' back to 'chat'
|
||||||
op.alter_column('chat', 'old_chat', new_column_name='chat', existing_type=sa.Text())
|
op.alter_column('chat', 'old_chat', new_column_name='chat', existing_type=sa.Text())
|
||||||
Reference in new issue
Block a user