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 cc31ccc
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 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,16 @@ 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 55 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#L55

Added line #L55 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 59 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#L57-L59

Added lines #L57 - L59 were not covered by tests

if len(to_be_updated) >= 1000:
Expand All @@ -60,9 +66,15 @@ 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 67 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#L66-L67

Added lines #L66 - L67 were not covered by tests

is_text_field = issubclass(

Check warning on line 69 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#L69

Added line #L69 was not covered by tests
Partial._meta.get_field("data"),
models.TextField,
)
for auth in Partial.objects.using(db_alias).all():
new_value = auth.data_new

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#L74

Added line #L74 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 78 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#L76-L78

Added lines #L76 - L78 were not covered by tests


Expand Down

0 comments on commit cc31ccc

Please sign in to comment.