Skip to content

Commit

Permalink
Merge pull request #35 from 3YOURMIND/fix-cache-in-folder
Browse files Browse the repository at this point in the history
  • Loading branch information
flixx authored Jan 5, 2019
2 parents 99534e4 + 7a34ac9 commit b1716b9
Show file tree
Hide file tree
Showing 16 changed files with 97 additions and 31 deletions.
5 changes: 2 additions & 3 deletions django_migration_linter/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import os
import pickle

from django_migration_linter.utils import split_path, compose_migration_path
from django_migration_linter.utils import split_path


class Cache(dict):
Expand All @@ -42,8 +42,7 @@ def save(self):
with open(self.filename, "wb") as f:
pickle.dump(self, f, protocol=2)

def md5(self, app_name, migration):
path = compose_migration_path(self.django_folder, app_name, migration)
def md5(self, path):
hash_md5 = hashlib.md5()
with open(path, "rb") as f:
for chunk in iter(lambda: f.read(4096), b""):
Expand Down
12 changes: 12 additions & 0 deletions django_migration_linter/migration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
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)

def __lt__(self, other):
if not isinstance(other, Migration):
return NotImplemented
return self.abs_path < other.abs_path
16 changes: 8 additions & 8 deletions django_migration_linter/migration_linter.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@

from django_migration_linter.cache import Cache
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 .migration import Migration
from .sql_analyser import analyse_sql_statements

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -72,11 +72,13 @@ def __init__(self, project_path, **kwargs):
if not self.no_cache:
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="")
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 self.old_cache[md5hash]["result"] == "IGNORE":
print("IGNORE (cached)")
Expand Down Expand Up @@ -146,7 +148,7 @@ def lint_all_migrations(self, git_commit_id=None):

# Lint those migrations
for m in migrations:
self.lint_migration(*m)
self.lint_migration(m)

if not self.no_cache:
self.new_cache.save()
Expand Down Expand Up @@ -215,8 +217,7 @@ def _gather_migrations_git(self, git_commit_id):
re.search(r"\/{0}\/.*\.py".format(MIGRATION_FOLDER_NAME), line)
and "__init__" not in line
):
app_name, migration_name = split_migration_path(line)
migrations.append((app_name, migration_name))
migrations.append(Migration(os.path.join(self.django_path, line)))
diff_process.wait()

if diff_process.returncode != 0:
Expand All @@ -237,8 +238,7 @@ def _gather_all_migrations(self):
and file_name != "__init__.py"
):
full_migration_path = os.path.join(root, file_name)
app_name, migration_name = split_migration_path(full_migration_path)
migrations.append((app_name, migration_name))
migrations.append(Migration(full_migration_path))
return migrations

def should_ignore_migration(self, app_name, migration_name):
Expand Down
25 changes: 15 additions & 10 deletions tests/functional/test_cmd_line_call.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@ def test_call_linter_cmd_line_working(self):
process.wait()
self.assertEqual(process.returncode, 0)
lines = list(map(utils.clean_bytes_to_str, process.stdout.readlines()))
self.assertEqual(len(lines), 5)
self.assertEqual(len(lines), 6)
self.assertEqual(
sorted(lines[:3]),
sorted(lines[:4]),
sorted([
"(test_app1, 0001_initial)... OK",
"(test_app1, 0002_a_new_null_field)... OK",
"(test_app2, 0001_foo)... OK",
"(test_app3, 0001_initial)... OK",
])
)

Expand Down Expand Up @@ -71,13 +72,14 @@ def test_call_linter_cmd_line_exclude_apps(self):
process.wait()
self.assertEqual(process.returncode, 0)
lines = list(map(utils.clean_bytes_to_str, process.stdout.readlines()))
self.assertEqual(len(lines), 5)
self.assertEqual(len(lines), 6)
self.assertEqual(
sorted(lines[:3]),
sorted(lines[:4]),
sorted([
"(test_app1, 0001_initial)... OK",
"(test_app1, 0002_a_new_null_field)... OK",
"(test_app2, 0001_foo)... IGNORE",
"(test_app3, 0001_initial)... OK",
])
)

Expand All @@ -91,13 +93,14 @@ def test_call_linter_cmd_line_include_apps(self):
process.wait()
self.assertEqual(process.returncode, 0)
lines = list(map(utils.clean_bytes_to_str, process.stdout.readlines()))
self.assertEqual(len(lines), 5)
self.assertEqual(len(lines), 6)
self.assertEqual(
sorted(lines[:3]),
sorted(lines[:4]),
sorted([
"(test_app1, 0001_initial)... IGNORE",
"(test_app1, 0002_a_new_null_field)... IGNORE",
"(test_app2, 0001_foo)... OK",
"(test_app3, 0001_initial)... IGNORE",
])
)

Expand All @@ -111,13 +114,14 @@ def test_call_linter_cmd_line_ignore_name(self):
process.wait()
self.assertEqual(process.returncode, 0)
lines = list(map(utils.clean_bytes_to_str, process.stdout.readlines()))
self.assertEqual(len(lines), 5)
self.assertEqual(len(lines), 6)
self.assertEqual(
sorted(lines[:3]),
sorted(lines[:4]),
sorted([
"(test_app1, 0001_initial)... IGNORE",
"(test_app1, 0002_a_new_null_field)... OK",
"(test_app2, 0001_foo)... OK",
"(test_app3, 0001_initial)... IGNORE",
])
)

Expand All @@ -131,13 +135,14 @@ def test_call_linter_cmd_line_ignore_name_contains(self):
process.wait()
self.assertEqual(process.returncode, 0)
lines = list(map(utils.clean_bytes_to_str, process.stdout.readlines()))
self.assertEqual(len(lines), 5)
self.assertEqual(len(lines), 6)
self.assertEqual(
sorted(lines[:3]),
sorted(lines[:4]),
sorted([
"(test_app1, 0001_initial)... IGNORE",
"(test_app1, 0002_a_new_null_field)... OK",
"(test_app2, 0001_foo)... IGNORE",
"(test_app3, 0001_initial)... IGNORE",
])
)

Expand Down
10 changes: 6 additions & 4 deletions tests/functional/test_multiple_calls.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
# limitations under the License.

import unittest
import os

from django_migration_linter import MigrationLinter
from django_migration_linter.migration import Migration
from tests import fixtures


Expand All @@ -24,10 +26,10 @@ def test_multiple_linters(self):
l2 = MigrationLinter(fixtures.RENAME_COLUMN_PROJECT)
l3 = MigrationLinter(fixtures.CORRECT_PROJECT)

l1.lint_migration('test_app', '0002_add_new_not_null_field')
l2.lint_migration('test_app', '0002_rename_column')
l3.lint_migration('test_app1', '0001_initial')
l3.lint_migration('test_app1', '0002_a_new_null_field')
l1.lint_migration(Migration(os.path.join(fixtures.ADD_NOT_NULL_COLUMN_PROJECT, 'test_app/migrations/0002_add_new_not_null_field.py')))
l2.lint_migration(Migration(os.path.join(fixtures.RENAME_COLUMN_PROJECT, 'test_app/migrations/0002_rename_column.py')))
l3.lint_migration(Migration(os.path.join(fixtures.CORRECT_PROJECT, 'test_app1/migrations/0001_initial.py')))
l3.lint_migration(Migration(os.path.join(fixtures.CORRECT_PROJECT, 'test_app1/migrations/0002_a_new_null_field.py')))

self.assertTrue(l1.has_errors)
self.assertTrue(l2.has_errors)
Expand Down
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class TestApp3Config(AppConfig):
name = 'test_app3'
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 2.1.4 on 2019-01-04 20:55

from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='C',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('not_null_field', models.IntegerField()),
],
),
]
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from django.db import models

class C(models.Model):
not_null_field = models.IntegerField(null=False)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.shortcuts import render

# Create your views here.
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
"""

import os
import sys

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

sys.path.insert(0, os.path.join(BASE_DIR, 'apps'))

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/
Expand All @@ -38,7 +40,8 @@
'django.contrib.messages',
'django.contrib.staticfiles',
'test_app1',
'test_app2'
'test_app2',
'apps.test_app3',
]

MIDDLEWARE = [
Expand Down
16 changes: 11 additions & 5 deletions tests/unit/test_linter.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
# limitations under the License.

import unittest
import os

from django_migration_linter import MigrationLinter
from django_migration_linter.migration import Migration
from tests import fixtures


Expand All @@ -32,13 +34,16 @@ def test_has_errors(self):
linter = MigrationLinter(project_path)
self.assertFalse(linter.has_errors)

linter.lint_migration('test_app', '0001_create_table')
m = Migration(os.path.join(project_path, 'test_app/migrations/0001_create_table.py'))
linter.lint_migration(m)
self.assertFalse(linter.has_errors)

linter.lint_migration('test_app', '0002_add_new_not_null_field')
m = Migration(os.path.join(project_path, 'test_app/migrations/0002_add_new_not_null_field.py'))
linter.lint_migration(m)
self.assertTrue(linter.has_errors)

linter.lint_migration('test_app', '0001_create_table')
m = Migration(os.path.join(project_path, 'test_app/migrations/0001_create_table.py'))
linter.lint_migration(m)
self.assertTrue(linter.has_errors)

def test_linter_creation(self):
Expand Down Expand Up @@ -80,12 +85,13 @@ def test_ignore_migration_full_name(self):
def test_gather_all_migrations(self):
linter = MigrationLinter(fixtures.CORRECT_PROJECT)
migrations = linter._gather_all_migrations()
self.assertEqual(len(migrations), 3)
self.assertEqual(len(migrations), 4)
self.assertEqual(
sorted(migrations),
sorted([(m.app_name, m.name) for m in migrations]),
sorted([
("test_app1", "0001_initial"),
("test_app1", "0002_a_new_null_field"),
("test_app2", "0001_foo"),
("test_app3", "0001_initial"),
])
)

0 comments on commit b1716b9

Please sign in to comment.