Skip to content

Commit e9ad5ac

Browse files
authored
refactor: 修复migrations的问题 --story=130082912 (#587)
1 parent 846c9cf commit e9ad5ac

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

bkflow/template/migrations/0009_alter_templatesnapshot.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,26 @@
2121

2222
from django.db import migrations
2323

24-
from bkflow.space.configs import FlowVersioning
25-
from bkflow.space.models import SpaceConfig
26-
from bkflow.template.models import Template, TemplateOperationRecord, TemplateSnapshot
2724
from bkflow.utils.version import bump_custom
2825

2926
logger = logging.getLogger(__name__)
3027

28+
# FlowVersioning 配置名称常量,避免导入 bkflow.space.configs
29+
FLOW_VERSIONING_CONFIG_NAME = "flow_versioning"
30+
3131

3232
def add_version_to_snapshots(apps, schema_editor):
3333
"""
3434
为未开启版本管理的空间下的模板快照添加版本号
3535
版本号从1.0.0开始,按创建时间依次递增
3636
"""
37+
# 使用历史模型,避免访问当前代码中还未迁移的字段
38+
SpaceConfig = apps.get_model("space", "SpaceConfig")
39+
Template = apps.get_model("template", "Template")
40+
TemplateSnapshot = apps.get_model("template", "TemplateSnapshot")
41+
3742
disabled_versioning_spaces = []
38-
all_space_configs = SpaceConfig.objects.filter(name=FlowVersioning.name)
43+
all_space_configs = SpaceConfig.objects.filter(name=FLOW_VERSIONING_CONFIG_NAME)
3944

4045
for config in all_space_configs:
4146
if config.value_type == "TEXT":
@@ -110,6 +115,9 @@ def add_version_to_snapshots(apps, schema_editor):
110115

111116
def reverse_add_version_to_snapshots(apps, schema_editor):
112117
"""回滚操作:只清除本次迁移修改的快照的版本号"""
118+
# 使用历史模型
119+
TemplateSnapshot = apps.get_model("template", "TemplateSnapshot")
120+
113121
# 尝试获取本次迁移修改的快照ID
114122
modified_snapshot_ids = []
115123
if hasattr(schema_editor.connection, "migration_data"):
@@ -126,6 +134,8 @@ def reverse_add_version_to_snapshots(apps, schema_editor):
126134
class Migration(migrations.Migration):
127135
dependencies = [
128136
("template", "0008_alter_templatesnapshot_version"),
137+
# 需要依赖 space app,因为迁移中使用了 SpaceConfig 模型
138+
("space", "0002_spaceconfig"),
129139
]
130140

131141
operations = [

0 commit comments

Comments
 (0)