Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sqlmigrate.py 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. from django.core.management.base import BaseCommand, CommandError
  2. from django.db import DEFAULT_DB_ALIAS, connections
  3. from django.db.migrations.executor import MigrationExecutor
  4. from django.db.migrations.loader import AmbiguityError
  5. class Command(BaseCommand):
  6. help = "Prints the SQL statements for the named migration."
  7. output_transaction = True
  8. def add_arguments(self, parser):
  9. parser.add_argument('app_label', help='App label of the application containing the migration.')
  10. parser.add_argument('migration_name', help='Migration name to print the SQL for.')
  11. parser.add_argument(
  12. '--database', default=DEFAULT_DB_ALIAS,
  13. help='Nominates a database to create SQL for. Defaults to the "default" database.',
  14. )
  15. parser.add_argument(
  16. '--backwards', action='store_true', dest='backwards',
  17. help='Creates SQL to unapply the migration, rather than to apply it',
  18. )
  19. def execute(self, *args, **options):
  20. # sqlmigrate doesn't support coloring its output but we need to force
  21. # no_color=True so that the BEGIN/COMMIT statements added by
  22. # output_transaction don't get colored either.
  23. options['no_color'] = True
  24. return super().execute(*args, **options)
  25. def handle(self, *args, **options):
  26. # Get the database we're operating from
  27. connection = connections[options['database']]
  28. # Load up an executor to get all the migration data
  29. executor = MigrationExecutor(connection)
  30. # Resolve command-line arguments into a migration
  31. app_label, migration_name = options['app_label'], options['migration_name']
  32. if app_label not in executor.loader.migrated_apps:
  33. raise CommandError("App '%s' does not have migrations" % app_label)
  34. try:
  35. migration = executor.loader.get_migration_by_prefix(app_label, migration_name)
  36. except AmbiguityError:
  37. raise CommandError("More than one migration matches '%s' in app '%s'. Please be more specific." % (
  38. migration_name, app_label))
  39. except KeyError:
  40. raise CommandError("Cannot find a migration matching '%s' from app '%s'. Is it in INSTALLED_APPS?" % (
  41. migration_name, app_label))
  42. targets = [(app_label, migration.name)]
  43. # Show begin/end around output only for atomic migrations
  44. self.output_transaction = migration.atomic
  45. # Make a plan that represents just the requested migrations and show SQL
  46. # for it
  47. plan = [(executor.loader.graph.nodes[targets[0]], options['backwards'])]
  48. sql_statements = executor.collect_sql(plan)
  49. return '\n'.join(sql_statements)