perf(fulfillment): 优化能力刷新候选查询并新增精英能力投影
This commit is contained in:
+125
@@ -0,0 +1,125 @@
|
||||
"""add account elite capability projection columns and backfill
|
||||
|
||||
Revision ID: 20261129_0140
|
||||
Revises: 20261128_0139
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
revision: str = "20261129_0140"
|
||||
down_revision: str | None = "20261128_0139"
|
||||
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 not inspector.has_table("accounts"):
|
||||
return
|
||||
|
||||
if not _has_column(inspector, "accounts", "elite_covered"):
|
||||
op.add_column(
|
||||
"accounts",
|
||||
sa.Column("elite_covered", sa.Boolean(), nullable=True),
|
||||
)
|
||||
if not _has_column(inspector, "accounts", "elite_eligible_until"):
|
||||
op.add_column(
|
||||
"accounts",
|
||||
sa.Column("elite_eligible_until", sa.DateTime(), nullable=True),
|
||||
)
|
||||
|
||||
inspector = sa.inspect(bind)
|
||||
if not _has_index(inspector, "accounts", "ix_accounts_elite_supply"):
|
||||
op.create_index(
|
||||
"ix_accounts_elite_supply",
|
||||
"accounts",
|
||||
[
|
||||
"auto_managed",
|
||||
"ck_check_status",
|
||||
"status",
|
||||
"elite_covered",
|
||||
"elite_eligible_until",
|
||||
"value_tier",
|
||||
"id",
|
||||
],
|
||||
)
|
||||
|
||||
if inspector.has_table("account_commodity_states"):
|
||||
if bind.dialect.name == "mysql":
|
||||
op.execute(
|
||||
sa.text(
|
||||
"""
|
||||
UPDATE accounts a
|
||||
LEFT JOIN (
|
||||
SELECT
|
||||
account_id,
|
||||
MAX(CASE WHEN observed_at IS NOT NULL THEN 1 ELSE 0 END) AS has_observed,
|
||||
MAX(
|
||||
CASE
|
||||
WHEN account_eligibility = 'eligible'
|
||||
AND offer_state IN ('active', 'before')
|
||||
AND valid_until IS NOT NULL
|
||||
THEN valid_until
|
||||
ELSE NULL
|
||||
END
|
||||
) AS eligible_until
|
||||
FROM account_commodity_states
|
||||
WHERE platform = 'douyu' AND scope = 'elite'
|
||||
GROUP BY account_id
|
||||
) s ON s.account_id = a.id
|
||||
SET a.elite_covered = COALESCE(s.has_observed, 0),
|
||||
a.elite_eligible_until = s.eligible_until
|
||||
"""
|
||||
)
|
||||
)
|
||||
else:
|
||||
op.execute(
|
||||
sa.text(
|
||||
"""
|
||||
UPDATE accounts
|
||||
SET elite_covered = CASE
|
||||
WHEN EXISTS (
|
||||
SELECT 1 FROM account_commodity_states s
|
||||
WHERE s.account_id = accounts.id
|
||||
AND s.platform = 'douyu'
|
||||
AND s.scope = 'elite'
|
||||
AND s.observed_at IS NOT NULL
|
||||
) THEN 1 ELSE 0 END,
|
||||
elite_eligible_until = (
|
||||
SELECT MAX(s.valid_until)
|
||||
FROM account_commodity_states s
|
||||
WHERE s.account_id = accounts.id
|
||||
AND s.platform = 'douyu'
|
||||
AND s.scope = 'elite'
|
||||
AND s.account_eligibility = 'eligible'
|
||||
AND s.offer_state IN ('active', 'before')
|
||||
AND s.valid_until IS NOT NULL
|
||||
)
|
||||
"""
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
bind = op.get_bind()
|
||||
inspector = sa.inspect(bind)
|
||||
if not inspector.has_table("accounts"):
|
||||
return
|
||||
if _has_index(inspector, "accounts", "ix_accounts_elite_supply"):
|
||||
op.drop_index("ix_accounts_elite_supply", table_name="accounts")
|
||||
if _has_column(inspector, "accounts", "elite_eligible_until"):
|
||||
op.drop_column("accounts", "elite_eligible_until")
|
||||
if _has_column(inspector, "accounts", "elite_covered"):
|
||||
op.drop_column("accounts", "elite_covered")
|
||||
Reference in New Issue
Block a user