diff --git a/django_migration_linter/__init__.py b/django_migration_linter/__init__.py index dd30599..a548f66 100644 --- a/django_migration_linter/__init__.py +++ b/django_migration_linter/__init__.py @@ -13,4 +13,4 @@ # limitations under the License. from .migration_linter import * # noqa -from .constants import * # noqa +from .operations import * # noqa diff --git a/django_migration_linter/constants.py b/django_migration_linter/constants.py deleted file mode 100644 index b8356c6..0000000 --- a/django_migration_linter/constants.py +++ /dev/null @@ -1,4 +0,0 @@ -# Can be used to mark ignored migrations. -# This is a noop for SQL. Comments would be better. -# But comments are stripped by RunSQL -IGNORE_MIGRATION = ("SELECT 'dml-ignore'", ) diff --git a/django_migration_linter/operations.py b/django_migration_linter/operations.py new file mode 100644 index 0000000..cf98135 --- /dev/null +++ b/django_migration_linter/operations.py @@ -0,0 +1,38 @@ +# Copyright 2018 3YOURMIND GmbH + +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at + +# http://www.apache.org/licenses/LICENSE-2.0 + +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from django.db.migrations.operations.base import Operation + +IGNORE_MIGRATION_SQL = "-- dml ignores this migration" + + +class IgnoreMigration(Operation): + + reversible = True + # Must be true to be generated through the sqlmigrate command + reduces_to_sql = True + + def state_forwards(self, app_label, state): + pass + + def database_forwards( + self, app_label, schema_editor, from_state, to_state): + schema_editor.execute(IGNORE_MIGRATION_SQL) + + def database_backwards( + self, app_label, schema_editor, from_state, to_state): + schema_editor.execute(IGNORE_MIGRATION_SQL + "(reversed)") + + def describe(self): + return "The Django migration linter will ignore this migration" diff --git a/django_migration_linter/sql_analyser.py b/django_migration_linter/sql_analyser.py index edb655b..fa0a0d9 100644 --- a/django_migration_linter/sql_analyser.py +++ b/django_migration_linter/sql_analyser.py @@ -15,7 +15,9 @@ import re import logging -from django_migration_linter.constants import IGNORE_MIGRATION +from django_migration_linter.operations import IGNORE_MIGRATION_SQL + +IGNORED_MIGRATION = "IGNORED_MIGRATION" logger = logging.getLogger(__name__) @@ -66,8 +68,8 @@ migration_tests = ( 'fn': has_default, 'err_msg': '' }, { - 'code': 'IGNORED_MIGRATION', - 'fn': lambda sql, **kw: re.search(IGNORE_MIGRATION[0], sql), + 'code': IGNORED_MIGRATION, + 'fn': lambda sql, **kw: re.search(IGNORE_MIGRATION_SQL, sql), 'err_msg': '', } ) @@ -79,7 +81,7 @@ def analyse_sql_statements(sql_statements): for statement in sql_statements: for test in migration_tests: if test['fn'](statement, errors=errors): - if test['code'] == 'IGNORED_MIGRATION': + if test['code'] == IGNORED_MIGRATION: logger.info( 'Testing {0} -- IGNORING MIGRATION'.format(statement) ) diff --git a/tests/test_project_fixtures/test_project_ignore_migration/test_app/migrations/0002_rename_table.py b/tests/test_project_fixtures/test_project_ignore_migration/test_app/migrations/0002_rename_table.py index 54fc7d9..d521e87 100644 --- a/tests/test_project_fixtures/test_project_ignore_migration/test_app/migrations/0002_rename_table.py +++ b/tests/test_project_fixtures/test_project_ignore_migration/test_app/migrations/0002_rename_table.py @@ -4,8 +4,7 @@ from __future__ import unicode_literals from django.db import migrations -from django_migration_linter import IGNORE_MIGRATION - +import django_migration_linter as linter class Migration(migrations.Migration): @@ -14,7 +13,7 @@ class Migration(migrations.Migration): ] operations = [ - migrations.RunSQL(IGNORE_MIGRATION), + linter.IgnoreMigration(), migrations.RenameModel( old_name='A', new_name='B',