Skip to content

Commit

Permalink
migrations: optimize extra_data migration
Browse files Browse the repository at this point in the history
This makes it more friendly to huge databases.

Fixes #501
  • Loading branch information
nijel committed Oct 17, 2023
1 parent a8713fa commit 36c3e59
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions social_django/migrations/0013_migrate_extra_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,27 @@ def migrate_json_field(apps, schema_editor):
UserSocialAuth = apps.get_model("social_django", "UserSocialAuth")
Partial = apps.get_model("social_django", "Partial")
db_alias = schema_editor.connection.alias
for auth in UserSocialAuth.objects.using(db_alias).all():
to_be_updated = []
for auth in (
UserSocialAuth.objects.using(db_alias).exclude(extra_data='""').iterator()
):
old_value = auth.extra_data
if isinstance(old_value, str):
try:
old_value = json.loads(old_value)
except json.JSONDecodeError as error:
print(f"Failed to migrate extra_data {old_value}: {error}")
auth.extra_data_new = old_value
auth.save(update_fields=["extra_data_new"])
to_be_updated.append(auth)

if len(to_be_updated) >= 1000:
UserSocialAuth.objects.bulk_update(to_be_updated, ["extra_data_new"])
to_be_updated.clear()

if to_be_updated:
UserSocialAuth.objects.bulk_update(to_be_updated, ["extra_data_new"])
to_be_updated.clear()

for auth in Partial.objects.using(db_alias).all():
old_value = auth.data
if isinstance(old_value, str):
Expand Down

0 comments on commit 36c3e59

Please sign in to comment.