更新所有业务API文档,明确持久化资源的正式ID必须为服务端生成的稳定UUID,本地调试或接口失败时可使用语义ID作为fallback。新增数据库迁移脚本0022_opaque_ids,用于将历史语义ID转换为稳定UUID,并同步外键关联、详情记录的key字段以及审计日志的实体ID引用。新增该迁移的单元测试用例,验证ID替换与关联数据同步的逻辑正确性。调整MiniAPP前端代码,优化导航工具函数的格式,移除废弃函数并修改首页跳转逻辑,使用接口返回的UUID作为详情跳转参数。
61 lines
1.8 KiB
Python
61 lines
1.8 KiB
Python
from importlib.util import module_from_spec, spec_from_file_location
|
|
from pathlib import Path
|
|
from uuid import UUID, uuid4
|
|
|
|
|
|
MIGRATION_PATH = Path(__file__).parents[1] / "alembic" / "versions" / "0022_opaque_ids.py"
|
|
SPEC = spec_from_file_location("opaque_ids_migration", MIGRATION_PATH)
|
|
assert SPEC and SPEC.loader
|
|
MIGRATION = module_from_spec(SPEC)
|
|
SPEC.loader.exec_module(MIGRATION)
|
|
|
|
|
|
def test_existing_semantic_ids_are_replaced_with_stable_uuid_values():
|
|
existing_uuid = str(uuid4())
|
|
|
|
mapping = MIGRATION.build_id_mapping(["family-route", existing_uuid])
|
|
|
|
assert set(mapping) == {"family-route"}
|
|
UUID(mapping["family-route"])
|
|
assert mapping["family-route"] != "family-route"
|
|
|
|
|
|
def test_uuid_values_are_not_regenerated_during_migration():
|
|
existing_uuid = str(uuid4())
|
|
|
|
assert MIGRATION.is_opaque_id(existing_uuid)
|
|
assert not MIGRATION.is_opaque_id("family-route")
|
|
assert MIGRATION.build_id_mapping([existing_uuid]) == {}
|
|
|
|
|
|
def test_foreign_key_specs_capture_constraints_for_recreation():
|
|
foreign_keys = [
|
|
(
|
|
"WanfaRoute",
|
|
{
|
|
"name": "WanfaRoute_categoryId_fkey",
|
|
"constrained_columns": ["categoryId"],
|
|
"referred_table": "WanfaCategory",
|
|
"referred_columns": ["id"],
|
|
"options": {"ondelete": "RESTRICT"},
|
|
},
|
|
)
|
|
]
|
|
|
|
specs = MIGRATION.collect_fk_specs(
|
|
foreign_keys,
|
|
{"WanfaCategory": {"family-route": "category-uuid"}},
|
|
)
|
|
|
|
assert specs == [
|
|
{
|
|
"name": "WanfaRoute_categoryId_fkey",
|
|
"source_table": "WanfaRoute",
|
|
"source_columns": ["categoryId"],
|
|
"target_table": "WanfaCategory",
|
|
"target_columns": ["id"],
|
|
"ondelete": "RESTRICT",
|
|
"onupdate": None,
|
|
}
|
|
]
|