fix migration version 46 | upgrade

This commit is contained in:
CREDO23 2025-12-13 21:49:45 +02:00
parent 181df4237e
commit 84100380c4

View file

@ -12,6 +12,7 @@ to track all document updates (indexers, processors, and editor).
from collections.abc import Sequence from collections.abc import Sequence
import sqlalchemy as sa import sqlalchemy as sa
from sqlalchemy import inspect, text
from alembic import op from alembic import op
@ -24,29 +25,33 @@ depends_on: str | Sequence[str] | None = None
def upgrade() -> None: def upgrade() -> None:
"""Upgrade schema - Migrate last_edited_at to updated_at, then remove last_edited_at.""" """Upgrade schema - Migrate last_edited_at to updated_at, then remove last_edited_at."""
# Step 1: Copy last_edited_at values to updated_at where updated_at is NULL conn = op.get_bind()
# This preserves edit timestamps for documents that were edited via BlockNote inspector = inspect(conn)
op.execute( columns = [col["name"] for col in inspector.get_columns("documents")]
"""
UPDATE documents
SET updated_at = last_edited_at
WHERE last_edited_at IS NOT NULL
AND updated_at IS NULL
"""
)
# Step 2: For documents where both exist, use the most recent timestamp if "last_edited_at" in columns:
op.execute( # Step 1: Copy last_edited_at values to updated_at where updated_at is NULL
""" conn.execute(
UPDATE documents text("""
SET updated_at = GREATEST(updated_at, last_edited_at) UPDATE documents
WHERE last_edited_at IS NOT NULL SET updated_at = last_edited_at
AND updated_at IS NOT NULL WHERE last_edited_at IS NOT NULL
""" AND updated_at IS NULL
) """)
)
# Step 3: Drop the last_edited_at column # Step 2: For documents where both exist, use the most recent timestamp
op.drop_column("documents", "last_edited_at") conn.execute(
text("""
UPDATE documents
SET updated_at = GREATEST(updated_at, last_edited_at)
WHERE last_edited_at IS NOT NULL
AND updated_at IS NOT NULL
""")
)
# Step 3: Drop the last_edited_at column
op.drop_column("documents", "last_edited_at")
def downgrade() -> None: def downgrade() -> None:
@ -55,5 +60,11 @@ def downgrade() -> None:
"documents", "documents",
sa.Column("last_edited_at", sa.TIMESTAMP(timezone=True), nullable=True), sa.Column("last_edited_at", sa.TIMESTAMP(timezone=True), nullable=True),
) )
# Note: We cannot restore the original last_edited_at values after downgrade # Optionally restore values from updated_at
# as that data is merged into updated_at op.execute(
text("""
UPDATE documents
SET last_edited_at = updated_at
WHERE updated_at IS NOT NULL
""")
)