feat(platform): 增加履约分类映射管理并锁定迁移
This commit is contained in:
@@ -0,0 +1,243 @@
|
||||
"""履约工作台增加销售模式、活动批次和报价快照。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
revision = "20261121_0106"
|
||||
down_revision = "20261121_0105"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
MAPPING_TABLE = "platform_commodity_mappings"
|
||||
TASK_TABLE = "platform_fulfillment_tasks"
|
||||
OLD_UNIQUE = "uq_platform_commodity_mapping_sku"
|
||||
NEW_UNIQUE = "uq_platform_commodity_mapping_campaign"
|
||||
|
||||
|
||||
def _column_exists(table: str, column: str) -> bool:
|
||||
return bool(
|
||||
op.get_bind()
|
||||
.execute(
|
||||
sa.text(
|
||||
"""
|
||||
SELECT COUNT(*)
|
||||
FROM information_schema.columns
|
||||
WHERE table_schema = DATABASE()
|
||||
AND table_name = :table
|
||||
AND column_name = :column
|
||||
"""
|
||||
),
|
||||
{"table": table, "column": column},
|
||||
)
|
||||
.scalar()
|
||||
)
|
||||
|
||||
|
||||
def _constraint_exists(table: str, name: str) -> bool:
|
||||
return bool(
|
||||
op.get_bind()
|
||||
.execute(
|
||||
sa.text(
|
||||
"""
|
||||
SELECT COUNT(*)
|
||||
FROM information_schema.table_constraints
|
||||
WHERE constraint_schema = DATABASE()
|
||||
AND table_name = :table
|
||||
AND constraint_name = :name
|
||||
"""
|
||||
),
|
||||
{"table": table, "name": name},
|
||||
)
|
||||
.scalar()
|
||||
)
|
||||
|
||||
|
||||
def _index_exists(table: str, name: str) -> bool:
|
||||
return bool(
|
||||
op.get_bind()
|
||||
.execute(
|
||||
sa.text(
|
||||
"""
|
||||
SELECT COUNT(*)
|
||||
FROM information_schema.statistics
|
||||
WHERE table_schema = DATABASE()
|
||||
AND table_name = :table
|
||||
AND index_name = :name
|
||||
"""
|
||||
),
|
||||
{"table": table, "name": name},
|
||||
)
|
||||
.scalar()
|
||||
)
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
mapping_columns = (
|
||||
sa.Column(
|
||||
"campaign_key",
|
||||
sa.String(length=128),
|
||||
nullable=False,
|
||||
server_default="douyu-current",
|
||||
),
|
||||
sa.Column(
|
||||
"campaign_name",
|
||||
sa.String(length=255),
|
||||
nullable=False,
|
||||
server_default="",
|
||||
),
|
||||
sa.Column(
|
||||
"sale_mode",
|
||||
sa.String(length=16),
|
||||
nullable=False,
|
||||
server_default="unknown",
|
||||
),
|
||||
sa.Column("available_from", sa.DateTime(), nullable=True),
|
||||
sa.Column("available_until", sa.DateTime(), nullable=True),
|
||||
sa.Column("released_at", sa.DateTime(), nullable=True),
|
||||
sa.Column(
|
||||
"goods_status",
|
||||
sa.String(length=32),
|
||||
nullable=False,
|
||||
server_default="",
|
||||
),
|
||||
sa.Column(
|
||||
"version",
|
||||
sa.Integer(),
|
||||
nullable=False,
|
||||
server_default="1",
|
||||
),
|
||||
sa.Column("archived_at", sa.DateTime(), nullable=True),
|
||||
sa.Column("archived_by", sa.Integer(), nullable=True),
|
||||
)
|
||||
for column in mapping_columns:
|
||||
if not _column_exists(MAPPING_TABLE, column.name):
|
||||
op.add_column(MAPPING_TABLE, column)
|
||||
if not _constraint_exists(MAPPING_TABLE, "fk_platform_mapping_archived_by"):
|
||||
op.create_foreign_key(
|
||||
"fk_platform_mapping_archived_by",
|
||||
MAPPING_TABLE,
|
||||
"users",
|
||||
["archived_by"],
|
||||
["id"],
|
||||
ondelete="SET NULL",
|
||||
)
|
||||
|
||||
op.execute(
|
||||
sa.text(
|
||||
"""
|
||||
UPDATE platform_commodity_mappings
|
||||
SET campaign_key = 'douyu-current'
|
||||
WHERE campaign_key IS NULL OR campaign_key = ''
|
||||
"""
|
||||
)
|
||||
)
|
||||
if _constraint_exists(MAPPING_TABLE, OLD_UNIQUE):
|
||||
op.drop_constraint(OLD_UNIQUE, MAPPING_TABLE, type_="unique")
|
||||
if not _constraint_exists(MAPPING_TABLE, NEW_UNIQUE):
|
||||
op.create_unique_constraint(
|
||||
NEW_UNIQUE,
|
||||
MAPPING_TABLE,
|
||||
["target_platform", "internal_sku_key", "campaign_key"],
|
||||
)
|
||||
if not _index_exists(MAPPING_TABLE, "ix_platform_mapping_campaign"):
|
||||
op.create_index(
|
||||
"ix_platform_mapping_campaign",
|
||||
MAPPING_TABLE,
|
||||
["target_platform", "campaign_key", "archived_at"],
|
||||
)
|
||||
|
||||
task_columns = (
|
||||
sa.Column(
|
||||
"sale_mode",
|
||||
sa.String(length=16),
|
||||
nullable=False,
|
||||
server_default="unknown",
|
||||
),
|
||||
sa.Column(
|
||||
"campaign_key",
|
||||
sa.String(length=128),
|
||||
nullable=False,
|
||||
server_default="",
|
||||
),
|
||||
sa.Column(
|
||||
"campaign_name",
|
||||
sa.String(length=255),
|
||||
nullable=False,
|
||||
server_default="",
|
||||
),
|
||||
sa.Column("offer_available_from", sa.DateTime(), nullable=True),
|
||||
sa.Column("offer_available_until", sa.DateTime(), nullable=True),
|
||||
sa.Column(
|
||||
"classification_source",
|
||||
sa.String(length=24),
|
||||
nullable=False,
|
||||
server_default="unknown",
|
||||
),
|
||||
)
|
||||
for column in task_columns:
|
||||
if not _column_exists(TASK_TABLE, column.name):
|
||||
op.add_column(TASK_TABLE, column)
|
||||
|
||||
op.execute(
|
||||
sa.text(
|
||||
"""
|
||||
UPDATE platform_fulfillment_tasks AS task
|
||||
JOIN platform_commodity_mappings AS mapping
|
||||
ON mapping.id = task.commodity_mapping_id
|
||||
SET task.sale_mode = mapping.sale_mode,
|
||||
task.campaign_key = mapping.campaign_key,
|
||||
task.campaign_name = mapping.campaign_name,
|
||||
task.offer_available_from = mapping.available_from,
|
||||
task.offer_available_until = mapping.available_until,
|
||||
task.classification_source = 'mapping'
|
||||
WHERE task.sale_mode = 'unknown'
|
||||
AND task.classification_source = 'unknown'
|
||||
"""
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
if _index_exists(MAPPING_TABLE, "ix_platform_mapping_campaign"):
|
||||
op.drop_index("ix_platform_mapping_campaign", table_name=MAPPING_TABLE)
|
||||
if _constraint_exists(MAPPING_TABLE, NEW_UNIQUE):
|
||||
op.drop_constraint(NEW_UNIQUE, MAPPING_TABLE, type_="unique")
|
||||
if not _constraint_exists(MAPPING_TABLE, OLD_UNIQUE):
|
||||
op.create_unique_constraint(
|
||||
OLD_UNIQUE,
|
||||
MAPPING_TABLE,
|
||||
["target_platform", "internal_sku_key"],
|
||||
)
|
||||
if _constraint_exists(MAPPING_TABLE, "fk_platform_mapping_archived_by"):
|
||||
op.drop_constraint(
|
||||
"fk_platform_mapping_archived_by",
|
||||
MAPPING_TABLE,
|
||||
type_="foreignkey",
|
||||
)
|
||||
|
||||
for column in (
|
||||
"classification_source",
|
||||
"offer_available_until",
|
||||
"offer_available_from",
|
||||
"campaign_name",
|
||||
"campaign_key",
|
||||
"sale_mode",
|
||||
):
|
||||
if _column_exists(TASK_TABLE, column):
|
||||
op.drop_column(TASK_TABLE, column)
|
||||
for column in (
|
||||
"archived_by",
|
||||
"archived_at",
|
||||
"version",
|
||||
"goods_status",
|
||||
"released_at",
|
||||
"available_until",
|
||||
"available_from",
|
||||
"sale_mode",
|
||||
"campaign_name",
|
||||
"campaign_key",
|
||||
):
|
||||
if _column_exists(MAPPING_TABLE, column):
|
||||
op.drop_column(MAPPING_TABLE, column)
|
||||
@@ -0,0 +1,84 @@
|
||||
"""修复 douyu_tasks.active_binding_key 缺失导致的服务启动失败。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
revision = "20261121_0107"
|
||||
down_revision = "20261121_0106"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
INDEX_NAME = "uq_douyu_tasks_active_binding"
|
||||
|
||||
|
||||
def _columns() -> set[str]:
|
||||
inspector = sa.inspect(op.get_bind())
|
||||
if not inspector.has_table("douyu_tasks"):
|
||||
return set()
|
||||
return {column["name"] for column in inspector.get_columns("douyu_tasks")}
|
||||
|
||||
|
||||
def _indexes() -> set[str]:
|
||||
inspector = sa.inspect(op.get_bind())
|
||||
if not inspector.has_table("douyu_tasks"):
|
||||
return set()
|
||||
return {index["name"] for index in inspector.get_indexes("douyu_tasks")}
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
if "active_binding_key" not in _columns():
|
||||
op.add_column(
|
||||
"douyu_tasks",
|
||||
sa.Column(
|
||||
"active_binding_key",
|
||||
sa.String(length=64),
|
||||
sa.Computed(
|
||||
"CASE WHEN task_type = 'get_bind_qr' "
|
||||
"AND platform_task_id IS NOT NULL "
|
||||
"AND status IN ('planned', 'pending', 'running') "
|
||||
"THEN CONCAT('bind:', platform_task_id) ELSE NULL END",
|
||||
persisted=False,
|
||||
),
|
||||
nullable=True,
|
||||
),
|
||||
)
|
||||
op.execute(
|
||||
sa.text(
|
||||
"""
|
||||
UPDATE douyu_tasks AS task
|
||||
JOIN (
|
||||
SELECT platform_task_id, MAX(id) AS keep_id
|
||||
FROM douyu_tasks
|
||||
WHERE task_type = 'get_bind_qr'
|
||||
AND platform_task_id IS NOT NULL
|
||||
AND status IN ('planned', 'pending', 'running')
|
||||
GROUP BY platform_task_id
|
||||
HAVING COUNT(*) > 1
|
||||
) AS duplicate
|
||||
ON duplicate.platform_task_id = task.platform_task_id
|
||||
SET task.status = 'stopped',
|
||||
task.message = '已被新的绑定二维码任务取代(迁移修复)',
|
||||
task.finished_at = UTC_TIMESTAMP(),
|
||||
task.next_run_at = NULL
|
||||
WHERE task.task_type = 'get_bind_qr'
|
||||
AND task.status IN ('planned', 'pending', 'running')
|
||||
AND task.id <> duplicate.keep_id
|
||||
"""
|
||||
)
|
||||
)
|
||||
if INDEX_NAME not in _indexes():
|
||||
op.create_index(
|
||||
INDEX_NAME,
|
||||
"douyu_tasks",
|
||||
["active_binding_key"],
|
||||
unique=True,
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
if INDEX_NAME in _indexes():
|
||||
op.drop_index(INDEX_NAME, table_name="douyu_tasks")
|
||||
if "active_binding_key" in _columns():
|
||||
op.drop_column("douyu_tasks", "active_binding_key")
|
||||
Reference in New Issue
Block a user