-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds check to forbid auto-names for migrations
- Loading branch information
Showing
14 changed files
with
794 additions
and
166 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
File renamed without changes.
9 changes: 8 additions & 1 deletion
9
django_test_app/main_app/migrations/0004_auto_20191119_2125.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
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,46 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from fnmatch import fnmatch | ||
from typing import List, Set, Tuple | ||
|
||
from django.conf import settings | ||
from django.core.checks import CheckMessage, Warning | ||
from django.db.migrations.loader import MigrationLoader | ||
from typing_extensions import Final | ||
|
||
#: We use this value as a unique identifier of this check. | ||
CHECK_NAME: Final = 'django_test_migrations.autonames' | ||
|
||
#: Settings name for this check to ignore some migrations. | ||
_SETTINGS_NAME: Final = 'DTM_IGNORED_MIGRATIONS' | ||
|
||
|
||
def check_migration_names(*args, **kwargs) -> List[CheckMessage]: | ||
"""Finds automatic names in available migrations.""" | ||
loader = MigrationLoader(None, ignore_no_migrations=True) | ||
loader.load_disk() | ||
|
||
ignored_migrations: Set[Tuple[str, str]] = getattr( | ||
settings, _SETTINGS_NAME, set(), | ||
) | ||
|
||
messages = [] | ||
for app_label, migration_name in loader.disk_migrations.keys(): | ||
if (app_label, migration_name) in ignored_migrations: | ||
continue | ||
|
||
if fnmatch(migration_name, '????_auto_*'): | ||
messages.append( | ||
Warning( | ||
'Migration {0}.{1} has an automatic name.'.format( | ||
app_label, migration_name, | ||
), | ||
hint=( | ||
'Rename the migration to describe its contents, ' + | ||
"or if it's from a third party app, add to " + | ||
_SETTINGS_NAME | ||
), | ||
id='{0}.E001'.format(CHECK_NAME), | ||
), | ||
) | ||
return messages |
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,22 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from django.apps import AppConfig | ||
from django.core import checks | ||
|
||
from django_test_migrations.autonames import CHECK_NAME, check_migration_names | ||
|
||
|
||
class AutoNames(AppConfig): | ||
""" | ||
Class to install this check into ``INSTALLED_APPS`` in ``django``. | ||
See: | ||
https://docs.djangoproject.com/en/3.0/ref/applications/ | ||
""" | ||
|
||
name = CHECK_NAME | ||
|
||
def ready(self): | ||
"""That's how we register our check when apps are ready.""" | ||
checks.register(checks.Tags.compatibility)(check_migration_names) |
Oops, something went wrong.