-
-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy path0038_clean_fallbackfields.py
More file actions
60 lines (51 loc) · 2.05 KB
/
0038_clean_fallbackfields.py
File metadata and controls
60 lines (51 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from django.db import migrations
from openwisp_utils.fields import FallbackMixin
def clean_fallback_fields(apps, schema_editor):
"""
This data migration is necessary to clean up the database
from unnecessary stored fallback values. In the previous
implementation, the fallback value was stored in the database.
However, this was not the intended behavior and was a bug. This migration
sets the fallback fields to None when the value in the database
is the same as the fallback value, effectively removing the
unnecessary data from the database.
"""
OrganizationRadiusSettings = apps.get_model(
"openwisp_radius", "OrganizationRadiusSettings"
)
fallback_fields = []
fallback_field_names = []
for field in OrganizationRadiusSettings._meta.get_fields():
if isinstance(field, FallbackMixin):
fallback_fields.append(field)
fallback_field_names.append(field.name)
updated_settings = []
for radius_settings in OrganizationRadiusSettings.objects.iterator():
changed = False
for field in fallback_fields:
if getattr(radius_settings, field.name) == field.fallback:
setattr(radius_settings, field.name, None)
changed = True
if changed:
updated_settings.append(radius_settings)
if len(updated_settings) > 100:
OrganizationRadiusSettings.objects.bulk_update(
updated_settings, fields=[field.name for field in fallback_fields]
)
updated_settings = []
if updated_settings:
OrganizationRadiusSettings.objects.bulk_update(
updated_settings, fields=[field.name for field in fallback_fields]
)
class Migration(migrations.Migration):
dependencies = [
(
"openwisp_radius",
"0037_alter_organizationradiussettings_allowed_mobile_prefixes_and_more",
),
]
operations = [
migrations.RunPython(
clean_fallback_fields, reverse_code=migrations.RunPython.noop
),
]