2013-09-07 04:27:51 +08:00
|
|
|
from django.core.management.base import BaseCommand, CommandError
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.db import DEFAULT_DB_ALIAS, connections
|
2013-09-07 04:27:51 +08:00
|
|
|
from django.db.migrations.executor import MigrationExecutor
|
|
|
|
from django.db.migrations.loader import AmbiguityError
|
|
|
|
|
|
|
|
|
|
|
|
class Command(BaseCommand):
|
|
|
|
help = "Prints the SQL statements for the named migration."
|
|
|
|
|
2014-08-16 23:21:14 +08:00
|
|
|
output_transaction = True
|
|
|
|
|
2014-06-07 04:39:33 +08:00
|
|
|
def add_arguments(self, parser):
|
2016-03-29 06:33:29 +08:00
|
|
|
parser.add_argument('app_label', help='App label of the application containing the migration.')
|
|
|
|
parser.add_argument('migration_name', help='Migration name to print the SQL for.')
|
|
|
|
parser.add_argument(
|
|
|
|
'--database', default=DEFAULT_DB_ALIAS,
|
|
|
|
help='Nominates a database to create SQL for. Defaults to the "default" database.',
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'--backwards', action='store_true', dest='backwards',
|
|
|
|
default=False,
|
|
|
|
help='Creates SQL to unapply the migration, rather than to apply it',
|
|
|
|
)
|
2013-09-07 04:27:51 +08:00
|
|
|
|
2014-08-16 23:21:14 +08:00
|
|
|
def execute(self, *args, **options):
|
|
|
|
# sqlmigrate doesn't support coloring its output but we need to force
|
|
|
|
# no_color=True so that the BEGIN/COMMIT statements added by
|
|
|
|
# output_transaction don't get colored either.
|
|
|
|
options['no_color'] = True
|
2017-01-21 21:13:44 +08:00
|
|
|
return super().execute(*args, **options)
|
2014-08-16 23:21:14 +08:00
|
|
|
|
2014-06-07 04:39:33 +08:00
|
|
|
def handle(self, *args, **options):
|
2013-09-07 04:27:51 +08:00
|
|
|
# Get the database we're operating from
|
2014-06-07 04:39:33 +08:00
|
|
|
connection = connections[options['database']]
|
2013-09-07 04:27:51 +08:00
|
|
|
|
|
|
|
# Load up an executor to get all the migration data
|
|
|
|
executor = MigrationExecutor(connection)
|
|
|
|
|
|
|
|
# Resolve command-line arguments into a migration
|
2014-06-07 04:39:33 +08:00
|
|
|
app_label, migration_name = options['app_label'], options['migration_name']
|
|
|
|
if app_label not in executor.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)
|
|
|
|
except AmbiguityError:
|
2014-06-20 19:52:36 +08:00
|
|
|
raise CommandError("More than one migration matches '%s' in app '%s'. Please be more specific." % (
|
|
|
|
migration_name, app_label))
|
2014-06-07 04:39:33 +08:00
|
|
|
except KeyError:
|
2014-06-20 19:52:36 +08:00
|
|
|
raise CommandError("Cannot find a migration matching '%s' from app '%s'. Is it in INSTALLED_APPS?" % (
|
|
|
|
migration_name, app_label))
|
2014-06-07 04:39:33 +08:00
|
|
|
targets = [(app_label, migration.name)]
|
2013-09-07 04:27:51 +08:00
|
|
|
|
2016-01-31 04:46:28 +08:00
|
|
|
# Show begin/end around output only for atomic migrations
|
|
|
|
self.output_transaction = migration.atomic
|
|
|
|
|
2013-09-07 04:27:51 +08:00
|
|
|
# Make a plan that represents just the requested migrations and show SQL
|
|
|
|
# for it
|
2014-06-07 04:39:33 +08:00
|
|
|
plan = [(executor.loader.graph.nodes[targets[0]], options['backwards'])]
|
2013-09-07 04:27:51 +08:00
|
|
|
sql_statements = executor.collect_sql(plan)
|
2014-08-16 23:21:14 +08:00
|
|
|
return '\n'.join(sql_statements)
|