## 问题 产品列表与排行页的「最近涨跌」全是"暂无"。原因是它只能由**两个交易日的收盘价** 现算,而 `fin_market_price` 每只产品每天只有一行 —— 库里头一天只有一天数据时 根本算不出来。 ## 但行情源本来就给了这个数 腾讯行情接口的第 32 位就是**当日涨跌幅**(适配器此前只解析了开高低收量额,没取它)。 所以不必等第二个交易日去算,把源给的数存下来即可。 ## 改动 - **迁移** `20260913_market_price_change_pct`:给 `fin_market_price` **新增一个可空列** `change_pct DECIMAL(10,4)`。只加列、不改任何已有字段(AGENTS.md 规则 2 允许), 可空且不回填,既有读写方全部不受影响。 - **适配器**:`_parse_tencent` 增解析位置 4(昨收)与位置 32(涨跌幅)。 - **同步服务**:写入 `change_pct`;降级路径(净值兜底)没有这个数就写 **NULL**。 - **接口**:`_resolve_change_pct` **优先用存下来的值**;迁移前的历史行没有该列的值, 才回退到"今日收盘 vs 昨收"现算。仍为 `null` 时前端显示"暂无" —— **不得当成 0**(`formatPercent(null)` 会渲染成 `+0.00%`,那等于说"今天平盘")。 ## 契约测试同步 两处契约断言因结构演进需要跟进 —— 它们的作用正是拦住这类变更: - `test_fund_readonly_contract.py`:`fin_market_price` 列数 **13 → 14** - `test_advisor_migration_contract.py`:迁移 head 更新为新版本 (核心断言仍是"链收敛到唯一 head",此处只是钉住末端) 验证:实测 20 只产品**全部有真实涨跌幅**(如 515450 −0.43%、159511 +1.23%); unit+contract **1397 passed**;integration **110 passed**;ruff 通过; mypy 251 文件 0 错;`audit_schema` 与 `migration_state_check` 均通过;e2e 冒烟 40/40。
68 lines
2.9 KiB
Python
68 lines
2.9 KiB
Python
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
|
|
# 核心断言是上面那句"链收敛到一个 head";下面钉住当前末端版本,便于发现迁移被误删或分叉。
|
|
# ⚠️ **新增迁移后要同步更新这个值**。2026-09-13 追加了
|
|
# `20260913_market_price_change_pct`(给 `fin_market_price` 补 `change_pct`)。
|
|
assert script.get_heads()[0] == "20260913_market_price_change_pct"
|
|
|
|
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
|