mirror of
https://github.com/django/django.git
synced 2024-12-22 17:16:24 +00:00
Refs #31318 -- Moved MigrationExecutor.collect_sql() to MigrationLoader.
collect_sql() is used only in sqlmigrate.
This commit is contained in:
parent
b88ad1d356
commit
71c1b7fb34
@ -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)
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user