-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refs kiwitcms/Kiwi#1774 New linter: warn about missing backwards migr…
…ations
- Loading branch information
Showing
7 changed files
with
78 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from pylint import interfaces | ||
from pylint.checkers import utils, BaseChecker | ||
from pylint_django.__pkginfo__ import BASE_ID | ||
from pylint_django.utils import is_migrations_module | ||
|
||
|
||
class MissingBackwardsMigrationChecker(BaseChecker): | ||
__implements__ = (interfaces.IAstroidChecker,) | ||
|
||
name = 'missing-backwards-migration-checker' | ||
|
||
msgs = {'W%s05' % BASE_ID: ('Always include backwards migration callable', | ||
'missing-backwards-migration-callable', | ||
'Always include a backwards/reverse callable counterpart' | ||
' so that the migration is not irreversable.')} | ||
|
||
def visit_call(self, node): | ||
try: | ||
module = node.frame().parent | ||
except: # noqa: E722, pylint: disable=bare-except | ||
return | ||
|
||
if not is_migrations_module(module): | ||
return | ||
|
||
if node.func.as_string().endswith('RunPython') and len(node.args) < 2: | ||
if node.keywords: | ||
for keyword in node.keywords: | ||
if keyword.arg == 'reverse_code': | ||
return | ||
self.add_message('missing-backwards-migration-callable', | ||
node=node) | ||
else: | ||
self.add_message('missing-backwards-migration-callable', | ||
node=node) |
13 changes: 13 additions & 0 deletions
13
pylint_django/tests/input/migrations/0003_without_backwards.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# pylint: disable=missing-docstring | ||
from django.db import migrations | ||
|
||
|
||
def forwards_test(apps, schema_editor): | ||
pass | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
operations = [ | ||
migrations.RunPython(forwards_test) | ||
] |
17 changes: 17 additions & 0 deletions
17
pylint_django/tests/input/migrations/0004_with_backwards.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# pylint: disable=missing-docstring | ||
from django.db import migrations | ||
|
||
|
||
def forwards_test(apps, schema_editor): | ||
pass | ||
|
||
|
||
def backwards_test(apps, schema_editor): | ||
pass | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
operations = [ | ||
migrations.RunPython(forwards_test, backwards_test) | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters