From 34a6d9d47e9f6e9b4a3a9bb337ac4cf1ba4de20b Mon Sep 17 00:00:00 2001 From: David Wobrock Date: Fri, 26 Jun 2020 20:13:30 +0200 Subject: [PATCH] Make makemigrations override less strict by suggesting to keep the migration --- .../management/commands/makemigrations.py | 58 ++++++++++++++----- 1 file changed, 44 insertions(+), 14 deletions(-) diff --git a/django_migration_linter/management/commands/makemigrations.py b/django_migration_linter/management/commands/makemigrations.py index 16f9f82..6336b2f 100644 --- a/django_migration_linter/management/commands/makemigrations.py +++ b/django_migration_linter/management/commands/makemigrations.py @@ -4,6 +4,7 @@ from django.conf import settings from django.core.management.commands.makemigrations import ( Command as MakeMigrationsCommand, ) +from django.db.migrations.questioner import InteractiveMigrationQuestioner from django.db.migrations.writer import MigrationWriter from django_migration_linter import MigrationLinter @@ -11,6 +12,19 @@ from django_migration_linter import MigrationLinter from ..utils import register_linting_configuration_options +def ask_should_keep_migration(): + questioner = InteractiveMigrationQuestioner() + msg = """\nThe migration linter detected that this migration is not be backward compatible. +- If you keep the migration, you will want to fix the issue or ignore the migration. +- By default, the newly created migration file will be deleted. +Do you want to keep the migration? [y/N]""" + return questioner._boolean_input(msg, False) + + +def default_should_keep_migration(): + return False + + class Command(MakeMigrationsCommand): help = "Creates new migration(s) for apps and lints them." @@ -36,9 +50,21 @@ class Command(MakeMigrationsCommand): and not self.lint ): return + if self.dry_run: + """ + Since we rely on the 'sqlmigrate' to lint the migrations, we can only + lint if the migration files have been generated. Since the 'dry-run' + option won't generate the files, we cannot lint migrations. + """ return + should_keep_migration = ( + ask_should_keep_migration + if self.interactive + else default_should_keep_migration + ) + # Lint migrations linter = MigrationLinter( path=os.environ["DJANGO_SETTINGS_MODULE"], @@ -53,21 +79,25 @@ class Command(MakeMigrationsCommand): self.stdout.write( self.style.MIGRATE_HEADING("Linting for '%s':" % app_label) + "\n" ) + for migration in app_migrations: linter.lint_migration(migration) if linter.has_errors: - writer = MigrationWriter(migration) - os.remove(writer.path) - - if self.verbosity >= 1: - try: - migration_string = os.path.relpath(writer.path) - except ValueError: - migration_string = writer.path - if migration_string.startswith(".."): - migration_string = writer.path - self.stdout.write( - "Deleted %s\n" - % (self.style.MIGRATE_LABEL(migration_string)) - ) + if not should_keep_migration(): + self.delete_migration(migration) linter.reset_counters() + + def delete_migration(self, migration): + writer = MigrationWriter(migration) + os.remove(writer.path) + + if self.verbosity >= 1: + try: + migration_string = os.path.relpath(writer.path) + except ValueError: + migration_string = writer.path + if migration_string.startswith(".."): + migration_string = writer.path + self.stdout.write( + "Deleted %s\n" % (self.style.MIGRATE_LABEL(migration_string)) + )