Skip to content

Commit

Permalink
fix: only call json.dumps if fields are TextField
Browse files Browse the repository at this point in the history
  • Loading branch information
browniebroke committed Dec 19, 2023
1 parent 41330d1 commit 7a0f16b
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions social_django/migrations/0013_migrate_extra_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import json

from django.db import migrations
from django.db import migrations, models


def migrate_json_field(apps, schema_editor):
Expand Down Expand Up @@ -46,10 +46,15 @@ def migrate_json_field_backwards(apps, schema_editor):
Partial = apps.get_model("social_django", "Partial")
db_alias = schema_editor.connection.alias
to_be_updated = []

Check warning on line 48 in social_django/migrations/0013_migrate_extra_data.py

View check run for this annotation

Codecov / codecov/patch

social_django/migrations/0013_migrate_extra_data.py#L45-L48

Added lines #L45 - L48 were not covered by tests

is_text_field = isinstance(

Check warning on line 50 in social_django/migrations/0013_migrate_extra_data.py

View check run for this annotation

Codecov / codecov/patch

social_django/migrations/0013_migrate_extra_data.py#L50

Added line #L50 was not covered by tests
UserSocialAuth._meta.get_field("extra_data"), models.TextField
)
for auth in UserSocialAuth.objects.using(db_alias).iterator():
new_value = auth.extra_data_new

Check warning on line 54 in social_django/migrations/0013_migrate_extra_data.py

View check run for this annotation

Codecov / codecov/patch

social_django/migrations/0013_migrate_extra_data.py#L54

Added line #L54 was not covered by tests
old_value = json.dumps(new_value)
auth.extra_data = old_value
if is_text_field:
new_value = json.dumps(new_value)
auth.extra_data = new_value
to_be_updated.append(auth)

Check warning on line 58 in social_django/migrations/0013_migrate_extra_data.py

View check run for this annotation

Codecov / codecov/patch

social_django/migrations/0013_migrate_extra_data.py#L56-L58

Added lines #L56 - L58 were not covered by tests

if len(to_be_updated) >= 1000:
Expand All @@ -60,9 +65,12 @@ def migrate_json_field_backwards(apps, schema_editor):
UserSocialAuth.objects.bulk_update(to_be_updated, ["extra_data"])
to_be_updated.clear()

Check warning on line 66 in social_django/migrations/0013_migrate_extra_data.py

View check run for this annotation

Codecov / codecov/patch

social_django/migrations/0013_migrate_extra_data.py#L65-L66

Added lines #L65 - L66 were not covered by tests

is_text_field = issubclass(Partial._meta.get_field("data"), models.TextField)

Check warning on line 68 in social_django/migrations/0013_migrate_extra_data.py

View check run for this annotation

Codecov / codecov/patch

social_django/migrations/0013_migrate_extra_data.py#L68

Added line #L68 was not covered by tests
for auth in Partial.objects.using(db_alias).all():
new_value = auth.data_new

Check warning on line 70 in social_django/migrations/0013_migrate_extra_data.py

View check run for this annotation

Codecov / codecov/patch

social_django/migrations/0013_migrate_extra_data.py#L70

Added line #L70 was not covered by tests
auth.data = json.dumps(new_value)
if is_text_field:
new_value = json.dumps(new_value)
auth.data = new_value
auth.save(update_fields=["data"])

Check warning on line 74 in social_django/migrations/0013_migrate_extra_data.py

View check run for this annotation

Codecov / codecov/patch

social_django/migrations/0013_migrate_extra_data.py#L72-L74

Added lines #L72 - L74 were not covered by tests


Expand Down

0 comments on commit 7a0f16b

Please sign in to comment.