From 181df4237e5b11770c56f81c18e53a8d607b16de Mon Sep 17 00:00:00 2001 From: CREDO23 Date: Sat, 13 Dec 2025 21:48:54 +0200 Subject: [PATCH] fix migration version 45 | upgrade --- .../45_add_updated_at_to_documents.py | 34 ++++++++++++------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/surfsense_backend/alembic/versions/45_add_updated_at_to_documents.py b/surfsense_backend/alembic/versions/45_add_updated_at_to_documents.py index 8a0d3b875..39572d76e 100644 --- a/surfsense_backend/alembic/versions/45_add_updated_at_to_documents.py +++ b/surfsense_backend/alembic/versions/45_add_updated_at_to_documents.py @@ -12,6 +12,7 @@ for efficient time-based filtering. from collections.abc import Sequence import sqlalchemy as sa +from sqlalchemy import inspect from alembic import op @@ -24,19 +25,28 @@ depends_on: str | Sequence[str] | None = None def upgrade() -> None: """Upgrade schema - Add updated_at field with index to documents.""" - op.add_column( - "documents", - sa.Column("updated_at", sa.TIMESTAMP(timezone=True), nullable=True), - ) - op.create_index( - "ix_documents_updated_at", - "documents", - ["updated_at"], - ) + connection = op.get_bind() + inspector = inspect(connection) + columns = [col["name"] for col in inspector.get_columns("documents")] + + if "updated_at" not in columns: + op.add_column( + "documents", + sa.Column("updated_at", sa.TIMESTAMP(timezone=True), nullable=True), + ) + op.create_index( + "ix_documents_updated_at", + "documents", + ["updated_at"], + ) def downgrade() -> None: """Downgrade schema - Remove updated_at field and index.""" - # Use if_exists to handle cases where index wasn't created (migration modified after apply) - op.drop_index("ix_documents_updated_at", table_name="documents", if_exists=True) - op.drop_column("documents", "updated_at") + connection = op.get_bind() + inspector = inspect(connection) + columns = [col["name"] for col in inspector.get_columns("documents")] + + if "updated_at" in columns: + op.drop_index("ix_documents_updated_at", table_name="documents", if_exists=True) + op.drop_column("documents", "updated_at")