2026-09-11 12:16:06 +08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
import re
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
from alembic.config import Config
|
|
|
|
|
from alembic.script import ScriptDirectory
|
|
|
|
|
|
|
|
|
|
ROOT = Path(__file__).resolve().parents[2]
|
|
|
|
|
VERSIONS = ROOT / "alembic" / "versions"
|
|
|
|
|
BASELINE = ROOT / "alembic" / "baseline_generated.sql"
|
|
|
|
|
|
|
|
|
|
ADVISOR_FILES = (
|
|
|
|
|
"20260910_advisor_investment_goal.py",
|
|
|
|
|
"20260910_advisor_product_industry_exposure.py",
|
|
|
|
|
"20260910_advisor_portfolio_projection_checkpoint.py",
|
|
|
|
|
"20260910_advisor_product_metric_snapshot.py",
|
|
|
|
|
"20260910_advisor_product_asset_classification.py",
|
|
|
|
|
"20260910_advisor_product_reference_snapshot.py",
|
|
|
|
|
"20260910_advisor_offsite_fund_reference.py",
|
|
|
|
|
"20260910_advisor_product_price_history.py",
|
|
|
|
|
"20260910_advisor_product_governance_reference.py",
|
|
|
|
|
"20260910_advisor_governance_monitor_and_quotes.py",
|
|
|
|
|
"20260910_advisor_market_quote_resilience.py",
|
|
|
|
|
"20260910_advisor_data_quality_backtest.py",
|
|
|
|
|
"20260911_advisor_goal_conversation.py",
|
|
|
|
|
"20260911_advisor_profile_tag_governance.py",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def created_tables(path: Path) -> set[str]:
|
|
|
|
|
content = path.read_text(encoding="utf-8")
|
|
|
|
|
return set(re.findall(r"CREATE TABLE(?: IF NOT EXISTS)?\s+`?([A-Za-z0-9_]+)`?", content))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_advisor_migrations_form_one_chain_from_qyqy_head() -> None:
|
|
|
|
|
script = ScriptDirectory.from_config(Config(str(ROOT / "alembic.ini")))
|
|
|
|
|
assert len(script.get_heads()) == 1
|
2026-09-12 17:24:37 +08:00
|
|
|
|
|
|
|
|
# ⚠️ 这里**不写死 head 名**。原先断言
|
|
|
|
|
# `script.get_heads()[0] == "20260911_merge_adv_risk_heads"`
|
|
|
|
|
# 那是"投顾迁移刚加完那一刻"的快照;之后任何人新增迁移(2026-09-12 的
|
|
|
|
|
# 客户权益迁移 `20260912_customer_benefit` 即是一例)都会让本用例变红,
|
|
|
|
|
# 而变红的原因与投顾链的对错**无关** —— 断言测到的是时间,不是契约。
|
|
|
|
|
#
|
|
|
|
|
# 原意是"投顾链确实接在这条主链上、没另起一条分支"。改为断言
|
|
|
|
|
# **投顾链尾是当前 head 的祖先**:既保住这个意思,又不受后续迁移影响。
|
|
|
|
|
head = script.get_heads()[0]
|
|
|
|
|
ancestors = {rev.revision for rev in script.iterate_revisions(head, "base")}
|
|
|
|
|
advisor_tail = re.search(
|
|
|
|
|
r'revision = "([^"]+)"',
|
|
|
|
|
(VERSIONS / ADVISOR_FILES[-1]).read_text(encoding="utf-8"),
|
|
|
|
|
)
|
|
|
|
|
assert advisor_tail is not None
|
|
|
|
|
assert advisor_tail.group(1) in ancestors
|
2026-09-11 12:16:06 +08:00
|
|
|
|
|
|
|
|
first = (VERSIONS / ADVISOR_FILES[0]).read_text(encoding="utf-8")
|
|
|
|
|
assert 'down_revision = "20260910_drop_review_separation"' in first
|
|
|
|
|
for previous, current in zip(ADVISOR_FILES, ADVISOR_FILES[1:], strict=False):
|
|
|
|
|
previous_content = (VERSIONS / previous).read_text(encoding="utf-8")
|
|
|
|
|
current_content = (VERSIONS / current).read_text(encoding="utf-8")
|
|
|
|
|
previous_revision = re.search(r'revision = "([^"]+)"', previous_content)
|
|
|
|
|
assert previous_revision is not None
|
|
|
|
|
assert f'down_revision = "{previous_revision.group(1)}"' in current_content
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_advisor_migrations_only_create_additive_tables() -> None:
|
|
|
|
|
baseline_tables = created_tables(BASELINE)
|
|
|
|
|
advisor_tables: set[str] = set()
|
|
|
|
|
for filename in ADVISOR_FILES:
|
|
|
|
|
content = (VERSIONS / filename).read_text(encoding="utf-8")
|
|
|
|
|
assert "ALTER TABLE" not in content
|
|
|
|
|
assert "DROP TABLE" not in content
|
|
|
|
|
advisor_tables.update(created_tables(VERSIONS / filename))
|
|
|
|
|
|
|
|
|
|
assert advisor_tables
|
|
|
|
|
assert not advisor_tables & baseline_tables
|
|
|
|
|
assert "fin_sim_order" not in advisor_tables
|
|
|
|
|
assert "fin_transaction" not in advisor_tables
|
|
|
|
|
assert "fin_holding" not in advisor_tables
|