mirror of
https://github.com/fabro-sh/fabro.git
synced 2026-09-13 23:14:17 +00:00
fabro(01KKTJ3AECG4ERKB1F3ZK3M7CS): solve (success)
Fabro-Run: 01KKTJ3AECG4ERKB1F3ZK3M7CS
Fabro-Completed: 3
Fabro-Checkpoint: aa6e3f2ee7
⚒️ Generated with [Fabro](https://fabro.sh)
This commit is contained in:
parent
7077016f27
commit
a06cabe3ed
2 changed files with 1674 additions and 0 deletions
65
django/core/management/commands/sqlmigrate.py
Normal file
65
django/core/management/commands/sqlmigrate.py
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
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
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = "Prints the SQL statements for the named migration."
|
||||
|
||||
output_transaction = True
|
||||
|
||||
def add_arguments(self, parser):
|
||||
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',
|
||||
help='Creates SQL to unapply the migration, rather than to apply it',
|
||||
)
|
||||
|
||||
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
|
||||
return super().execute(*args, **options)
|
||||
|
||||
def handle(self, *args, **options):
|
||||
# Get the database we're operating from
|
||||
connection = connections[options['database']]
|
||||
|
||||
# Load up an executor to get all the migration data
|
||||
executor = MigrationExecutor(connection)
|
||||
|
||||
# Resolve command-line arguments into a migration
|
||||
app_label, migration_name = options['app_label'], options['migration_name']
|
||||
# Validate app_label
|
||||
try:
|
||||
apps.get_app_config(app_label)
|
||||
except LookupError as err:
|
||||
raise CommandError(str(err))
|
||||
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:
|
||||
raise CommandError("More than one migration matches '%s' in app '%s'. Please be more specific." % (
|
||||
migration_name, app_label))
|
||||
except KeyError:
|
||||
raise CommandError("Cannot find a migration matching '%s' from app '%s'. Is it in INSTALLED_APPS?" % (
|
||||
migration_name, app_label))
|
||||
targets = [(app_label, migration.name)]
|
||||
|
||||
# Show begin/end around output only for atomic migrations
|
||||
self.output_transaction = migration.atomic and connection.features.can_rollback_ddl
|
||||
|
||||
# Make a plan that represents just the requested migrations and show SQL
|
||||
# for it
|
||||
plan = [(executor.loader.graph.nodes[targets[0]], options['backwards'])]
|
||||
sql_statements = executor.collect_sql(plan)
|
||||
return '\n'.join(sql_statements)
|
||||
1609
tests/migrations/test_commands.py
Normal file
1609
tests/migrations/test_commands.py
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Add table
Reference in a new issue