feat(platform): 增加履约分类映射管理并锁定迁移
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
"""已发布 Alembic 迁移不可被静默修改。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from web.backend.migrations import immutability
|
||||
|
||||
|
||||
def test_committed_migration_manifest_matches_files():
|
||||
immutability.verify_manifest()
|
||||
|
||||
|
||||
def test_manifest_rejects_modified_released_migration(tmp_path: Path):
|
||||
versions = tmp_path / "versions"
|
||||
versions.mkdir()
|
||||
migration = versions / "20260101_0001_example.py"
|
||||
migration.write_text('revision = "20260101_0001"\n', encoding="utf-8")
|
||||
manifest = tmp_path / "migration_manifest.json"
|
||||
immutability.write_manifest(versions_dir=versions, manifest_path=manifest)
|
||||
|
||||
migration.write_text(
|
||||
'revision = "20260101_0001"\n# accidental released change\n',
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
with pytest.raises(immutability.MigrationManifestError, match="内容被修改"):
|
||||
immutability.verify_manifest(
|
||||
versions_dir=versions,
|
||||
manifest_path=manifest,
|
||||
)
|
||||
|
||||
with pytest.raises(immutability.MigrationManifestError, match="拒绝更新"):
|
||||
immutability.write_manifest(versions_dir=versions, manifest_path=manifest)
|
||||
+165
-1
@@ -32,6 +32,7 @@ from web.backend.models import (
|
||||
AutomationJob,
|
||||
DouyuExternalOrder,
|
||||
HuyaExternalOrder,
|
||||
PlatformCommodityMapping,
|
||||
PlatformFulfillmentTask,
|
||||
User,
|
||||
WeiboAccount,
|
||||
@@ -41,7 +42,7 @@ TESTS_DIR = Path(__file__).resolve().parent
|
||||
PROJECT_ROOT = TESTS_DIR.parent
|
||||
VERSIONS_DIR = PROJECT_ROOT / "web" / "backend" / "migrations" / "versions"
|
||||
|
||||
HEAD_REVISION = "20261121_0105"
|
||||
HEAD_REVISION = "20261121_0107"
|
||||
|
||||
# 迁移新增、但不声明在模型里的复合查询索引。
|
||||
EXTRA_INDEXES = (
|
||||
@@ -371,6 +372,169 @@ class TestMigrationSmoke:
|
||||
with admin_engine.begin() as conn:
|
||||
conn.execute(text(f"DROP DATABASE IF EXISTS `{mig_db}`;"))
|
||||
|
||||
def test_0106_adds_fulfillment_classification_and_backfills_mapping(self):
|
||||
creds = get_mysql_credentials()
|
||||
mig_db = f"douyu_login_mig_{secrets.token_hex(4)}"
|
||||
mig_url = (
|
||||
f"mysql+pymysql://{quote_plus(creds['user'])}:"
|
||||
f"{quote_plus(creds['password'])}@{creds['host']}:{creds['port']}/"
|
||||
f"{mig_db}?charset=utf8mb4"
|
||||
)
|
||||
admin_engine = get_test_engine()
|
||||
mig_engine = create_engine(mig_url)
|
||||
original_url = database_module.DATABASE_URL
|
||||
with admin_engine.begin() as conn:
|
||||
conn.execute(
|
||||
text(
|
||||
f"CREATE DATABASE `{mig_db}` "
|
||||
"CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci;"
|
||||
)
|
||||
)
|
||||
try:
|
||||
database_module.DATABASE_URL = mig_url
|
||||
cfg = _migration_config()
|
||||
cfg.set_main_option("sqlalchemy.url", mig_url)
|
||||
command.upgrade(cfg, "20261121_0105")
|
||||
with mig_engine.begin() as conn:
|
||||
conn.execute(
|
||||
text(
|
||||
"""
|
||||
INSERT INTO platform_commodity_mappings (
|
||||
target_platform, internal_sku_key, commodity_id,
|
||||
goods_name, required_points, scope, enabled,
|
||||
created_at, updated_at
|
||||
) VALUES (
|
||||
'douyu', '套装-分类迁移', 'bag-classification',
|
||||
'套装-分类迁移', 1280, 'elite', 1,
|
||||
UTC_TIMESTAMP(), UTC_TIMESTAMP()
|
||||
)
|
||||
"""
|
||||
)
|
||||
)
|
||||
|
||||
command.upgrade(cfg, "head")
|
||||
with Session(mig_engine) as session:
|
||||
row = session.query(PlatformCommodityMapping).one()
|
||||
assert row.campaign_key == "douyu-current"
|
||||
assert row.sale_mode == "unknown"
|
||||
assert row.version == 1
|
||||
assert row.archived_at is None
|
||||
|
||||
command.downgrade(cfg, "20261121_0105")
|
||||
columns = {
|
||||
item["name"]
|
||||
for item in inspect(mig_engine).get_columns(
|
||||
"platform_commodity_mappings"
|
||||
)
|
||||
}
|
||||
assert "sale_mode" not in columns
|
||||
assert "campaign_key" not in columns
|
||||
finally:
|
||||
database_module.DATABASE_URL = original_url
|
||||
mig_engine.dispose()
|
||||
with admin_engine.begin() as conn:
|
||||
conn.execute(text(f"DROP DATABASE IF EXISTS `{mig_db}`;"))
|
||||
|
||||
def test_0107_repairs_missing_active_binding_key_and_deduplicates(self):
|
||||
creds = get_mysql_credentials()
|
||||
mig_db = f"douyu_login_mig_{secrets.token_hex(4)}"
|
||||
mig_url = (
|
||||
f"mysql+pymysql://{quote_plus(creds['user'])}:"
|
||||
f"{quote_plus(creds['password'])}@{creds['host']}:{creds['port']}/"
|
||||
f"{mig_db}?charset=utf8mb4"
|
||||
)
|
||||
admin_engine = get_test_engine()
|
||||
mig_engine = create_engine(mig_url)
|
||||
original_url = database_module.DATABASE_URL
|
||||
with admin_engine.begin() as conn:
|
||||
conn.execute(
|
||||
text(
|
||||
f"CREATE DATABASE `{mig_db}` "
|
||||
"CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci;"
|
||||
)
|
||||
)
|
||||
try:
|
||||
database_module.DATABASE_URL = mig_url
|
||||
cfg = _migration_config()
|
||||
cfg.set_main_option("sqlalchemy.url", mig_url)
|
||||
command.upgrade(cfg, "20261121_0106")
|
||||
with Session(mig_engine) as session:
|
||||
user = User(
|
||||
username="repair-0107-user",
|
||||
password_hash="hash",
|
||||
role="super_admin",
|
||||
)
|
||||
session.add(user)
|
||||
session.commit()
|
||||
account = Account(
|
||||
username="repair-0107-account",
|
||||
password="p",
|
||||
email="repair-0107@example.com",
|
||||
email_password="ep",
|
||||
)
|
||||
session.add(account)
|
||||
session.commit()
|
||||
user_id = user.id
|
||||
account_id = account.id
|
||||
with mig_engine.begin() as conn:
|
||||
conn.execute(
|
||||
text(
|
||||
"ALTER TABLE douyu_tasks "
|
||||
"DROP INDEX uq_douyu_tasks_active_binding"
|
||||
)
|
||||
)
|
||||
conn.execute(
|
||||
text("ALTER TABLE douyu_tasks DROP COLUMN active_binding_key")
|
||||
)
|
||||
for _ in range(2):
|
||||
conn.execute(
|
||||
text(
|
||||
"""
|
||||
INSERT INTO douyu_tasks (
|
||||
batch_id, account_id, task_type, handbook_scope,
|
||||
status, message, created_by, created_at,
|
||||
platform_task_id, queue_lane, attempt_count
|
||||
) VALUES (
|
||||
'repair-0107-batch', :account_id, 'get_bind_qr',
|
||||
'elite', 'running', 'repair-test', :user_id,
|
||||
UTC_TIMESTAMP(), 555, 'binding_poll', 0
|
||||
)
|
||||
"""
|
||||
),
|
||||
{"account_id": account_id, "user_id": user_id},
|
||||
)
|
||||
|
||||
command.upgrade(cfg, "head")
|
||||
with mig_engine.connect() as conn:
|
||||
columns = {
|
||||
item["name"]
|
||||
for item in inspect(conn).get_columns("douyu_tasks")
|
||||
}
|
||||
indexes = {
|
||||
item["name"]
|
||||
for item in inspect(conn).get_indexes("douyu_tasks")
|
||||
}
|
||||
rows = conn.execute(
|
||||
text(
|
||||
"""
|
||||
SELECT status, message, active_binding_key
|
||||
FROM douyu_tasks
|
||||
WHERE batch_id = 'repair-0107-batch'
|
||||
ORDER BY id
|
||||
"""
|
||||
)
|
||||
).mappings().all()
|
||||
assert "active_binding_key" in columns
|
||||
assert "uq_douyu_tasks_active_binding" in indexes
|
||||
assert [row["status"] for row in rows] == ["stopped", "running"]
|
||||
assert rows[0]["active_binding_key"] is None
|
||||
assert rows[1]["active_binding_key"] == "bind:555"
|
||||
finally:
|
||||
database_module.DATABASE_URL = original_url
|
||||
mig_engine.dispose()
|
||||
with admin_engine.begin() as conn:
|
||||
conn.execute(text(f"DROP DATABASE IF EXISTS `{mig_db}`;"))
|
||||
|
||||
def test_0077_converts_super_admin_membership_to_overlay(self):
|
||||
creds = get_mysql_credentials()
|
||||
mig_db = f"douyu_login_mig_{secrets.token_hex(4)}"
|
||||
|
||||
@@ -455,6 +455,95 @@ class TestSyncAndMapping(_Base):
|
||||
assert task.execution_status == "ready"
|
||||
assert task.commodity_id == COMMODITY
|
||||
assert task.required_points == 1280
|
||||
assert task.sale_mode == "unknown"
|
||||
assert task.campaign_key == "douyu-current"
|
||||
assert task.classification_source == "unknown"
|
||||
|
||||
def test_mapping_sale_mode_is_snapshotted_and_drives_presale_queue(self):
|
||||
task = self.task(mapped=False)
|
||||
self.session.add(
|
||||
DouyuGoodsSnapshot(
|
||||
commodity_id=COMMODITY,
|
||||
name=INTERNAL_SKU,
|
||||
score=1280,
|
||||
)
|
||||
)
|
||||
self.session.commit()
|
||||
available_from = NOW + timedelta(days=1)
|
||||
|
||||
mapping = service.create_mapping(
|
||||
self.session,
|
||||
internal_sku_key=INTERNAL_SKU,
|
||||
campaign_key="douyu-2026-12-return",
|
||||
campaign_name="12 月返场",
|
||||
commodity_id=COMMODITY,
|
||||
goods_name=INTERNAL_SKU,
|
||||
required_points=1280,
|
||||
sale_mode="presale",
|
||||
available_from=available_from,
|
||||
available_until=available_from + timedelta(days=7),
|
||||
released_at=None,
|
||||
enabled=True,
|
||||
current=self.admin,
|
||||
)
|
||||
|
||||
self.session.refresh(task)
|
||||
detail = service.get_task_detail(self.session, task.id)
|
||||
assert mapping["saleMode"] == "presale"
|
||||
assert mapping["availabilityState"] == "waiting_release"
|
||||
assert task.sale_mode == "presale"
|
||||
assert task.campaign_key == "douyu-2026-12-return"
|
||||
assert task.classification_source == "mapping"
|
||||
assert detail["task"]["queueBucket"] == "presale_waiting"
|
||||
assert detail["task"]["availabilityState"] == "waiting_release"
|
||||
|
||||
def test_mapping_crud_archive_and_delete_guard(self):
|
||||
task = self.task(mapped=False)
|
||||
self.session.commit()
|
||||
created = service.create_mapping(
|
||||
self.session,
|
||||
internal_sku_key=INTERNAL_SKU,
|
||||
campaign_key="douyu-2026-12-direct",
|
||||
campaign_name="12 月现货",
|
||||
commodity_id=COMMODITY,
|
||||
goods_name=INTERNAL_SKU,
|
||||
required_points=1280,
|
||||
sale_mode="direct",
|
||||
available_from=NOW,
|
||||
available_until=NOW + timedelta(days=30),
|
||||
released_at=NOW,
|
||||
enabled=True,
|
||||
current=self.admin,
|
||||
)
|
||||
updated = service.update_mapping(
|
||||
self.session,
|
||||
mapping_id=created["id"],
|
||||
internal_sku_key=INTERNAL_SKU,
|
||||
campaign_key="douyu-2026-12-direct",
|
||||
campaign_name="12 月现货修正",
|
||||
commodity_id=COMMODITY,
|
||||
goods_name="套装-兔兔白日梦(斗鱼)",
|
||||
required_points=1280,
|
||||
sale_mode="direct",
|
||||
available_from=NOW,
|
||||
available_until=NOW + timedelta(days=30),
|
||||
released_at=NOW,
|
||||
enabled=True,
|
||||
current=self.admin,
|
||||
)
|
||||
|
||||
assert updated["campaignName"] == "12 月现货修正"
|
||||
assert updated["version"] == 2
|
||||
with pytest.raises(HTTPException, match="只能归档"):
|
||||
service.delete_mapping(self.session, mapping_id=created["id"])
|
||||
archived = service.archive_mapping(
|
||||
self.session,
|
||||
mapping_id=created["id"],
|
||||
current=self.admin,
|
||||
)
|
||||
self.session.refresh(task)
|
||||
assert archived["archivedAt"] is not None
|
||||
assert task.execution_status == "pending_mapping"
|
||||
|
||||
|
||||
class TestFulfillmentCollabSync(_Base):
|
||||
|
||||
Reference in New Issue
Block a user