Skip to content

Commit

Permalink
employee_record: Fix the archive serialized as string once and for all
Browse files Browse the repository at this point in the history
  • Loading branch information
rsebille committed Jan 2, 2025
1 parent d7b66d0 commit 8acfca2
Showing 1 changed file with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
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('id', '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),
]

0 comments on commit 8acfca2

Please sign in to comment.