diff --git a/django/core/management/commands/sqlmigrate.py b/django/core/management/commands/sqlmigrate.py index b75e0a7438..d05e615c89 100644 --- a/django/core/management/commands/sqlmigrate.py +++ b/django/core/management/commands/sqlmigrate.py @@ -62,7 +62,7 @@ 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.collect_sql(plan) + sql_statements = executor.loader.collect_sql(plan) if not sql_statements and options['verbosity'] >= 1: self.stderr.write('No operations found.') return '\n'.join(sql_statements) diff --git a/django/db/migrations/executor.py b/django/db/migrations/executor.py index 2765ac28bd..83d624e08a 100644 --- a/django/db/migrations/executor.py +++ b/django/db/migrations/executor.py @@ -210,24 +210,6 @@ class MigrationExecutor: return state - def collect_sql(self, plan): - """ - Take a migration plan and return a list of collected SQL statements - that represent the best-efforts version of that plan. - """ - statements = [] - state = None - for migration, backwards in plan: - with self.connection.schema_editor(collect_sql=True, atomic=migration.atomic) as schema_editor: - if state is None: - state = self.loader.project_state((migration.app_label, migration.name), at_end=False) - if not backwards: - state = migration.apply(state, schema_editor, collect_sql=True) - else: - state = migration.unapply(state, schema_editor, collect_sql=True) - statements.extend(schema_editor.collected_sql) - return statements - def apply_migration(self, state, migration, fake=False, fake_initial=False): """Run a migration forwards.""" migration_recorded = False diff --git a/django/db/migrations/loader.py b/django/db/migrations/loader.py index 09a58d1e24..d41f6d3606 100644 --- a/django/db/migrations/loader.py +++ b/django/db/migrations/loader.py @@ -320,3 +320,21 @@ class MigrationLoader: See graph.make_state() for the meaning of "nodes" and "at_end". """ return self.graph.make_state(nodes=nodes, at_end=at_end, real_apps=list(self.unmigrated_apps)) + + def collect_sql(self, plan): + """ + Take a migration plan and return a list of collected SQL statements + that represent the best-efforts version of that plan. + """ + statements = [] + state = None + for migration, backwards in plan: + with self.connection.schema_editor(collect_sql=True, atomic=migration.atomic) as schema_editor: + if state is None: + state = self.project_state((migration.app_label, migration.name), at_end=False) + if not backwards: + state = migration.apply(state, schema_editor, collect_sql=True) + else: + state = migration.unapply(state, schema_editor, collect_sql=True) + statements.extend(schema_editor.collected_sql) + return statements