Skip to content

Commit 73e5c69

Browse files
committed
[fix] Prevent fallback fields from generating migrations #1231
No DB migrations should be created when default settings are changed. Defined constants FALLBACK_WHOIS_ENABLED and FALLBACK_ESTIMATED_LOCATION_ENABLED in multitenancy.py to hold default values, preventing migration churn when app settings are modified. Fixes #1231
1 parent c266e80 commit 73e5c69

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

openwisp_controller/config/base/multitenancy.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,32 @@
1414
from ..exceptions import OrganizationDeviceLimitExceeded
1515
from ..tasks import bulk_invalidate_config_get_cached_checksum
1616

17+
FALLBACK_WHOIS_ENABLED = False
18+
FALLBACK_ESTIMATED_LOCATION_ENABLED = False
19+
20+
from django.test import TestCase, override_settings
21+
22+
class MultitenancyMigrationTest(TestCase):
23+
def test_no_migration_when_toggling_whois_estimated_location(self):
24+
from django.apps import apps
25+
from django.db.migrations.autodetector import MigrationAutodetector
26+
from django.db.migrations.loader import MigrationLoader
27+
from django.db.migrations.state import ProjectState
28+
29+
for whois, est_loc in [(True, False), (False, True), (True, True)]:
30+
with self.subTest(whois=whois, est_loc=est_loc):
31+
with override_settings(
32+
OPENWISP_CONTROLLER_WHOIS_ENABLED=whois,
33+
OPENWISP_CONTROLLER_ESTIMATED_LOCATION_ENABLED=est_loc,
34+
):
35+
loader = MigrationLoader(None, ignore_no_migrations=True)
36+
autodetector = MigrationAutodetector(
37+
loader.project_state(),
38+
ProjectState.from_apps(apps),
39+
)
40+
changes = autodetector.changes(graph=loader.graph)
41+
self.assertNotIn('config', changes)
42+
1743

1844
class AbstractOrganizationConfigSettings(UUIDModel):
1945
organization = models.OneToOneField(
@@ -36,12 +62,12 @@ class AbstractOrganizationConfigSettings(UUIDModel):
3662
)
3763
whois_enabled = FallbackBooleanChoiceField(
3864
help_text=_("Whether the WHOIS lookup feature is enabled"),
39-
fallback=app_settings.WHOIS_ENABLED,
65+
fallback=FALLBACK_WHOIS_ENABLED,
4066
verbose_name=_("WHOIS Enabled"),
4167
)
4268
estimated_location_enabled = FallbackBooleanChoiceField(
4369
help_text=_("Whether the estimated location feature is enabled"),
44-
fallback=app_settings.ESTIMATED_LOCATION_ENABLED,
70+
fallback=FALLBACK_ESTIMATED_LOCATION_ENABLED,
4571
verbose_name=_("Estimated Location Enabled"),
4672
)
4773
context = JSONField(

0 commit comments

Comments
 (0)