Add Migration class to hold the path, app name and migration name in one object + adapt existing logic
This commit is contained in:
1 parent
f0021dc139
commit
c6df7e53a0
3 files changed
+17
-11
No files matched your search
@@ -16,7 +16,7 @@ import hashlib
|
|||||||
import os
|
import os
|
||||||
import pickle
|
import pickle
|
||||||
|
|
||||||
from django_migration_linter.utils import split_path, compose_migration_path
|
from django_migration_linter.utils import split_path
|
||||||
|
|
||||||
|
|
||||||
class Cache(dict):
|
class Cache(dict):
|
||||||
@@ -42,8 +42,7 @@ class Cache(dict):
|
|||||||
with open(self.filename, "wb") as f:
|
with open(self.filename, "wb") as f:
|
||||||
pickle.dump(self, f, protocol=2)
|
pickle.dump(self, f, protocol=2)
|
||||||
|
|
||||||
def md5(self, app_name, migration):
|
def md5(self, path):
|
||||||
path = compose_migration_path(self.django_folder, app_name, migration)
|
|
||||||
hash_md5 = hashlib.md5()
|
hash_md5 = hashlib.md5()
|
||||||
with open(path, "rb") as f:
|
with open(path, "rb") as f:
|
||||||
for chunk in iter(lambda: f.read(4096), b""):
|
for chunk in iter(lambda: f.read(4096), b""):
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
from django_migration_linter.utils import split_migration_path
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(object):
|
||||||
|
def __init__(self, abs_path):
|
||||||
|
self.abs_path = abs_path
|
||||||
|
self.app_name, self.name = split_migration_path(self.abs_path)
|
||||||
@@ -21,8 +21,8 @@ import sys
|
|||||||
|
|
||||||
from django_migration_linter.cache import Cache
|
from django_migration_linter.cache import Cache
|
||||||
from django_migration_linter.constants import DEFAULT_CACHE_PATH, MIGRATION_FOLDER_NAME
|
from django_migration_linter.constants import DEFAULT_CACHE_PATH, MIGRATION_FOLDER_NAME
|
||||||
from django_migration_linter.utils import split_migration_path
|
|
||||||
from . import utils
|
from . import utils
|
||||||
|
from .migration import Migration
|
||||||
from .sql_analyser import analyse_sql_statements
|
from .sql_analyser import analyse_sql_statements
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
@@ -72,11 +72,13 @@ class MigrationLinter(object):
|
|||||||
if not self.no_cache:
|
if not self.no_cache:
|
||||||
self.old_cache.load()
|
self.old_cache.load()
|
||||||
|
|
||||||
def lint_migration(self, app_name, migration_name):
|
def lint_migration(self, migration):
|
||||||
|
app_name = migration.app_name
|
||||||
|
migration_name = migration.name
|
||||||
print("({0}, {1})... ".format(app_name, migration_name), end="")
|
print("({0}, {1})... ".format(app_name, migration_name), end="")
|
||||||
self.nb_total += 1
|
self.nb_total += 1
|
||||||
|
|
||||||
md5hash = self.old_cache.md5(app_name, migration_name)
|
md5hash = self.old_cache.md5(migration.abs_path)
|
||||||
if md5hash in self.old_cache:
|
if md5hash in self.old_cache:
|
||||||
if self.old_cache[md5hash]["result"] == "IGNORE":
|
if self.old_cache[md5hash]["result"] == "IGNORE":
|
||||||
print("IGNORE (cached)")
|
print("IGNORE (cached)")
|
||||||
@@ -146,7 +148,7 @@ class MigrationLinter(object):
|
|||||||
|
|
||||||
# Lint those migrations
|
# Lint those migrations
|
||||||
for m in migrations:
|
for m in migrations:
|
||||||
self.lint_migration(*m)
|
self.lint_migration(m)
|
||||||
|
|
||||||
if not self.no_cache:
|
if not self.no_cache:
|
||||||
self.new_cache.save()
|
self.new_cache.save()
|
||||||
@@ -215,8 +217,7 @@ class MigrationLinter(object):
|
|||||||
re.search(r"\/{0}\/.*\.py".format(MIGRATION_FOLDER_NAME), line)
|
re.search(r"\/{0}\/.*\.py".format(MIGRATION_FOLDER_NAME), line)
|
||||||
and "__init__" not in line
|
and "__init__" not in line
|
||||||
):
|
):
|
||||||
app_name, migration_name = split_migration_path(line)
|
migrations.append(Migration(line))
|
||||||
migrations.append((app_name, migration_name))
|
|
||||||
diff_process.wait()
|
diff_process.wait()
|
||||||
|
|
||||||
if diff_process.returncode != 0:
|
if diff_process.returncode != 0:
|
||||||
@@ -237,8 +238,7 @@ class MigrationLinter(object):
|
|||||||
and file_name != "__init__.py"
|
and file_name != "__init__.py"
|
||||||
):
|
):
|
||||||
full_migration_path = os.path.join(root, file_name)
|
full_migration_path = os.path.join(root, file_name)
|
||||||
app_name, migration_name = split_migration_path(full_migration_path)
|
migrations.append(Migration(full_migration_path))
|
||||||
migrations.append((app_name, migration_name))
|
|
||||||
return migrations
|
return migrations
|
||||||
|
|
||||||
def should_ignore_migration(self, app_name, migration_name):
|
def should_ignore_migration(self, app_name, migration_name):
|
||||||
|
|||||||
Reference in new issue
Block a user