2020-03-26 17:08:58 +08:00
|
|
|
import sys
|
2014-11-05 18:01:48 +08:00
|
|
|
import time
|
2015-01-28 20:35:27 +08:00
|
|
|
from importlib import import_module
|
2013-06-08 01:47:17 +08:00
|
|
|
|
2013-12-24 19:25:17 +08:00
|
|
|
from django.apps import apps
|
2013-02-04 21:35:52 +08:00
|
|
|
from django.core.management.base import BaseCommand, CommandError, no_translations
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.core.management.sql import emit_post_migrate_signal, emit_pre_migrate_signal
|
2016-12-24 11:34:52 +08:00
|
|
|
from django.db import DEFAULT_DB_ALIAS, connections, router
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.db.migrations.autodetector import MigrationAutodetector
|
2013-06-08 01:47:17 +08:00
|
|
|
from django.db.migrations.executor import MigrationExecutor
|
2014-09-03 16:51:07 +08:00
|
|
|
from django.db.migrations.loader import AmbiguityError
|
2016-05-13 23:58:54 +08:00
|
|
|
from django.db.migrations.state import ModelState, ProjectState
|
2013-07-26 23:39:44 +08:00
|
|
|
from django.utils.module_loading import module_has_submodule
|
2018-05-19 23:38:02 +08:00
|
|
|
from django.utils.text import Truncator
|
2013-06-08 01:47:17 +08:00
|
|
|
|
|
|
|
|
2013-07-23 02:43:58 +08:00
|
|
|
class Command(BaseCommand):
|
2013-06-08 01:47:17 +08:00
|
|
|
help = (
|
|
|
|
"Updates database schema. Manages both apps with migrations and those without."
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2020-05-14 06:00:41 +08:00
|
|
|
requires_system_checks = []
|
2014-06-07 04:39:33 +08:00
|
|
|
|
|
|
|
def add_arguments(self, parser):
|
2020-02-07 15:46:13 +08:00
|
|
|
parser.add_argument(
|
|
|
|
"--skip-checks",
|
|
|
|
action="store_true",
|
|
|
|
help="Skip system checks.",
|
|
|
|
)
|
2016-03-29 06:33:29 +08:00
|
|
|
parser.add_argument(
|
|
|
|
"app_label",
|
|
|
|
nargs="?",
|
|
|
|
help="App label of an application to synchronize the state.",
|
2014-09-04 20:15:09 +08:00
|
|
|
)
|
2016-03-29 06:33:29 +08:00
|
|
|
parser.add_argument(
|
|
|
|
"migration_name",
|
|
|
|
nargs="?",
|
|
|
|
help="Database state will be brought to the state after that "
|
|
|
|
'migration. Use the name "zero" to unapply all migrations.',
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
2017-04-02 08:03:56 +08:00
|
|
|
"--noinput",
|
|
|
|
"--no-input",
|
|
|
|
action="store_false",
|
|
|
|
dest="interactive",
|
2016-03-29 06:33:29 +08:00
|
|
|
help="Tells Django to NOT prompt the user for input of any kind.",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
2018-07-03 05:54:57 +08:00
|
|
|
"--database",
|
2016-03-29 06:33:29 +08:00
|
|
|
default=DEFAULT_DB_ALIAS,
|
|
|
|
help=(
|
|
|
|
'Nominates a database to synchronize. Defaults to the "default" '
|
|
|
|
"database."
|
|
|
|
),
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
2018-07-03 05:54:57 +08:00
|
|
|
"--fake",
|
|
|
|
action="store_true",
|
2016-03-29 06:33:29 +08:00
|
|
|
help="Mark migrations as run without actually running them.",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
2018-07-03 05:54:57 +08:00
|
|
|
"--fake-initial",
|
|
|
|
action="store_true",
|
2015-02-12 19:48:28 +08:00
|
|
|
help=(
|
|
|
|
"Detect if tables already exist and fake-apply initial migrations if "
|
|
|
|
"so. Make sure that the current database schema matches your initial "
|
2016-03-29 06:33:29 +08:00
|
|
|
"migration before using this flag. Django will only check for an "
|
|
|
|
"existing table name."
|
|
|
|
),
|
|
|
|
)
|
2018-05-19 23:38:02 +08:00
|
|
|
parser.add_argument(
|
|
|
|
"--plan",
|
|
|
|
action="store_true",
|
|
|
|
help="Shows a list of the migration actions that will be performed.",
|
|
|
|
)
|
2016-03-29 06:33:29 +08:00
|
|
|
parser.add_argument(
|
2018-07-03 05:54:57 +08:00
|
|
|
"--run-syncdb",
|
|
|
|
action="store_true",
|
2016-03-29 06:33:29 +08:00
|
|
|
help="Creates tables for apps without migrations.",
|
|
|
|
)
|
2020-03-26 17:08:58 +08:00
|
|
|
parser.add_argument(
|
|
|
|
"--check",
|
|
|
|
action="store_true",
|
|
|
|
dest="check_unapplied",
|
|
|
|
help="Exits with a non-zero status if unapplied migrations exist.",
|
|
|
|
)
|
2021-12-25 07:37:22 +08:00
|
|
|
parser.add_argument(
|
|
|
|
"--prune",
|
|
|
|
action="store_true",
|
|
|
|
dest="prune",
|
|
|
|
help="Delete nonexistent migrations from the django_migrations table.",
|
|
|
|
)
|
2013-06-08 01:47:17 +08:00
|
|
|
|
2013-02-04 21:35:52 +08:00
|
|
|
@no_translations
|
2013-07-23 02:43:58 +08:00
|
|
|
def handle(self, *args, **options):
|
2020-02-07 15:46:13 +08:00
|
|
|
database = options["database"]
|
|
|
|
if not options["skip_checks"]:
|
|
|
|
self.check(databases=[database])
|
2013-06-08 01:47:17 +08:00
|
|
|
|
2016-03-03 10:01:36 +08:00
|
|
|
self.verbosity = options["verbosity"]
|
|
|
|
self.interactive = options["interactive"]
|
2013-06-08 01:47:17 +08:00
|
|
|
|
|
|
|
# Import the 'management' module within each installed app, to register
|
|
|
|
# dispatcher events.
|
2013-12-24 19:25:17 +08:00
|
|
|
for app_config in apps.get_app_configs():
|
2013-12-27 01:40:28 +08:00
|
|
|
if module_has_submodule(app_config.module, "management"):
|
2013-12-19 22:57:23 +08:00
|
|
|
import_module(".management", app_config.name)
|
2013-06-08 01:47:17 +08:00
|
|
|
|
|
|
|
# Get the database we're operating from
|
2020-02-07 15:46:13 +08:00
|
|
|
connection = connections[database]
|
2013-06-08 01:47:17 +08:00
|
|
|
|
2014-12-06 18:05:54 +08:00
|
|
|
# Hook for backends needing any database preparation
|
|
|
|
connection.prepare_database()
|
2013-06-08 01:47:17 +08:00
|
|
|
# Work out which apps have migrations and which do not
|
|
|
|
executor = MigrationExecutor(connection, self.migration_progress_callback)
|
|
|
|
|
2016-04-02 20:46:59 +08:00
|
|
|
# Raise an error if any migrations are applied before their dependencies.
|
|
|
|
executor.loader.check_consistent_history(connection)
|
|
|
|
|
2013-12-05 00:01:31 +08:00
|
|
|
# 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()
|
|
|
|
)
|
2014-09-04 20:15:09 +08:00
|
|
|
raise CommandError(
|
2015-08-21 06:56:57 +08:00
|
|
|
"Conflicting migrations detected; multiple leaf nodes in the "
|
|
|
|
"migration graph: (%s).\nTo fix them run "
|
2014-09-04 20:15:09 +08:00
|
|
|
"'python manage.py makemigrations --merge'" % name_str
|
|
|
|
)
|
2013-12-05 00:01:31 +08:00
|
|
|
|
2013-07-23 02:43:58 +08:00
|
|
|
# If they supplied command line arguments, work out what they mean.
|
2018-07-16 11:32:47 +08:00
|
|
|
run_syncdb = options["run_syncdb"]
|
2013-07-23 02:43:58 +08:00
|
|
|
target_app_labels_only = True
|
2018-06-19 15:35:55 +08:00
|
|
|
if options["app_label"]:
|
|
|
|
# Validate app_label.
|
|
|
|
app_label = options["app_label"]
|
|
|
|
try:
|
|
|
|
apps.get_app_config(app_label)
|
|
|
|
except LookupError as err:
|
|
|
|
raise CommandError(str(err))
|
2018-07-16 11:32:47 +08:00
|
|
|
if run_syncdb:
|
|
|
|
if app_label in executor.loader.migrated_apps:
|
|
|
|
raise CommandError(
|
|
|
|
"Can't use run_syncdb with app '%s' as it has migrations."
|
|
|
|
% app_label
|
|
|
|
)
|
|
|
|
elif app_label not in executor.loader.migrated_apps:
|
2018-06-19 15:35:55 +08:00
|
|
|
raise CommandError("App '%s' does not have migrations." % app_label)
|
|
|
|
|
|
|
|
if options["app_label"] and options["migration_name"]:
|
|
|
|
migration_name = options["migration_name"]
|
2013-07-23 02:43:58 +08:00
|
|
|
if migration_name == "zero":
|
2013-07-25 20:52:35 +08:00
|
|
|
targets = [(app_label, None)]
|
2013-07-23 02:43:58 +08:00
|
|
|
else:
|
|
|
|
try:
|
|
|
|
migration = executor.loader.get_migration_by_prefix(
|
|
|
|
app_label, migration_name
|
|
|
|
)
|
|
|
|
except AmbiguityError:
|
2014-09-04 20:15:09 +08:00
|
|
|
raise CommandError(
|
|
|
|
"More than one migration matches '%s' in app '%s'. "
|
|
|
|
"Please be more specific." % (migration_name, app_label)
|
|
|
|
)
|
2013-07-23 02:43:58 +08:00
|
|
|
except KeyError:
|
2014-06-20 19:52:36 +08:00
|
|
|
raise CommandError(
|
|
|
|
"Cannot find a migration matching '%s' from app '%s'."
|
|
|
|
% (migration_name, app_label)
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2021-08-02 23:07:58 +08:00
|
|
|
target = (app_label, migration.name)
|
|
|
|
# Partially applied squashed migrations are not included in the
|
|
|
|
# graph, use the last replacement instead.
|
|
|
|
if (
|
|
|
|
target not in executor.loader.graph.nodes
|
|
|
|
and target in executor.loader.replacements
|
|
|
|
):
|
|
|
|
incomplete_migration = executor.loader.replacements[target]
|
|
|
|
target = incomplete_migration.replaces[-1]
|
|
|
|
targets = [target]
|
2013-07-23 02:43:58 +08:00
|
|
|
target_app_labels_only = False
|
2014-06-07 04:39:33 +08:00
|
|
|
elif options["app_label"]:
|
2013-07-23 02:43:58 +08:00
|
|
|
targets = [
|
|
|
|
key for key in executor.loader.graph.leaf_nodes() if key[0] == app_label
|
|
|
|
]
|
|
|
|
else:
|
|
|
|
targets = executor.loader.graph.leaf_nodes()
|
|
|
|
|
2021-12-25 07:37:22 +08:00
|
|
|
if options["prune"]:
|
|
|
|
if not options["app_label"]:
|
|
|
|
raise CommandError(
|
|
|
|
"Migrations can be pruned only when an app is specified."
|
|
|
|
)
|
|
|
|
if self.verbosity > 0:
|
|
|
|
self.stdout.write("Pruning migrations:", self.style.MIGRATE_HEADING)
|
|
|
|
to_prune = set(executor.loader.applied_migrations) - set(
|
|
|
|
executor.loader.disk_migrations
|
|
|
|
)
|
|
|
|
squashed_migrations_with_deleted_replaced_migrations = [
|
|
|
|
migration_key
|
|
|
|
for migration_key, migration_obj in executor.loader.replacements.items()
|
|
|
|
if any(replaced in to_prune for replaced in migration_obj.replaces)
|
|
|
|
]
|
|
|
|
if squashed_migrations_with_deleted_replaced_migrations:
|
|
|
|
self.stdout.write(
|
|
|
|
self.style.NOTICE(
|
|
|
|
" Cannot use --prune because the following squashed "
|
|
|
|
"migrations have their 'replaces' attributes and may not "
|
|
|
|
"be recorded as applied:"
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2021-12-25 07:37:22 +08:00
|
|
|
)
|
|
|
|
for migration in squashed_migrations_with_deleted_replaced_migrations:
|
|
|
|
app, name = migration
|
|
|
|
self.stdout.write(f" {app}.{name}")
|
|
|
|
self.stdout.write(
|
|
|
|
self.style.NOTICE(
|
|
|
|
" Re-run 'manage.py migrate' if they are not marked as "
|
|
|
|
"applied, and remove 'replaces' attributes in their "
|
|
|
|
"Migration classes."
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2021-12-25 07:37:22 +08:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
to_prune = sorted(
|
|
|
|
migration for migration in to_prune if migration[0] == app_label
|
|
|
|
)
|
|
|
|
if to_prune:
|
|
|
|
for migration in to_prune:
|
|
|
|
app, name = migration
|
|
|
|
if self.verbosity > 0:
|
|
|
|
self.stdout.write(
|
|
|
|
self.style.MIGRATE_LABEL(f" Pruning {app}.{name}"),
|
|
|
|
ending="",
|
|
|
|
)
|
|
|
|
executor.recorder.record_unapplied(app, name)
|
|
|
|
if self.verbosity > 0:
|
|
|
|
self.stdout.write(self.style.SUCCESS(" OK"))
|
|
|
|
elif self.verbosity > 0:
|
|
|
|
self.stdout.write(" No migrations to prune.")
|
|
|
|
|
2013-06-08 01:47:17 +08:00
|
|
|
plan = executor.migration_plan(targets)
|
2020-03-26 17:08:58 +08:00
|
|
|
exit_dry = plan and options["check_unapplied"]
|
2013-06-08 01:47:17 +08:00
|
|
|
|
2018-05-19 23:38:02 +08:00
|
|
|
if options["plan"]:
|
|
|
|
self.stdout.write("Planned operations:", self.style.MIGRATE_LABEL)
|
|
|
|
if not plan:
|
|
|
|
self.stdout.write(" No planned migration operations.")
|
|
|
|
for migration, backwards in plan:
|
|
|
|
self.stdout.write(str(migration), self.style.MIGRATE_HEADING)
|
|
|
|
for operation in migration.operations:
|
|
|
|
message, is_error = self.describe_operation(operation, backwards)
|
|
|
|
style = self.style.WARNING if is_error else None
|
|
|
|
self.stdout.write(" " + message, style)
|
2020-03-26 17:08:58 +08:00
|
|
|
if exit_dry:
|
|
|
|
sys.exit(1)
|
2018-05-19 23:38:02 +08:00
|
|
|
return
|
2020-03-26 17:08:58 +08:00
|
|
|
if exit_dry:
|
|
|
|
sys.exit(1)
|
2021-12-25 07:37:22 +08:00
|
|
|
if options["prune"]:
|
|
|
|
return
|
2018-05-19 23:38:02 +08:00
|
|
|
|
2018-07-16 11:32:47 +08:00
|
|
|
# At this point, ignore run_syncdb if there aren't any apps to sync.
|
2018-05-19 23:38:02 +08:00
|
|
|
run_syncdb = options["run_syncdb"] and executor.loader.unmigrated_apps
|
2013-07-23 02:43:58 +08:00
|
|
|
# Print some useful info
|
2013-06-08 01:47:17 +08:00
|
|
|
if self.verbosity >= 1:
|
2013-07-23 02:43:58 +08:00
|
|
|
self.stdout.write(self.style.MIGRATE_HEADING("Operations to perform:"))
|
2014-12-27 02:56:08 +08:00
|
|
|
if run_syncdb:
|
2018-07-16 11:32:47 +08:00
|
|
|
if options["app_label"]:
|
|
|
|
self.stdout.write(
|
|
|
|
self.style.MIGRATE_LABEL(
|
|
|
|
" Synchronize unmigrated app: %s" % app_label
|
|
|
|
)
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
self.stdout.write(
|
|
|
|
self.style.MIGRATE_LABEL(" Synchronize unmigrated apps: ")
|
|
|
|
+ (", ".join(sorted(executor.loader.unmigrated_apps)))
|
|
|
|
)
|
2013-07-23 02:43:58 +08:00
|
|
|
if target_app_labels_only:
|
2014-09-04 20:15:09 +08:00
|
|
|
self.stdout.write(
|
|
|
|
self.style.MIGRATE_LABEL(" Apply all migrations: ")
|
2017-06-02 07:08:59 +08:00
|
|
|
+ (", ".join(sorted({a for a, n in targets})) or "(none)")
|
2014-09-04 20:15:09 +08:00
|
|
|
)
|
2013-07-23 02:43:58 +08:00
|
|
|
else:
|
|
|
|
if targets[0][1] is None:
|
2020-05-01 22:54:15 +08:00
|
|
|
self.stdout.write(
|
|
|
|
self.style.MIGRATE_LABEL(" Unapply all migrations: ")
|
|
|
|
+ str(targets[0][0])
|
2014-09-04 20:15:09 +08:00
|
|
|
)
|
2013-07-23 02:43:58 +08:00
|
|
|
else:
|
2014-09-04 20:15:09 +08:00
|
|
|
self.stdout.write(
|
|
|
|
self.style.MIGRATE_LABEL(" Target specific migration: ")
|
|
|
|
+ "%s, from %s" % (targets[0][1], targets[0][0])
|
|
|
|
)
|
2013-06-08 01:47:17 +08:00
|
|
|
|
2016-08-21 06:05:04 +08:00
|
|
|
pre_migrate_state = executor._create_project_state(with_applied_migrations=True)
|
|
|
|
pre_migrate_apps = pre_migrate_state.apps
|
2016-05-13 23:58:54 +08:00
|
|
|
emit_pre_migrate_signal(
|
2021-01-29 23:19:06 +08:00
|
|
|
self.verbosity,
|
|
|
|
self.interactive,
|
|
|
|
connection.alias,
|
|
|
|
stdout=self.stdout,
|
|
|
|
apps=pre_migrate_apps,
|
|
|
|
plan=plan,
|
2016-05-13 23:58:54 +08:00
|
|
|
)
|
2014-12-27 00:19:43 +08:00
|
|
|
|
2013-06-08 01:47:17 +08:00
|
|
|
# Run the syncdb phase.
|
2014-12-27 02:56:08 +08:00
|
|
|
if run_syncdb:
|
2013-07-25 20:52:35 +08:00
|
|
|
if self.verbosity >= 1:
|
|
|
|
self.stdout.write(
|
|
|
|
self.style.MIGRATE_HEADING("Synchronizing apps without migrations:")
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2018-07-16 11:32:47 +08:00
|
|
|
if options["app_label"]:
|
|
|
|
self.sync_apps(connection, [app_label])
|
|
|
|
else:
|
|
|
|
self.sync_apps(connection, executor.loader.unmigrated_apps)
|
2013-06-08 01:47:17 +08:00
|
|
|
|
|
|
|
# Migrate!
|
|
|
|
if self.verbosity >= 1:
|
|
|
|
self.stdout.write(self.style.MIGRATE_HEADING("Running migrations:"))
|
|
|
|
if not plan:
|
|
|
|
if self.verbosity >= 1:
|
2014-06-20 14:40:59 +08:00
|
|
|
self.stdout.write(" No migrations to apply.")
|
2013-12-04 21:32:46 +08:00
|
|
|
# If there's changes that aren't in migrations yet, tell them
|
|
|
|
# how to fix it.
|
|
|
|
autodetector = MigrationAutodetector(
|
2014-05-01 03:25:12 +08:00
|
|
|
executor.loader.project_state(),
|
2013-12-24 19:25:17 +08:00
|
|
|
ProjectState.from_apps(apps),
|
2013-12-04 21:32:46 +08:00
|
|
|
)
|
|
|
|
changes = autodetector.changes(graph=executor.loader.graph)
|
|
|
|
if changes:
|
2014-09-04 20:15:09 +08:00
|
|
|
self.stdout.write(
|
|
|
|
self.style.NOTICE(
|
2020-06-06 00:02:21 +08:00
|
|
|
" Your models in app(s): %s have changes that are not "
|
|
|
|
"yet reflected in a migration, and so won't be "
|
|
|
|
"applied." % ", ".join(repr(app) for app in sorted(changes))
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2014-09-04 20:15:09 +08:00
|
|
|
)
|
|
|
|
self.stdout.write(
|
|
|
|
self.style.NOTICE(
|
|
|
|
" Run 'manage.py makemigrations' to make new "
|
|
|
|
"migrations, and then re-run 'manage.py migrate' to "
|
|
|
|
"apply them."
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2014-09-04 20:15:09 +08:00
|
|
|
)
|
2016-08-21 04:34:06 +08:00
|
|
|
fake = False
|
|
|
|
fake_initial = False
|
2013-06-08 01:47:17 +08:00
|
|
|
else:
|
2016-03-03 10:01:36 +08:00
|
|
|
fake = options["fake"]
|
|
|
|
fake_initial = options["fake_initial"]
|
2016-08-21 06:05:04 +08:00
|
|
|
post_migrate_state = executor.migrate(
|
|
|
|
targets,
|
|
|
|
plan=plan,
|
|
|
|
state=pre_migrate_state.clone(),
|
|
|
|
fake=fake,
|
|
|
|
fake_initial=fake_initial,
|
2016-08-21 04:34:06 +08:00
|
|
|
)
|
2016-11-06 19:03:05 +08:00
|
|
|
# post_migrate signals have access to all models. Ensure that all models
|
|
|
|
# are reloaded in case any are delayed.
|
|
|
|
post_migrate_state.clear_delayed_apps_cache()
|
2016-08-21 06:05:04 +08:00
|
|
|
post_migrate_apps = post_migrate_state.apps
|
2016-05-13 23:58:54 +08:00
|
|
|
|
|
|
|
# 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
|
|
|
|
model_keys.append(model_key)
|
|
|
|
post_migrate_apps.unregister_model(*model_key)
|
|
|
|
post_migrate_apps.render_multiple(
|
2016-06-16 21:07:51 +08:00
|
|
|
[ModelState.from_model(apps.get_model(*model)) for model in model_keys]
|
2016-05-13 23:58:54 +08:00
|
|
|
)
|
2013-06-08 01:47:17 +08:00
|
|
|
|
2013-07-30 18:52:52 +08:00
|
|
|
# Send the post_migrate signal, so individual apps can do whatever they need
|
|
|
|
# to do at this point.
|
2016-05-13 23:58:54 +08:00
|
|
|
emit_post_migrate_signal(
|
2021-01-29 23:19:06 +08:00
|
|
|
self.verbosity,
|
|
|
|
self.interactive,
|
|
|
|
connection.alias,
|
|
|
|
stdout=self.stdout,
|
|
|
|
apps=post_migrate_apps,
|
|
|
|
plan=plan,
|
2016-05-13 23:58:54 +08:00
|
|
|
)
|
2013-07-30 18:52:52 +08:00
|
|
|
|
2015-01-12 04:07:45 +08:00
|
|
|
def migration_progress_callback(self, action, migration=None, fake=False):
|
2013-06-08 01:47:17 +08:00
|
|
|
if self.verbosity >= 1:
|
2014-11-05 18:01:48 +08:00
|
|
|
compute_time = self.verbosity > 1
|
2013-06-08 01:47:17 +08:00
|
|
|
if action == "apply_start":
|
2014-11-05 18:01:48 +08:00
|
|
|
if compute_time:
|
2019-05-09 00:34:22 +08:00
|
|
|
self.start = time.monotonic()
|
2019-02-14 02:59:44 +08:00
|
|
|
self.stdout.write(" Applying %s..." % migration, ending="")
|
2013-06-08 01:47:17 +08:00
|
|
|
self.stdout.flush()
|
|
|
|
elif action == "apply_success":
|
2019-05-09 00:34:22 +08:00
|
|
|
elapsed = (
|
|
|
|
" (%.3fs)" % (time.monotonic() - self.start) if compute_time else ""
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2013-10-30 23:17:28 +08:00
|
|
|
if fake:
|
2015-11-18 07:33:03 +08:00
|
|
|
self.stdout.write(self.style.SUCCESS(" FAKED" + elapsed))
|
2013-10-30 23:17:28 +08:00
|
|
|
else:
|
2015-11-18 07:33:03 +08:00
|
|
|
self.stdout.write(self.style.SUCCESS(" OK" + elapsed))
|
2013-06-08 01:47:17 +08:00
|
|
|
elif action == "unapply_start":
|
2014-11-05 18:01:48 +08:00
|
|
|
if compute_time:
|
2019-05-09 00:34:22 +08:00
|
|
|
self.start = time.monotonic()
|
2019-02-14 02:59:44 +08:00
|
|
|
self.stdout.write(" Unapplying %s..." % migration, ending="")
|
2013-06-08 01:47:17 +08:00
|
|
|
self.stdout.flush()
|
|
|
|
elif action == "unapply_success":
|
2019-05-09 00:34:22 +08:00
|
|
|
elapsed = (
|
|
|
|
" (%.3fs)" % (time.monotonic() - self.start) if compute_time else ""
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2013-10-30 23:17:28 +08:00
|
|
|
if fake:
|
2015-11-18 07:33:03 +08:00
|
|
|
self.stdout.write(self.style.SUCCESS(" FAKED" + elapsed))
|
2013-10-30 23:17:28 +08:00
|
|
|
else:
|
2015-11-18 07:33:03 +08:00
|
|
|
self.stdout.write(self.style.SUCCESS(" OK" + elapsed))
|
2015-01-12 04:07:45 +08:00
|
|
|
elif action == "render_start":
|
|
|
|
if compute_time:
|
2019-05-09 00:34:22 +08:00
|
|
|
self.start = time.monotonic()
|
2019-02-14 02:59:44 +08:00
|
|
|
self.stdout.write(" Rendering model states...", ending="")
|
2015-01-12 04:07:45 +08:00
|
|
|
self.stdout.flush()
|
|
|
|
elif action == "render_success":
|
2019-05-09 00:34:22 +08:00
|
|
|
elapsed = (
|
|
|
|
" (%.3fs)" % (time.monotonic() - self.start) if compute_time else ""
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2015-11-18 07:33:03 +08:00
|
|
|
self.stdout.write(self.style.SUCCESS(" DONE" + elapsed))
|
2013-06-08 01:47:17 +08:00
|
|
|
|
2013-12-24 19:25:17 +08:00
|
|
|
def sync_apps(self, connection, app_labels):
|
2016-12-24 11:34:52 +08:00
|
|
|
"""Run the old syncdb-style operation on a list of app_labels."""
|
|
|
|
with connection.cursor() as cursor:
|
2014-01-09 23:05:15 +08:00
|
|
|
tables = connection.introspection.table_names(cursor)
|
2013-06-08 01:47:17 +08:00
|
|
|
|
2016-12-24 11:34:52 +08:00
|
|
|
# Build the manifest of apps and models that are to be synchronized.
|
|
|
|
all_models = [
|
|
|
|
(
|
|
|
|
app_config.label,
|
|
|
|
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
|
|
|
|
]
|
2013-06-08 01:47:17 +08:00
|
|
|
|
2016-12-24 11:34:52 +08:00
|
|
|
def model_installed(model):
|
|
|
|
opts = model._meta
|
2018-11-21 16:06:50 +08:00
|
|
|
converter = connection.introspection.identifier_converter
|
2016-12-24 11:34:52 +08:00
|
|
|
return not (
|
|
|
|
(converter(opts.db_table) in tables)
|
|
|
|
or (
|
|
|
|
opts.auto_created
|
|
|
|
and converter(opts.auto_created._meta.db_table) in tables
|
|
|
|
)
|
2014-01-09 23:05:15 +08:00
|
|
|
)
|
2013-06-08 01:47:17 +08:00
|
|
|
|
2019-02-05 19:22:08 +08:00
|
|
|
manifest = {
|
|
|
|
app_name: list(filter(model_installed, model_list))
|
2016-12-24 11:34:52 +08:00
|
|
|
for app_name, model_list in all_models
|
2019-02-05 19:22:08 +08:00
|
|
|
}
|
2014-09-15 02:26:16 +08:00
|
|
|
|
2016-12-24 11:34:52 +08:00
|
|
|
# Create the tables for each model
|
|
|
|
if self.verbosity >= 1:
|
2020-04-25 21:51:17 +08:00
|
|
|
self.stdout.write(" Creating tables...")
|
2016-12-24 11:34:52 +08:00
|
|
|
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):
|
|
|
|
continue
|
|
|
|
if self.verbosity >= 3:
|
|
|
|
self.stdout.write(
|
2020-04-25 21:51:17 +08:00
|
|
|
" Processing %s.%s model"
|
|
|
|
% (app_name, model._meta.object_name)
|
2016-12-24 11:34:52 +08:00
|
|
|
)
|
|
|
|
if self.verbosity >= 1:
|
2020-04-25 21:51:17 +08:00
|
|
|
self.stdout.write(
|
|
|
|
" Creating table %s" % model._meta.db_table
|
|
|
|
)
|
2016-12-24 11:34:52 +08:00
|
|
|
editor.create_model(model)
|
2013-06-08 01:47:17 +08:00
|
|
|
|
2016-12-24 11:34:52 +08:00
|
|
|
# Deferred SQL is executed when exiting the editor's context.
|
|
|
|
if self.verbosity >= 1:
|
2020-04-25 21:51:17 +08:00
|
|
|
self.stdout.write(" Running deferred SQL...")
|
2018-05-19 23:38:02 +08:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def describe_operation(operation, backwards):
|
|
|
|
"""Return a string that describes a migration operation for --plan."""
|
|
|
|
prefix = ""
|
2019-10-14 15:37:45 +08:00
|
|
|
is_error = False
|
2018-05-19 23:38:02 +08:00
|
|
|
if hasattr(operation, "code"):
|
|
|
|
code = operation.reverse_code if backwards else operation.code
|
2019-10-14 15:37:45 +08:00
|
|
|
action = (code.__doc__ or "") if code else None
|
2018-05-19 23:38:02 +08:00
|
|
|
elif hasattr(operation, "sql"):
|
|
|
|
action = operation.reverse_sql if backwards else operation.sql
|
|
|
|
else:
|
|
|
|
action = ""
|
|
|
|
if backwards:
|
|
|
|
prefix = "Undo "
|
2019-10-14 15:37:45 +08:00
|
|
|
if action is not None:
|
|
|
|
action = str(action).replace("\n", "")
|
|
|
|
elif backwards:
|
2018-05-19 23:38:02 +08:00
|
|
|
action = "IRREVERSIBLE"
|
|
|
|
is_error = True
|
|
|
|
if action:
|
|
|
|
action = " -> " + action
|
|
|
|
truncated = Truncator(action)
|
|
|
|
return prefix + operation.describe() + truncated.chars(40), is_error
|