diff --git a/django_migration_linter/management/commands/lintmigrations.py b/django_migration_linter/management/commands/lintmigrations.py index 9826ad2..fec4fdf 100644 --- a/django_migration_linter/management/commands/lintmigrations.py +++ b/django_migration_linter/management/commands/lintmigrations.py @@ -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"], ) diff --git a/django_migration_linter/migration_linter.py b/django_migration_linter/migration_linter.py index 60861f2..3ce96ae 100644 --- a/django_migration_linter/migration_linter.py +++ b/django_migration_linter/migration_linter.py @@ -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() diff --git a/tests/functional/test_migration_linter.py b/tests/functional/test_migration_linter.py index ed95302..50abee8 100644 --- a/tests/functional/test_migration_linter.py +++ b/tests/functional/test_migration_linter.py @@ -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 ***