migrate 是按什么顺序执行迁移的?指定 app 时会多做哪些?回退时谁先撤销?这篇用 --plan 把每种情形的计划直接打出来,再用真实执行验证。
Planned operations: a.0001_initial Create model A a.0002_a_z Add field z to a b.0001_initial Create model B c.0001_initial Create model C c.0002_c_w Add field w to c contenttypes.0001_initial Create model ContentType Alter unique_together for contenttype (1 constraint(s)) contenttypes.0002_remove_content_type_name Change Meta options on contenttype Alter field name on contenttype Raw Python operation Remove field name from contenttype
计划顺序不是「按 app 分组」也不是「按编号排序」,只保证依赖先于被依赖者。不要依赖计划里没有依赖声明的顺序。
migrate b --plan:
Planned operations:
a.0001_initial
Create model A
b.0001_initial
Create model B
migrate a 0001 --plan:
Planned operations:
a.0001_initial
Create model Amigrate <app> <name> 是「迁移到这个点」:补齐依赖并停在这里;如果目标在当前状态之前,就是回退。
('a', '0001_initial')
('a', '0002_a_z')
('b', '0001_initial')
('c', '0001_initial')
('c', '0002_c_w')
('contenttypes', '0001_initial')
('contenttypes', '0002_remove_content_type_name')记录的顺序就是实际执行的顺序(id 递增)。
migrate a 0001 --plan(现在库里全部已应用):
Planned operations:
a.0002_a_z
Undo Add field z to a
migrate a zero --plan:
Planned operations:
b.0001_initial
Undo Create model B
a.0002_a_z
Undo Add field z to a
a.0001_initial
Undo Create model A回退计划同样由图决定:要撤销的迁移的「依赖者」必须先被撤销。
[X] c.0001_initial [X] a.0001_initial [X] a.0002_a_z [X] b.0001_initial [X] c.0002_c_w [X] contenttypes.0001_initial [X] contenttypes.0002_remove_content_type_name
本实验里我只验证了 showmigrations --plan 里的顺序变了,没有真正在会因顺序出错的场景里验证。