diff --git a/CHANGELOG.md b/CHANGELOG.md index 371fb1d..e671655 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ - Fix index creation detection when table is being created in the transaction (issue #178) - Handle unique index creation as adding a unique constraint (issue #183) - Allow any option to be set/unset in config file (issue #167) +- Allow using Django settings for any option to be set/unset (issue #198) - Raise when unsupported database vendor, allow passing an option to select SQL analyser (issue #138 and #169) ## 3.0.1 diff --git a/django_migration_linter/management/commands/lintmigrations.py b/django_migration_linter/management/commands/lintmigrations.py index 39d7d8e..e0a4691 100644 --- a/django_migration_linter/management/commands/lintmigrations.py +++ b/django_migration_linter/management/commands/lintmigrations.py @@ -6,6 +6,7 @@ import sys from importlib import import_module import toml +from django.conf import settings from django.core.management.base import BaseCommand from ...constants import __version__ @@ -151,9 +152,14 @@ class Command(BaseCommand): else: logging.basicConfig(format="%(message)s") + django_settings_options = self.read_django_settings(options) config_options = self.read_config_file(options) toml_options = self.read_toml_file(options) - for k, v in itertools.chain(config_options.items(), toml_options.items()): + for k, v in itertools.chain( + django_settings_options.items(), + config_options.items(), + toml_options.items(), + ): if not options[k]: options[k] = v @@ -186,6 +192,19 @@ class Command(BaseCommand): if linter.has_errors: sys.exit(1) + @staticmethod + def read_django_settings(options): + django_settings_options = dict() + + django_migration_linter_settings = getattr( + settings, "MIGRATION_LINTER_OPTIONS", dict() + ) + for key in options: + if key in django_migration_linter_settings: + django_settings_options[key] = django_migration_linter_settings[key] + + return django_settings_options + @staticmethod def read_config_file(options): config_options = dict() diff --git a/docs/usage.md b/docs/usage.md index 7686e9d..48fbdd0 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -41,6 +41,17 @@ Below the detailed command line options, which can all also be defined using a c | `--warnings-as-errors` | Handle warnings as errors and therefore return an error status code if we should. | | `--sql-analyser` | Specify the SQL analyser that should be used. Allowed values: 'sqlite', 'mysql', 'postgresql'. | +## Django settings configuration + +All settings can be defined in the Django settings: + +``` +MIGRATION_LINTER_OPTIONS = { + "no_cache": True, + "exclude_apps": ["users"] +} +``` + ## File configuration Example `setup.cfg` file: diff --git a/tests/functional/test_lintmigrations_command.py b/tests/functional/test_lintmigrations_command.py index b4dc1e8..ac31312 100644 --- a/tests/functional/test_lintmigrations_command.py +++ b/tests/functional/test_lintmigrations_command.py @@ -2,6 +2,7 @@ from unittest.mock import patch from django.core.management import call_command from django.test import TransactionTestCase +from django.test.utils import override_settings class LintMigrationsCommandTestCase(TransactionTestCase): @@ -29,3 +30,7 @@ class LintMigrationsCommandTestCase(TransactionTestCase): with self.assertRaises(SystemExit): call_command("lintmigrations", app_label="app_drop_table") + + @override_settings(MIGRATION_LINTER_OPTIONS={"app_label": "app_correct"}) + def test_django_settings_option(self): + call_command("lintmigrations")