Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add `unique_type` to line discount models. (#15775)
Piotr Zabieglik1ab4f3bbcebe
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 4ce1ddd81e..e79571decb 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -58,6 +58,7 @@ All notable, unreleased changes to this project will be documented in this file.- Added new input `AppInput.identifier`.- Added new parameter `identifier` for `create_app` command.- When `taxAppId` is provided for `TaxConfiguration` do not allow to finalize `checkoutComplete` or `draftOrderComplete` mutations if Tax App or Avatax plugin didn't respond.+- Add `unique_type` to `OrderLineDiscount` and `CheckoutLineDiscount` models - #15774 by @zedzior# 3.18.0diff --git a/saleor/discount/migrations/0078_add_unique_type.py b/saleor/discount/migrations/0078_add_unique_type.pynew file mode 100644index 0000000000..85dd280310--- /dev/null+++ b/saleor/discount/migrations/0078_add_unique_type.py@@ -0,0 +1,44 @@+# Generated by Django 3.2.24 on 2024-04-08 11:16++from django.db import migrations, models+++class Migration(migrations.Migration):+ dependencies = [+ ("discount", "0077_merge_20240307_1217"),+ ]++ operations = [+ migrations.AddField(+ model_name="checkoutlinediscount",+ name="unique_type",+ field=models.CharField(+ blank=True,+ choices=[+ ("sale", "Sale"),+ ("voucher", "Voucher"),+ ("manual", "Manual"),+ ("promotion", "Promotion"),+ ("order_promotion", "Order promotion"),+ ],+ max_length=64,+ null=True,+ ),+ ),+ migrations.AddField(+ model_name="orderlinediscount",+ name="unique_type",+ field=models.CharField(+ blank=True,+ choices=[+ ("sale", "Sale"),+ ("voucher", "Voucher"),+ ("manual", "Manual"),+ ("promotion", "Promotion"),+ ("order_promotion", "Order promotion"),+ ],+ max_length=64,+ null=True,+ ),+ ),+ ]diff --git a/saleor/discount/migrations/0079_add_index_for_unique_type.py b/saleor/discount/migrations/0079_add_index_for_unique_type.pynew file mode 100644index 0000000000..b97205ae32--- /dev/null+++ b/saleor/discount/migrations/0079_add_index_for_unique_type.py@@ -0,0 +1,33 @@+# Generated by Django 3.2.24 on 2024-04-08 11:18++from django.db import migrations+++class Migration(migrations.Migration):+ atomic = False+ dependencies = [+ ("discount", "0078_add_unique_type"),+ ]++ operations = [+ migrations.RunSQL(+ sql="""+ CREATE UNIQUE INDEX CONCURRENTLY checkoutlinediscount_line_id_unique_type_idx+ ON discount_checkoutlinediscount USING btree (line_id, unique_type);+ """,+ reverse_sql="""+ DROP INDEX CONCURRENTLY IF EXISTS+ checkoutlinediscount_line_id_unique_type_idx;+ """,+ ),+ migrations.RunSQL(+ sql="""+ CREATE UNIQUE INDEX CONCURRENTLY orderlinediscount_line_id_unique_type_idx+ ON discount_orderlinediscount USING btree (line_id, unique_type);+ """,+ reverse_sql="""+ DROP INDEX CONCURRENTLY IF EXISTS+ orderlinediscount_line_id_unique_type_idx;+ """,+ ),+ ]diff --git a/saleor/discount/migrations/0080_add_unique_type_constraint.py b/saleor/discount/migrations/0080_add_unique_type_constraint.pynew file mode 100644index 0000000000..ce3c8e8e00--- /dev/null+++ b/saleor/discount/migrations/0080_add_unique_type_constraint.py@@ -0,0 +1,54 @@+# Generated by Django 3.2.24 on 2024-04-08 11:20++from django.db import migrations, models+++class Migration(migrations.Migration):+ dependencies = [+ ("discount", "0079_add_index_for_unique_type"),+ ]++ operations = [+ migrations.SeparateDatabaseAndState(+ database_operations=[+ migrations.RunSQL(+ sql="""+ ALTER TABLE discount_checkoutlinediscount+ ADD CONSTRAINT unique_checkoutline_discount_type+ UNIQUE USING INDEX checkoutlinediscount_line_id_unique_type_idx;+ """,+ reverse_sql="""+ ALTER TABLE discount_checkoutlinediscount DROP CONSTRAINT IF EXISTS+ unique_checkoutline_discount_type;+ """,+ ),+ migrations.RunSQL(+ sql="""+ ALTER TABLE discount_orderlinediscount+ ADD CONSTRAINT unique_orderline_discount_type+ UNIQUE USING INDEX orderlinediscount_line_id_unique_type_idx;+ """,+ reverse_sql="""+ ALTER TABLE discount_orderlinediscount DROP CONSTRAINT IF EXISTS+ unique_orderline_discount_type;+ """,+ ),+ ],+ state_operations=[+ migrations.AddConstraint(+ model_name="checkoutlinediscount",+ constraint=models.UniqueConstraint(+ fields=("line_id", "unique_type"),+ name="unique_checkoutline_discount_type",+ ),+ ),+ migrations.AddConstraint(+ model_name="orderlinediscount",+ constraint=models.UniqueConstraint(+ fields=("line_id", "unique_type"),+ name="unique_orderline_discount_type",+ ),+ ),+ ],+ )+ ]diff --git a/saleor/discount/models.py b/saleor/discount/models.pyindex 61461e6d9e..2027e4b705 100644--- a/saleor/discount/models.py+++ b/saleor/discount/models.py@@ -539,6 +539,13 @@ class OrderLineDiscount(BaseDiscount):null=True,on_delete=models.CASCADE,)+ # Saleor in version 3.19 and below, doesn't have any unique constraint applied on+ # discounts for checkout/order. To not have an impact on existing DB objects,+ # the new field `unique_type` will be used for new discount records.+ # This will ensure that we always apply a single specific discount type.+ unique_type = models.CharField(+ max_length=64, null=True, blank=True, choices=DiscountType.CHOICES+ )class Meta:indexes = [@@ -548,6 +555,12 @@ class OrderLineDiscount(BaseDiscount):GinIndex(fields=["voucher_code"], name="orderlinedisc_voucher_code_idx"),]ordering = ("created_at", "id")+ constraints = [+ models.UniqueConstraint(+ fields=["line_id", "unique_type"],+ name="unique_orderline_discount_type",+ ),+ ]class CheckoutDiscount(BaseDiscount):@@ -578,6 +591,13 @@ class CheckoutLineDiscount(BaseDiscount):null=True,on_delete=models.CASCADE,)+ # Saleor in version 3.19 and below, doesn't have any unique constraint applied on+ # discounts for checkout/order. To not have an impact on existing DB objects,+ # the new field `unique_type` will be used for new discount records.+ # This will ensure that we always apply a single specific discount type.+ unique_type = models.CharField(+ max_length=64, null=True, blank=True, choices=DiscountType.CHOICES+ )class Meta:indexes = [@@ -587,6 +607,12 @@ class CheckoutLineDiscount(BaseDiscount):GinIndex(fields=["voucher_code"], name="checklinedisc_voucher_code_idx"),]ordering = ("created_at", "id")+ constraints = [+ models.UniqueConstraint(+ fields=["line_id", "unique_type"],+ name="unique_checkoutline_discount_type",+ ),+ ]class PromotionEvent(models.Model):