From db2b3d7fd86cc7d76424671ead3c62a3f6302fe7 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Thu, 14 May 2026 13:46:54 +0900 Subject: [PATCH] refac --- .../6283dc0e4d8d_add_channel_file_table.py | 55 ++++++++++--------- .../7826ab40b532_update_file_table.py | 13 +++-- .../8452d01d26d7_add_chat_message_table.py | 9 ++- 3 files changed, 46 insertions(+), 31 deletions(-) diff --git a/backend/open_webui/migrations/versions/6283dc0e4d8d_add_channel_file_table.py b/backend/open_webui/migrations/versions/6283dc0e4d8d_add_channel_file_table.py index b4d9d4c534..7f2913bd5e 100644 --- a/backend/open_webui/migrations/versions/6283dc0e4d8d_add_channel_file_table.py +++ b/backend/open_webui/migrations/versions/6283dc0e4d8d_add_channel_file_table.py @@ -20,31 +20,36 @@ depends_on: Union[str, Sequence[str], None] = None def upgrade() -> None: - op.create_table( - 'channel_file', - sa.Column('id', sa.Text(), primary_key=True), - sa.Column('user_id', sa.Text(), nullable=False), - sa.Column( - 'channel_id', - sa.Text(), - sa.ForeignKey('channel.id', ondelete='CASCADE'), - nullable=False, - ), - sa.Column( - 'file_id', - sa.Text(), - sa.ForeignKey('file.id', ondelete='CASCADE'), - nullable=False, - ), - sa.Column('created_at', sa.BigInteger(), nullable=False), - sa.Column('updated_at', sa.BigInteger(), nullable=False), - # indexes - sa.Index('ix_channel_file_channel_id', 'channel_id'), - sa.Index('ix_channel_file_file_id', 'file_id'), - sa.Index('ix_channel_file_user_id', 'user_id'), - # unique constraints - sa.UniqueConstraint('channel_id', 'file_id', name='uq_channel_file_channel_file'), # prevent duplicate entries - ) + conn = op.get_bind() + inspector = sa.inspect(conn) + existing_tables = set(inspector.get_table_names()) + + if 'channel_file' not in existing_tables: + op.create_table( + 'channel_file', + sa.Column('id', sa.Text(), primary_key=True), + sa.Column('user_id', sa.Text(), nullable=False), + sa.Column( + 'channel_id', + sa.Text(), + sa.ForeignKey('channel.id', ondelete='CASCADE'), + nullable=False, + ), + sa.Column( + 'file_id', + sa.Text(), + sa.ForeignKey('file.id', ondelete='CASCADE'), + nullable=False, + ), + sa.Column('created_at', sa.BigInteger(), nullable=False), + sa.Column('updated_at', sa.BigInteger(), nullable=False), + # indexes + sa.Index('ix_channel_file_channel_id', 'channel_id'), + sa.Index('ix_channel_file_file_id', 'file_id'), + sa.Index('ix_channel_file_user_id', 'user_id'), + # unique constraints + sa.UniqueConstraint('channel_id', 'file_id', name='uq_channel_file_channel_file'), # prevent duplicate entries + ) def downgrade() -> None: diff --git a/backend/open_webui/migrations/versions/7826ab40b532_update_file_table.py b/backend/open_webui/migrations/versions/7826ab40b532_update_file_table.py index 81d31248b5..b3313e3762 100644 --- a/backend/open_webui/migrations/versions/7826ab40b532_update_file_table.py +++ b/backend/open_webui/migrations/versions/7826ab40b532_update_file_table.py @@ -16,10 +16,15 @@ depends_on = None def upgrade(): - op.add_column( - 'file', - sa.Column('access_control', sa.JSON(), nullable=True), - ) + conn = op.get_bind() + inspector = sa.inspect(conn) + file_cols = {c['name'] for c in inspector.get_columns('file')} + + if 'access_control' not in file_cols: + op.add_column( + 'file', + sa.Column('access_control', sa.JSON(), nullable=True), + ) def downgrade(): diff --git a/backend/open_webui/migrations/versions/8452d01d26d7_add_chat_message_table.py b/backend/open_webui/migrations/versions/8452d01d26d7_add_chat_message_table.py index d37ec0016e..e247e5788e 100644 --- a/backend/open_webui/migrations/versions/8452d01d26d7_add_chat_message_table.py +++ b/backend/open_webui/migrations/versions/8452d01d26d7_add_chat_message_table.py @@ -56,6 +56,13 @@ def _flush_batch(conn, table, batch): def upgrade() -> None: + conn = op.get_bind() + inspector = sa.inspect(conn) + existing_tables = set(inspector.get_table_names()) + + if 'chat_message' in existing_tables: + return # Already created — skip everything + # Step 1: Create table op.create_table( 'chat_message', @@ -85,8 +92,6 @@ def upgrade() -> None: op.create_index('chat_message_user_created_idx', 'chat_message', ['user_id', 'created_at']) # Step 2: Backfill from existing chats - conn = op.get_bind() - chat_table = sa.table( 'chat', sa.column('id', sa.Text()),