Add command to only check migration files in a given file

This commit is contained in:
yongming authored and David Wobrock committed 2019-10-10 22:50:17 +02:00
1 parent 14cafd017d
commit adf61dd669
2 files changed
+33 -2

No files matched your search

+20 -1
View File
@@ -85,10 +85,12 @@ 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):
def lint_all_migrations(self, git_commit_id=None, migrations_file_path=None):
# Collect migrations
if 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:
migrations = self._gather_all_migrations()
@@ -248,6 +250,23 @@ class MigrationLinter(object):
raise Exception("Error while executing git diff command")
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):
# Prune Django apps
for (app_label, _), migration in self.migration_loader.disk_migrations.items():