Development of an internal social media platform with personalised dashboards for students
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

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