-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
employee_record: Fix the archive serialized as string once and for all
- Loading branch information
Showing
1 changed file
with
36 additions
and
0 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
itou/employee_record/migrations/0002_fix_archived_json_as_string.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import json | ||
import time | ||
|
||
from django.db import migrations | ||
|
||
|
||
def forward(apps, schema_editor): | ||
print() | ||
EmployeeRecord = apps.get_model("employee_record", "EmployeeRecord") | ||
total_updated = 0 | ||
to_update = [] | ||
for obj in EmployeeRecord.objects.filter(archived_json__isnull=False, archived_json__startswith='"{').only( | ||
"pk", "archived_json" | ||
): | ||
obj.archived_json = json.loads(obj.archived_json) | ||
to_update.append(obj) | ||
if len(to_update) % 1000 == 0: | ||
total_updated += EmployeeRecord.objects.bulk_update(to_update, {"archived_json"}) | ||
print(f"{total_updated} objects updated") | ||
to_update = [] | ||
time.sleep(0.5) | ||
if to_update: | ||
total_updated += EmployeeRecord.objects.bulk_update(to_update, {"archived_json"}) | ||
print(f"{total_updated} objects updated") | ||
|
||
|
||
class Migration(migrations.Migration): | ||
atomic = False | ||
|
||
dependencies = [ | ||
("employee_record", "0001_initial"), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython(code=forward, reverse_code=migrations.RunPython.noop, elidable=True), | ||
] |