Add support for positional app_label and migration_name arguments

This commit is contained in:
David Wobrock committed 2021-05-26 18:53:19 +02:00
1 parent bf525ae79d
commit 6d595f1555
3 files changed
+37 -7

No files matched your search

@@ -26,6 +26,18 @@ class Command(BaseCommand):
help = "Lint your migrations"
def add_arguments(self, parser):
parser.add_argument(
"app_label",
nargs="?",
type=str,
help="App label of an application to lint migrations.",
)
parser.add_argument(
"migration_name",
nargs="?",
type=str,
help="Linting will only be done on that migration only.",
)
parser.add_argument(
"--git-commit-id",
type=str,
@@ -191,6 +203,8 @@ class Command(BaseCommand):
warnings_as_errors=config["warnings_as_errors"],
)
linter.lint_all_migrations(
app_label=options["app_label"],
migration_name=options["migration_name"],
git_commit_id=options["git_commit_id"],
migrations_file_path=options["include_migrations_from"],
)
+22 -2
View File
@@ -101,7 +101,13 @@ class MigrationLinter(object):
def should_use_cache(self):
return self.django_path and not self.no_cache
def lint_all_migrations(self, git_commit_id=None, migrations_file_path=None):
def lint_all_migrations(
self,
app_label=None,
migration_name=None,
git_commit_id=None,
migrations_file_path=None,
):
# Collect migrations
migrations_list = self.read_migrations_list(migrations_file_path)
if git_commit_id:
@@ -113,8 +119,22 @@ class MigrationLinter(object):
sorted_migrations = sorted(
migrations, key=lambda migration: (migration.app_label, migration.name)
)
specific_target_migration = (
self.migration_loader.get_migration_by_prefix(app_label, migration_name)
if app_label and migration_name
else None
)
for m in sorted_migrations:
self.lint_migration(m)
if app_label and migration_name:
if m == specific_target_migration:
self.lint_migration(m)
elif app_label:
if m.app_label == app_label:
self.lint_migration(m)
else:
self.lint_migration(m)
if self.should_use_cache():
self.new_cache.save()
+1 -5
View File
@@ -23,16 +23,12 @@ class BaseBackwardCompatibilityDetection(object):
self.assertNotEqual(linter.nb_valid + linter.nb_erroneous, 0)
def _launch_linter(self, app=None, commit_id=None):
if app is not None:
app = [app]
linter = MigrationLinter(
self.test_project_path,
include_apps=app,
database=next(iter(self.databases)),
no_cache=True,
)
linter.lint_all_migrations(git_commit_id=commit_id)
linter.lint_all_migrations(app_label=app, git_commit_id=commit_id)
return linter
# *** Tests ***