Switch branches/tags
Find file Copy path
312 lines (283 sloc) 13.6 KB
import time
from collections import OrderedDict
from importlib import import_module
from django.apps import apps
from django.core.checks import Tags, run_checks
from import (
BaseCommand, CommandError, no_translations,
from import (
emit_post_migrate_signal, emit_pre_migrate_signal,
from django.db import DEFAULT_DB_ALIAS, connections, router
from django.db.migrations.autodetector import MigrationAutodetector
from django.db.migrations.executor import MigrationExecutor
from django.db.migrations.loader import AmbiguityError
from django.db.migrations.state import ModelState, ProjectState
from django.utils.module_loading import module_has_submodule
class Command(BaseCommand):
help = "Updates database schema. Manages both apps with migrations and those without."
def add_arguments(self, parser):
'app_label', nargs='?',
help='App label of an application to synchronize the state.',
'migration_name', nargs='?',
help='Database state will be brought to the state after that '
'migration. Use the name "zero" to unapply all migrations.',
'--noinput', '--no-input', action='store_false', dest='interactive',
help='Tells Django to NOT prompt the user for input of any kind.',
help='Nominates a database to synchronize. Defaults to the "default" database.',
'--fake', action='store_true',
help='Mark migrations as run without actually running them.',
'--fake-initial', action='store_true',
help='Detect if tables already exist and fake-apply initial migrations if so. Make sure '
'that the current database schema matches your initial migration before using this '
'flag. Django will only check for an existing table name.',
'--run-syncdb', action='store_true',
help='Creates tables for apps without migrations.',
def _run_checks(self, **kwargs):
issues = run_checks(tags=[Tags.database])
return issues
def handle(self, *args, **options):
self.verbosity = options['verbosity']
self.interactive = options['interactive']
# Import the 'management' module within each installed app, to register
# dispatcher events.
for app_config in apps.get_app_configs():
if module_has_submodule(app_config.module, "management"):
# Get the database we're operating from
db = options['database']
connection = connections[db]
# Hook for backends needing any database preparation
# Work out which apps have migrations and which do not
executor = MigrationExecutor(connection, self.migration_progress_callback)
# Raise an error if any migrations are applied before their dependencies.
# Before anything else, see if there's conflicting apps and drop out
# hard if there are any
conflicts = executor.loader.detect_conflicts()
if conflicts:
name_str = "; ".join(
"%s in %s" % (", ".join(names), app)
for app, names in conflicts.items()
raise CommandError(
"Conflicting migrations detected; multiple leaf nodes in the "
"migration graph: (%s).\nTo fix them run "
"'python makemigrations --merge'" % name_str
# If they supplied command line arguments, work out what they mean.
target_app_labels_only = True
if options['app_label']:
# Validate app_label.
app_label = options['app_label']
except LookupError as err:
raise CommandError(str(err))
if app_label not in executor.loader.migrated_apps:
raise CommandError("App '%s' does not have migrations." % app_label)
if options['app_label'] and options['migration_name']:
migration_name = options['migration_name']
if migration_name == "zero":
targets = [(app_label, None)]
migration = executor.loader.get_migration_by_prefix(app_label, migration_name)
except AmbiguityError:
raise CommandError(
"More than one migration matches '%s' in app '%s'. "
"Please be more specific." %
(migration_name, app_label)
except KeyError:
raise CommandError("Cannot find a migration matching '%s' from app '%s'." % (
migration_name, app_label))
targets = [(app_label,]
target_app_labels_only = False
elif options['app_label']:
targets = [key for key in executor.loader.graph.leaf_nodes() if key[0] == app_label]
targets = executor.loader.graph.leaf_nodes()
plan = executor.migration_plan(targets)
run_syncdb = options['run_syncdb'] and executor.loader.unmigrated_apps
# Print some useful info
if self.verbosity >= 1:
self.stdout.write("Operations to perform:"))
if run_syncdb:
self.stdout.write(" Synchronize unmigrated apps: ") +
(", ".join(sorted(executor.loader.unmigrated_apps)))
if target_app_labels_only:
self.stdout.write(" Apply all migrations: ") +
(", ".join(sorted({a for a, n in targets})) or "(none)")
if targets[0][1] is None:
" Unapply all migrations: ") + "%s" % (targets[0][0],)
" Target specific migration: ") + "%s, from %s"
% (targets[0][1], targets[0][0])
pre_migrate_state = executor._create_project_state(with_applied_migrations=True)
pre_migrate_apps = pre_migrate_state.apps
self.verbosity, self.interactive, connection.alias, apps=pre_migrate_apps, plan=plan,
# Run the syncdb phase.
if run_syncdb:
if self.verbosity >= 1:
self.stdout.write("Synchronizing apps without migrations:"))
self.sync_apps(connection, executor.loader.unmigrated_apps)
# Migrate!
if self.verbosity >= 1:
self.stdout.write("Running migrations:"))
if not plan:
if self.verbosity >= 1:
self.stdout.write(" No migrations to apply.")
# If there's changes that aren't in migrations yet, tell them how to fix it.
autodetector = MigrationAutodetector(
changes = autodetector.changes(graph=executor.loader.graph)
if changes:
" Your models have changes that are not yet reflected "
"in a migration, and so won't be applied."
" Run ' makemigrations' to make new "
"migrations, and then re-run ' migrate' to "
"apply them."
fake = False
fake_initial = False
fake = options['fake']
fake_initial = options['fake_initial']
post_migrate_state = executor.migrate(
targets, plan=plan, state=pre_migrate_state.clone(), fake=fake,
# post_migrate signals have access to all models. Ensure that all models
# are reloaded in case any are delayed.
post_migrate_apps = post_migrate_state.apps
# Re-render models of real apps to include relationships now that
# we've got a final state. This wouldn't be necessary if real apps
# models were rendered with relationships in the first place.
with post_migrate_apps.bulk_update():
model_keys = []
for model_state in post_migrate_apps.real_models:
model_key = model_state.app_label, model_state.name_lower
ModelState.from_model(apps.get_model(*model)) for model in model_keys
# Send the post_migrate signal, so individual apps can do whatever they need
# to do at this point.
self.verbosity, self.interactive, connection.alias, apps=post_migrate_apps, plan=plan,
def migration_progress_callback(self, action, migration=None, fake=False):
if self.verbosity >= 1:
compute_time = self.verbosity > 1
if action == "apply_start":
if compute_time:
self.start = time.time()
self.stdout.write(" Applying %s..." % migration, ending="")
elif action == "apply_success":
elapsed = " (%.3fs)" % (time.time() - self.start) if compute_time else ""
if fake:
self.stdout.write(" FAKED" + elapsed))
self.stdout.write(" OK" + elapsed))
elif action == "unapply_start":
if compute_time:
self.start = time.time()
self.stdout.write(" Unapplying %s..." % migration, ending="")
elif action == "unapply_success":
elapsed = " (%.3fs)" % (time.time() - self.start) if compute_time else ""
if fake:
self.stdout.write(" FAKED" + elapsed))
self.stdout.write(" OK" + elapsed))
elif action == "render_start":
if compute_time:
self.start = time.time()
self.stdout.write(" Rendering model states...", ending="")
elif action == "render_success":
elapsed = " (%.3fs)" % (time.time() - self.start) if compute_time else ""
self.stdout.write(" DONE" + elapsed))
def sync_apps(self, connection, app_labels):
"""Run the old syncdb-style operation on a list of app_labels."""
with connection.cursor() as cursor:
tables = connection.introspection.table_names(cursor)
# Build the manifest of apps and models that are to be synchronized.
all_models = [
router.get_migratable_models(app_config, connection.alias, include_auto_created=False),
for app_config in apps.get_app_configs()
if app_config.models_module is not None and app_config.label in app_labels
def model_installed(model):
opts = model._meta
converter = connection.introspection.table_name_converter
return not (
(converter(opts.db_table) in tables) or
(opts.auto_created and converter(opts.auto_created._meta.db_table) in tables)
manifest = OrderedDict(
(app_name, list(filter(model_installed, model_list)))
for app_name, model_list in all_models
# Create the tables for each model
if self.verbosity >= 1:
self.stdout.write(" Creating tables...\n")
with connection.schema_editor() as editor:
for app_name, model_list in manifest.items():
for model in model_list:
# Never install unmanaged models, etc.
if not model._meta.can_migrate(connection):
if self.verbosity >= 3:
" Processing %s.%s model\n" % (app_name, model._meta.object_name)
if self.verbosity >= 1:
self.stdout.write(" Creating table %s\n" % model._meta.db_table)
# Deferred SQL is executed when exiting the editor's context.
if self.verbosity >= 1:
self.stdout.write(" Running deferred SQL...\n")