Refs #29026 -- Added log() to makemigrations.

This commit is contained in:
Jacob Walls 2021-08-23 22:14:38 -04:00 committed by Mariusz Felisiak
parent cd6bddd44e
commit f153e9214f
1 changed files with 22 additions and 19 deletions

View File

@ -58,6 +58,9 @@ class Command(BaseCommand):
help='Exit with a non-zero status if model changes are missing migrations.', help='Exit with a non-zero status if model changes are missing migrations.',
) )
def log(self, msg):
self.stdout.write(msg)
@no_translations @no_translations
def handle(self, *app_labels, **options): def handle(self, *app_labels, **options):
self.verbosity = options['verbosity'] self.verbosity = options['verbosity']
@ -132,7 +135,7 @@ class Command(BaseCommand):
# If they want to merge and there's nothing to merge, then politely exit # If they want to merge and there's nothing to merge, then politely exit
if self.merge and not conflicts: if self.merge and not conflicts:
self.stdout.write("No conflicts detected to merge.") self.log('No conflicts detected to merge.')
return return
# If they want to merge and there is something to merge, then # If they want to merge and there is something to merge, then
@ -181,11 +184,11 @@ class Command(BaseCommand):
if self.verbosity >= 1: if self.verbosity >= 1:
if app_labels: if app_labels:
if len(app_labels) == 1: if len(app_labels) == 1:
self.stdout.write("No changes detected in app '%s'" % app_labels.pop()) self.log("No changes detected in app '%s'" % app_labels.pop())
else: else:
self.stdout.write("No changes detected in apps '%s'" % ("', '".join(app_labels))) self.log("No changes detected in apps '%s'" % ("', '".join(app_labels)))
else: else:
self.stdout.write("No changes detected") self.log('No changes detected')
else: else:
self.write_migration_files(changes) self.write_migration_files(changes)
if check_changes: if check_changes:
@ -198,7 +201,7 @@ class Command(BaseCommand):
directory_created = {} directory_created = {}
for app_label, app_migrations in changes.items(): for app_label, app_migrations in changes.items():
if self.verbosity >= 1: if self.verbosity >= 1:
self.stdout.write(self.style.MIGRATE_HEADING("Migrations for '%s':" % app_label)) self.log(self.style.MIGRATE_HEADING("Migrations for '%s':" % app_label))
for migration in app_migrations: for migration in app_migrations:
# Describe the migration # Describe the migration
writer = MigrationWriter(migration, self.include_header) writer = MigrationWriter(migration, self.include_header)
@ -211,9 +214,9 @@ class Command(BaseCommand):
migration_string = writer.path migration_string = writer.path
if migration_string.startswith('..'): if migration_string.startswith('..'):
migration_string = writer.path migration_string = writer.path
self.stdout.write(' %s\n' % self.style.MIGRATE_LABEL(migration_string)) self.log(' %s\n' % self.style.MIGRATE_LABEL(migration_string))
for operation in migration.operations: for operation in migration.operations:
self.stdout.write(' - %s' % operation.describe()) self.log(' - %s' % operation.describe())
if not self.dry_run: if not self.dry_run:
# Write the migrations file to the disk. # Write the migrations file to the disk.
migrations_directory = os.path.dirname(writer.path) migrations_directory = os.path.dirname(writer.path)
@ -229,12 +232,12 @@ class Command(BaseCommand):
fh.write(migration_string) fh.write(migration_string)
elif self.verbosity == 3: elif self.verbosity == 3:
# Alternatively, makemigrations --dry-run --verbosity 3 # Alternatively, makemigrations --dry-run --verbosity 3
# will output the migrations to stdout rather than saving # will log the migrations rather than saving the file to
# the file to the disk. # the disk.
self.stdout.write(self.style.MIGRATE_HEADING( self.log(self.style.MIGRATE_HEADING(
"Full migrations file '%s':" % writer.filename "Full migrations file '%s':" % writer.filename
)) ))
self.stdout.write(writer.as_string()) self.log(writer.as_string())
def handle_merge(self, loader, conflicts): def handle_merge(self, loader, conflicts):
""" """
@ -276,11 +279,11 @@ class Command(BaseCommand):
# (can_optimize_through) to automatically see if they're # (can_optimize_through) to automatically see if they're
# mergeable. For now, we always just prompt the user. # mergeable. For now, we always just prompt the user.
if self.verbosity > 0: if self.verbosity > 0:
self.stdout.write(self.style.MIGRATE_HEADING("Merging %s" % app_label)) self.log(self.style.MIGRATE_HEADING('Merging %s' % app_label))
for migration in merge_migrations: for migration in merge_migrations:
self.stdout.write(self.style.MIGRATE_LABEL(" Branch %s" % migration.name)) self.log(self.style.MIGRATE_LABEL(' Branch %s' % migration.name))
for operation in migration.merged_operations: for operation in migration.merged_operations:
self.stdout.write(' - %s' % operation.describe()) self.log(' - %s' % operation.describe())
if questioner.ask_merge(app_label): if questioner.ask_merge(app_label):
# If they still want to merge it, then write out an empty # If they still want to merge it, then write out an empty
# file depending on the migrations needing merging. # file depending on the migrations needing merging.
@ -314,12 +317,12 @@ class Command(BaseCommand):
with open(writer.path, "w", encoding='utf-8') as fh: with open(writer.path, "w", encoding='utf-8') as fh:
fh.write(writer.as_string()) fh.write(writer.as_string())
if self.verbosity > 0: if self.verbosity > 0:
self.stdout.write("\nCreated new merge migration %s" % writer.path) self.log('\nCreated new merge migration %s' % writer.path)
elif self.verbosity == 3: elif self.verbosity == 3:
# Alternatively, makemigrations --merge --dry-run --verbosity 3 # Alternatively, makemigrations --merge --dry-run --verbosity 3
# will output the merge migrations to stdout rather than saving # will log the merge migrations rather than saving the file
# the file to the disk. # to the disk.
self.stdout.write(self.style.MIGRATE_HEADING( self.log(self.style.MIGRATE_HEADING(
"Full merge migrations file '%s':" % writer.filename "Full merge migrations file '%s':" % writer.filename
)) ))
self.stdout.write(writer.as_string()) self.log(writer.as_string())