Refs #31318 -- Optimized sqlmigrate by using MigrationLoader.

Only loader from MigrationExecutor was used.
This commit is contained in:
David Wobrock 2020-03-09 08:30:05 +01:00 committed by Mariusz Felisiak
parent 71c1b7fb34
commit 271e108b29
1 changed files with 7 additions and 8 deletions

View File

@ -1,8 +1,7 @@
from django.apps import apps
from django.core.management.base import BaseCommand, CommandError
from django.db import DEFAULT_DB_ALIAS, connections
from django.db.migrations.executor import MigrationExecutor
from django.db.migrations.loader import AmbiguityError
from django.db.migrations.loader import AmbiguityError, MigrationLoader
class Command(BaseCommand):
@ -33,8 +32,8 @@ class Command(BaseCommand):
# Get the database we're operating from
connection = connections[options['database']]
# Load up an executor to get all the migration data
executor = MigrationExecutor(connection)
# Load up an loader to get all the migration data
loader = MigrationLoader(connection)
# Resolve command-line arguments into a migration
app_label, migration_name = options['app_label'], options['migration_name']
@ -43,10 +42,10 @@ class Command(BaseCommand):
apps.get_app_config(app_label)
except LookupError as err:
raise CommandError(str(err))
if app_label not in executor.loader.migrated_apps:
if app_label not in loader.migrated_apps:
raise CommandError("App '%s' does not have migrations" % app_label)
try:
migration = executor.loader.get_migration_by_prefix(app_label, migration_name)
migration = 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))
@ -61,8 +60,8 @@ class Command(BaseCommand):
# Make a plan that represents just the requested migrations and show SQL
# for it
plan = [(executor.loader.graph.nodes[target], options['backwards'])]
sql_statements = executor.loader.collect_sql(plan)
plan = [(loader.graph.nodes[target], options['backwards'])]
sql_statements = loader.collect_sql(plan)
if not sql_statements and options['verbosity'] >= 1:
self.stderr.write('No operations found.')
return '\n'.join(sql_statements)