2121
2222from 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
2724from bkflow .utils .version import bump_custom
2825
2926logger = logging .getLogger (__name__ )
3027
28+ # FlowVersioning 配置名称常量,避免导入 bkflow.space.configs
29+ FLOW_VERSIONING_CONFIG_NAME = "flow_versioning"
30+
3131
3232def 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
111116def 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):
126134class 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