Merge branch 'main' of https://gitee.com/wg123/video-gen
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
"""add notification indexes for query optimization
|
||||
"""add core business indexes and contact submit_date
|
||||
|
||||
Revision ID: n123456789ab
|
||||
Revises: c72a6f69e641
|
||||
Create Date: 2026-06-29 12:00:00.000000
|
||||
Create Date: 2026-06-29 14:00:00.000000
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
@@ -10,7 +10,6 @@ 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
|
||||
@@ -18,7 +17,120 @@ depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
# Composite index for personal notifications: (user_id, is_read, created_at)
|
||||
# 1. Add submit_date column to contact_requests
|
||||
op.add_column(
|
||||
"contact_requests",
|
||||
sa.Column("submit_date", sa.String(10), nullable=True)
|
||||
)
|
||||
|
||||
# Backfill submit_date from created_at for existing records
|
||||
op.execute(
|
||||
"UPDATE contact_requests SET submit_date = TO_CHAR(created_at AT TIME ZONE 'UTC', 'YYYY-MM-DD') WHERE submit_date IS NULL"
|
||||
)
|
||||
|
||||
# Set NOT NULL after backfill
|
||||
op.alter_column("contact_requests", "submit_date", nullable=False)
|
||||
|
||||
# Create index on submit_date
|
||||
op.create_index(
|
||||
"ix_contact_requests_submit_date",
|
||||
"contact_requests",
|
||||
["submit_date"],
|
||||
unique=False,
|
||||
)
|
||||
|
||||
# 2. Contact requests unique constraint: (user_id, submit_date)
|
||||
op.create_unique_constraint(
|
||||
"uq_contact_user_date",
|
||||
"contact_requests",
|
||||
["user_id", "submit_date"],
|
||||
)
|
||||
|
||||
# 3. Contact requests composite indexes
|
||||
op.create_index(
|
||||
"idx_contact_handled_created",
|
||||
"contact_requests",
|
||||
["is_handled", "created_at"],
|
||||
unique=False,
|
||||
)
|
||||
op.create_index(
|
||||
"idx_contact_user_date_created",
|
||||
"contact_requests",
|
||||
["user_id", "submit_date", "created_at"],
|
||||
unique=False,
|
||||
)
|
||||
|
||||
# 4. Generation records composite indexes
|
||||
op.create_index(
|
||||
"idx_genrec_user_status_created",
|
||||
"generation_records",
|
||||
["user_id", "status", "created_at"],
|
||||
unique=False,
|
||||
)
|
||||
op.create_index(
|
||||
"idx_genrec_project_status",
|
||||
"generation_records",
|
||||
["project_id", "status"],
|
||||
unique=False,
|
||||
)
|
||||
|
||||
# 5. Payment orders composite indexes
|
||||
op.create_index(
|
||||
"idx_payorder_user_status_created",
|
||||
"payment_orders",
|
||||
["user_id", "status", "created_at"],
|
||||
unique=False,
|
||||
)
|
||||
op.create_index(
|
||||
"idx_payorder_status_created",
|
||||
"payment_orders",
|
||||
["status", "created_at"],
|
||||
unique=False,
|
||||
)
|
||||
|
||||
# 6. Upload task composite indexes
|
||||
op.create_index(
|
||||
"idx_upload_user_status_created",
|
||||
"upload_task",
|
||||
["user_id", "status", "created_at"],
|
||||
unique=False,
|
||||
)
|
||||
op.create_index(
|
||||
"idx_upload_oauth_status",
|
||||
"upload_task",
|
||||
["oauth_id", "status"],
|
||||
unique=False,
|
||||
)
|
||||
|
||||
# 7. Menu configs indexes
|
||||
op.create_index(
|
||||
op.f("ix_menu_configs_parent_id"),
|
||||
"menu_configs",
|
||||
["parent_id"],
|
||||
unique=False,
|
||||
)
|
||||
op.create_index(
|
||||
"idx_menu_target_active_sort",
|
||||
"menu_configs",
|
||||
["menu_target", "is_active", "sort_order"],
|
||||
unique=False,
|
||||
)
|
||||
|
||||
# 8. Operation logs composite indexes
|
||||
op.create_index(
|
||||
"idx_oplog_user_created",
|
||||
"operation_logs",
|
||||
["user_id", "created_at"],
|
||||
unique=False,
|
||||
)
|
||||
op.create_index(
|
||||
"idx_oplog_created",
|
||||
"operation_logs",
|
||||
["created_at"],
|
||||
unique=False,
|
||||
)
|
||||
|
||||
# 9. Notification indexes (from previous optimization)
|
||||
op.create_index(
|
||||
"idx_notif_user_isread_created",
|
||||
"notifications",
|
||||
@@ -26,16 +138,13 @@ def upgrade() -> None:
|
||||
unique=False,
|
||||
)
|
||||
|
||||
# Composite index for notification_reads lookups
|
||||
# 10. Notification reads indexes
|
||||
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",
|
||||
@@ -47,3 +156,18 @@ 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")
|
||||
op.drop_index("idx_oplog_created", table_name="operation_logs")
|
||||
op.drop_index("idx_oplog_user_created", table_name="operation_logs")
|
||||
op.drop_index("idx_menu_target_active_sort", table_name="menu_configs")
|
||||
op.drop_index(op.f("ix_menu_configs_parent_id"), table_name="menu_configs")
|
||||
op.drop_index("idx_upload_oauth_status", table_name="upload_task")
|
||||
op.drop_index("idx_upload_user_status_created", table_name="upload_task")
|
||||
op.drop_index("idx_payorder_status_created", table_name="payment_orders")
|
||||
op.drop_index("idx_payorder_user_status_created", table_name="payment_orders")
|
||||
op.drop_index("idx_genrec_project_status", table_name="generation_records")
|
||||
op.drop_index("idx_genrec_user_status_created", table_name="generation_records")
|
||||
op.drop_index("idx_contact_user_date_created", table_name="contact_requests")
|
||||
op.drop_index("idx_contact_handled_created", table_name="contact_requests")
|
||||
op.drop_constraint("uq_contact_user_date", "contact_requests", type_="unique")
|
||||
op.drop_index("ix_contact_requests_submit_date", table_name="contact_requests")
|
||||
op.drop_column("contact_requests", "submit_date")
|
||||
|
||||
Reference in New Issue
Block a user