Skip to content

Saleor Core: the high performance, composable, headless commerce API.

FIX for migration from 3.17 version (#14786)

Michał Macioszczyke2031f0dc458
diff --git a/saleor/discount/migrations/0055_vouchercode.py b/saleor/discount/migrations/0055_vouchercode.py
index 4db3021fe6..d44ceccd46 100644
--- a/saleor/discount/migrations/0055_vouchercode.py
+++ b/saleor/discount/migrations/0055_vouchercode.py
@@ -48,4 +48,12 @@ class Migration(migrations.Migration):
""",
reverse_sql=migrations.RunSQL.noop,
),
+ migrations.RunSQL(
+ sql="""
+ ALTER TABLE discount_vouchercode
+ ALTER COLUMN created_at
+ SET DEFAULT CURRENT_TIMESTAMP;
+ """,
+ reverse_sql=migrations.RunSQL.noop,
+ ),
]
diff --git a/saleor/discount/migrations/0065_vouchercode_created_at_check.py b/saleor/discount/migrations/0065_vouchercode_created_at_check.py
new file mode 100644
index 0000000000..03f9b982c1
--- /dev/null
+++ b/saleor/discount/migrations/0065_vouchercode_created_at_check.py
@@ -0,0 +1,21 @@
+# Generated by Django 3.2.23 on 2023-11-17 08:08
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("discount", "0064_alter_promotionrule_catalogue_predicate"),
+ ]
+
+ operations = [
+ migrations.RunSQL(
+ sql=(
+ """
+ ALTER TABLE discount_vouchercode
+ ADD COLUMN IF NOT EXISTS created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL;
+ """
+ ),
+ reverse_sql=migrations.RunSQL.noop,
+ ),
+ ]
diff --git a/saleor/discount/migrations/0069_merge_20231117_0959.py b/saleor/discount/migrations/0069_merge_20231117_0959.py
new file mode 100644
index 0000000000..c7ee4b7aa7
--- /dev/null
+++ b/saleor/discount/migrations/0069_merge_20231117_0959.py
@@ -0,0 +1,12 @@
+# Generated by Django 3.2.23 on 2023-11-17 09:59
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("discount", "0065_vouchercode_created_at_check"),
+ ("discount", "0068_merge_20231106_1013"),
+ ]
+
+ operations = []
diff --git a/saleor/graphql/discount/dataloaders.py b/saleor/graphql/discount/dataloaders.py
index b56bd1a10f..a0b1a7da6c 100644
--- a/saleor/graphql/discount/dataloaders.py
+++ b/saleor/graphql/discount/dataloaders.py
@@ -2,6 +2,7 @@ from collections import defaultdict
from typing import Optional
from django.db.models import Exists, F, OuterRef, Sum
+from django.db.models.functions import Coalesce
from promise import Promise
from ...channel.models import Channel
@@ -73,7 +74,7 @@ class UsedByVoucherIDLoader(DataLoader):
vouchers = (
Voucher.objects.using(self.database_connection_name)
.filter(id__in=keys)
- .annotate(max_used=Sum("codes__used"))
+ .annotate(max_used=Coalesce(Sum("codes__used"), 0))
)
vouchers_map = {}
for voucher in vouchers: