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,25 +25,29 @@ 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."""
conn = op.get_bind()
inspector = inspect(conn)
columns = [col["name"] for col in inspector.get_columns("documents")]
if "last_edited_at" in columns:
# Step 1: Copy last_edited_at values to updated_at where updated_at is NULL # Step 1: Copy last_edited_at values to updated_at where updated_at is NULL
# This preserves edit timestamps for documents that were edited via BlockNote conn.execute(
op.execute( text("""
"""
UPDATE documents UPDATE documents
SET updated_at = last_edited_at SET updated_at = last_edited_at
WHERE last_edited_at IS NOT NULL WHERE last_edited_at IS NOT NULL
AND updated_at IS NULL AND updated_at IS NULL
""" """)
) )
# Step 2: For documents where both exist, use the most recent timestamp # Step 2: For documents where both exist, use the most recent timestamp
op.execute( conn.execute(
""" text("""
UPDATE documents UPDATE documents
SET updated_at = GREATEST(updated_at, last_edited_at) SET updated_at = GREATEST(updated_at, last_edited_at)
WHERE last_edited_at IS NOT NULL WHERE last_edited_at IS NOT NULL
AND updated_at IS NOT NULL AND updated_at IS NOT NULL
""" """)
) )
# Step 3: Drop the last_edited_at column # Step 3: Drop the last_edited_at column
@ -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
""")
)