Add command to only check migration files in a given file
This commit is contained in:
1 parent
14cafd017d
commit
adf61dd669
2 files changed
+33
-2
No files matched your search
@@ -25,6 +25,15 @@ class Command(BaseCommand):
|
|||||||
"the initial repo commit will be used"
|
"the initial repo commit will be used"
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--include-migrations-in-file",
|
||||||
|
metavar="FILE_PATH",
|
||||||
|
type=str,
|
||||||
|
nargs="?",
|
||||||
|
help=(
|
||||||
|
"if specified, only migrations inside the file " "will be considered",
|
||||||
|
),
|
||||||
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"--ignore-name-contains",
|
"--ignore-name-contains",
|
||||||
type=str,
|
type=str,
|
||||||
@@ -122,7 +131,10 @@ class Command(BaseCommand):
|
|||||||
only_unapplied_migrations=options["unapplied_migrations"],
|
only_unapplied_migrations=options["unapplied_migrations"],
|
||||||
exclude_migration_tests=options["exclude_migration_tests"],
|
exclude_migration_tests=options["exclude_migration_tests"],
|
||||||
)
|
)
|
||||||
linter.lint_all_migrations(git_commit_id=options["commit_id"])
|
linter.lint_all_migrations(
|
||||||
|
git_commit_id=options["commit_id"],
|
||||||
|
migrations_file_path=options["include_migrations_in_file"],
|
||||||
|
)
|
||||||
linter.print_summary()
|
linter.print_summary()
|
||||||
if linter.has_errors:
|
if linter.has_errors:
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|||||||
@@ -85,10 +85,12 @@ 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):
|
def lint_all_migrations(self, git_commit_id=None, migrations_file_path=None):
|
||||||
# Collect migrations
|
# Collect migrations
|
||||||
if git_commit_id:
|
if git_commit_id:
|
||||||
migrations = self._gather_migrations_git(git_commit_id)
|
migrations = self._gather_migrations_git(git_commit_id)
|
||||||
|
elif migrations_file_path:
|
||||||
|
migrations = self._gather_migrations_from_file(migrations_file_path)
|
||||||
else:
|
else:
|
||||||
migrations = self._gather_all_migrations()
|
migrations = self._gather_all_migrations()
|
||||||
|
|
||||||
@@ -248,6 +250,23 @@ class MigrationLinter(object):
|
|||||||
raise Exception("Error while executing git diff command")
|
raise Exception("Error while executing git diff command")
|
||||||
return migrations
|
return migrations
|
||||||
|
|
||||||
|
def _gather_migrations_from_file(self, migrations_file_path):
|
||||||
|
from django.db.migrations.loader import MIGRATIONS_MODULE_NAME
|
||||||
|
|
||||||
|
migrations = []
|
||||||
|
|
||||||
|
with open(migrations_file_path, "r") as f:
|
||||||
|
lines = f.read()
|
||||||
|
for line in lines:
|
||||||
|
if (
|
||||||
|
re.search(r"/{0}/.*\.py".format(MIGRATIONS_MODULE_NAME), line)
|
||||||
|
and "__init__" not in line
|
||||||
|
):
|
||||||
|
app_label, name = split_migration_path(line)
|
||||||
|
migration = self.migration_loader.disk_migrations[app_label, name]
|
||||||
|
migrations.append(migration)
|
||||||
|
return migrations
|
||||||
|
|
||||||
def _gather_all_migrations(self):
|
def _gather_all_migrations(self):
|
||||||
# Prune Django apps
|
# Prune Django apps
|
||||||
for (app_label, _), migration in self.migration_loader.disk_migrations.items():
|
for (app_label, _), migration in self.migration_loader.disk_migrations.items():
|
||||||
|
|||||||
Reference in new issue
Block a user