Use a custom migration operation instead of a RunSQL statement

This commit is contained in:
David-Wobrock authored and David Wobrock committed 2018-12-23 17:46:57 +01:00
1 parent 6fe51d2e08
commit fe6433a065
5 files changed
+47 -12

No files matched your search

+1 -1
View File
@@ -13,4 +13,4 @@
# limitations under the License.
from .migration_linter import * # noqa
from .constants import * # noqa
from .operations import * # noqa
-4
View File
@@ -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'", )
+38
View File
@@ -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"
+6 -4
View File
@@ -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)
)