refac
This commit is contained in:
1 parent
1b9d22e324
commit
dc0f8ae6f2
2 files changed
+28
-22
No files matched your search
@@ -93,8 +93,11 @@ def upgrade():
|
|||||||
conn.execute(update_stmt)
|
conn.execute(update_stmt)
|
||||||
|
|
||||||
# Add columns `pinned` and `meta` to 'chat'
|
# Add columns `pinned` and `meta` to 'chat'
|
||||||
op.add_column('chat', sa.Column('pinned', sa.Boolean(), nullable=True))
|
chat_columns = {c['name'] for c in inspector.get_columns('chat')}
|
||||||
op.add_column('chat', sa.Column('meta', sa.JSON(), nullable=False, server_default='{}'))
|
if 'pinned' not in chat_columns:
|
||||||
|
op.add_column('chat', sa.Column('pinned', sa.Boolean(), nullable=True))
|
||||||
|
if 'meta' not in chat_columns:
|
||||||
|
op.add_column('chat', sa.Column('meta', sa.JSON(), nullable=False, server_default='{}'))
|
||||||
|
|
||||||
chatidtag = table('chatidtag', column('chat_id', sa.String()), column('tag_name', sa.String()))
|
chatidtag = table('chatidtag', column('chat_id', sa.String()), column('tag_name', sa.String()))
|
||||||
chat = table(
|
chat = table(
|
||||||
|
|||||||
@@ -47,29 +47,32 @@ def upgrade():
|
|||||||
# If the column is already JSON, no need to do anything
|
# If the column is already JSON, no need to do anything
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# Step 3: Migrate data from 'old_chat' to 'chat'
|
# Step 3: Migrate data from 'old_chat' to 'chat' (only if old_chat exists)
|
||||||
chat_table = table(
|
# Re-check columns after potential rename above
|
||||||
'chat',
|
current_cols = {c['name'] for c in inspector.get_columns('chat')}
|
||||||
sa.Column('id', sa.String(), primary_key=True),
|
if 'old_chat' in current_cols:
|
||||||
sa.Column('old_chat', sa.Text()),
|
chat_table = table(
|
||||||
sa.Column('chat', sa.JSON()),
|
'chat',
|
||||||
)
|
sa.Column('id', sa.String(), primary_key=True),
|
||||||
|
sa.Column('old_chat', sa.Text()),
|
||||||
|
sa.Column('chat', sa.JSON()),
|
||||||
|
)
|
||||||
|
|
||||||
# - Selecting all data from the table
|
# - Selecting all data from the table
|
||||||
connection = op.get_bind()
|
connection = op.get_bind()
|
||||||
results = connection.execute(select(chat_table.c.id, chat_table.c.old_chat))
|
results = connection.execute(select(chat_table.c.id, chat_table.c.old_chat))
|
||||||
for row in results:
|
for row in results:
|
||||||
try:
|
try:
|
||||||
# Convert text JSON to actual JSON object, assuming the text is in JSON format
|
# Convert text JSON to actual JSON object, assuming the text is in JSON format
|
||||||
json_data = json.loads(row.old_chat)
|
json_data = json.loads(row.old_chat)
|
||||||
except json.JSONDecodeError:
|
except json.JSONDecodeError:
|
||||||
json_data = None # Handle cases where the text cannot be converted to JSON
|
json_data = None # Handle cases where the text cannot be converted to JSON
|
||||||
|
|
||||||
connection.execute(sa.update(chat_table).where(chat_table.c.id == row.id).values(chat=json_data))
|
connection.execute(sa.update(chat_table).where(chat_table.c.id == row.id).values(chat=json_data))
|
||||||
|
|
||||||
# Step 4: Drop 'old_chat' column
|
# Step 4: Drop 'old_chat' column
|
||||||
print("Dropping 'old_chat' column")
|
print("Dropping 'old_chat' column")
|
||||||
op.drop_column('chat', 'old_chat')
|
op.drop_column('chat', 'old_chat')
|
||||||
|
|
||||||
|
|
||||||
def downgrade():
|
def downgrade():
|
||||||
|
|||||||
Reference in new issue
Block a user