Skip to content

Commit e6cfbaa

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. Corrected help_text on the estimated_location_enabled field to explicitly match the original migration. Fixes #1231
1 parent c266e80 commit e6cfbaa

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

openwisp_controller/config/base/multitenancy.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
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+
1720

1821
class AbstractOrganizationConfigSettings(UUIDModel):
1922
organization = models.OneToOneField(
@@ -36,12 +39,12 @@ class AbstractOrganizationConfigSettings(UUIDModel):
3639
)
3740
whois_enabled = FallbackBooleanChoiceField(
3841
help_text=_("Whether the WHOIS lookup feature is enabled"),
39-
fallback=app_settings.WHOIS_ENABLED,
42+
fallback=FALLBACK_WHOIS_ENABLED,
4043
verbose_name=_("WHOIS Enabled"),
4144
)
4245
estimated_location_enabled = FallbackBooleanChoiceField(
4346
help_text=_("Whether the estimated location feature is enabled"),
44-
fallback=app_settings.ESTIMATED_LOCATION_ENABLED,
47+
fallback=FALLBACK_ESTIMATED_LOCATION_ENABLED,
4548
verbose_name=_("Estimated Location Enabled"),
4649
)
4750
context = JSONField(

openwisp_controller/config/tests/test_config.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,3 +1000,26 @@ def test_checksum_db_accounts_for_vpnclient(self):
10001000
config.refresh_from_db()
10011001
config._invalidate_backend_instance_cache()
10021002
self.assertEqual(config.checksum, config.checksum_db)
1003+
1004+
1005+
class MultitenancyMigrationTest(TestCase):
1006+
def test_no_migration_when_toggling_whois_estimated_location(self):
1007+
from django.apps import apps
1008+
from django.db.migrations.autodetector import MigrationAutodetector
1009+
from django.db.migrations.loader import MigrationLoader
1010+
from django.db.migrations.state import ProjectState
1011+
from django.test import override_settings
1012+
1013+
for whois, est_loc in [(True, False), (False, True), (True, True)]:
1014+
with self.subTest(whois=whois, est_loc=est_loc):
1015+
with override_settings(
1016+
OPENWISP_CONTROLLER_WHOIS_ENABLED=whois,
1017+
OPENWISP_CONTROLLER_ESTIMATED_LOCATION_ENABLED=est_loc,
1018+
):
1019+
loader = MigrationLoader(None, ignore_no_migrations=True)
1020+
autodetector = MigrationAutodetector(
1021+
loader.project_state(),
1022+
ProjectState.from_apps(apps),
1023+
)
1024+
changes = autodetector.changes(graph=loader.graph)
1025+
self.assertNotIn("config", changes)

0 commit comments

Comments
 (0)