Skip to content

Commit b6504a6

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 cc2e830 commit b6504a6

2 files changed

Lines changed: 72 additions & 2 deletions

File tree

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: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,69 @@ def test_fallback_decimal_field(self):
180180
book.save(update_fields=["price"])
181181
book.refresh_from_db(fields=["price"])
182182
self.assertEqual(book.price, 56)
183+
184+
def test_fallback_field_deconstruct(self):
185+
with self.subTest("FallbackBooleanChoiceField"):
186+
field = OrganizationRadiusSettings._meta.get_field("is_active")
187+
name, path, args, kwargs = field.deconstruct()
188+
self.assertNotIn("fallback", kwargs)
189+
with self.subTest("FallbackCharField"):
190+
field = OrganizationRadiusSettings._meta.get_field("greeting_text")
191+
name, path, args, kwargs = field.deconstruct()
192+
self.assertNotIn("fallback", kwargs)
193+
with self.subTest("FallbackDecimalField"):
194+
field = Book._meta.get_field("price")
195+
name, path, args, kwargs = field.deconstruct()
196+
self.assertNotIn("fallback", kwargs)
197+
with self.subTest("FallbackPositiveIntegerField"):
198+
field = Shelf._meta.get_field("books_count")
199+
name, path, args, kwargs = field.deconstruct()
200+
self.assertNotIn("fallback", kwargs)
201+
with self.subTest("Plain field without fallback"):
202+
field = Shelf._meta.get_field("name")
203+
name, path, args, kwargs = field.deconstruct()
204+
self.assertNotIn("fallback", kwargs)
205+
206+
def test_fallback_field_no_migration_on_fallback_change(self):
207+
import importlib
208+
209+
from django.db.migrations.autodetector import MigrationAutodetector
210+
from django.db.migrations.loader import MigrationLoader
211+
from django.db.migrations.questioner import NonInteractiveMigrationQuestioner
212+
213+
loader = MigrationLoader(None, ignore_no_migrations=True)
214+
current_state = loader.project_state()
215+
recorded_state = current_state.clone()
216+
217+
new_fallback_by_field = {
218+
"is_active": True,
219+
"price": 99.0,
220+
"books_count": 999,
221+
}
222+
field_specs = [
223+
("test_project", "organizationradiussettings", "is_active"),
224+
("test_project", "book", "price"),
225+
("test_project", "shelf", "books_count"),
226+
]
227+
for app_label, model_name, field_name in field_specs:
228+
live_field = current_state.models[(app_label, model_name)].fields[
229+
field_name
230+
]
231+
_, path, orig_args, orig_kwargs = live_field.deconstruct()
232+
orig_kwargs["fallback"] = new_fallback_by_field[field_name]
233+
module_path, class_name = path.rsplit(".", 1)
234+
cls = getattr(importlib.import_module(module_path), class_name)
235+
recorded_state.models[(app_label, model_name)].fields[field_name] = cls(
236+
*orig_args, **orig_kwargs
237+
)
238+
239+
changes = MigrationAutodetector(
240+
recorded_state,
241+
current_state,
242+
NonInteractiveMigrationQuestioner(),
243+
).changes(graph=loader.graph)
244+
self.assertEqual(
245+
changes,
246+
{},
247+
msg="Changing the fallback value should not generate migrations",
248+
)

0 commit comments

Comments
 (0)