autodetector.py 78 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779
  1. import functools
  2. import re
  3. from collections import defaultdict
  4. from graphlib import TopologicalSorter
  5. from itertools import chain
  6. from django.conf import settings
  7. from django.db import models
  8. from django.db.migrations import operations
  9. from django.db.migrations.migration import Migration
  10. from django.db.migrations.operations.models import AlterModelOptions
  11. from django.db.migrations.optimizer import MigrationOptimizer
  12. from django.db.migrations.questioner import MigrationQuestioner
  13. from django.db.migrations.utils import (
  14. COMPILED_REGEX_TYPE,
  15. RegexObject,
  16. resolve_relation,
  17. )
  18. class MigrationAutodetector:
  19. """
  20. Take a pair of ProjectStates and compare them to see what the first would
  21. need doing to make it match the second (the second usually being the
  22. project's current state).
  23. Note that this naturally operates on entire projects at a time,
  24. as it's likely that changes interact (for example, you can't
  25. add a ForeignKey without having a migration to add the table it
  26. depends on first). A user interface may offer single-app usage
  27. if it wishes, with the caveat that it may not always be possible.
  28. """
  29. def __init__(self, from_state, to_state, questioner=None):
  30. self.from_state = from_state
  31. self.to_state = to_state
  32. self.questioner = questioner or MigrationQuestioner()
  33. self.existing_apps = {app for app, model in from_state.models}
  34. def changes(self, graph, trim_to_apps=None, convert_apps=None, migration_name=None):
  35. """
  36. Main entry point to produce a list of applicable changes.
  37. Take a graph to base names on and an optional set of apps
  38. to try and restrict to (restriction is not guaranteed)
  39. """
  40. changes = self._detect_changes(convert_apps, graph)
  41. changes = self.arrange_for_graph(changes, graph, migration_name)
  42. if trim_to_apps:
  43. changes = self._trim_to_apps(changes, trim_to_apps)
  44. return changes
  45. def deep_deconstruct(self, obj):
  46. """
  47. Recursive deconstruction for a field and its arguments.
  48. Used for full comparison for rename/alter; sometimes a single-level
  49. deconstruction will not compare correctly.
  50. """
  51. if isinstance(obj, list):
  52. return [self.deep_deconstruct(value) for value in obj]
  53. elif isinstance(obj, tuple):
  54. return tuple(self.deep_deconstruct(value) for value in obj)
  55. elif isinstance(obj, dict):
  56. return {key: self.deep_deconstruct(value) for key, value in obj.items()}
  57. elif isinstance(obj, functools.partial):
  58. return (
  59. obj.func,
  60. self.deep_deconstruct(obj.args),
  61. self.deep_deconstruct(obj.keywords),
  62. )
  63. elif isinstance(obj, COMPILED_REGEX_TYPE):
  64. return RegexObject(obj)
  65. elif isinstance(obj, type):
  66. # If this is a type that implements 'deconstruct' as an instance method,
  67. # avoid treating this as being deconstructible itself - see #22951
  68. return obj
  69. elif hasattr(obj, "deconstruct"):
  70. deconstructed = obj.deconstruct()
  71. if isinstance(obj, models.Field):
  72. # we have a field which also returns a name
  73. deconstructed = deconstructed[1:]
  74. path, args, kwargs = deconstructed
  75. return (
  76. path,
  77. [self.deep_deconstruct(value) for value in args],
  78. {key: self.deep_deconstruct(value) for key, value in kwargs.items()},
  79. )
  80. else:
  81. return obj
  82. def only_relation_agnostic_fields(self, fields):
  83. """
  84. Return a definition of the fields that ignores field names and
  85. what related fields actually relate to. Used for detecting renames (as
  86. the related fields change during renames).
  87. """
  88. fields_def = []
  89. for name, field in sorted(fields.items()):
  90. deconstruction = self.deep_deconstruct(field)
  91. if field.remote_field and field.remote_field.model:
  92. deconstruction[2].pop("to", None)
  93. fields_def.append(deconstruction)
  94. return fields_def
  95. def _detect_changes(self, convert_apps=None, graph=None):
  96. """
  97. Return a dict of migration plans which will achieve the
  98. change from from_state to to_state. The dict has app labels
  99. as keys and a list of migrations as values.
  100. The resulting migrations aren't specially named, but the names
  101. do matter for dependencies inside the set.
  102. convert_apps is the list of apps to convert to use migrations
  103. (i.e. to make initial migrations for, in the usual case)
  104. graph is an optional argument that, if provided, can help improve
  105. dependency generation and avoid potential circular dependencies.
  106. """
  107. # The first phase is generating all the operations for each app
  108. # and gathering them into a big per-app list.
  109. # Then go through that list, order it, and split into migrations to
  110. # resolve dependencies caused by M2Ms and FKs.
  111. self.generated_operations = {}
  112. self.altered_indexes = {}
  113. self.altered_constraints = {}
  114. self.renamed_fields = {}
  115. # Prepare some old/new state and model lists, separating
  116. # proxy models and ignoring unmigrated apps.
  117. self.old_model_keys = set()
  118. self.old_proxy_keys = set()
  119. self.old_unmanaged_keys = set()
  120. self.new_model_keys = set()
  121. self.new_proxy_keys = set()
  122. self.new_unmanaged_keys = set()
  123. for (app_label, model_name), model_state in self.from_state.models.items():
  124. if not model_state.options.get("managed", True):
  125. self.old_unmanaged_keys.add((app_label, model_name))
  126. elif app_label not in self.from_state.real_apps:
  127. if model_state.options.get("proxy"):
  128. self.old_proxy_keys.add((app_label, model_name))
  129. else:
  130. self.old_model_keys.add((app_label, model_name))
  131. for (app_label, model_name), model_state in self.to_state.models.items():
  132. if not model_state.options.get("managed", True):
  133. self.new_unmanaged_keys.add((app_label, model_name))
  134. elif app_label not in self.from_state.real_apps or (
  135. convert_apps and app_label in convert_apps
  136. ):
  137. if model_state.options.get("proxy"):
  138. self.new_proxy_keys.add((app_label, model_name))
  139. else:
  140. self.new_model_keys.add((app_label, model_name))
  141. self.from_state.resolve_fields_and_relations()
  142. self.to_state.resolve_fields_and_relations()
  143. # Renames have to come first
  144. self.generate_renamed_models()
  145. # Prepare lists of fields and generate through model map
  146. self._prepare_field_lists()
  147. self._generate_through_model_map()
  148. # Generate non-rename model operations
  149. self.generate_deleted_models()
  150. self.generate_created_models()
  151. self.generate_deleted_proxies()
  152. self.generate_created_proxies()
  153. self.generate_altered_options()
  154. self.generate_altered_managers()
  155. self.generate_altered_db_table_comment()
  156. # Create the renamed fields and store them in self.renamed_fields.
  157. # They are used by create_altered_indexes(), generate_altered_fields(),
  158. # generate_removed_altered_index/unique_together(), and
  159. # generate_altered_index/unique_together().
  160. self.create_renamed_fields()
  161. # Create the altered indexes and store them in self.altered_indexes.
  162. # This avoids the same computation in generate_removed_indexes()
  163. # and generate_added_indexes().
  164. self.create_altered_indexes()
  165. self.create_altered_constraints()
  166. # Generate index removal operations before field is removed
  167. self.generate_removed_constraints()
  168. self.generate_removed_indexes()
  169. # Generate field renaming operations.
  170. self.generate_renamed_fields()
  171. self.generate_renamed_indexes()
  172. # Generate removal of foo together.
  173. self.generate_removed_altered_unique_together()
  174. self.generate_removed_altered_index_together() # RemovedInDjango51Warning.
  175. # Generate field operations.
  176. self.generate_removed_fields()
  177. self.generate_added_fields()
  178. self.generate_altered_fields()
  179. self.generate_altered_order_with_respect_to()
  180. self.generate_altered_unique_together()
  181. self.generate_altered_index_together() # RemovedInDjango51Warning.
  182. self.generate_added_indexes()
  183. self.generate_added_constraints()
  184. self.generate_altered_db_table()
  185. self._sort_migrations()
  186. self._build_migration_list(graph)
  187. self._optimize_migrations()
  188. return self.migrations
  189. def _prepare_field_lists(self):
  190. """
  191. Prepare field lists and a list of the fields that used through models
  192. in the old state so dependencies can be made from the through model
  193. deletion to the field that uses it.
  194. """
  195. self.kept_model_keys = self.old_model_keys & self.new_model_keys
  196. self.kept_proxy_keys = self.old_proxy_keys & self.new_proxy_keys
  197. self.kept_unmanaged_keys = self.old_unmanaged_keys & self.new_unmanaged_keys
  198. self.through_users = {}
  199. self.old_field_keys = {
  200. (app_label, model_name, field_name)
  201. for app_label, model_name in self.kept_model_keys
  202. for field_name in self.from_state.models[
  203. app_label, self.renamed_models.get((app_label, model_name), model_name)
  204. ].fields
  205. }
  206. self.new_field_keys = {
  207. (app_label, model_name, field_name)
  208. for app_label, model_name in self.kept_model_keys
  209. for field_name in self.to_state.models[app_label, model_name].fields
  210. }
  211. def _generate_through_model_map(self):
  212. """Through model map generation."""
  213. for app_label, model_name in sorted(self.old_model_keys):
  214. old_model_name = self.renamed_models.get(
  215. (app_label, model_name), model_name
  216. )
  217. old_model_state = self.from_state.models[app_label, old_model_name]
  218. for field_name, field in old_model_state.fields.items():
  219. if hasattr(field, "remote_field") and getattr(
  220. field.remote_field, "through", None
  221. ):
  222. through_key = resolve_relation(
  223. field.remote_field.through, app_label, model_name
  224. )
  225. self.through_users[through_key] = (
  226. app_label,
  227. old_model_name,
  228. field_name,
  229. )
  230. @staticmethod
  231. def _resolve_dependency(dependency):
  232. """
  233. Return the resolved dependency and a boolean denoting whether or not
  234. it was swappable.
  235. """
  236. if dependency[0] != "__setting__":
  237. return dependency, False
  238. resolved_app_label, resolved_object_name = getattr(
  239. settings, dependency[1]
  240. ).split(".")
  241. return (resolved_app_label, resolved_object_name.lower()) + dependency[2:], True
  242. def _build_migration_list(self, graph=None):
  243. """
  244. Chop the lists of operations up into migrations with dependencies on
  245. each other. Do this by going through an app's list of operations until
  246. one is found that has an outgoing dependency that isn't in another
  247. app's migration yet (hasn't been chopped off its list). Then chop off
  248. the operations before it into a migration and move onto the next app.
  249. If the loops completes without doing anything, there's a circular
  250. dependency (which _should_ be impossible as the operations are
  251. all split at this point so they can't depend and be depended on).
  252. """
  253. self.migrations = {}
  254. num_ops = sum(len(x) for x in self.generated_operations.values())
  255. chop_mode = False
  256. while num_ops:
  257. # On every iteration, we step through all the apps and see if there
  258. # is a completed set of operations.
  259. # If we find that a subset of the operations are complete we can
  260. # try to chop it off from the rest and continue, but we only
  261. # do this if we've already been through the list once before
  262. # without any chopping and nothing has changed.
  263. for app_label in sorted(self.generated_operations):
  264. chopped = []
  265. dependencies = set()
  266. for operation in list(self.generated_operations[app_label]):
  267. deps_satisfied = True
  268. operation_dependencies = set()
  269. for dep in operation._auto_deps:
  270. # Temporarily resolve the swappable dependency to
  271. # prevent circular references. While keeping the
  272. # dependency checks on the resolved model, add the
  273. # swappable dependencies.
  274. original_dep = dep
  275. dep, is_swappable_dep = self._resolve_dependency(dep)
  276. if dep[0] != app_label:
  277. # External app dependency. See if it's not yet
  278. # satisfied.
  279. for other_operation in self.generated_operations.get(
  280. dep[0], []
  281. ):
  282. if self.check_dependency(other_operation, dep):
  283. deps_satisfied = False
  284. break
  285. if not deps_satisfied:
  286. break
  287. else:
  288. if is_swappable_dep:
  289. operation_dependencies.add(
  290. (original_dep[0], original_dep[1])
  291. )
  292. elif dep[0] in self.migrations:
  293. operation_dependencies.add(
  294. (dep[0], self.migrations[dep[0]][-1].name)
  295. )
  296. else:
  297. # If we can't find the other app, we add a
  298. # first/last dependency, but only if we've
  299. # already been through once and checked
  300. # everything.
  301. if chop_mode:
  302. # If the app already exists, we add a
  303. # dependency on the last migration, as
  304. # we don't know which migration
  305. # contains the target field. If it's
  306. # not yet migrated or has no
  307. # migrations, we use __first__.
  308. if graph and graph.leaf_nodes(dep[0]):
  309. operation_dependencies.add(
  310. graph.leaf_nodes(dep[0])[0]
  311. )
  312. else:
  313. operation_dependencies.add(
  314. (dep[0], "__first__")
  315. )
  316. else:
  317. deps_satisfied = False
  318. if deps_satisfied:
  319. chopped.append(operation)
  320. dependencies.update(operation_dependencies)
  321. del self.generated_operations[app_label][0]
  322. else:
  323. break
  324. # Make a migration! Well, only if there's stuff to put in it
  325. if dependencies or chopped:
  326. if not self.generated_operations[app_label] or chop_mode:
  327. subclass = type(
  328. "Migration",
  329. (Migration,),
  330. {"operations": [], "dependencies": []},
  331. )
  332. instance = subclass(
  333. "auto_%i" % (len(self.migrations.get(app_label, [])) + 1),
  334. app_label,
  335. )
  336. instance.dependencies = list(dependencies)
  337. instance.operations = chopped
  338. instance.initial = app_label not in self.existing_apps
  339. self.migrations.setdefault(app_label, []).append(instance)
  340. chop_mode = False
  341. else:
  342. self.generated_operations[app_label] = (
  343. chopped + self.generated_operations[app_label]
  344. )
  345. new_num_ops = sum(len(x) for x in self.generated_operations.values())
  346. if new_num_ops == num_ops:
  347. if not chop_mode:
  348. chop_mode = True
  349. else:
  350. raise ValueError(
  351. "Cannot resolve operation dependencies: %r"
  352. % self.generated_operations
  353. )
  354. num_ops = new_num_ops
  355. def _sort_migrations(self):
  356. """
  357. Reorder to make things possible. Reordering may be needed so FKs work
  358. nicely inside the same app.
  359. """
  360. for app_label, ops in sorted(self.generated_operations.items()):
  361. ts = TopologicalSorter()
  362. for op in ops:
  363. ts.add(op)
  364. for dep in op._auto_deps:
  365. # Resolve intra-app dependencies to handle circular
  366. # references involving a swappable model.
  367. dep = self._resolve_dependency(dep)[0]
  368. if dep[0] != app_label:
  369. continue
  370. ts.add(op, *(x for x in ops if self.check_dependency(x, dep)))
  371. self.generated_operations[app_label] = list(ts.static_order())
  372. def _optimize_migrations(self):
  373. # Add in internal dependencies among the migrations
  374. for app_label, migrations in self.migrations.items():
  375. for m1, m2 in zip(migrations, migrations[1:]):
  376. m2.dependencies.append((app_label, m1.name))
  377. # De-dupe dependencies
  378. for migrations in self.migrations.values():
  379. for migration in migrations:
  380. migration.dependencies = list(set(migration.dependencies))
  381. # Optimize migrations
  382. for app_label, migrations in self.migrations.items():
  383. for migration in migrations:
  384. migration.operations = MigrationOptimizer().optimize(
  385. migration.operations, app_label
  386. )
  387. def check_dependency(self, operation, dependency):
  388. """
  389. Return True if the given operation depends on the given dependency,
  390. False otherwise.
  391. """
  392. # Created model
  393. if dependency[2] is None and dependency[3] is True:
  394. return (
  395. isinstance(operation, operations.CreateModel)
  396. and operation.name_lower == dependency[1].lower()
  397. )
  398. # Created field
  399. elif dependency[2] is not None and dependency[3] is True:
  400. return (
  401. isinstance(operation, operations.CreateModel)
  402. and operation.name_lower == dependency[1].lower()
  403. and any(dependency[2] == x for x, y in operation.fields)
  404. ) or (
  405. isinstance(operation, operations.AddField)
  406. and operation.model_name_lower == dependency[1].lower()
  407. and operation.name_lower == dependency[2].lower()
  408. )
  409. # Removed field
  410. elif dependency[2] is not None and dependency[3] is False:
  411. return (
  412. isinstance(operation, operations.RemoveField)
  413. and operation.model_name_lower == dependency[1].lower()
  414. and operation.name_lower == dependency[2].lower()
  415. )
  416. # Removed model
  417. elif dependency[2] is None and dependency[3] is False:
  418. return (
  419. isinstance(operation, operations.DeleteModel)
  420. and operation.name_lower == dependency[1].lower()
  421. )
  422. # Field being altered
  423. elif dependency[2] is not None and dependency[3] == "alter":
  424. return (
  425. isinstance(operation, operations.AlterField)
  426. and operation.model_name_lower == dependency[1].lower()
  427. and operation.name_lower == dependency[2].lower()
  428. )
  429. # order_with_respect_to being unset for a field
  430. elif dependency[2] is not None and dependency[3] == "order_wrt_unset":
  431. return (
  432. isinstance(operation, operations.AlterOrderWithRespectTo)
  433. and operation.name_lower == dependency[1].lower()
  434. and (operation.order_with_respect_to or "").lower()
  435. != dependency[2].lower()
  436. )
  437. # Field is removed and part of an index/unique_together
  438. elif dependency[2] is not None and dependency[3] == "foo_together_change":
  439. return (
  440. isinstance(
  441. operation,
  442. (operations.AlterUniqueTogether, operations.AlterIndexTogether),
  443. )
  444. and operation.name_lower == dependency[1].lower()
  445. )
  446. # Unknown dependency. Raise an error.
  447. else:
  448. raise ValueError("Can't handle dependency %r" % (dependency,))
  449. def add_operation(self, app_label, operation, dependencies=None, beginning=False):
  450. # Dependencies are
  451. # (app_label, model_name, field_name, create/delete as True/False)
  452. operation._auto_deps = dependencies or []
  453. if beginning:
  454. self.generated_operations.setdefault(app_label, []).insert(0, operation)
  455. else:
  456. self.generated_operations.setdefault(app_label, []).append(operation)
  457. def swappable_first_key(self, item):
  458. """
  459. Place potential swappable models first in lists of created models (only
  460. real way to solve #22783).
  461. """
  462. try:
  463. model_state = self.to_state.models[item]
  464. base_names = {
  465. base if isinstance(base, str) else base.__name__
  466. for base in model_state.bases
  467. }
  468. string_version = "%s.%s" % (item[0], item[1])
  469. if (
  470. model_state.options.get("swappable")
  471. or "AbstractUser" in base_names
  472. or "AbstractBaseUser" in base_names
  473. or settings.AUTH_USER_MODEL.lower() == string_version.lower()
  474. ):
  475. return ("___" + item[0], "___" + item[1])
  476. except LookupError:
  477. pass
  478. return item
  479. def generate_renamed_models(self):
  480. """
  481. Find any renamed models, generate the operations for them, and remove
  482. the old entry from the model lists. Must be run before other
  483. model-level generation.
  484. """
  485. self.renamed_models = {}
  486. self.renamed_models_rel = {}
  487. added_models = self.new_model_keys - self.old_model_keys
  488. for app_label, model_name in sorted(added_models):
  489. model_state = self.to_state.models[app_label, model_name]
  490. model_fields_def = self.only_relation_agnostic_fields(model_state.fields)
  491. removed_models = self.old_model_keys - self.new_model_keys
  492. for rem_app_label, rem_model_name in removed_models:
  493. if rem_app_label == app_label:
  494. rem_model_state = self.from_state.models[
  495. rem_app_label, rem_model_name
  496. ]
  497. rem_model_fields_def = self.only_relation_agnostic_fields(
  498. rem_model_state.fields
  499. )
  500. if model_fields_def == rem_model_fields_def:
  501. if self.questioner.ask_rename_model(
  502. rem_model_state, model_state
  503. ):
  504. dependencies = []
  505. fields = list(model_state.fields.values()) + [
  506. field.remote_field
  507. for relations in self.to_state.relations[
  508. app_label, model_name
  509. ].values()
  510. for field in relations.values()
  511. ]
  512. for field in fields:
  513. if field.is_relation:
  514. dependencies.extend(
  515. self._get_dependencies_for_foreign_key(
  516. app_label,
  517. model_name,
  518. field,
  519. self.to_state,
  520. )
  521. )
  522. self.add_operation(
  523. app_label,
  524. operations.RenameModel(
  525. old_name=rem_model_state.name,
  526. new_name=model_state.name,
  527. ),
  528. dependencies=dependencies,
  529. )
  530. self.renamed_models[app_label, model_name] = rem_model_name
  531. renamed_models_rel_key = "%s.%s" % (
  532. rem_model_state.app_label,
  533. rem_model_state.name_lower,
  534. )
  535. self.renamed_models_rel[
  536. renamed_models_rel_key
  537. ] = "%s.%s" % (
  538. model_state.app_label,
  539. model_state.name_lower,
  540. )
  541. self.old_model_keys.remove((rem_app_label, rem_model_name))
  542. self.old_model_keys.add((app_label, model_name))
  543. break
  544. def generate_created_models(self):
  545. """
  546. Find all new models (both managed and unmanaged) and make create
  547. operations for them as well as separate operations to create any
  548. foreign key or M2M relationships (these are optimized later, if
  549. possible).
  550. Defer any model options that refer to collections of fields that might
  551. be deferred (e.g. unique_together, index_together).
  552. """
  553. old_keys = self.old_model_keys | self.old_unmanaged_keys
  554. added_models = self.new_model_keys - old_keys
  555. added_unmanaged_models = self.new_unmanaged_keys - old_keys
  556. all_added_models = chain(
  557. sorted(added_models, key=self.swappable_first_key, reverse=True),
  558. sorted(added_unmanaged_models, key=self.swappable_first_key, reverse=True),
  559. )
  560. for app_label, model_name in all_added_models:
  561. model_state = self.to_state.models[app_label, model_name]
  562. # Gather related fields
  563. related_fields = {}
  564. primary_key_rel = None
  565. for field_name, field in model_state.fields.items():
  566. if field.remote_field:
  567. if field.remote_field.model:
  568. if field.primary_key:
  569. primary_key_rel = field.remote_field.model
  570. elif not field.remote_field.parent_link:
  571. related_fields[field_name] = field
  572. if getattr(field.remote_field, "through", None):
  573. related_fields[field_name] = field
  574. # Are there indexes/unique|index_together to defer?
  575. indexes = model_state.options.pop("indexes")
  576. constraints = model_state.options.pop("constraints")
  577. unique_together = model_state.options.pop("unique_together", None)
  578. # RemovedInDjango51Warning.
  579. index_together = model_state.options.pop("index_together", None)
  580. order_with_respect_to = model_state.options.pop(
  581. "order_with_respect_to", None
  582. )
  583. # Depend on the deletion of any possible proxy version of us
  584. dependencies = [
  585. (app_label, model_name, None, False),
  586. ]
  587. # Depend on all bases
  588. for base in model_state.bases:
  589. if isinstance(base, str) and "." in base:
  590. base_app_label, base_name = base.split(".", 1)
  591. dependencies.append((base_app_label, base_name, None, True))
  592. # Depend on the removal of base fields if the new model has
  593. # a field with the same name.
  594. old_base_model_state = self.from_state.models.get(
  595. (base_app_label, base_name)
  596. )
  597. new_base_model_state = self.to_state.models.get(
  598. (base_app_label, base_name)
  599. )
  600. if old_base_model_state and new_base_model_state:
  601. removed_base_fields = (
  602. set(old_base_model_state.fields)
  603. .difference(
  604. new_base_model_state.fields,
  605. )
  606. .intersection(model_state.fields)
  607. )
  608. for removed_base_field in removed_base_fields:
  609. dependencies.append(
  610. (base_app_label, base_name, removed_base_field, False)
  611. )
  612. # Depend on the other end of the primary key if it's a relation
  613. if primary_key_rel:
  614. dependencies.append(
  615. resolve_relation(
  616. primary_key_rel,
  617. app_label,
  618. model_name,
  619. )
  620. + (None, True)
  621. )
  622. # Generate creation operation
  623. self.add_operation(
  624. app_label,
  625. operations.CreateModel(
  626. name=model_state.name,
  627. fields=[
  628. d
  629. for d in model_state.fields.items()
  630. if d[0] not in related_fields
  631. ],
  632. options=model_state.options,
  633. bases=model_state.bases,
  634. managers=model_state.managers,
  635. ),
  636. dependencies=dependencies,
  637. beginning=True,
  638. )
  639. # Don't add operations which modify the database for unmanaged models
  640. if not model_state.options.get("managed", True):
  641. continue
  642. # Generate operations for each related field
  643. for name, field in sorted(related_fields.items()):
  644. dependencies = self._get_dependencies_for_foreign_key(
  645. app_label,
  646. model_name,
  647. field,
  648. self.to_state,
  649. )
  650. # Depend on our own model being created
  651. dependencies.append((app_label, model_name, None, True))
  652. # Make operation
  653. self.add_operation(
  654. app_label,
  655. operations.AddField(
  656. model_name=model_name,
  657. name=name,
  658. field=field,
  659. ),
  660. dependencies=list(set(dependencies)),
  661. )
  662. # Generate other opns
  663. if order_with_respect_to:
  664. self.add_operation(
  665. app_label,
  666. operations.AlterOrderWithRespectTo(
  667. name=model_name,
  668. order_with_respect_to=order_with_respect_to,
  669. ),
  670. dependencies=[
  671. (app_label, model_name, order_with_respect_to, True),
  672. (app_label, model_name, None, True),
  673. ],
  674. )
  675. related_dependencies = [
  676. (app_label, model_name, name, True) for name in sorted(related_fields)
  677. ]
  678. related_dependencies.append((app_label, model_name, None, True))
  679. for index in indexes:
  680. self.add_operation(
  681. app_label,
  682. operations.AddIndex(
  683. model_name=model_name,
  684. index=index,
  685. ),
  686. dependencies=related_dependencies,
  687. )
  688. for constraint in constraints:
  689. self.add_operation(
  690. app_label,
  691. operations.AddConstraint(
  692. model_name=model_name,
  693. constraint=constraint,
  694. ),
  695. dependencies=related_dependencies,
  696. )
  697. if unique_together:
  698. self.add_operation(
  699. app_label,
  700. operations.AlterUniqueTogether(
  701. name=model_name,
  702. unique_together=unique_together,
  703. ),
  704. dependencies=related_dependencies,
  705. )
  706. # RemovedInDjango51Warning.
  707. if index_together:
  708. self.add_operation(
  709. app_label,
  710. operations.AlterIndexTogether(
  711. name=model_name,
  712. index_together=index_together,
  713. ),
  714. dependencies=related_dependencies,
  715. )
  716. # Fix relationships if the model changed from a proxy model to a
  717. # concrete model.
  718. relations = self.to_state.relations
  719. if (app_label, model_name) in self.old_proxy_keys:
  720. for related_model_key, related_fields in relations[
  721. app_label, model_name
  722. ].items():
  723. related_model_state = self.to_state.models[related_model_key]
  724. for related_field_name, related_field in related_fields.items():
  725. self.add_operation(
  726. related_model_state.app_label,
  727. operations.AlterField(
  728. model_name=related_model_state.name,
  729. name=related_field_name,
  730. field=related_field,
  731. ),
  732. dependencies=[(app_label, model_name, None, True)],
  733. )
  734. def generate_created_proxies(self):
  735. """
  736. Make CreateModel statements for proxy models. Use the same statements
  737. as that way there's less code duplication, but for proxy models it's
  738. safe to skip all the pointless field stuff and chuck out an operation.
  739. """
  740. added = self.new_proxy_keys - self.old_proxy_keys
  741. for app_label, model_name in sorted(added):
  742. model_state = self.to_state.models[app_label, model_name]
  743. assert model_state.options.get("proxy")
  744. # Depend on the deletion of any possible non-proxy version of us
  745. dependencies = [
  746. (app_label, model_name, None, False),
  747. ]
  748. # Depend on all bases
  749. for base in model_state.bases:
  750. if isinstance(base, str) and "." in base:
  751. base_app_label, base_name = base.split(".", 1)
  752. dependencies.append((base_app_label, base_name, None, True))
  753. # Generate creation operation
  754. self.add_operation(
  755. app_label,
  756. operations.CreateModel(
  757. name=model_state.name,
  758. fields=[],
  759. options=model_state.options,
  760. bases=model_state.bases,
  761. managers=model_state.managers,
  762. ),
  763. # Depend on the deletion of any possible non-proxy version of us
  764. dependencies=dependencies,
  765. )
  766. def generate_deleted_models(self):
  767. """
  768. Find all deleted models (managed and unmanaged) and make delete
  769. operations for them as well as separate operations to delete any
  770. foreign key or M2M relationships (these are optimized later, if
  771. possible).
  772. Also bring forward removal of any model options that refer to
  773. collections of fields - the inverse of generate_created_models().
  774. """
  775. new_keys = self.new_model_keys | self.new_unmanaged_keys
  776. deleted_models = self.old_model_keys - new_keys
  777. deleted_unmanaged_models = self.old_unmanaged_keys - new_keys
  778. all_deleted_models = chain(
  779. sorted(deleted_models), sorted(deleted_unmanaged_models)
  780. )
  781. for app_label, model_name in all_deleted_models:
  782. model_state = self.from_state.models[app_label, model_name]
  783. # Gather related fields
  784. related_fields = {}
  785. for field_name, field in model_state.fields.items():
  786. if field.remote_field:
  787. if field.remote_field.model:
  788. related_fields[field_name] = field
  789. if getattr(field.remote_field, "through", None):
  790. related_fields[field_name] = field
  791. # Generate option removal first
  792. unique_together = model_state.options.pop("unique_together", None)
  793. # RemovedInDjango51Warning.
  794. index_together = model_state.options.pop("index_together", None)
  795. if unique_together:
  796. self.add_operation(
  797. app_label,
  798. operations.AlterUniqueTogether(
  799. name=model_name,
  800. unique_together=None,
  801. ),
  802. )
  803. # RemovedInDjango51Warning.
  804. if index_together:
  805. self.add_operation(
  806. app_label,
  807. operations.AlterIndexTogether(
  808. name=model_name,
  809. index_together=None,
  810. ),
  811. )
  812. # Then remove each related field
  813. for name in sorted(related_fields):
  814. self.add_operation(
  815. app_label,
  816. operations.RemoveField(
  817. model_name=model_name,
  818. name=name,
  819. ),
  820. )
  821. # Finally, remove the model.
  822. # This depends on both the removal/alteration of all incoming fields
  823. # and the removal of all its own related fields, and if it's
  824. # a through model the field that references it.
  825. dependencies = []
  826. relations = self.from_state.relations
  827. for (
  828. related_object_app_label,
  829. object_name,
  830. ), relation_related_fields in relations[app_label, model_name].items():
  831. for field_name, field in relation_related_fields.items():
  832. dependencies.append(
  833. (related_object_app_label, object_name, field_name, False),
  834. )
  835. if not field.many_to_many:
  836. dependencies.append(
  837. (
  838. related_object_app_label,
  839. object_name,
  840. field_name,
  841. "alter",
  842. ),
  843. )
  844. for name in sorted(related_fields):
  845. dependencies.append((app_label, model_name, name, False))
  846. # We're referenced in another field's through=
  847. through_user = self.through_users.get((app_label, model_state.name_lower))
  848. if through_user:
  849. dependencies.append(
  850. (through_user[0], through_user[1], through_user[2], False)
  851. )
  852. # Finally, make the operation, deduping any dependencies
  853. self.add_operation(
  854. app_label,
  855. operations.DeleteModel(
  856. name=model_state.name,
  857. ),
  858. dependencies=list(set(dependencies)),
  859. )
  860. def generate_deleted_proxies(self):
  861. """Make DeleteModel options for proxy models."""
  862. deleted = self.old_proxy_keys - self.new_proxy_keys
  863. for app_label, model_name in sorted(deleted):
  864. model_state = self.from_state.models[app_label, model_name]
  865. assert model_state.options.get("proxy")
  866. self.add_operation(
  867. app_label,
  868. operations.DeleteModel(
  869. name=model_state.name,
  870. ),
  871. )
  872. def create_renamed_fields(self):
  873. """Work out renamed fields."""
  874. self.renamed_operations = []
  875. old_field_keys = self.old_field_keys.copy()
  876. for app_label, model_name, field_name in sorted(
  877. self.new_field_keys - old_field_keys
  878. ):
  879. old_model_name = self.renamed_models.get(
  880. (app_label, model_name), model_name
  881. )
  882. old_model_state = self.from_state.models[app_label, old_model_name]
  883. new_model_state = self.to_state.models[app_label, model_name]
  884. field = new_model_state.get_field(field_name)
  885. # Scan to see if this is actually a rename!
  886. field_dec = self.deep_deconstruct(field)
  887. for rem_app_label, rem_model_name, rem_field_name in sorted(
  888. old_field_keys - self.new_field_keys
  889. ):
  890. if rem_app_label == app_label and rem_model_name == model_name:
  891. old_field = old_model_state.get_field(rem_field_name)
  892. old_field_dec = self.deep_deconstruct(old_field)
  893. if (
  894. field.remote_field
  895. and field.remote_field.model
  896. and "to" in old_field_dec[2]
  897. ):
  898. old_rel_to = old_field_dec[2]["to"]
  899. if old_rel_to in self.renamed_models_rel:
  900. old_field_dec[2]["to"] = self.renamed_models_rel[old_rel_to]
  901. old_field.set_attributes_from_name(rem_field_name)
  902. old_db_column = old_field.get_attname_column()[1]
  903. if old_field_dec == field_dec or (
  904. # Was the field renamed and db_column equal to the
  905. # old field's column added?
  906. old_field_dec[0:2] == field_dec[0:2]
  907. and dict(old_field_dec[2], db_column=old_db_column)
  908. == field_dec[2]
  909. ):
  910. if self.questioner.ask_rename(
  911. model_name, rem_field_name, field_name, field
  912. ):
  913. self.renamed_operations.append(
  914. (
  915. rem_app_label,
  916. rem_model_name,
  917. old_field.db_column,
  918. rem_field_name,
  919. app_label,
  920. model_name,
  921. field,
  922. field_name,
  923. )
  924. )
  925. old_field_keys.remove(
  926. (rem_app_label, rem_model_name, rem_field_name)
  927. )
  928. old_field_keys.add((app_label, model_name, field_name))
  929. self.renamed_fields[
  930. app_label, model_name, field_name
  931. ] = rem_field_name
  932. break
  933. def generate_renamed_fields(self):
  934. """Generate RenameField operations."""
  935. for (
  936. rem_app_label,
  937. rem_model_name,
  938. rem_db_column,
  939. rem_field_name,
  940. app_label,
  941. model_name,
  942. field,
  943. field_name,
  944. ) in self.renamed_operations:
  945. # A db_column mismatch requires a prior noop AlterField for the
  946. # subsequent RenameField to be a noop on attempts at preserving the
  947. # old name.
  948. if rem_db_column != field.db_column:
  949. altered_field = field.clone()
  950. altered_field.name = rem_field_name
  951. self.add_operation(
  952. app_label,
  953. operations.AlterField(
  954. model_name=model_name,
  955. name=rem_field_name,
  956. field=altered_field,
  957. ),
  958. )
  959. self.add_operation(
  960. app_label,
  961. operations.RenameField(
  962. model_name=model_name,
  963. old_name=rem_field_name,
  964. new_name=field_name,
  965. ),
  966. )
  967. self.old_field_keys.remove((rem_app_label, rem_model_name, rem_field_name))
  968. self.old_field_keys.add((app_label, model_name, field_name))
  969. def generate_added_fields(self):
  970. """Make AddField operations."""
  971. for app_label, model_name, field_name in sorted(
  972. self.new_field_keys - self.old_field_keys
  973. ):
  974. self._generate_added_field(app_label, model_name, field_name)
  975. def _generate_added_field(self, app_label, model_name, field_name):
  976. field = self.to_state.models[app_label, model_name].get_field(field_name)
  977. # Adding a field always depends at least on its removal.
  978. dependencies = [(app_label, model_name, field_name, False)]
  979. # Fields that are foreignkeys/m2ms depend on stuff.
  980. if field.remote_field and field.remote_field.model:
  981. dependencies.extend(
  982. self._get_dependencies_for_foreign_key(
  983. app_label,
  984. model_name,
  985. field,
  986. self.to_state,
  987. )
  988. )
  989. # You can't just add NOT NULL fields with no default or fields
  990. # which don't allow empty strings as default.
  991. time_fields = (models.DateField, models.DateTimeField, models.TimeField)
  992. preserve_default = (
  993. field.null
  994. or field.has_default()
  995. or field.db_default is not models.NOT_PROVIDED
  996. or field.many_to_many
  997. or (field.blank and field.empty_strings_allowed)
  998. or (isinstance(field, time_fields) and field.auto_now)
  999. )
  1000. if not preserve_default:
  1001. field = field.clone()
  1002. if isinstance(field, time_fields) and field.auto_now_add:
  1003. field.default = self.questioner.ask_auto_now_add_addition(
  1004. field_name, model_name
  1005. )
  1006. else:
  1007. field.default = self.questioner.ask_not_null_addition(
  1008. field_name, model_name
  1009. )
  1010. if (
  1011. field.unique
  1012. and field.default is not models.NOT_PROVIDED
  1013. and callable(field.default)
  1014. ):
  1015. self.questioner.ask_unique_callable_default_addition(field_name, model_name)
  1016. self.add_operation(
  1017. app_label,
  1018. operations.AddField(
  1019. model_name=model_name,
  1020. name=field_name,
  1021. field=field,
  1022. preserve_default=preserve_default,
  1023. ),
  1024. dependencies=dependencies,
  1025. )
  1026. def generate_removed_fields(self):
  1027. """Make RemoveField operations."""
  1028. for app_label, model_name, field_name in sorted(
  1029. self.old_field_keys - self.new_field_keys
  1030. ):
  1031. self._generate_removed_field(app_label, model_name, field_name)
  1032. def _generate_removed_field(self, app_label, model_name, field_name):
  1033. self.add_operation(
  1034. app_label,
  1035. operations.RemoveField(
  1036. model_name=model_name,
  1037. name=field_name,
  1038. ),
  1039. # We might need to depend on the removal of an
  1040. # order_with_respect_to or index/unique_together operation;
  1041. # this is safely ignored if there isn't one
  1042. dependencies=[
  1043. (app_label, model_name, field_name, "order_wrt_unset"),
  1044. (app_label, model_name, field_name, "foo_together_change"),
  1045. ],
  1046. )
  1047. def generate_altered_fields(self):
  1048. """
  1049. Make AlterField operations, or possibly RemovedField/AddField if alter
  1050. isn't possible.
  1051. """
  1052. for app_label, model_name, field_name in sorted(
  1053. self.old_field_keys & self.new_field_keys
  1054. ):
  1055. # Did the field change?
  1056. old_model_name = self.renamed_models.get(
  1057. (app_label, model_name), model_name
  1058. )
  1059. old_field_name = self.renamed_fields.get(
  1060. (app_label, model_name, field_name), field_name
  1061. )
  1062. old_field = self.from_state.models[app_label, old_model_name].get_field(
  1063. old_field_name
  1064. )
  1065. new_field = self.to_state.models[app_label, model_name].get_field(
  1066. field_name
  1067. )
  1068. dependencies = []
  1069. # Implement any model renames on relations; these are handled by RenameModel
  1070. # so we need to exclude them from the comparison
  1071. if hasattr(new_field, "remote_field") and getattr(
  1072. new_field.remote_field, "model", None
  1073. ):
  1074. rename_key = resolve_relation(
  1075. new_field.remote_field.model, app_label, model_name
  1076. )
  1077. if rename_key in self.renamed_models:
  1078. new_field.remote_field.model = old_field.remote_field.model
  1079. # Handle ForeignKey which can only have a single to_field.
  1080. remote_field_name = getattr(new_field.remote_field, "field_name", None)
  1081. if remote_field_name:
  1082. to_field_rename_key = rename_key + (remote_field_name,)
  1083. if to_field_rename_key in self.renamed_fields:
  1084. # Repoint both model and field name because to_field
  1085. # inclusion in ForeignKey.deconstruct() is based on
  1086. # both.
  1087. new_field.remote_field.model = old_field.remote_field.model
  1088. new_field.remote_field.field_name = (
  1089. old_field.remote_field.field_name
  1090. )
  1091. # Handle ForeignObjects which can have multiple from_fields/to_fields.
  1092. from_fields = getattr(new_field, "from_fields", None)
  1093. if from_fields:
  1094. from_rename_key = (app_label, model_name)
  1095. new_field.from_fields = tuple(
  1096. [
  1097. self.renamed_fields.get(
  1098. from_rename_key + (from_field,), from_field
  1099. )
  1100. for from_field in from_fields
  1101. ]
  1102. )
  1103. new_field.to_fields = tuple(
  1104. [
  1105. self.renamed_fields.get(rename_key + (to_field,), to_field)
  1106. for to_field in new_field.to_fields
  1107. ]
  1108. )
  1109. if old_from_fields := getattr(old_field, "from_fields", None):
  1110. old_field.from_fields = tuple(old_from_fields)
  1111. old_field.to_fields = tuple(old_field.to_fields)
  1112. dependencies.extend(
  1113. self._get_dependencies_for_foreign_key(
  1114. app_label,
  1115. model_name,
  1116. new_field,
  1117. self.to_state,
  1118. )
  1119. )
  1120. if hasattr(new_field, "remote_field") and getattr(
  1121. new_field.remote_field, "through", None
  1122. ):
  1123. rename_key = resolve_relation(
  1124. new_field.remote_field.through, app_label, model_name
  1125. )
  1126. if rename_key in self.renamed_models:
  1127. new_field.remote_field.through = old_field.remote_field.through
  1128. old_field_dec = self.deep_deconstruct(old_field)
  1129. new_field_dec = self.deep_deconstruct(new_field)
  1130. # If the field was confirmed to be renamed it means that only
  1131. # db_column was allowed to change which generate_renamed_fields()
  1132. # already accounts for by adding an AlterField operation.
  1133. if old_field_dec != new_field_dec and old_field_name == field_name:
  1134. both_m2m = old_field.many_to_many and new_field.many_to_many
  1135. neither_m2m = not old_field.many_to_many and not new_field.many_to_many
  1136. if both_m2m or neither_m2m:
  1137. # Either both fields are m2m or neither is
  1138. preserve_default = True
  1139. if (
  1140. old_field.null
  1141. and not new_field.null
  1142. and not new_field.has_default()
  1143. and new_field.db_default is models.NOT_PROVIDED
  1144. and not new_field.many_to_many
  1145. ):
  1146. field = new_field.clone()
  1147. new_default = self.questioner.ask_not_null_alteration(
  1148. field_name, model_name
  1149. )
  1150. if new_default is not models.NOT_PROVIDED:
  1151. field.default = new_default
  1152. preserve_default = False
  1153. else:
  1154. field = new_field
  1155. self.add_operation(
  1156. app_label,
  1157. operations.AlterField(
  1158. model_name=model_name,
  1159. name=field_name,
  1160. field=field,
  1161. preserve_default=preserve_default,
  1162. ),
  1163. dependencies=dependencies,
  1164. )
  1165. else:
  1166. # We cannot alter between m2m and concrete fields
  1167. self._generate_removed_field(app_label, model_name, field_name)
  1168. self._generate_added_field(app_label, model_name, field_name)
  1169. def create_altered_indexes(self):
  1170. option_name = operations.AddIndex.option_name
  1171. self.renamed_index_together_values = defaultdict(list)
  1172. for app_label, model_name in sorted(self.kept_model_keys):
  1173. old_model_name = self.renamed_models.get(
  1174. (app_label, model_name), model_name
  1175. )
  1176. old_model_state = self.from_state.models[app_label, old_model_name]
  1177. new_model_state = self.to_state.models[app_label, model_name]
  1178. old_indexes = old_model_state.options[option_name]
  1179. new_indexes = new_model_state.options[option_name]
  1180. added_indexes = [idx for idx in new_indexes if idx not in old_indexes]
  1181. removed_indexes = [idx for idx in old_indexes if idx not in new_indexes]
  1182. renamed_indexes = []
  1183. # Find renamed indexes.
  1184. remove_from_added = []
  1185. remove_from_removed = []
  1186. for new_index in added_indexes:
  1187. new_index_dec = new_index.deconstruct()
  1188. new_index_name = new_index_dec[2].pop("name")
  1189. for old_index in removed_indexes:
  1190. old_index_dec = old_index.deconstruct()
  1191. old_index_name = old_index_dec[2].pop("name")
  1192. # Indexes are the same except for the names.
  1193. if (
  1194. new_index_dec == old_index_dec
  1195. and new_index_name != old_index_name
  1196. ):
  1197. renamed_indexes.append((old_index_name, new_index_name, None))
  1198. remove_from_added.append(new_index)
  1199. remove_from_removed.append(old_index)
  1200. # Find index_together changed to indexes.
  1201. for (
  1202. old_value,
  1203. new_value,
  1204. index_together_app_label,
  1205. index_together_model_name,
  1206. dependencies,
  1207. ) in self._get_altered_foo_together_operations(
  1208. operations.AlterIndexTogether.option_name
  1209. ):
  1210. if (
  1211. app_label != index_together_app_label
  1212. or model_name != index_together_model_name
  1213. ):
  1214. continue
  1215. removed_values = old_value.difference(new_value)
  1216. for removed_index_together in removed_values:
  1217. renamed_index_together_indexes = []
  1218. for new_index in added_indexes:
  1219. _, args, kwargs = new_index.deconstruct()
  1220. # Ensure only 'fields' are defined in the Index.
  1221. if (
  1222. not args
  1223. and new_index.fields == list(removed_index_together)
  1224. and set(kwargs) == {"name", "fields"}
  1225. ):
  1226. renamed_index_together_indexes.append(new_index)
  1227. if len(renamed_index_together_indexes) == 1:
  1228. renamed_index = renamed_index_together_indexes[0]
  1229. remove_from_added.append(renamed_index)
  1230. renamed_indexes.append(
  1231. (None, renamed_index.name, removed_index_together)
  1232. )
  1233. self.renamed_index_together_values[
  1234. index_together_app_label, index_together_model_name
  1235. ].append(removed_index_together)
  1236. # Remove renamed indexes from the lists of added and removed
  1237. # indexes.
  1238. added_indexes = [
  1239. idx for idx in added_indexes if idx not in remove_from_added
  1240. ]
  1241. removed_indexes = [
  1242. idx for idx in removed_indexes if idx not in remove_from_removed
  1243. ]
  1244. self.altered_indexes.update(
  1245. {
  1246. (app_label, model_name): {
  1247. "added_indexes": added_indexes,
  1248. "removed_indexes": removed_indexes,
  1249. "renamed_indexes": renamed_indexes,
  1250. }
  1251. }
  1252. )
  1253. def generate_added_indexes(self):
  1254. for (app_label, model_name), alt_indexes in self.altered_indexes.items():
  1255. dependencies = self._get_dependencies_for_model(app_label, model_name)
  1256. for index in alt_indexes["added_indexes"]:
  1257. self.add_operation(
  1258. app_label,
  1259. operations.AddIndex(
  1260. model_name=model_name,
  1261. index=index,
  1262. ),
  1263. dependencies=dependencies,
  1264. )
  1265. def generate_removed_indexes(self):
  1266. for (app_label, model_name), alt_indexes in self.altered_indexes.items():
  1267. for index in alt_indexes["removed_indexes"]:
  1268. self.add_operation(
  1269. app_label,
  1270. operations.RemoveIndex(
  1271. model_name=model_name,
  1272. name=index.name,
  1273. ),
  1274. )
  1275. def generate_renamed_indexes(self):
  1276. for (app_label, model_name), alt_indexes in self.altered_indexes.items():
  1277. for old_index_name, new_index_name, old_fields in alt_indexes[
  1278. "renamed_indexes"
  1279. ]:
  1280. self.add_operation(
  1281. app_label,
  1282. operations.RenameIndex(
  1283. model_name=model_name,
  1284. new_name=new_index_name,
  1285. old_name=old_index_name,
  1286. old_fields=old_fields,
  1287. ),
  1288. )
  1289. def create_altered_constraints(self):
  1290. option_name = operations.AddConstraint.option_name
  1291. for app_label, model_name in sorted(self.kept_model_keys):
  1292. old_model_name = self.renamed_models.get(
  1293. (app_label, model_name), model_name
  1294. )
  1295. old_model_state = self.from_state.models[app_label, old_model_name]
  1296. new_model_state = self.to_state.models[app_label, model_name]
  1297. old_constraints = old_model_state.options[option_name]
  1298. new_constraints = new_model_state.options[option_name]
  1299. add_constraints = [c for c in new_constraints if c not in old_constraints]
  1300. rem_constraints = [c for c in old_constraints if c not in new_constraints]
  1301. self.altered_constraints.update(
  1302. {
  1303. (app_label, model_name): {
  1304. "added_constraints": add_constraints,
  1305. "removed_constraints": rem_constraints,
  1306. }
  1307. }
  1308. )
  1309. def generate_added_constraints(self):
  1310. for (
  1311. app_label,
  1312. model_name,
  1313. ), alt_constraints in self.altered_constraints.items():
  1314. dependencies = self._get_dependencies_for_model(app_label, model_name)
  1315. for constraint in alt_constraints["added_constraints"]:
  1316. self.add_operation(
  1317. app_label,
  1318. operations.AddConstraint(
  1319. model_name=model_name,
  1320. constraint=constraint,
  1321. ),
  1322. dependencies=dependencies,
  1323. )
  1324. def generate_removed_constraints(self):
  1325. for (
  1326. app_label,
  1327. model_name,
  1328. ), alt_constraints in self.altered_constraints.items():
  1329. for constraint in alt_constraints["removed_constraints"]:
  1330. self.add_operation(
  1331. app_label,
  1332. operations.RemoveConstraint(
  1333. model_name=model_name,
  1334. name=constraint.name,
  1335. ),
  1336. )
  1337. @staticmethod
  1338. def _get_dependencies_for_foreign_key(app_label, model_name, field, project_state):
  1339. remote_field_model = None
  1340. if hasattr(field.remote_field, "model"):
  1341. remote_field_model = field.remote_field.model
  1342. else:
  1343. relations = project_state.relations[app_label, model_name]
  1344. for (remote_app_label, remote_model_name), fields in relations.items():
  1345. if any(
  1346. field == related_field.remote_field
  1347. for related_field in fields.values()
  1348. ):
  1349. remote_field_model = f"{remote_app_label}.{remote_model_name}"
  1350. break
  1351. # Account for FKs to swappable models
  1352. swappable_setting = getattr(field, "swappable_setting", None)
  1353. if swappable_setting is not None:
  1354. dep_app_label = "__setting__"
  1355. dep_object_name = swappable_setting
  1356. else:
  1357. dep_app_label, dep_object_name = resolve_relation(
  1358. remote_field_model,
  1359. app_label,
  1360. model_name,
  1361. )
  1362. dependencies = [(dep_app_label, dep_object_name, None, True)]
  1363. if getattr(field.remote_field, "through", None):
  1364. through_app_label, through_object_name = resolve_relation(
  1365. field.remote_field.through,
  1366. app_label,
  1367. model_name,
  1368. )
  1369. dependencies.append((through_app_label, through_object_name, None, True))
  1370. return dependencies
  1371. def _get_dependencies_for_model(self, app_label, model_name):
  1372. """Return foreign key dependencies of the given model."""
  1373. dependencies = []
  1374. model_state = self.to_state.models[app_label, model_name]
  1375. for field in model_state.fields.values():
  1376. if field.is_relation:
  1377. dependencies.extend(
  1378. self._get_dependencies_for_foreign_key(
  1379. app_label,
  1380. model_name,
  1381. field,
  1382. self.to_state,
  1383. )
  1384. )
  1385. return dependencies
  1386. def _get_altered_foo_together_operations(self, option_name):
  1387. for app_label, model_name in sorted(self.kept_model_keys):
  1388. old_model_name = self.renamed_models.get(
  1389. (app_label, model_name), model_name
  1390. )
  1391. old_model_state = self.from_state.models[app_label, old_model_name]
  1392. new_model_state = self.to_state.models[app_label, model_name]
  1393. # We run the old version through the field renames to account for those
  1394. old_value = old_model_state.options.get(option_name)
  1395. old_value = (
  1396. {
  1397. tuple(
  1398. self.renamed_fields.get((app_label, model_name, n), n)
  1399. for n in unique
  1400. )
  1401. for unique in old_value
  1402. }
  1403. if old_value
  1404. else set()
  1405. )
  1406. new_value = new_model_state.options.get(option_name)
  1407. new_value = set(new_value) if new_value else set()
  1408. if old_value != new_value:
  1409. dependencies = []
  1410. for foo_togethers in new_value:
  1411. for field_name in foo_togethers:
  1412. field = new_model_state.get_field(field_name)
  1413. if field.remote_field and field.remote_field.model:
  1414. dependencies.extend(
  1415. self._get_dependencies_for_foreign_key(
  1416. app_label,
  1417. model_name,
  1418. field,
  1419. self.to_state,
  1420. )
  1421. )
  1422. yield (
  1423. old_value,
  1424. new_value,
  1425. app_label,
  1426. model_name,
  1427. dependencies,
  1428. )
  1429. def _generate_removed_altered_foo_together(self, operation):
  1430. for (
  1431. old_value,
  1432. new_value,
  1433. app_label,
  1434. model_name,
  1435. dependencies,
  1436. ) in self._get_altered_foo_together_operations(operation.option_name):
  1437. if operation == operations.AlterIndexTogether:
  1438. old_value = {
  1439. value
  1440. for value in old_value
  1441. if value
  1442. not in self.renamed_index_together_values[app_label, model_name]
  1443. }
  1444. removal_value = new_value.intersection(old_value)
  1445. if removal_value or old_value:
  1446. self.add_operation(
  1447. app_label,
  1448. operation(
  1449. name=model_name, **{operation.option_name: removal_value}
  1450. ),
  1451. dependencies=dependencies,
  1452. )
  1453. def generate_removed_altered_unique_together(self):
  1454. self._generate_removed_altered_foo_together(operations.AlterUniqueTogether)
  1455. # RemovedInDjango51Warning.
  1456. def generate_removed_altered_index_together(self):
  1457. self._generate_removed_altered_foo_together(operations.AlterIndexTogether)
  1458. def _generate_altered_foo_together(self, operation):
  1459. for (
  1460. old_value,
  1461. new_value,
  1462. app_label,
  1463. model_name,
  1464. dependencies,
  1465. ) in self._get_altered_foo_together_operations(operation.option_name):
  1466. removal_value = new_value.intersection(old_value)
  1467. if new_value != removal_value:
  1468. self.add_operation(
  1469. app_label,
  1470. operation(name=model_name, **{operation.option_name: new_value}),
  1471. dependencies=dependencies,
  1472. )
  1473. def generate_altered_unique_together(self):
  1474. self._generate_altered_foo_together(operations.AlterUniqueTogether)
  1475. # RemovedInDjango51Warning.
  1476. def generate_altered_index_together(self):
  1477. self._generate_altered_foo_together(operations.AlterIndexTogether)
  1478. def generate_altered_db_table(self):
  1479. models_to_check = self.kept_model_keys.union(
  1480. self.kept_proxy_keys, self.kept_unmanaged_keys
  1481. )
  1482. for app_label, model_name in sorted(models_to_check):
  1483. old_model_name = self.renamed_models.get(
  1484. (app_label, model_name), model_name
  1485. )
  1486. old_model_state = self.from_state.models[app_label, old_model_name]
  1487. new_model_state = self.to_state.models[app_label, model_name]
  1488. old_db_table_name = old_model_state.options.get("db_table")
  1489. new_db_table_name = new_model_state.options.get("db_table")
  1490. if old_db_table_name != new_db_table_name:
  1491. self.add_operation(
  1492. app_label,
  1493. operations.AlterModelTable(
  1494. name=model_name,
  1495. table=new_db_table_name,
  1496. ),
  1497. )
  1498. def generate_altered_db_table_comment(self):
  1499. models_to_check = self.kept_model_keys.union(
  1500. self.kept_proxy_keys, self.kept_unmanaged_keys
  1501. )
  1502. for app_label, model_name in sorted(models_to_check):
  1503. old_model_name = self.renamed_models.get(
  1504. (app_label, model_name), model_name
  1505. )
  1506. old_model_state = self.from_state.models[app_label, old_model_name]
  1507. new_model_state = self.to_state.models[app_label, model_name]
  1508. old_db_table_comment = old_model_state.options.get("db_table_comment")
  1509. new_db_table_comment = new_model_state.options.get("db_table_comment")
  1510. if old_db_table_comment != new_db_table_comment:
  1511. self.add_operation(
  1512. app_label,
  1513. operations.AlterModelTableComment(
  1514. name=model_name,
  1515. table_comment=new_db_table_comment,
  1516. ),
  1517. )
  1518. def generate_altered_options(self):
  1519. """
  1520. Work out if any non-schema-affecting options have changed and make an
  1521. operation to represent them in state changes (in case Python code in
  1522. migrations needs them).
  1523. """
  1524. models_to_check = self.kept_model_keys.union(
  1525. self.kept_proxy_keys,
  1526. self.kept_unmanaged_keys,
  1527. # unmanaged converted to managed
  1528. self.old_unmanaged_keys & self.new_model_keys,
  1529. # managed converted to unmanaged
  1530. self.old_model_keys & self.new_unmanaged_keys,
  1531. )
  1532. for app_label, model_name in sorted(models_to_check):
  1533. old_model_name = self.renamed_models.get(
  1534. (app_label, model_name), model_name
  1535. )
  1536. old_model_state = self.from_state.models[app_label, old_model_name]
  1537. new_model_state = self.to_state.models[app_label, model_name]
  1538. old_options = {
  1539. key: value
  1540. for key, value in old_model_state.options.items()
  1541. if key in AlterModelOptions.ALTER_OPTION_KEYS
  1542. }
  1543. new_options = {
  1544. key: value
  1545. for key, value in new_model_state.options.items()
  1546. if key in AlterModelOptions.ALTER_OPTION_KEYS
  1547. }
  1548. if old_options != new_options:
  1549. self.add_operation(
  1550. app_label,
  1551. operations.AlterModelOptions(
  1552. name=model_name,
  1553. options=new_options,
  1554. ),
  1555. )
  1556. def generate_altered_order_with_respect_to(self):
  1557. for app_label, model_name in sorted(self.kept_model_keys):
  1558. old_model_name = self.renamed_models.get(
  1559. (app_label, model_name), model_name
  1560. )
  1561. old_model_state = self.from_state.models[app_label, old_model_name]
  1562. new_model_state = self.to_state.models[app_label, model_name]
  1563. if old_model_state.options.get(
  1564. "order_with_respect_to"
  1565. ) != new_model_state.options.get("order_with_respect_to"):
  1566. # Make sure it comes second if we're adding
  1567. # (removal dependency is part of RemoveField)
  1568. dependencies = []
  1569. if new_model_state.options.get("order_with_respect_to"):
  1570. dependencies.append(
  1571. (
  1572. app_label,
  1573. model_name,
  1574. new_model_state.options["order_with_respect_to"],
  1575. True,
  1576. )
  1577. )
  1578. # Actually generate the operation
  1579. self.add_operation(
  1580. app_label,
  1581. operations.AlterOrderWithRespectTo(
  1582. name=model_name,
  1583. order_with_respect_to=new_model_state.options.get(
  1584. "order_with_respect_to"
  1585. ),
  1586. ),
  1587. dependencies=dependencies,
  1588. )
  1589. def generate_altered_managers(self):
  1590. for app_label, model_name in sorted(self.kept_model_keys):
  1591. old_model_name = self.renamed_models.get(
  1592. (app_label, model_name), model_name
  1593. )
  1594. old_model_state = self.from_state.models[app_label, old_model_name]
  1595. new_model_state = self.to_state.models[app_label, model_name]
  1596. if old_model_state.managers != new_model_state.managers:
  1597. self.add_operation(
  1598. app_label,
  1599. operations.AlterModelManagers(
  1600. name=model_name,
  1601. managers=new_model_state.managers,
  1602. ),
  1603. )
  1604. def arrange_for_graph(self, changes, graph, migration_name=None):
  1605. """
  1606. Take a result from changes() and a MigrationGraph, and fix the names
  1607. and dependencies of the changes so they extend the graph from the leaf
  1608. nodes for each app.
  1609. """
  1610. leaves = graph.leaf_nodes()
  1611. name_map = {}
  1612. for app_label, migrations in list(changes.items()):
  1613. if not migrations:
  1614. continue
  1615. # Find the app label's current leaf node
  1616. app_leaf = None
  1617. for leaf in leaves:
  1618. if leaf[0] == app_label:
  1619. app_leaf = leaf
  1620. break
  1621. # Do they want an initial migration for this app?
  1622. if app_leaf is None and not self.questioner.ask_initial(app_label):
  1623. # They don't.
  1624. for migration in migrations:
  1625. name_map[(app_label, migration.name)] = (app_label, "__first__")
  1626. del changes[app_label]
  1627. continue
  1628. # Work out the next number in the sequence
  1629. if app_leaf is None:
  1630. next_number = 1
  1631. else:
  1632. next_number = (self.parse_number(app_leaf[1]) or 0) + 1
  1633. # Name each migration
  1634. for i, migration in enumerate(migrations):
  1635. if i == 0 and app_leaf:
  1636. migration.dependencies.append(app_leaf)
  1637. new_name_parts = ["%04i" % next_number]
  1638. if migration_name:
  1639. new_name_parts.append(migration_name)
  1640. elif i == 0 and not app_leaf:
  1641. new_name_parts.append("initial")
  1642. else:
  1643. new_name_parts.append(migration.suggest_name()[:100])
  1644. new_name = "_".join(new_name_parts)
  1645. name_map[(app_label, migration.name)] = (app_label, new_name)
  1646. next_number += 1
  1647. migration.name = new_name
  1648. # Now fix dependencies
  1649. for migrations in changes.values():
  1650. for migration in migrations:
  1651. migration.dependencies = [
  1652. name_map.get(d, d) for d in migration.dependencies
  1653. ]
  1654. return changes
  1655. def _trim_to_apps(self, changes, app_labels):
  1656. """
  1657. Take changes from arrange_for_graph() and set of app labels, and return
  1658. a modified set of changes which trims out as many migrations that are
  1659. not in app_labels as possible. Note that some other migrations may
  1660. still be present as they may be required dependencies.
  1661. """
  1662. # Gather other app dependencies in a first pass
  1663. app_dependencies = {}
  1664. for app_label, migrations in changes.items():
  1665. for migration in migrations:
  1666. for dep_app_label, name in migration.dependencies:
  1667. app_dependencies.setdefault(app_label, set()).add(dep_app_label)
  1668. required_apps = set(app_labels)
  1669. # Keep resolving till there's no change
  1670. old_required_apps = None
  1671. while old_required_apps != required_apps:
  1672. old_required_apps = set(required_apps)
  1673. required_apps.update(
  1674. *[app_dependencies.get(app_label, ()) for app_label in required_apps]
  1675. )
  1676. # Remove all migrations that aren't needed
  1677. for app_label in list(changes):
  1678. if app_label not in required_apps:
  1679. del changes[app_label]
  1680. return changes
  1681. @classmethod
  1682. def parse_number(cls, name):
  1683. """
  1684. Given a migration name, try to extract a number from the beginning of
  1685. it. For a squashed migration such as '0001_squashed_0004…', return the
  1686. second number. If no number is found, return None.
  1687. """
  1688. if squashed_match := re.search(r".*_squashed_(\d+)", name):
  1689. return int(squashed_match[1])
  1690. match = re.match(r"^\d+", name)
  1691. if match:
  1692. return int(match[0])
  1693. return None