From 1b9d22e324181b96511a15cbacc40fdbb439ad79 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Thu, 14 May 2026 13:45:59 +0900 Subject: [PATCH] refac --- ...update_channel_file_and_knowledge_table.py | 27 ++++--- ...pdate_channel_and_channel_members_table.py | 75 +++++++++++-------- 2 files changed, 59 insertions(+), 43 deletions(-) diff --git a/backend/open_webui/migrations/versions/81cc2ce44d79_update_channel_file_and_knowledge_table.py b/backend/open_webui/migrations/versions/81cc2ce44d79_update_channel_file_and_knowledge_table.py index e069b2d1d6..2294f89562 100644 --- a/backend/open_webui/migrations/versions/81cc2ce44d79_update_channel_file_and_knowledge_table.py +++ b/backend/open_webui/migrations/versions/81cc2ce44d79_update_channel_file_and_knowledge_table.py @@ -20,20 +20,27 @@ depends_on: Union[str, Sequence[str], None] = None def upgrade() -> None: + conn = op.get_bind() + inspector = sa.inspect(conn) + # Add message_id column to channel_file table - with op.batch_alter_table('channel_file', schema=None) as batch_op: - batch_op.add_column( - sa.Column( - 'message_id', - sa.Text(), - sa.ForeignKey('message.id', ondelete='CASCADE', name='fk_channel_file_message_id'), - nullable=True, + cf_cols = {c['name'] for c in inspector.get_columns('channel_file')} + if 'message_id' not in cf_cols: + with op.batch_alter_table('channel_file', schema=None) as batch_op: + batch_op.add_column( + sa.Column( + 'message_id', + sa.Text(), + sa.ForeignKey('message.id', ondelete='CASCADE', name='fk_channel_file_message_id'), + nullable=True, + ) ) - ) # Add data column to knowledge table - with op.batch_alter_table('knowledge', schema=None) as batch_op: - batch_op.add_column(sa.Column('data', sa.JSON(), nullable=True)) + k_cols = {c['name'] for c in inspector.get_columns('knowledge')} + if 'data' not in k_cols: + with op.batch_alter_table('knowledge', schema=None) as batch_op: + batch_op.add_column(sa.Column('data', sa.JSON(), nullable=True)) def downgrade() -> None: diff --git a/backend/open_webui/migrations/versions/90ef40d4714e_update_channel_and_channel_members_table.py b/backend/open_webui/migrations/versions/90ef40d4714e_update_channel_and_channel_members_table.py index baad674612..5936667624 100644 --- a/backend/open_webui/migrations/versions/90ef40d4714e_update_channel_and_channel_members_table.py +++ b/backend/open_webui/migrations/versions/90ef40d4714e_update_channel_and_channel_members_table.py @@ -20,42 +20,53 @@ depends_on: Union[str, Sequence[str], None] = None def upgrade() -> None: + conn = op.get_bind() + inspector = sa.inspect(conn) + existing_tables = set(inspector.get_table_names()) + # Update 'channel' table - op.add_column('channel', sa.Column('is_private', sa.Boolean(), nullable=True)) - - op.add_column('channel', sa.Column('archived_at', sa.BigInteger(), nullable=True)) - op.add_column('channel', sa.Column('archived_by', sa.Text(), nullable=True)) - - op.add_column('channel', sa.Column('deleted_at', sa.BigInteger(), nullable=True)) - op.add_column('channel', sa.Column('deleted_by', sa.Text(), nullable=True)) - - op.add_column('channel', sa.Column('updated_by', sa.Text(), nullable=True)) + channel_cols = {c['name'] for c in inspector.get_columns('channel')} + if 'is_private' not in channel_cols: + op.add_column('channel', sa.Column('is_private', sa.Boolean(), nullable=True)) + if 'archived_at' not in channel_cols: + op.add_column('channel', sa.Column('archived_at', sa.BigInteger(), nullable=True)) + if 'archived_by' not in channel_cols: + op.add_column('channel', sa.Column('archived_by', sa.Text(), nullable=True)) + if 'deleted_at' not in channel_cols: + op.add_column('channel', sa.Column('deleted_at', sa.BigInteger(), nullable=True)) + if 'deleted_by' not in channel_cols: + op.add_column('channel', sa.Column('deleted_by', sa.Text(), nullable=True)) + if 'updated_by' not in channel_cols: + op.add_column('channel', sa.Column('updated_by', sa.Text(), nullable=True)) # Update 'channel_member' table - op.add_column('channel_member', sa.Column('role', sa.Text(), nullable=True)) - op.add_column('channel_member', sa.Column('invited_by', sa.Text(), nullable=True)) - op.add_column('channel_member', sa.Column('invited_at', sa.BigInteger(), nullable=True)) + cm_cols = {c['name'] for c in inspector.get_columns('channel_member')} + if 'role' not in cm_cols: + op.add_column('channel_member', sa.Column('role', sa.Text(), nullable=True)) + if 'invited_by' not in cm_cols: + op.add_column('channel_member', sa.Column('invited_by', sa.Text(), nullable=True)) + if 'invited_at' not in cm_cols: + op.add_column('channel_member', sa.Column('invited_at', sa.BigInteger(), nullable=True)) # Create 'channel_webhook' table - op.create_table( - 'channel_webhook', - sa.Column('id', sa.Text(), primary_key=True, unique=True, nullable=False), - sa.Column('user_id', sa.Text(), nullable=False), - sa.Column( - 'channel_id', - sa.Text(), - sa.ForeignKey('channel.id', ondelete='CASCADE'), - nullable=False, - ), - sa.Column('name', sa.Text(), nullable=False), - sa.Column('profile_image_url', sa.Text(), nullable=True), - sa.Column('token', sa.Text(), nullable=False), - sa.Column('last_used_at', sa.BigInteger(), nullable=True), - sa.Column('created_at', sa.BigInteger(), nullable=False), - sa.Column('updated_at', sa.BigInteger(), nullable=False), - ) - - pass + if 'channel_webhook' not in existing_tables: + op.create_table( + 'channel_webhook', + sa.Column('id', sa.Text(), primary_key=True, unique=True, nullable=False), + sa.Column('user_id', sa.Text(), nullable=False), + sa.Column( + 'channel_id', + sa.Text(), + sa.ForeignKey('channel.id', ondelete='CASCADE'), + nullable=False, + ), + sa.Column('name', sa.Text(), nullable=False), + sa.Column('profile_image_url', sa.Text(), nullable=True), + sa.Column('token', sa.Text(), nullable=False), + sa.Column('last_used_at', sa.BigInteger(), nullable=True), + sa.Column('created_at', sa.BigInteger(), nullable=False), + sa.Column('updated_at', sa.BigInteger(), nullable=False), + ) def downgrade() -> None: @@ -74,5 +85,3 @@ def downgrade() -> None: # Drop 'channel_webhook' table op.drop_table('channel_webhook') - - pass