This repository has been archived by the owner on Nov 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 254
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add data migration to make voucher names unique
- Loading branch information
1 parent
6f0b745
commit ddbfe3e
Showing
2 changed files
with
36 additions
and
2 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
ecommerce/extensions/voucher/migrations/0013_make_voucher_names_unique.py
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,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), | ||
] |
4 changes: 2 additions & 2 deletions
4
...her/migrations/0013_auto_20231108_1355.py → ...her/migrations/0014_auto_20231114_1156.py
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