Merge branch 'master' into cache
This commit is contained in:
24 files changed
+396
-37
No files matched your search
@@ -13,3 +13,4 @@
|
||||
# limitations under the License.
|
||||
|
||||
from .migration_linter import * # noqa
|
||||
from .operations import * # noqa
|
||||
@@ -100,6 +100,12 @@ class MigrationLinter(object):
|
||||
analysis_result = analyse_sql_statements(
|
||||
sql_statements)
|
||||
errors = analysis_result['errors']
|
||||
|
||||
if analysis_result['ignored']:
|
||||
print('IGNORE')
|
||||
self.nb_ignored += 1
|
||||
return
|
||||
|
||||
if not errors:
|
||||
print('OK')
|
||||
self.new_cache[md5hash] = {'result': 'OK'}
|
||||
@@ -206,7 +212,7 @@ class MigrationLinter(object):
|
||||
utils.clean_bytes_to_str, diff_process.stdout.readlines()):
|
||||
# Only gather lines that include added migrations
|
||||
if re.search(
|
||||
'\/{0}\/.*\.py'.format(MIGRATION_FOLDER_NAME),
|
||||
r'\/{0}\/.*\.py'.format(MIGRATION_FOLDER_NAME),
|
||||
line) and \
|
||||
'__init__' not in line:
|
||||
app_name, migration_name = split_migration_path(line)
|
||||
@@ -226,7 +232,7 @@ class MigrationLinter(object):
|
||||
def _gather_all_migrations(self):
|
||||
migrations = []
|
||||
for root, dirs, files in os.walk(self.django_path):
|
||||
for file_name in files:
|
||||
for file_name in sorted(files):
|
||||
if os.path.basename(root) == MIGRATION_FOLDER_NAME and \
|
||||
file_name.endswith('.py') and \
|
||||
file_name != '__init__.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"
|
||||
@@ -15,6 +15,10 @@
|
||||
import re
|
||||
import logging
|
||||
|
||||
from django_migration_linter.operations import IGNORE_MIGRATION_SQL
|
||||
|
||||
IGNORED_MIGRATION = "IGNORED_MIGRATION"
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@@ -63,29 +67,45 @@ migration_tests = (
|
||||
'code': '',
|
||||
'fn': has_default,
|
||||
'err_msg': ''
|
||||
}, {
|
||||
'code': IGNORED_MIGRATION,
|
||||
'fn': lambda sql, **kw: re.search(IGNORE_MIGRATION_SQL, sql),
|
||||
'err_msg': '',
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def analyse_sql_statements(sql_statements):
|
||||
errors = []
|
||||
ignored = False
|
||||
for statement in sql_statements:
|
||||
for test in migration_tests:
|
||||
if test['fn'](statement, errors=errors):
|
||||
logger.info('Testing {0} -- ERROR'.format(statement))
|
||||
table_search = re.search(
|
||||
'TABLE `([^`]*)`', statement, re.IGNORECASE)
|
||||
col_search = re.search(
|
||||
'COLUMN `([^`]*)`', statement, re.IGNORECASE)
|
||||
err = {
|
||||
'err_msg': test['err_msg'],
|
||||
'code': test['code'],
|
||||
'table': table_search.group(1) if table_search else None,
|
||||
'column': col_search.group(1) if col_search else None
|
||||
}
|
||||
errors.append(err)
|
||||
if test['code'] == IGNORED_MIGRATION:
|
||||
logger.info(
|
||||
'Testing {0} -- IGNORING MIGRATION'.format(statement)
|
||||
)
|
||||
ignored = True
|
||||
else:
|
||||
logger.info('Testing {0} -- ERROR'.format(statement))
|
||||
table_search = re.search(
|
||||
'TABLE `([^`]*)`', statement, re.IGNORECASE)
|
||||
col_search = re.search(
|
||||
'COLUMN `([^`]*)`', statement, re.IGNORECASE)
|
||||
err = {
|
||||
'err_msg':
|
||||
test['err_msg'],
|
||||
'code':
|
||||
test['code'],
|
||||
'table':
|
||||
table_search.group(1) if table_search else None,
|
||||
'column':
|
||||
col_search.group(1) if col_search else None
|
||||
}
|
||||
errors.append(err)
|
||||
else:
|
||||
logger.info('Testing {0} -- PASSED'.format(statement))
|
||||
return {
|
||||
'errors': errors,
|
||||
'ignored': ignored
|
||||
}
|
||||
Reference in new issue
Block a user