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.

migrate.py 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. import time
  2. from collections import OrderedDict
  3. from importlib import import_module
  4. from django.apps import apps
  5. from django.core.checks import Tags, run_checks
  6. from django.core.management.base import (
  7. BaseCommand, CommandError, no_translations,
  8. )
  9. from django.core.management.sql import (
  10. emit_post_migrate_signal, emit_pre_migrate_signal,
  11. )
  12. from django.db import DEFAULT_DB_ALIAS, connections, router
  13. from django.db.migrations.autodetector import MigrationAutodetector
  14. from django.db.migrations.executor import MigrationExecutor
  15. from django.db.migrations.loader import AmbiguityError
  16. from django.db.migrations.state import ModelState, ProjectState
  17. from django.utils.module_loading import module_has_submodule
  18. class Command(BaseCommand):
  19. help = "Updates database schema. Manages both apps with migrations and those without."
  20. def add_arguments(self, parser):
  21. parser.add_argument(
  22. 'app_label', nargs='?',
  23. help='App label of an application to synchronize the state.',
  24. )
  25. parser.add_argument(
  26. 'migration_name', nargs='?',
  27. help='Database state will be brought to the state after that '
  28. 'migration. Use the name "zero" to unapply all migrations.',
  29. )
  30. parser.add_argument(
  31. '--noinput', '--no-input', action='store_false', dest='interactive',
  32. help='Tells Django to NOT prompt the user for input of any kind.',
  33. )
  34. parser.add_argument(
  35. '--database', action='store', dest='database',
  36. default=DEFAULT_DB_ALIAS,
  37. help='Nominates a database to synchronize. Defaults to the "default" database.',
  38. )
  39. parser.add_argument(
  40. '--fake', action='store_true', dest='fake',
  41. help='Mark migrations as run without actually running them.',
  42. )
  43. parser.add_argument(
  44. '--fake-initial', action='store_true', dest='fake_initial',
  45. help='Detect if tables already exist and fake-apply initial migrations if so. Make sure '
  46. 'that the current database schema matches your initial migration before using this '
  47. 'flag. Django will only check for an existing table name.',
  48. )
  49. parser.add_argument(
  50. '--run-syncdb', action='store_true', dest='run_syncdb',
  51. help='Creates tables for apps without migrations.',
  52. )
  53. def _run_checks(self, **kwargs):
  54. issues = run_checks(tags=[Tags.database])
  55. issues.extend(super()._run_checks(**kwargs))
  56. return issues
  57. @no_translations
  58. def handle(self, *args, **options):
  59. self.verbosity = options['verbosity']
  60. self.interactive = options['interactive']
  61. # Import the 'management' module within each installed app, to register
  62. # dispatcher events.
  63. for app_config in apps.get_app_configs():
  64. if module_has_submodule(app_config.module, "management"):
  65. import_module('.management', app_config.name)
  66. # Get the database we're operating from
  67. db = options['database']
  68. connection = connections[db]
  69. # Hook for backends needing any database preparation
  70. connection.prepare_database()
  71. # Work out which apps have migrations and which do not
  72. executor = MigrationExecutor(connection, self.migration_progress_callback)
  73. # Raise an error if any migrations are applied before their dependencies.
  74. executor.loader.check_consistent_history(connection)
  75. # Before anything else, see if there's conflicting apps and drop out
  76. # hard if there are any
  77. conflicts = executor.loader.detect_conflicts()
  78. if conflicts:
  79. name_str = "; ".join(
  80. "%s in %s" % (", ".join(names), app)
  81. for app, names in conflicts.items()
  82. )
  83. raise CommandError(
  84. "Conflicting migrations detected; multiple leaf nodes in the "
  85. "migration graph: (%s).\nTo fix them run "
  86. "'python manage.py makemigrations --merge'" % name_str
  87. )
  88. # If they supplied command line arguments, work out what they mean.
  89. target_app_labels_only = True
  90. if options['app_label'] and options['migration_name']:
  91. app_label, migration_name = options['app_label'], options['migration_name']
  92. if app_label not in executor.loader.migrated_apps:
  93. raise CommandError(
  94. "App '%s' does not have migrations." % app_label
  95. )
  96. if migration_name == "zero":
  97. targets = [(app_label, None)]
  98. else:
  99. try:
  100. migration = executor.loader.get_migration_by_prefix(app_label, migration_name)
  101. except AmbiguityError:
  102. raise CommandError(
  103. "More than one migration matches '%s' in app '%s'. "
  104. "Please be more specific." %
  105. (migration_name, app_label)
  106. )
  107. except KeyError:
  108. raise CommandError("Cannot find a migration matching '%s' from app '%s'." % (
  109. migration_name, app_label))
  110. targets = [(app_label, migration.name)]
  111. target_app_labels_only = False
  112. elif options['app_label']:
  113. app_label = options['app_label']
  114. if app_label not in executor.loader.migrated_apps:
  115. raise CommandError(
  116. "App '%s' does not have migrations." % app_label
  117. )
  118. targets = [key for key in executor.loader.graph.leaf_nodes() if key[0] == app_label]
  119. else:
  120. targets = executor.loader.graph.leaf_nodes()
  121. plan = executor.migration_plan(targets)
  122. run_syncdb = options['run_syncdb'] and executor.loader.unmigrated_apps
  123. # Print some useful info
  124. if self.verbosity >= 1:
  125. self.stdout.write(self.style.MIGRATE_HEADING("Operations to perform:"))
  126. if run_syncdb:
  127. self.stdout.write(
  128. self.style.MIGRATE_LABEL(" Synchronize unmigrated apps: ") +
  129. (", ".join(sorted(executor.loader.unmigrated_apps)))
  130. )
  131. if target_app_labels_only:
  132. self.stdout.write(
  133. self.style.MIGRATE_LABEL(" Apply all migrations: ") +
  134. (", ".join(sorted({a for a, n in targets})) or "(none)")
  135. )
  136. else:
  137. if targets[0][1] is None:
  138. self.stdout.write(self.style.MIGRATE_LABEL(
  139. " Unapply all migrations: ") + "%s" % (targets[0][0],)
  140. )
  141. else:
  142. self.stdout.write(self.style.MIGRATE_LABEL(
  143. " Target specific migration: ") + "%s, from %s"
  144. % (targets[0][1], targets[0][0])
  145. )
  146. pre_migrate_state = executor._create_project_state(with_applied_migrations=True)
  147. pre_migrate_apps = pre_migrate_state.apps
  148. emit_pre_migrate_signal(
  149. self.verbosity, self.interactive, connection.alias, apps=pre_migrate_apps, plan=plan,
  150. )
  151. # Run the syncdb phase.
  152. if run_syncdb:
  153. if self.verbosity >= 1:
  154. self.stdout.write(self.style.MIGRATE_HEADING("Synchronizing apps without migrations:"))
  155. self.sync_apps(connection, executor.loader.unmigrated_apps)
  156. # Migrate!
  157. if self.verbosity >= 1:
  158. self.stdout.write(self.style.MIGRATE_HEADING("Running migrations:"))
  159. if not plan:
  160. if self.verbosity >= 1:
  161. self.stdout.write(" No migrations to apply.")
  162. # If there's changes that aren't in migrations yet, tell them how to fix it.
  163. autodetector = MigrationAutodetector(
  164. executor.loader.project_state(),
  165. ProjectState.from_apps(apps),
  166. )
  167. changes = autodetector.changes(graph=executor.loader.graph)
  168. if changes:
  169. self.stdout.write(self.style.NOTICE(
  170. " Your models have changes that are not yet reflected "
  171. "in a migration, and so won't be applied."
  172. ))
  173. self.stdout.write(self.style.NOTICE(
  174. " Run 'manage.py makemigrations' to make new "
  175. "migrations, and then re-run 'manage.py migrate' to "
  176. "apply them."
  177. ))
  178. fake = False
  179. fake_initial = False
  180. else:
  181. fake = options['fake']
  182. fake_initial = options['fake_initial']
  183. post_migrate_state = executor.migrate(
  184. targets, plan=plan, state=pre_migrate_state.clone(), fake=fake,
  185. fake_initial=fake_initial,
  186. )
  187. # post_migrate signals have access to all models. Ensure that all models
  188. # are reloaded in case any are delayed.
  189. post_migrate_state.clear_delayed_apps_cache()
  190. post_migrate_apps = post_migrate_state.apps
  191. # Re-render models of real apps to include relationships now that
  192. # we've got a final state. This wouldn't be necessary if real apps
  193. # models were rendered with relationships in the first place.
  194. with post_migrate_apps.bulk_update():
  195. model_keys = []
  196. for model_state in post_migrate_apps.real_models:
  197. model_key = model_state.app_label, model_state.name_lower
  198. model_keys.append(model_key)
  199. post_migrate_apps.unregister_model(*model_key)
  200. post_migrate_apps.render_multiple([
  201. ModelState.from_model(apps.get_model(*model)) for model in model_keys
  202. ])
  203. # Send the post_migrate signal, so individual apps can do whatever they need
  204. # to do at this point.
  205. emit_post_migrate_signal(
  206. self.verbosity, self.interactive, connection.alias, apps=post_migrate_apps, plan=plan,
  207. )
  208. def migration_progress_callback(self, action, migration=None, fake=False):
  209. if self.verbosity >= 1:
  210. compute_time = self.verbosity > 1
  211. if action == "apply_start":
  212. if compute_time:
  213. self.start = time.time()
  214. self.stdout.write(" Applying %s..." % migration, ending="")
  215. self.stdout.flush()
  216. elif action == "apply_success":
  217. elapsed = " (%.3fs)" % (time.time() - self.start) if compute_time else ""
  218. if fake:
  219. self.stdout.write(self.style.SUCCESS(" FAKED" + elapsed))
  220. else:
  221. self.stdout.write(self.style.SUCCESS(" OK" + elapsed))
  222. elif action == "unapply_start":
  223. if compute_time:
  224. self.start = time.time()
  225. self.stdout.write(" Unapplying %s..." % migration, ending="")
  226. self.stdout.flush()
  227. elif action == "unapply_success":
  228. elapsed = " (%.3fs)" % (time.time() - self.start) if compute_time else ""
  229. if fake:
  230. self.stdout.write(self.style.SUCCESS(" FAKED" + elapsed))
  231. else:
  232. self.stdout.write(self.style.SUCCESS(" OK" + elapsed))
  233. elif action == "render_start":
  234. if compute_time:
  235. self.start = time.time()
  236. self.stdout.write(" Rendering model states...", ending="")
  237. self.stdout.flush()
  238. elif action == "render_success":
  239. elapsed = " (%.3fs)" % (time.time() - self.start) if compute_time else ""
  240. self.stdout.write(self.style.SUCCESS(" DONE" + elapsed))
  241. def sync_apps(self, connection, app_labels):
  242. """Run the old syncdb-style operation on a list of app_labels."""
  243. with connection.cursor() as cursor:
  244. tables = connection.introspection.table_names(cursor)
  245. # Build the manifest of apps and models that are to be synchronized.
  246. all_models = [
  247. (
  248. app_config.label,
  249. router.get_migratable_models(app_config, connection.alias, include_auto_created=False),
  250. )
  251. for app_config in apps.get_app_configs()
  252. if app_config.models_module is not None and app_config.label in app_labels
  253. ]
  254. def model_installed(model):
  255. opts = model._meta
  256. converter = connection.introspection.table_name_converter
  257. return not (
  258. (converter(opts.db_table) in tables) or
  259. (opts.auto_created and converter(opts.auto_created._meta.db_table) in tables)
  260. )
  261. manifest = OrderedDict(
  262. (app_name, list(filter(model_installed, model_list)))
  263. for app_name, model_list in all_models
  264. )
  265. # Create the tables for each model
  266. if self.verbosity >= 1:
  267. self.stdout.write(" Creating tables...\n")
  268. with connection.schema_editor() as editor:
  269. for app_name, model_list in manifest.items():
  270. for model in model_list:
  271. # Never install unmanaged models, etc.
  272. if not model._meta.can_migrate(connection):
  273. continue
  274. if self.verbosity >= 3:
  275. self.stdout.write(
  276. " Processing %s.%s model\n" % (app_name, model._meta.object_name)
  277. )
  278. if self.verbosity >= 1:
  279. self.stdout.write(" Creating table %s\n" % model._meta.db_table)
  280. editor.create_model(model)
  281. # Deferred SQL is executed when exiting the editor's context.
  282. if self.verbosity >= 1:
  283. self.stdout.write(" Running deferred SQL...\n")