-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: remove spaces from the product version text_id fields
- Loading branch information
1 parent
5e93478
commit 8876864
Showing
1 changed file
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"), | ||
), | ||
] |