Exclude not critical test (#2)

* Exclude not critical test

* Add exclude-tests option

* Update documentation

* Clean forgotten print

* Improve Readme, with examples
This commit is contained in:
Vladyslav Malynych authored and David Wobrock committed 2019-07-28 12:28:56 +02:00
1 parent e93d4ec4c7
commit 8eede82a38
4 files changed
+59 -4

No files matched your search

+48 -1
View File
@@ -43,7 +43,7 @@ Add the migration linter your ``INSTALLED_APPS``:
]
``python manage.py lintmigrations [GIT_COMMIT_ID] [--ignore-name-contains IGNORE_NAME_CONTAINS] [--include-apps INCLUDE_APPS [INCLUDE_APPS ...] | --exclude-apps EXCLUDE_APPS [EXCLUDE_APPS ...]] [--project-root-path DJANGO_PROJECT_FOLDER]``
``python manage.py lintmigrations [GIT_COMMIT_ID] [--ignore-name-contains IGNORE_NAME_CONTAINS] [--include-apps INCLUDE_APPS [INCLUDE_APPS ...] | --exclude-apps EXCLUDE_APPS [EXCLUDE_APPS ...]] [--exclude-tests EXCLUDE_TESTS [EXCLUDE TESTS ...]] [--project-root-path DJANGO_PROJECT_FOLDER]``
================================================== ===========================================================================================================================
Parameter Description
@@ -53,6 +53,7 @@ Add the migration linter your ``INSTALLED_APPS``:
``--ignore-name IGNORE_NAME [IGNORE_NAME ...]`` Ignore migrations with exactly one of these names.
``--include-apps INCLUDE_APPS [INCLUDE_APPS ...]`` Check only migrations that are in the specified django apps.
``--exclude-apps EXCLUDE_APPS [EXCLUDE_APPS ...]`` Ignore migrations that are in the specified django apps.
``--exclude-tests EXCLUDE_TESTS [EXCLUDE TES...]`` Specify tests to be ignored (e.g. ALTER_COLUMN).
``--verbose or -v`` Print more information during execution.
``--database DATABASE`` Specify the database for which to generate the SQL. Defaults to *default*.
``--cache-path PATH`` specify a directory that should be used to store cache-files in.
@@ -99,6 +100,52 @@ You can also ignore migrations by adding this to your migrations:
]
# ...
Ignoring tests
-------------------
You can also ignore tests by adding this option during execution:
``python manage.py lintmigrations --exclude-tests EXCLUDE_TESTS``
The tests list can be found in sql_analyzer (migration_tests):
.. code-block::
migration_tests = (
{
"code": "NOT_NULL",
"fn": lambda sql, **kw: re.search("(?<!DROP) NOT NULL", sql)
and not re.search("CREATE TABLE", sql),
"err_msg": "NOT NULL constraint on columns",
},
{
"code": "DROP_COLUMN",
"fn": lambda sql, **kw: re.search("DROP COLUMN", sql),
"err_msg": "DROPPING columns",
},
{
"code": "RENAME_COLUMN",
"fn": lambda sql, **kw: re.search("ALTER TABLE .* CHANGE", sql)
or re.search("ALTER TABLE .* RENAME COLUMN", sql),
"err_msg": "RENAMING columns",
},
{
"code": "RENAME_TABLE",
"fn": lambda sql, **kw: re.search("RENAME TABLE", sql)
or re.search("ALTER TABLE .* RENAME TO", sql),
"err_msg": "RENAMING tables",
},
{
"code": "ALTER_COLUMN",
"fn": lambda sql, **kw: re.search("ALTER TABLE .* MODIFY", sql)
or re.search("ALTER TABLE .* ALTER COLUMN .* TYPE", sql),
"err_msg": (
"ALTERING columns (Could be backward compatible. "
"You may ignore this migration.)"
),
},
{"code": "", "fn": has_default, "err_msg": ""},
)
Cache
-----
@@ -50,6 +50,10 @@ class Command(BaseCommand):
"--project-root-path", type=str, nargs="?", help="django project root path"
)
parser.add_argument(
"--exclude-tests", type=str, nargs="*", help="Specify tests to be ignored (e.g. ALTER_COLUMN)"
)
cache_group = parser.add_mutually_exclusive_group(required=False)
cache_group.add_argument(
"--cache-path",
@@ -107,6 +111,7 @@ class Command(BaseCommand):
ignore_name=options["ignore_name"],
include_apps=options["include_apps"],
exclude_apps=options["exclude_apps"],
exclude_tests=options["exclude_tests"],
database=options["database"],
cache_path=options["cache_path"],
no_cache=options["no_cache"],
+4 -1
View File
@@ -42,6 +42,7 @@ class MigrationLinter(object):
ignore_name=None,
include_apps=None,
exclude_apps=None,
exclude_tests=None,
database=DEFAULT_DB_ALIAS,
cache_path=DEFAULT_CACHE_PATH,
no_cache=False,
@@ -54,6 +55,7 @@ class MigrationLinter(object):
self.ignore_name = ignore_name or tuple()
self.include_apps = include_apps
self.exclude_apps = exclude_apps
self.exclude_tests = exclude_tests
self.database = database or DEFAULT_DB_ALIAS
self.cache_path = cache_path or DEFAULT_CACHE_PATH
self.no_cache = no_cache
@@ -118,7 +120,8 @@ class MigrationLinter(object):
return
sql_statements = self.get_sql(app_label, migration_name)
errors = analyse_sql_statements(sql_statements)
exclude_tests = self.exclude_tests or []
errors = analyse_sql_statements(sql_statements, exclude_tests)
if not errors:
print("OK")
+2 -2
View File
@@ -70,11 +70,11 @@ migration_tests = (
)
def analyse_sql_statements(sql_statements):
def analyse_sql_statements(sql_statements, exclude_tests):
errors = []
for statement in sql_statements:
for test in migration_tests:
if test["fn"](statement, errors=errors):
if test["code"] not in exclude_tests and test["fn"](statement, errors=errors):
logger.debug("Testing {0} -- ERROR".format(statement))
table_search = re.search("TABLE `([^`]*)`", statement, re.IGNORECASE)
col_search = re.search("COLUMN `([^`]*)`", statement, re.IGNORECASE)