50 lines
1.4 KiB
Python
50 lines
1.4 KiB
Python
"""add notification indexes for query optimization
|
|
|
|
Revision ID: n123456789ab
|
|
Revises: c72a6f69e641
|
|
Create Date: 2026-06-29 12:00:00.000000
|
|
"""
|
|
from typing import Sequence, Union
|
|
|
|
from alembic import op
|
|
import sqlalchemy as sa
|
|
|
|
|
|
# revision identifiers, used by Alembic.
|
|
revision: str = "n123456789ab"
|
|
down_revision: Union[str, None] = "c72a6f69e641"
|
|
branch_labels: Union[str, Sequence[str], None] = None
|
|
depends_on: Union[str, Sequence[str], None] = None
|
|
|
|
|
|
def upgrade() -> None:
|
|
# Composite index for personal notifications: (user_id, is_read, created_at)
|
|
op.create_index(
|
|
"idx_notif_user_isread_created",
|
|
"notifications",
|
|
["user_id", "is_read", "created_at"],
|
|
unique=False,
|
|
)
|
|
|
|
# Composite index for notification_reads lookups
|
|
op.create_index(
|
|
"idx_notif_read_user_notif",
|
|
"notification_reads",
|
|
["user_id", "notification_id"],
|
|
unique=False,
|
|
)
|
|
|
|
# Unique constraint to prevent duplicate read records
|
|
# Check if constraint already exists first (in case of partial migration)
|
|
op.create_unique_constraint(
|
|
"uq_notif_read_user",
|
|
"notification_reads",
|
|
["notification_id", "user_id"],
|
|
)
|
|
|
|
|
|
def downgrade() -> None:
|
|
op.drop_constraint("uq_notif_read_user", "notification_reads", type_="unique")
|
|
op.drop_index("idx_notif_read_user_notif", table_name="notification_reads")
|
|
op.drop_index("idx_notif_user_isread_created", table_name="notifications")
|