Fix migration 057: use IF NOT EXISTS for indexes that may pre-exist

The ix_project_tasks_parent_task_id index already existed on the
production DB, causing migration 057 to fail with DuplicateTableError.
Switched all CREATE INDEX statements to raw SQL with IF NOT EXISTS.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Kyle 2026-03-17 03:30:19 +08:00
parent dad5c0e606
commit a7e93aa2a3

View File

@ -17,27 +17,11 @@ depends_on = None
def upgrade() -> None:
# 1a. Performance indexes for project_tasks
op.create_index(
"ix_project_tasks_project_id",
"project_tasks",
["project_id"],
)
op.create_index(
"ix_project_tasks_parent_task_id",
"project_tasks",
["parent_task_id"],
postgresql_where=sa.text("parent_task_id IS NOT NULL"),
)
op.create_index(
"ix_project_tasks_project_updated",
"project_tasks",
["project_id", sa.text("updated_at DESC")],
)
op.create_index(
"ix_projects_user_updated",
"projects",
["user_id", sa.text("updated_at DESC")],
)
# Use IF NOT EXISTS to handle indexes that may already exist on the DB
op.execute("CREATE INDEX IF NOT EXISTS ix_project_tasks_project_id ON project_tasks (project_id)")
op.execute("CREATE INDEX IF NOT EXISTS ix_project_tasks_parent_task_id ON project_tasks (parent_task_id) WHERE parent_task_id IS NOT NULL")
op.execute("CREATE INDEX IF NOT EXISTS ix_project_tasks_project_updated ON project_tasks (project_id, updated_at DESC)")
op.execute("CREATE INDEX IF NOT EXISTS ix_projects_user_updated ON projects (user_id, updated_at DESC)")
# 1b. Add user_id to task_comments for multi-user attribution
op.add_column(
@ -52,11 +36,7 @@ def upgrade() -> None:
)
# Calendar delta polling index (Phase 4 prep)
op.create_index(
"ix_events_calendar_updated",
"calendar_events",
["calendar_id", sa.text("updated_at DESC")],
)
op.execute("CREATE INDEX IF NOT EXISTS ix_events_calendar_updated ON calendar_events (calendar_id, updated_at DESC)")
def downgrade() -> None: