Skip to content
This repository has been archived by the owner on Nov 4, 2024. It is now read-only.

Commit

Permalink
feat: add data migration to make voucher names unique
Browse files Browse the repository at this point in the history
  • Loading branch information
mumarkhan999 committed Nov 14, 2023
1 parent 6f0b745 commit a13a202
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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 = "%s - %d" % (obj.name, obj.id)
updates.append(obj)

Voucher.objects.bulk_update(updates, ["field"])


class Migration(migrations.Migration):

dependencies = [
('voucher', '0012_voucher_is_public'),
]

operations = [
migrations.RunPython(make_voucher_names_unique, migrations.RunPython.noop),
]
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# 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


class Migration(migrations.Migration):

dependencies = [
('voucher', '0012_voucher_is_public'),
('voucher', '0013_make_voucher_names_unique'),
]

operations = [
Expand Down

0 comments on commit a13a202

Please sign in to comment.