fix(douyu): 修复斗鱼链路 P0/P1 隐患并增强 P2 运维队列可观测性
This commit is contained in:
@@ -0,0 +1,175 @@
|
||||
"""add douyu slow query covering indexes and role_norm column
|
||||
|
||||
Revision ID: 20261129_0141
|
||||
Revises: 20261129_0140
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
revision: str = "20261129_0141"
|
||||
down_revision: str | None = "20261129_0140"
|
||||
branch_labels: str | None = None
|
||||
depends_on: str | None = None
|
||||
|
||||
|
||||
def _has_column(inspector: sa.Inspector, table: str, column: str) -> bool:
|
||||
return any(col["name"] == column for col in inspector.get_columns(table))
|
||||
|
||||
|
||||
def _has_index(inspector: sa.Inspector, table: str, index_name: str) -> bool:
|
||||
return any(idx["name"] == index_name for idx in inspector.get_indexes(table))
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
bind = op.get_bind()
|
||||
inspector = sa.inspect(bind)
|
||||
|
||||
if inspector.has_table("account_events"):
|
||||
if not _has_index(
|
||||
inspector, "account_events", "ix_account_events_event_subject_occurred"
|
||||
):
|
||||
op.create_index(
|
||||
"ix_account_events_event_subject_occurred",
|
||||
"account_events",
|
||||
["event_type", "subject_type", "subject_key", "occurred_at"],
|
||||
)
|
||||
if not _has_index(
|
||||
inspector, "account_events", "ix_account_events_type_occurred"
|
||||
):
|
||||
op.create_index(
|
||||
"ix_account_events_type_occurred",
|
||||
"account_events",
|
||||
["event_type", "occurred_at"],
|
||||
)
|
||||
|
||||
if inspector.has_table("account_sync_runs"):
|
||||
if not _has_index(
|
||||
inspector, "account_sync_runs", "ix_account_sync_runs_trend_cover"
|
||||
):
|
||||
op.create_index(
|
||||
"ix_account_sync_runs_trend_cover",
|
||||
"account_sync_runs",
|
||||
["started_at", "domain", "status", "duration_ms"],
|
||||
)
|
||||
if _has_index(
|
||||
inspector,
|
||||
"account_sync_runs",
|
||||
"ix_account_sync_runs_started_domain_status",
|
||||
):
|
||||
op.drop_index(
|
||||
"ix_account_sync_runs_started_domain_status",
|
||||
table_name="account_sync_runs",
|
||||
)
|
||||
|
||||
if inspector.has_table("account_commodity_states") and not _has_index(
|
||||
inspector,
|
||||
"account_commodity_states",
|
||||
"ix_account_commodity_skin_options",
|
||||
):
|
||||
op.create_index(
|
||||
"ix_account_commodity_skin_options",
|
||||
"account_commodity_states",
|
||||
[
|
||||
"platform",
|
||||
"account_eligibility",
|
||||
"offer_state",
|
||||
"valid_until",
|
||||
"scope",
|
||||
"commodity_id",
|
||||
"commodity_name",
|
||||
],
|
||||
)
|
||||
|
||||
if inspector.has_table("platform_fulfillment_tasks"):
|
||||
if not _has_column(
|
||||
inspector, "platform_fulfillment_tasks", "customer_role_norm"
|
||||
):
|
||||
op.add_column(
|
||||
"platform_fulfillment_tasks",
|
||||
sa.Column(
|
||||
"customer_role_norm",
|
||||
sa.String(length=128),
|
||||
sa.Computed(
|
||||
"lower(trim(customer_role_name))",
|
||||
persisted=False,
|
||||
),
|
||||
nullable=True,
|
||||
),
|
||||
)
|
||||
inspector = sa.inspect(bind)
|
||||
if not _has_index(
|
||||
inspector,
|
||||
"platform_fulfillment_tasks",
|
||||
"ix_platform_fulfillment_role_norm",
|
||||
):
|
||||
op.create_index(
|
||||
"ix_platform_fulfillment_role_norm",
|
||||
"platform_fulfillment_tasks",
|
||||
["flow_version", "customer_role_norm", "execution_status"],
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
bind = op.get_bind()
|
||||
inspector = sa.inspect(bind)
|
||||
|
||||
if inspector.has_table("platform_fulfillment_tasks"):
|
||||
if _has_index(
|
||||
inspector,
|
||||
"platform_fulfillment_tasks",
|
||||
"ix_platform_fulfillment_role_norm",
|
||||
):
|
||||
op.drop_index(
|
||||
"ix_platform_fulfillment_role_norm",
|
||||
table_name="platform_fulfillment_tasks",
|
||||
)
|
||||
if _has_column(inspector, "platform_fulfillment_tasks", "customer_role_norm"):
|
||||
op.drop_column("platform_fulfillment_tasks", "customer_role_norm")
|
||||
|
||||
if inspector.has_table("account_commodity_states") and _has_index(
|
||||
inspector,
|
||||
"account_commodity_states",
|
||||
"ix_account_commodity_skin_options",
|
||||
):
|
||||
op.drop_index(
|
||||
"ix_account_commodity_skin_options",
|
||||
table_name="account_commodity_states",
|
||||
)
|
||||
|
||||
if inspector.has_table("account_sync_runs"):
|
||||
if not _has_index(
|
||||
inspector,
|
||||
"account_sync_runs",
|
||||
"ix_account_sync_runs_started_domain_status",
|
||||
):
|
||||
op.create_index(
|
||||
"ix_account_sync_runs_started_domain_status",
|
||||
"account_sync_runs",
|
||||
["started_at", "domain", "status"],
|
||||
)
|
||||
if _has_index(
|
||||
inspector, "account_sync_runs", "ix_account_sync_runs_trend_cover"
|
||||
):
|
||||
op.drop_index(
|
||||
"ix_account_sync_runs_trend_cover",
|
||||
table_name="account_sync_runs",
|
||||
)
|
||||
|
||||
if inspector.has_table("account_events"):
|
||||
if _has_index(
|
||||
inspector, "account_events", "ix_account_events_type_occurred"
|
||||
):
|
||||
op.drop_index(
|
||||
"ix_account_events_type_occurred",
|
||||
table_name="account_events",
|
||||
)
|
||||
if _has_index(
|
||||
inspector, "account_events", "ix_account_events_event_subject_occurred"
|
||||
):
|
||||
op.drop_index(
|
||||
"ix_account_events_event_subject_occurred",
|
||||
table_name="account_events",
|
||||
)
|
||||
Reference in New Issue
Block a user