From 459b1c3fda2ec3579fbd2ab408d0fdeb07c96b99 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Thu, 14 May 2026 13:45:05 +0900 Subject: [PATCH] refac --- .../c440947495f3_add_chat_file_table.py | 61 ++++++++++--------- .../versions/c69f45358db4_add_folder_table.py | 53 +++++++++------- 2 files changed, 62 insertions(+), 52 deletions(-) diff --git a/backend/open_webui/migrations/versions/c440947495f3_add_chat_file_table.py b/backend/open_webui/migrations/versions/c440947495f3_add_chat_file_table.py index 2b88ece98e..b1d0859fe1 100644 --- a/backend/open_webui/migrations/versions/c440947495f3_add_chat_file_table.py +++ b/backend/open_webui/migrations/versions/c440947495f3_add_chat_file_table.py @@ -19,36 +19,39 @@ depends_on: Union[str, Sequence[str], None] = None def upgrade() -> None: - op.create_table( - 'chat_file', - sa.Column('id', sa.Text(), primary_key=True), - sa.Column('user_id', sa.Text(), nullable=False), - sa.Column( - 'chat_id', - sa.Text(), - sa.ForeignKey('chat.id', ondelete='CASCADE'), - nullable=False, - ), - sa.Column( - 'file_id', - sa.Text(), - sa.ForeignKey('file.id', ondelete='CASCADE'), - nullable=False, - ), - sa.Column('message_id', sa.Text(), nullable=True), - sa.Column('created_at', sa.BigInteger(), nullable=False), - sa.Column('updated_at', sa.BigInteger(), nullable=False), - # indexes - sa.Index('ix_chat_file_chat_id', 'chat_id'), - sa.Index('ix_chat_file_file_id', 'file_id'), - sa.Index('ix_chat_file_message_id', 'message_id'), - sa.Index('ix_chat_file_user_id', 'user_id'), - # unique constraints - sa.UniqueConstraint('chat_id', 'file_id', name='uq_chat_file_chat_file'), # prevent duplicate entries - ) - pass + conn = op.get_bind() + inspector = sa.inspect(conn) + existing_tables = set(inspector.get_table_names()) + + if 'chat_file' not in existing_tables: + op.create_table( + 'chat_file', + sa.Column('id', sa.Text(), primary_key=True), + sa.Column('user_id', sa.Text(), nullable=False), + sa.Column( + 'chat_id', + sa.Text(), + sa.ForeignKey('chat.id', ondelete='CASCADE'), + nullable=False, + ), + sa.Column( + 'file_id', + sa.Text(), + sa.ForeignKey('file.id', ondelete='CASCADE'), + nullable=False, + ), + sa.Column('message_id', sa.Text(), nullable=True), + sa.Column('created_at', sa.BigInteger(), nullable=False), + sa.Column('updated_at', sa.BigInteger(), nullable=False), + # indexes + sa.Index('ix_chat_file_chat_id', 'chat_id'), + sa.Index('ix_chat_file_file_id', 'file_id'), + sa.Index('ix_chat_file_message_id', 'message_id'), + sa.Index('ix_chat_file_user_id', 'user_id'), + # unique constraints + sa.UniqueConstraint('chat_id', 'file_id', name='uq_chat_file_chat_file'), # prevent duplicate entries + ) def downgrade() -> None: op.drop_table('chat_file') - pass diff --git a/backend/open_webui/migrations/versions/c69f45358db4_add_folder_table.py b/backend/open_webui/migrations/versions/c69f45358db4_add_folder_table.py index 2440b395d6..646074427e 100644 --- a/backend/open_webui/migrations/versions/c69f45358db4_add_folder_table.py +++ b/backend/open_webui/migrations/versions/c69f45358db4_add_folder_table.py @@ -16,30 +16,37 @@ depends_on = None def upgrade(): - op.create_table( - 'folder', - sa.Column('id', sa.Text(), nullable=False), - sa.Column('parent_id', sa.Text(), nullable=True), - sa.Column('user_id', sa.Text(), nullable=False), - sa.Column('name', sa.Text(), nullable=False), - sa.Column('items', sa.JSON(), nullable=True), - sa.Column('meta', sa.JSON(), nullable=True), - sa.Column('is_expanded', sa.Boolean(), default=False, nullable=False), - sa.Column('created_at', sa.DateTime(), server_default=sa.func.now(), nullable=False), - sa.Column( - 'updated_at', - sa.DateTime(), - nullable=False, - server_default=sa.func.now(), - onupdate=sa.func.now(), - ), - sa.PrimaryKeyConstraint('id', 'user_id'), - ) + conn = op.get_bind() + inspector = sa.inspect(conn) + existing_tables = set(inspector.get_table_names()) - op.add_column( - 'chat', - sa.Column('folder_id', sa.Text(), nullable=True), - ) + if 'folder' not in existing_tables: + op.create_table( + 'folder', + sa.Column('id', sa.Text(), nullable=False), + sa.Column('parent_id', sa.Text(), nullable=True), + sa.Column('user_id', sa.Text(), nullable=False), + sa.Column('name', sa.Text(), nullable=False), + sa.Column('items', sa.JSON(), nullable=True), + sa.Column('meta', sa.JSON(), nullable=True), + sa.Column('is_expanded', sa.Boolean(), default=False, nullable=False), + sa.Column('created_at', sa.DateTime(), server_default=sa.func.now(), nullable=False), + sa.Column( + 'updated_at', + sa.DateTime(), + nullable=False, + server_default=sa.func.now(), + onupdate=sa.func.now(), + ), + sa.PrimaryKeyConstraint('id', 'user_id'), + ) + + chat_cols = {c['name'] for c in inspector.get_columns('chat')} + if 'folder_id' not in chat_cols: + op.add_column( + 'chat', + sa.Column('folder_id', sa.Text(), nullable=True), + ) def downgrade():