Skip to content

Commit

Permalink
fix: remove spaces from the product version text_id fields
Browse files Browse the repository at this point in the history
  • Loading branch information
arslanashraf7 committed Jun 20, 2024
1 parent 5e93478 commit 8876864
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions ecommerce/migrations/0043_remove_extra_spaces_text_id.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Generated by Django 4.2.13 on 2024-06-20 07:51

from django.db import migrations

from ecommerce.utils import (
create_update_rule,
rollback_update_rule,
)


def remove_text_id_space(apps, schema_editor):
"""Remove the extra spaces from the text_id field of the Product Version data"""
ProductVersion = apps.get_model("ecommerce", "ProductVersion")

bad_product_versions = ProductVersion.objects.filter(text_id__icontains=" ")
for product_version in bad_product_versions:
product_version.text_id = product_version.text_id.strip().replace(" ", "")
product_version.description = product_version.description.strip().replace(
" ", ""
)

ProductVersion.objects.bulk_update(bad_product_versions, ["text_id", "description"])


class Migration(migrations.Migration):
dependencies = [
("ecommerce", "0042_alter_coupon_coupon_code"),
]

operations = [
# Rolling back the protection rule temporarily so that we can fix the bad data in product versions
migrations.RunSQL(
sql=rollback_update_rule("productversion"),
reverse_sql=create_update_rule("productversion"),
),
# Fix the data
migrations.RunPython(remove_text_id_space, migrations.RunPython.noop),
# Putting back the protection rule so that product versions could not be changed in database
migrations.RunSQL(
sql=create_update_rule("productversion"),
reverse_sql=rollback_update_rule("productversion"),
),
]

0 comments on commit 8876864

Please sign in to comment.