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)
|
||||
Reference in New Issue
Block a user