Skip to content

Commit c440597

Browse files
committed
[fix]: Prevent FallbackMixin from generating spurious migrations
The `deconstruct()` method was serializing the fallback kwarg into Django migration files. This caused new migrations to be generated whenever the fallback default value changed in settings, even though no actual database schema change had occurred. The fix removes fallback from deconstruct() so Django no longer tracks it as part of the field migration state. fallback is also made optional in `__init__` (defaulting to None) so existing migrations that omit the kwarg remain valid. Fixes: #1231
1 parent e1d24be commit c440597

File tree

2 files changed

+63
-2
lines changed

2 files changed

+63
-2
lines changed

openwisp_utils/fields.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,20 @@ class FallbackMixin(object):
4848
"""
4949

5050
def __init__(self, *args, **kwargs):
51-
self.fallback = kwargs.pop("fallback")
51+
self.fallback = kwargs.pop("fallback", None)
5252
opts = dict(blank=True, null=True, default=None)
5353
opts.update(kwargs)
5454
super().__init__(*args, **opts)
5555

5656
def deconstruct(self):
5757
name, path, args, kwargs = super().deconstruct()
58-
kwargs["fallback"] = self.fallback
5958
return (name, path, args, kwargs)
6059

60+
def clone(self):
61+
obj = super().clone()
62+
obj.fallback = self.fallback
63+
return obj
64+
6165
def from_db_value(self, value, expression, connection):
6266
"""Called when fetching value from the database."""
6367
if value is None:

tests/test_project/tests/test_model.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
from django.core.exceptions import ValidationError
44
from django.db import connection
5+
from django.db.migrations.autodetector import MigrationAutodetector
6+
from django.db.migrations.loader import MigrationLoader
7+
from django.db.migrations.questioner import NonInteractiveMigrationQuestioner
58
from django.test import TestCase
69

710
from ..models import Book, OrganizationRadiusSettings, Project, Shelf
@@ -180,3 +183,57 @@ def test_fallback_decimal_field(self):
180183
book.save(update_fields=["price"])
181184
book.refresh_from_db(fields=["price"])
182185
self.assertEqual(book.price, 56)
186+
187+
def test_fallback_field_deconstruct(self):
188+
with self.subTest("FallbackBooleanChoiceField"):
189+
field = OrganizationRadiusSettings._meta.get_field("is_active")
190+
name, path, args, kwargs = field.deconstruct()
191+
self.assertNotIn("fallback", kwargs)
192+
with self.subTest("FallbackCharField"):
193+
field = OrganizationRadiusSettings._meta.get_field("greeting_text")
194+
name, path, args, kwargs = field.deconstruct()
195+
self.assertNotIn("fallback", kwargs)
196+
with self.subTest("FallbackDecimalField"):
197+
field = Book._meta.get_field("price")
198+
name, path, args, kwargs = field.deconstruct()
199+
self.assertNotIn("fallback", kwargs)
200+
with self.subTest("FallbackPositiveIntegerField"):
201+
field = Shelf._meta.get_field("books_count")
202+
name, path, args, kwargs = field.deconstruct()
203+
self.assertNotIn("fallback", kwargs)
204+
with self.subTest("Plain field without fallback"):
205+
field = Shelf._meta.get_field("name")
206+
name, path, args, kwargs = field.deconstruct()
207+
self.assertNotIn("fallback", kwargs)
208+
209+
def test_fallback_field_no_migration_on_fallback_change(self):
210+
loader = MigrationLoader(None, ignore_no_migrations=True)
211+
current_state = loader.project_state()
212+
recorded_state = current_state.clone()
213+
214+
new_fallback_by_field = {
215+
"is_active": True,
216+
"price": 99.0,
217+
"books_count": 999,
218+
}
219+
field_specs = [
220+
("test_project", "organizationradiussettings", "is_active"),
221+
("test_project", "book", "price"),
222+
("test_project", "shelf", "books_count"),
223+
]
224+
for app_label, model_name, field_name in field_specs:
225+
live_field = current_state.models[(app_label, model_name)].fields[
226+
field_name
227+
]
228+
name, path, orig_args, orig_kwargs = live_field.deconstruct()
229+
orig_kwargs["fallback"] = new_fallback_by_field[field_name]
230+
recorded_state.models[(app_label, model_name)].fields[field_name] = (
231+
live_field.__class__(*orig_args, **orig_kwargs)
232+
)
233+
234+
changes = MigrationAutodetector(
235+
recorded_state,
236+
current_state,
237+
NonInteractiveMigrationQuestioner(),
238+
).changes(graph=loader.graph)
239+
self.assertEqual(changes, {})

0 commit comments

Comments
 (0)