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
+36 -6

No files matched your search

@@ -26,6 +26,18 @@ class Command(BaseCommand):
help = "Lint your migrations" help = "Lint your migrations"
def add_arguments(self, parser): 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( parser.add_argument(
"--git-commit-id", "--git-commit-id",
type=str, type=str,
@@ -191,6 +203,8 @@ class Command(BaseCommand):
warnings_as_errors=config["warnings_as_errors"], warnings_as_errors=config["warnings_as_errors"],
) )
linter.lint_all_migrations( linter.lint_all_migrations(
app_label=options["app_label"],
migration_name=options["migration_name"],
git_commit_id=options["git_commit_id"], git_commit_id=options["git_commit_id"],
migrations_file_path=options["include_migrations_from"], migrations_file_path=options["include_migrations_from"],
) )
+21 -1
View File
@@ -101,7 +101,13 @@ class MigrationLinter(object):
def should_use_cache(self): def should_use_cache(self):
return self.django_path and not self.no_cache 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 # Collect migrations
migrations_list = self.read_migrations_list(migrations_file_path) migrations_list = self.read_migrations_list(migrations_file_path)
if git_commit_id: if git_commit_id:
@@ -113,7 +119,21 @@ class MigrationLinter(object):
sorted_migrations = sorted( sorted_migrations = sorted(
migrations, key=lambda migration: (migration.app_label, migration.name) 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: for m in sorted_migrations:
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) self.lint_migration(m)
if self.should_use_cache(): if self.should_use_cache():
+1 -5
View File
@@ -23,16 +23,12 @@ class BaseBackwardCompatibilityDetection(object):
self.assertNotEqual(linter.nb_valid + linter.nb_erroneous, 0) self.assertNotEqual(linter.nb_valid + linter.nb_erroneous, 0)
def _launch_linter(self, app=None, commit_id=None): def _launch_linter(self, app=None, commit_id=None):
if app is not None:
app = [app]
linter = MigrationLinter( linter = MigrationLinter(
self.test_project_path, self.test_project_path,
include_apps=app,
database=next(iter(self.databases)), database=next(iter(self.databases)),
no_cache=True, 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 return linter
# *** Tests *** # *** Tests ***