From e1bbc859f19aa5c88e9ed857a32303b101d2119b Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Sat, 10 Oct 2026 23:15:08 +0400 Subject: [PATCH] refac Co-Authored-By: Classic298 <27028174+Classic298@users.noreply.github.com> --- ...d91a04_add_postgres_pending_files_index.py | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 backend/open_webui/migrations/versions/b8f2c6d91a04_add_postgres_pending_files_index.py diff --git a/backend/open_webui/migrations/versions/b8f2c6d91a04_add_postgres_pending_files_index.py b/backend/open_webui/migrations/versions/b8f2c6d91a04_add_postgres_pending_files_index.py new file mode 100644 index 0000000000..d6951441dd --- /dev/null +++ b/backend/open_webui/migrations/versions/b8f2c6d91a04_add_postgres_pending_files_index.py @@ -0,0 +1,39 @@ +"""Add a PostgreSQL index for pending knowledge files.""" + +import logging + +import sqlalchemy as sa +from alembic import op + +revision = 'b8f2c6d91a04' +down_revision = 'f8c0e5b134cd' +branch_labels = None +depends_on = None + +INDEX_NAME = 'file_pending_knowledge_idx' + + +def upgrade() -> None: + conn = op.get_bind() + if conn.dialect.name != 'postgresql': + return + + try: + with conn.begin_nested(): + op.create_index( + INDEX_NAME, + 'file', + [sa.text("((meta -> 'data') ->> 'knowledge_id')")], + postgresql_where=sa.text("(data ->> 'status') IN ('pending', 'processing')"), + if_not_exists=True, + ) + except sa.exc.DBAPIError as exc: + if exc.connection_invalidated: + raise + # Alembic records a skipped attempt too; it will not retry on every startup. + logging.getLogger(__name__).warning('Skipped PostgreSQL index %s: %s', INDEX_NAME, exc) + + +def downgrade() -> None: + if op.get_bind().dialect.name == 'postgresql': + op.drop_index(INDEX_NAME, table_name='file', if_exists=True)