Skip to content

Commit 31a8a01

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 31a8a01

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

openwisp_controller/config/base/multitenancy.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,33 @@
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+
23+
class MultitenancyMigrationTest(TestCase):
24+
def test_no_migration_when_toggling_whois_estimated_location(self):
25+
from django.apps import apps
26+
from django.db.migrations.autodetector import MigrationAutodetector
27+
from django.db.migrations.loader import MigrationLoader
28+
from django.db.migrations.state import ProjectState
29+
30+
for whois, est_loc in [(True, False), (False, True), (True, True)]:
31+
with self.subTest(whois=whois, est_loc=est_loc):
32+
with override_settings(
33+
OPENWISP_CONTROLLER_WHOIS_ENABLED=whois,
34+
OPENWISP_CONTROLLER_ESTIMATED_LOCATION_ENABLED=est_loc,
35+
):
36+
loader = MigrationLoader(None, ignore_no_migrations=True)
37+
autodetector = MigrationAutodetector(
38+
loader.project_state(),
39+
ProjectState.from_apps(apps),
40+
)
41+
changes = autodetector.changes(graph=loader.graph)
42+
self.assertNotIn("config", changes)
43+
1744

1845
class AbstractOrganizationConfigSettings(UUIDModel):
1946
organization = models.OneToOneField(
@@ -36,12 +63,12 @@ class AbstractOrganizationConfigSettings(UUIDModel):
3663
)
3764
whois_enabled = FallbackBooleanChoiceField(
3865
help_text=_("Whether the WHOIS lookup feature is enabled"),
39-
fallback=app_settings.WHOIS_ENABLED,
66+
fallback=FALLBACK_WHOIS_ENABLED,
4067
verbose_name=_("WHOIS Enabled"),
4168
)
4269
estimated_location_enabled = FallbackBooleanChoiceField(
4370
help_text=_("Whether the estimated location feature is enabled"),
44-
fallback=app_settings.ESTIMATED_LOCATION_ENABLED,
71+
fallback=FALLBACK_ESTIMATED_LOCATION_ENABLED,
4572
verbose_name=_("Estimated Location Enabled"),
4673
)
4774
context = JSONField(

0 commit comments

Comments
 (0)