diff --git a/backend/open_webui/migrations/versions/3781e22d8b01_update_message_table.py b/backend/open_webui/migrations/versions/3781e22d8b01_update_message_table.py index a3cc5b9f90..aaebaacb49 100644 --- a/backend/open_webui/migrations/versions/3781e22d8b01_update_message_table.py +++ b/backend/open_webui/migrations/versions/3781e22d8b01_update_message_table.py @@ -16,38 +16,48 @@ depends_on = None def upgrade(): + conn = op.get_bind() + inspector = sa.inspect(conn) + existing_tables = set(inspector.get_table_names()) + # Add 'type' column to the 'channel' table - op.add_column( - 'channel', - sa.Column( - 'type', - sa.Text(), - nullable=True, - ), - ) + channel_cols = {c['name'] for c in inspector.get_columns('channel')} + if 'type' not in channel_cols: + op.add_column( + 'channel', + sa.Column( + 'type', + sa.Text(), + nullable=True, + ), + ) # Add 'parent_id' column to the 'message' table for threads - op.add_column( - 'message', - sa.Column('parent_id', sa.Text(), nullable=True), - ) + message_cols = {c['name'] for c in inspector.get_columns('message')} + if 'parent_id' not in message_cols: + op.add_column( + 'message', + sa.Column('parent_id', sa.Text(), nullable=True), + ) - op.create_table( - 'message_reaction', - sa.Column('id', sa.Text(), nullable=False, primary_key=True, unique=True), # Unique reaction ID - sa.Column('user_id', sa.Text(), nullable=False), # User who reacted - sa.Column('message_id', sa.Text(), nullable=False), # Message that was reacted to - sa.Column('name', sa.Text(), nullable=False), # Reaction name (e.g. "thumbs_up") - sa.Column('created_at', sa.BigInteger(), nullable=True), # Timestamp of when the reaction was added - ) + if 'message_reaction' not in existing_tables: + op.create_table( + 'message_reaction', + sa.Column('id', sa.Text(), nullable=False, primary_key=True, unique=True), # Unique reaction ID + sa.Column('user_id', sa.Text(), nullable=False), # User who reacted + sa.Column('message_id', sa.Text(), nullable=False), # Message that was reacted to + sa.Column('name', sa.Text(), nullable=False), # Reaction name (e.g. "thumbs_up") + sa.Column('created_at', sa.BigInteger(), nullable=True), # Timestamp of when the reaction was added + ) - op.create_table( - 'channel_member', - sa.Column('id', sa.Text(), nullable=False, primary_key=True, unique=True), # Record ID for the membership row - sa.Column('channel_id', sa.Text(), nullable=False), # Associated channel - sa.Column('user_id', sa.Text(), nullable=False), # Associated user - sa.Column('created_at', sa.BigInteger(), nullable=True), # Timestamp of when the user joined the channel - ) + if 'channel_member' not in existing_tables: + op.create_table( + 'channel_member', + sa.Column('id', sa.Text(), nullable=False, primary_key=True, unique=True), # Record ID for the membership row + sa.Column('channel_id', sa.Text(), nullable=False), # Associated channel + sa.Column('user_id', sa.Text(), nullable=False), # Associated user + sa.Column('created_at', sa.BigInteger(), nullable=True), # Timestamp of when the user joined the channel + ) def downgrade(): diff --git a/backend/open_webui/migrations/versions/37f288994c47_add_group_member_table.py b/backend/open_webui/migrations/versions/37f288994c47_add_group_member_table.py index c92773dab7..daf0aef172 100644 --- a/backend/open_webui/migrations/versions/37f288994c47_add_group_member_table.py +++ b/backend/open_webui/migrations/versions/37f288994c47_add_group_member_table.py @@ -22,6 +22,13 @@ 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()) + + if 'group_member' in existing_tables: + return # Already created — skip everything + # 1. Create new table op.create_table( 'group_member',