You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

makemigrations.py 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. import os
  2. import sys
  3. from itertools import takewhile
  4. from django.apps import apps
  5. from django.conf import settings
  6. from django.core.management.base import (
  7. BaseCommand, CommandError, no_translations,
  8. )
  9. from django.db import DEFAULT_DB_ALIAS, connections, router
  10. from django.db.migrations import Migration
  11. from django.db.migrations.autodetector import MigrationAutodetector
  12. from django.db.migrations.loader import MigrationLoader
  13. from django.db.migrations.questioner import (
  14. InteractiveMigrationQuestioner, MigrationQuestioner,
  15. NonInteractiveMigrationQuestioner,
  16. )
  17. from django.db.migrations.state import ProjectState
  18. from django.db.migrations.utils import get_migration_name_timestamp
  19. from django.db.migrations.writer import MigrationWriter
  20. class Command(BaseCommand):
  21. help = "Creates new migration(s) for apps."
  22. def add_arguments(self, parser):
  23. parser.add_argument(
  24. 'args', metavar='app_label', nargs='*',
  25. help='Specify the app label(s) to create migrations for.',
  26. )
  27. parser.add_argument(
  28. '--dry-run', action='store_true',
  29. help="Just show what migrations would be made; don't actually write them.",
  30. )
  31. parser.add_argument(
  32. '--merge', action='store_true',
  33. help="Enable fixing of migration conflicts.",
  34. )
  35. parser.add_argument(
  36. '--empty', action='store_true',
  37. help="Create an empty migration.",
  38. )
  39. parser.add_argument(
  40. '--noinput', '--no-input', action='store_false', dest='interactive',
  41. help='Tells Django to NOT prompt the user for input of any kind.',
  42. )
  43. parser.add_argument(
  44. '-n', '--name',
  45. help="Use this name for migration file(s).",
  46. )
  47. parser.add_argument(
  48. '--no-header', action='store_false', dest='include_header',
  49. help='Do not add header comments to new migration file(s).',
  50. )
  51. parser.add_argument(
  52. '--check', action='store_true', dest='check_changes',
  53. help='Exit with a non-zero status if model changes are missing migrations.',
  54. )
  55. @no_translations
  56. def handle(self, *app_labels, **options):
  57. self.verbosity = options['verbosity']
  58. self.interactive = options['interactive']
  59. self.dry_run = options['dry_run']
  60. self.merge = options['merge']
  61. self.empty = options['empty']
  62. self.migration_name = options['name']
  63. if self.migration_name and not self.migration_name.isidentifier():
  64. raise CommandError('The migration name must be a valid Python identifier.')
  65. self.include_header = options['include_header']
  66. check_changes = options['check_changes']
  67. # Make sure the app they asked for exists
  68. app_labels = set(app_labels)
  69. has_bad_labels = False
  70. for app_label in app_labels:
  71. try:
  72. apps.get_app_config(app_label)
  73. except LookupError as err:
  74. self.stderr.write(str(err))
  75. has_bad_labels = True
  76. if has_bad_labels:
  77. sys.exit(2)
  78. # Load the current graph state. Pass in None for the connection so
  79. # the loader doesn't try to resolve replaced migrations from DB.
  80. loader = MigrationLoader(None, ignore_no_migrations=True)
  81. # Raise an error if any migrations are applied before their dependencies.
  82. consistency_check_labels = {config.label for config in apps.get_app_configs()}
  83. # Non-default databases are only checked if database routers used.
  84. aliases_to_check = connections if settings.DATABASE_ROUTERS else [DEFAULT_DB_ALIAS]
  85. for alias in sorted(aliases_to_check):
  86. connection = connections[alias]
  87. if (connection.settings_dict['ENGINE'] != 'django.db.backends.dummy' and any(
  88. # At least one model must be migrated to the database.
  89. router.allow_migrate(connection.alias, app_label, model_name=model._meta.object_name)
  90. for app_label in consistency_check_labels
  91. for model in apps.get_app_config(app_label).get_models()
  92. )):
  93. loader.check_consistent_history(connection)
  94. # Before anything else, see if there's conflicting apps and drop out
  95. # hard if there are any and they don't want to merge
  96. conflicts = loader.detect_conflicts()
  97. # If app_labels is specified, filter out conflicting migrations for unspecified apps
  98. if app_labels:
  99. conflicts = {
  100. app_label: conflict for app_label, conflict in conflicts.items()
  101. if app_label in app_labels
  102. }
  103. if conflicts and not self.merge:
  104. name_str = "; ".join(
  105. "%s in %s" % (", ".join(names), app)
  106. for app, names in conflicts.items()
  107. )
  108. raise CommandError(
  109. "Conflicting migrations detected; multiple leaf nodes in the "
  110. "migration graph: (%s).\nTo fix them run "
  111. "'python manage.py makemigrations --merge'" % name_str
  112. )
  113. # If they want to merge and there's nothing to merge, then politely exit
  114. if self.merge and not conflicts:
  115. self.stdout.write("No conflicts detected to merge.")
  116. return
  117. # If they want to merge and there is something to merge, then
  118. # divert into the merge code
  119. if self.merge and conflicts:
  120. return self.handle_merge(loader, conflicts)
  121. if self.interactive:
  122. questioner = InteractiveMigrationQuestioner(specified_apps=app_labels, dry_run=self.dry_run)
  123. else:
  124. questioner = NonInteractiveMigrationQuestioner(specified_apps=app_labels, dry_run=self.dry_run)
  125. # Set up autodetector
  126. autodetector = MigrationAutodetector(
  127. loader.project_state(),
  128. ProjectState.from_apps(apps),
  129. questioner,
  130. )
  131. # If they want to make an empty migration, make one for each app
  132. if self.empty:
  133. if not app_labels:
  134. raise CommandError("You must supply at least one app label when using --empty.")
  135. # Make a fake changes() result we can pass to arrange_for_graph
  136. changes = {
  137. app: [Migration("custom", app)]
  138. for app in app_labels
  139. }
  140. changes = autodetector.arrange_for_graph(
  141. changes=changes,
  142. graph=loader.graph,
  143. migration_name=self.migration_name,
  144. )
  145. self.write_migration_files(changes)
  146. return
  147. # Detect changes
  148. changes = autodetector.changes(
  149. graph=loader.graph,
  150. trim_to_apps=app_labels or None,
  151. convert_apps=app_labels or None,
  152. migration_name=self.migration_name,
  153. )
  154. if not changes:
  155. # No changes? Tell them.
  156. if self.verbosity >= 1:
  157. if app_labels:
  158. if len(app_labels) == 1:
  159. self.stdout.write("No changes detected in app '%s'" % app_labels.pop())
  160. else:
  161. self.stdout.write("No changes detected in apps '%s'" % ("', '".join(app_labels)))
  162. else:
  163. self.stdout.write("No changes detected")
  164. else:
  165. self.write_migration_files(changes)
  166. if check_changes:
  167. sys.exit(1)
  168. def write_migration_files(self, changes):
  169. """
  170. Take a changes dict and write them out as migration files.
  171. """
  172. directory_created = {}
  173. for app_label, app_migrations in changes.items():
  174. if self.verbosity >= 1:
  175. self.stdout.write(self.style.MIGRATE_HEADING("Migrations for '%s':" % app_label) + "\n")
  176. for migration in app_migrations:
  177. # Describe the migration
  178. writer = MigrationWriter(migration, self.include_header)
  179. if self.verbosity >= 1:
  180. # Display a relative path if it's below the current working
  181. # directory, or an absolute path otherwise.
  182. try:
  183. migration_string = os.path.relpath(writer.path)
  184. except ValueError:
  185. migration_string = writer.path
  186. if migration_string.startswith('..'):
  187. migration_string = writer.path
  188. self.stdout.write(" %s\n" % (self.style.MIGRATE_LABEL(migration_string),))
  189. for operation in migration.operations:
  190. self.stdout.write(" - %s\n" % operation.describe())
  191. if not self.dry_run:
  192. # Write the migrations file to the disk.
  193. migrations_directory = os.path.dirname(writer.path)
  194. if not directory_created.get(app_label):
  195. if not os.path.isdir(migrations_directory):
  196. os.mkdir(migrations_directory)
  197. init_path = os.path.join(migrations_directory, "__init__.py")
  198. if not os.path.isfile(init_path):
  199. open(init_path, "w").close()
  200. # We just do this once per app
  201. directory_created[app_label] = True
  202. migration_string = writer.as_string()
  203. with open(writer.path, "w", encoding='utf-8') as fh:
  204. fh.write(migration_string)
  205. elif self.verbosity == 3:
  206. # Alternatively, makemigrations --dry-run --verbosity 3
  207. # will output the migrations to stdout rather than saving
  208. # the file to the disk.
  209. self.stdout.write(self.style.MIGRATE_HEADING(
  210. "Full migrations file '%s':" % writer.filename) + "\n"
  211. )
  212. self.stdout.write("%s\n" % writer.as_string())
  213. def handle_merge(self, loader, conflicts):
  214. """
  215. Handles merging together conflicted migrations interactively,
  216. if it's safe; otherwise, advises on how to fix it.
  217. """
  218. if self.interactive:
  219. questioner = InteractiveMigrationQuestioner()
  220. else:
  221. questioner = MigrationQuestioner(defaults={'ask_merge': True})
  222. for app_label, migration_names in conflicts.items():
  223. # Grab out the migrations in question, and work out their
  224. # common ancestor.
  225. merge_migrations = []
  226. for migration_name in migration_names:
  227. migration = loader.get_migration(app_label, migration_name)
  228. migration.ancestry = [
  229. mig for mig in loader.graph.forwards_plan((app_label, migration_name))
  230. if mig[0] == migration.app_label
  231. ]
  232. merge_migrations.append(migration)
  233. def all_items_equal(seq):
  234. return all(item == seq[0] for item in seq[1:])
  235. merge_migrations_generations = zip(*(m.ancestry for m in merge_migrations))
  236. common_ancestor_count = sum(1 for common_ancestor_generation
  237. in takewhile(all_items_equal, merge_migrations_generations))
  238. if not common_ancestor_count:
  239. raise ValueError("Could not find common ancestor of %s" % migration_names)
  240. # Now work out the operations along each divergent branch
  241. for migration in merge_migrations:
  242. migration.branch = migration.ancestry[common_ancestor_count:]
  243. migrations_ops = (loader.get_migration(node_app, node_name).operations
  244. for node_app, node_name in migration.branch)
  245. migration.merged_operations = sum(migrations_ops, [])
  246. # In future, this could use some of the Optimizer code
  247. # (can_optimize_through) to automatically see if they're
  248. # mergeable. For now, we always just prompt the user.
  249. if self.verbosity > 0:
  250. self.stdout.write(self.style.MIGRATE_HEADING("Merging %s" % app_label))
  251. for migration in merge_migrations:
  252. self.stdout.write(self.style.MIGRATE_LABEL(" Branch %s" % migration.name))
  253. for operation in migration.merged_operations:
  254. self.stdout.write(" - %s\n" % operation.describe())
  255. if questioner.ask_merge(app_label):
  256. # If they still want to merge it, then write out an empty
  257. # file depending on the migrations needing merging.
  258. numbers = [
  259. MigrationAutodetector.parse_number(migration.name)
  260. for migration in merge_migrations
  261. ]
  262. try:
  263. biggest_number = max(x for x in numbers if x is not None)
  264. except ValueError:
  265. biggest_number = 1
  266. subclass = type("Migration", (Migration,), {
  267. "dependencies": [(app_label, migration.name) for migration in merge_migrations],
  268. })
  269. migration_name = "%04i_%s" % (
  270. biggest_number + 1,
  271. self.migration_name or ("merge_%s" % get_migration_name_timestamp())
  272. )
  273. new_migration = subclass(migration_name, app_label)
  274. writer = MigrationWriter(new_migration, self.include_header)
  275. if not self.dry_run:
  276. # Write the merge migrations file to the disk
  277. with open(writer.path, "w", encoding='utf-8') as fh:
  278. fh.write(writer.as_string())
  279. if self.verbosity > 0:
  280. self.stdout.write("\nCreated new merge migration %s" % writer.path)
  281. elif self.verbosity == 3:
  282. # Alternatively, makemigrations --merge --dry-run --verbosity 3
  283. # will output the merge migrations to stdout rather than saving
  284. # the file to the disk.
  285. self.stdout.write(self.style.MIGRATE_HEADING(
  286. "Full merge migrations file '%s':" % writer.filename) + "\n"
  287. )
  288. self.stdout.write("%s\n" % writer.as_string())