From dc0f8ae6f2372b6e5dd42f3520813b09c54146c0 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Thu, 14 May 2026 13:46:08 +0900 Subject: [PATCH] refac --- .../versions/1af9b942657b_migrate_tags.py | 7 ++- .../242a2047eae0_update_chat_table.py | 43 ++++++++++--------- 2 files changed, 28 insertions(+), 22 deletions(-) diff --git a/backend/open_webui/migrations/versions/1af9b942657b_migrate_tags.py b/backend/open_webui/migrations/versions/1af9b942657b_migrate_tags.py index 7f7b901765..00d36375d0 100644 --- a/backend/open_webui/migrations/versions/1af9b942657b_migrate_tags.py +++ b/backend/open_webui/migrations/versions/1af9b942657b_migrate_tags.py @@ -93,8 +93,11 @@ def upgrade(): conn.execute(update_stmt) # Add columns `pinned` and `meta` to 'chat' - op.add_column('chat', sa.Column('pinned', sa.Boolean(), nullable=True)) - op.add_column('chat', sa.Column('meta', sa.JSON(), nullable=False, server_default='{}')) + chat_columns = {c['name'] for c in inspector.get_columns('chat')} + 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())) chat = table( diff --git a/backend/open_webui/migrations/versions/242a2047eae0_update_chat_table.py b/backend/open_webui/migrations/versions/242a2047eae0_update_chat_table.py index 6f9be66342..e1c42bfb70 100644 --- a/backend/open_webui/migrations/versions/242a2047eae0_update_chat_table.py +++ b/backend/open_webui/migrations/versions/242a2047eae0_update_chat_table.py @@ -47,29 +47,32 @@ def upgrade(): # If the column is already JSON, no need to do anything pass - # Step 3: Migrate data from 'old_chat' to 'chat' - chat_table = table( - 'chat', - sa.Column('id', sa.String(), primary_key=True), - sa.Column('old_chat', sa.Text()), - sa.Column('chat', sa.JSON()), - ) + # Step 3: Migrate data from 'old_chat' to 'chat' (only if old_chat exists) + # Re-check columns after potential rename above + current_cols = {c['name'] for c in inspector.get_columns('chat')} + if 'old_chat' in current_cols: + chat_table = table( + '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 - connection = op.get_bind() - results = connection.execute(select(chat_table.c.id, chat_table.c.old_chat)) - for row in results: - try: - # Convert text JSON to actual JSON object, assuming the text is in JSON format - json_data = json.loads(row.old_chat) - except json.JSONDecodeError: - json_data = None # Handle cases where the text cannot be converted to JSON + # - Selecting all data from the table + connection = op.get_bind() + results = connection.execute(select(chat_table.c.id, chat_table.c.old_chat)) + for row in results: + try: + # Convert text JSON to actual JSON object, assuming the text is in JSON format + json_data = json.loads(row.old_chat) + except json.JSONDecodeError: + 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 - print("Dropping 'old_chat' column") - op.drop_column('chat', 'old_chat') + # Step 4: Drop 'old_chat' column + print("Dropping 'old_chat' column") + op.drop_column('chat', 'old_chat') def downgrade():