fix(backend-migrations): 修复数据库迁移脚本的父版本依赖并添加验证测试
修正两个数据库迁移脚本的父修订版本配置,新增测试用例验证迁移依赖链,避免PostgreSQL执行迁移时出现缺表错误
This commit is contained in:
1 parent
af8c39f652
commit
e579a7a90f
4 files changed
+59
-4
No files matched your search
@@ -1,7 +1,7 @@
|
||||
"""unify order profile template mode
|
||||
|
||||
Revision ID: ab2c3d4e5f6a
|
||||
Revises: ff6a7b8c9d0e
|
||||
Revises: a68f8091a2b4
|
||||
Create Date: 2026-08-03 00:00:00.000000
|
||||
"""
|
||||
|
||||
@@ -10,7 +10,7 @@ import sqlalchemy as sa
|
||||
|
||||
|
||||
revision = 'ab2c3d4e5f6a'
|
||||
down_revision = 'ff6a7b8c9d0e'
|
||||
down_revision = 'a68f8091a2b4'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
"""T6 add line_match_evidence_fields to line inventory matches
|
||||
|
||||
Revision ID: er5f6g7h8i9j
|
||||
Revises: dq2e3f4g5h6i
|
||||
Revises: dq2e3f4g5h6i, cs3c4d5e6f7a
|
||||
Create Date: 2026-08-16 11:00:00.000000
|
||||
|
||||
"""
|
||||
@@ -12,7 +12,7 @@ import sqlalchemy as sa
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = 'er5f6g7h8i9j'
|
||||
down_revision = 'dq2e3f4g5h6i'
|
||||
down_revision = ('dq2e3f4g5h6i', 'cs3c4d5e6f7a')
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
"""
|
||||
backend/tests/test_line_match_evidence_migration_revision.py
|
||||
|
||||
用途:验证行级库存匹配证据字段迁移的父 revision 覆盖目标表创建链。
|
||||
依赖:er5f6g7h8i9j 迁移模块。
|
||||
逻辑:目标表由 cr2b3c4d5e6f 创建,库存 RPA 表由 dq2e3f4g5h6i 链提供;
|
||||
迁移必须以双父 revision 合并两条链,避免 PostgreSQL 在 ALTER TABLE 时缺表。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import importlib
|
||||
|
||||
|
||||
def test_line_match_evidence_migration_merges_match_and_rpa_chains():
|
||||
"""关键断言:证据字段迁移同时依赖匹配表链和库存 RPA 链。"""
|
||||
migration = importlib.import_module(
|
||||
'migrations.versions.er5f6g7h8i9j_add_line_match_evidence_fields',
|
||||
)
|
||||
|
||||
assert set(migration.down_revision) == {'dq2e3f4g5h6i', 'cs3c4d5e6f7a'}
|
||||
@@ -0,0 +1,34 @@
|
||||
"""
|
||||
backend/tests/test_order_profile_template_mode_migration_revision.py
|
||||
|
||||
用途:验证订单 Profile 模式收敛迁移在建表迁移之后执行。
|
||||
依赖:ab2c3d4e5f6a 与 a68f8091a2b4 migration metadata。
|
||||
逻辑:目标表 shipping_order_document_profiles 由 a68f8091a2b4 创建,
|
||||
因此本迁移必须直接接在该 revision 后,避免先执行 UPDATE/ALTER TABLE 时缺表。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import ast
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def _revision_metadata(path: Path) -> dict[str, object]:
|
||||
tree = ast.parse(path.read_text(encoding='utf-8-sig'))
|
||||
values: dict[str, object] = {}
|
||||
for node in tree.body:
|
||||
if isinstance(node, ast.Assign):
|
||||
for target in node.targets:
|
||||
if isinstance(target, ast.Name) and target.id in {'revision', 'down_revision'}:
|
||||
values[target.id] = ast.literal_eval(node.value)
|
||||
return values
|
||||
|
||||
|
||||
def test_profile_template_mode_migration_follows_profile_table_creation():
|
||||
"""关键断言:模式收敛迁移直接依赖 Profile 建表迁移。"""
|
||||
migration = _revision_metadata(Path(
|
||||
'backend/migrations/versions/ab1c2d3e4f5a_unify_order_profile_template_mode.py',
|
||||
))
|
||||
|
||||
assert migration['revision'] == 'ab2c3d4e5f6a'
|
||||
assert migration['down_revision'] == 'a68f8091a2b4'
|
||||
Reference in new issue
Block a user