fix (migration 43): Add an 'if not exists' guard

This commit is contained in:
CREDO23 2025-12-02 10:46:47 +02:00
parent 1d8cfbb143
commit 509ef211fa

View file

@ -16,6 +16,7 @@ import sqlalchemy as sa
from sqlalchemy.dialects import postgresql from sqlalchemy.dialects import postgresql
from alembic import op from alembic import op
from sqlalchemy import inspect
# revision identifiers, used by Alembic. # revision identifiers, used by Alembic.
revision: str = "43" revision: str = "43"
@ -25,51 +26,57 @@ depends_on: str | Sequence[str] | None = None
def upgrade() -> None: def upgrade() -> None:
"""Upgrade schema - Add BlockNote fields and trigger population task.""" """Upgrade schema - Add BlockNote fields (idempotent)."""
# Add the columns conn = op.get_bind()
op.add_column( inspector = inspect(conn)
"documents", existing_cols = {c["name"] for c in inspector.get_columns("documents")}
sa.Column(
"blocknote_document", postgresql.JSONB(astext_type=sa.Text()), nullable=True
),
)
op.add_column(
"documents",
sa.Column(
"content_needs_reindexing",
sa.Boolean(),
nullable=False,
server_default=sa.false(),
),
)
op.add_column(
"documents",
sa.Column("last_edited_at", sa.TIMESTAMP(timezone=True), nullable=True),
)
# Trigger the Celery task to populate blocknote_document for existing documents # Add blocknote_document (JSONB) if doest not exist
try: if "blocknote_document" not in existing_cols:
from app.tasks.celery_tasks.blocknote_migration_tasks import ( op.add_column(
populate_blocknote_for_documents_task, "documents",
sa.Column(
"blocknote_document",
postgresql.JSONB(astext_type=sa.Text()),
nullable=True,
),
) )
# Queue the task to run asynchronously # Add content_needs_reindexing (boolean) if doest not exist
populate_blocknote_for_documents_task.apply_async() if "content_needs_reindexing" not in existing_cols:
print( op.add_column(
"✓ Queued Celery task to populate blocknote_document for existing documents" "documents",
sa.Column(
"content_needs_reindexing",
sa.Boolean(),
nullable=False,
server_default=sa.false(),
),
) )
except Exception as e:
# If Celery is not available or task queueing fails, log but don't fail the migration # Add last_edited_at (timestamp with tz) if doest not exist
print(f"⚠ Warning: Could not queue blocknote population task: {e}") if "last_edited_at" not in existing_cols:
print(" You can manually trigger it later with:") op.add_column(
print( "documents",
" celery -A app.celery_app call app.tasks.celery_tasks.blocknote_migration_tasks.populate_blocknote_for_documents_task" sa.Column("last_edited_at", sa.TIMESTAMP(timezone=True), nullable=True),
) )
# NOTE: We intentionally do NOT import or queue Celery tasks here.
# Running background jobs during migrations causes hard-to-debug failures.
# After running migrations, trigger the backfill task manually (instructions below).
def downgrade() -> None: def downgrade() -> None:
"""Downgrade schema - Remove BlockNote fields.""" """Downgrade schema - Remove BlockNote fields (only if present)."""
op.drop_column("documents", "last_edited_at")
op.drop_column("documents", "content_needs_reindexing") conn = op.get_bind()
op.drop_column("documents", "blocknote_document") inspector = inspect(conn)
existing_cols = {c["name"] for c in inspector.get_columns("documents")}
if "last_edited_at" in existing_cols:
op.drop_column("documents", "last_edited_at")
if "content_needs_reindexing" in existing_cols:
op.drop_column("documents", "content_needs_reindexing")
if "blocknote_document" in existing_cols:
op.drop_column("documents", "blocknote_document")