From ddbfe3ed130b9e8770b96737a25426dd8f5d55df Mon Sep 17 00:00:00 2001 From: Muhammad Umar Khan Date: Tue, 14 Nov 2023 17:04:09 +0500 Subject: [PATCH] feat: add data migration to make voucher names unique --- .../0013_make_voucher_names_unique.py | 34 +++++++++++++++++++ ...108_1355.py => 0014_auto_20231114_1156.py} | 4 +-- 2 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 ecommerce/extensions/voucher/migrations/0013_make_voucher_names_unique.py rename ecommerce/extensions/voucher/migrations/{0013_auto_20231108_1355.py => 0014_auto_20231114_1156.py} (95%) diff --git a/ecommerce/extensions/voucher/migrations/0013_make_voucher_names_unique.py b/ecommerce/extensions/voucher/migrations/0013_make_voucher_names_unique.py new file mode 100644 index 00000000000..b83ef703736 --- /dev/null +++ b/ecommerce/extensions/voucher/migrations/0013_make_voucher_names_unique.py @@ -0,0 +1,34 @@ +# Generated by Django 3.2.20 on 2023-11-14 11:20 + +from django.core.paginator import Paginator +from django.db import migrations + + +def make_voucher_names_unique(apps, schema_editor): + """ + Appends a number to voucher names. + """ + Voucher = apps.get_model('voucher', 'Voucher') + vouchers = Voucher.objects.all() + paginator = Paginator(vouchers, 1000) + + for page_number in paginator.page_range: + page = paginator.page(page_number) + updates = [] + + for obj in page.object_list: + obj.name = '%d - %s' % (obj.id, obj.name) + updates.append(obj) + + Voucher.objects.bulk_update(updates, ['name']) + + +class Migration(migrations.Migration): + + dependencies = [ + ('voucher', '0012_voucher_is_public'), + ] + + operations = [ + migrations.RunPython(make_voucher_names_unique, migrations.RunPython.noop), + ] diff --git a/ecommerce/extensions/voucher/migrations/0013_auto_20231108_1355.py b/ecommerce/extensions/voucher/migrations/0014_auto_20231114_1156.py similarity index 95% rename from ecommerce/extensions/voucher/migrations/0013_auto_20231108_1355.py rename to ecommerce/extensions/voucher/migrations/0014_auto_20231114_1156.py index 96af491a9d2..07e9af22939 100644 --- a/ecommerce/extensions/voucher/migrations/0013_auto_20231108_1355.py +++ b/ecommerce/extensions/voucher/migrations/0014_auto_20231114_1156.py @@ -1,4 +1,4 @@ -# Generated by Django 3.2.20 on 2023-11-08 13:55 +# Generated by Django 3.2.20 on 2023-11-14 11:56 from django.db import migrations, models @@ -6,7 +6,7 @@ class Migration(migrations.Migration): dependencies = [ - ('voucher', '0012_voucher_is_public'), + ('voucher', '0013_make_voucher_names_unique'), ] operations = [