UMBRA/backend/alembic/versions/054_add_calendar_event_indexes.py
Kyle Pope e12687ca6f Add calendar_events indexes and optimize dashboard queries
Migration 054: three indexes on calendar_events table:
- (calendar_id, start_datetime) for range queries
- (parent_event_id) for recurrence bulk operations
- (calendar_id, is_starred, start_datetime) for starred widget

Dashboard: replaced correlated subquery with single materialized
list fetch for user_calendar_ids in both /dashboard and /upcoming
handlers — eliminates 2 redundant subquery evaluations per request.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-15 00:45:36 +08:00

37 lines
1.1 KiB
Python

"""Add performance indexes to calendar_events
Revision ID: 054
Revises: 053
"""
from alembic import op
revision = "054"
down_revision = "053"
def upgrade():
# Covers range queries in dashboard today's events and events list
op.create_index(
"ix_calendar_events_calendar_start",
"calendar_events",
["calendar_id", "start_datetime"],
)
# Covers bulk DELETE on recurrence edit/regeneration and sibling lookups
op.create_index(
"ix_calendar_events_parent_event_id",
"calendar_events",
["parent_event_id"],
)
# Covers starred widget query (calendar_id + is_starred + start_datetime)
op.create_index(
"ix_calendar_events_calendar_starred_start",
"calendar_events",
["calendar_id", "is_starred", "start_datetime"],
)
def downgrade():
op.drop_index("ix_calendar_events_calendar_starred_start", table_name="calendar_events")
op.drop_index("ix_calendar_events_parent_event_id", table_name="calendar_events")
op.drop_index("ix_calendar_events_calendar_start", table_name="calendar_events")