Make makemigrations override less strict by suggesting to keep the migration
This commit is contained in:
1 parent
0d8bc98f8e
commit
34a6d9d47e
1 file changed
+33
-3
@@ -4,6 +4,7 @@ from django.conf import settings
|
|||||||
from django.core.management.commands.makemigrations import (
|
from django.core.management.commands.makemigrations import (
|
||||||
Command as MakeMigrationsCommand,
|
Command as MakeMigrationsCommand,
|
||||||
)
|
)
|
||||||
|
from django.db.migrations.questioner import InteractiveMigrationQuestioner
|
||||||
from django.db.migrations.writer import MigrationWriter
|
from django.db.migrations.writer import MigrationWriter
|
||||||
|
|
||||||
from django_migration_linter import MigrationLinter
|
from django_migration_linter import MigrationLinter
|
||||||
@@ -11,6 +12,19 @@ from django_migration_linter import MigrationLinter
|
|||||||
from ..utils import register_linting_configuration_options
|
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):
|
class Command(MakeMigrationsCommand):
|
||||||
help = "Creates new migration(s) for apps and lints them."
|
help = "Creates new migration(s) for apps and lints them."
|
||||||
|
|
||||||
@@ -36,9 +50,21 @@ class Command(MakeMigrationsCommand):
|
|||||||
and not self.lint
|
and not self.lint
|
||||||
):
|
):
|
||||||
return
|
return
|
||||||
|
|
||||||
if self.dry_run:
|
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
|
return
|
||||||
|
|
||||||
|
should_keep_migration = (
|
||||||
|
ask_should_keep_migration
|
||||||
|
if self.interactive
|
||||||
|
else default_should_keep_migration
|
||||||
|
)
|
||||||
|
|
||||||
# Lint migrations
|
# Lint migrations
|
||||||
linter = MigrationLinter(
|
linter = MigrationLinter(
|
||||||
path=os.environ["DJANGO_SETTINGS_MODULE"],
|
path=os.environ["DJANGO_SETTINGS_MODULE"],
|
||||||
@@ -53,9 +79,15 @@ class Command(MakeMigrationsCommand):
|
|||||||
self.stdout.write(
|
self.stdout.write(
|
||||||
self.style.MIGRATE_HEADING("Linting for '%s':" % app_label) + "\n"
|
self.style.MIGRATE_HEADING("Linting for '%s':" % app_label) + "\n"
|
||||||
)
|
)
|
||||||
|
|
||||||
for migration in app_migrations:
|
for migration in app_migrations:
|
||||||
linter.lint_migration(migration)
|
linter.lint_migration(migration)
|
||||||
if linter.has_errors:
|
if linter.has_errors:
|
||||||
|
if not should_keep_migration():
|
||||||
|
self.delete_migration(migration)
|
||||||
|
linter.reset_counters()
|
||||||
|
|
||||||
|
def delete_migration(self, migration):
|
||||||
writer = MigrationWriter(migration)
|
writer = MigrationWriter(migration)
|
||||||
os.remove(writer.path)
|
os.remove(writer.path)
|
||||||
|
|
||||||
@@ -67,7 +99,5 @@ class Command(MakeMigrationsCommand):
|
|||||||
if migration_string.startswith(".."):
|
if migration_string.startswith(".."):
|
||||||
migration_string = writer.path
|
migration_string = writer.path
|
||||||
self.stdout.write(
|
self.stdout.write(
|
||||||
"Deleted %s\n"
|
"Deleted %s\n" % (self.style.MIGRATE_LABEL(migration_string))
|
||||||
% (self.style.MIGRATE_LABEL(migration_string))
|
|
||||||
)
|
)
|
||||||
linter.reset_counters()
|
|
||||||
Reference in new issue
Block a user