Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Migrate checkout line id from int to UUID (#9675)
Iga Karbowiak19a70a04af45
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 23150fa281..269900f280 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -26,6 +26,9 @@ All notable, unreleased changes to this project will be documented in this file.- Add webhooks `MENU_CREATED`, `MENU_UPDATED`, `MENU_DELETED`, `MENU_ITEM_CREATED`, `MENU_ITEM_UPDATED`, `MENU_ITEM_DELETED` - #9651 by @SzymJ- Add webhooks `VOUCHER_CREATED`, `VOUCHER_UPDATED`, `VOUCHER_DELETED` - #9657 by @SzymJ- Add webhooks `APP_CREATED`, `APP_UPDATED`, `APP_DELETED`, `APP_STATUS_CHANGED` - #9698 by @SzymJ+- Migrate checkout line id from int to UUID - #9675 by @IKarbowiak+ - Changed the checkout line `id` from `int` to `UUID`, the old ids still can be used+ for old checkout lines.# 3.3.1diff --git a/saleor/checkout/migrations/0043_add_token_old_id_created_at_to_checkout_line.py b/saleor/checkout/migrations/0043_add_token_old_id_created_at_to_checkout_line.pynew file mode 100644index 0000000000..470514601e--- /dev/null+++ b/saleor/checkout/migrations/0043_add_token_old_id_created_at_to_checkout_line.py@@ -0,0 +1,28 @@+# Generated by Django 3.2.13 on 2022-04-29 08:44++from django.db import migrations, models+++class Migration(migrations.Migration):++ dependencies = [+ ("checkout", "0042_rename_created_checkout_created_at"),+ ]++ operations = [+ migrations.AddField(+ model_name="checkoutline",+ name="created_at",+ field=models.DateTimeField(null=True),+ ),+ migrations.AddField(+ model_name="checkoutline",+ name="old_id",+ field=models.PositiveIntegerField(blank=True, null=True, unique=True),+ ),+ migrations.AddField(+ model_name="checkoutline",+ name="token",+ field=models.UUIDField(null=True, unique=True),+ ),+ ]diff --git a/saleor/checkout/migrations/0044_fulfill_checkout_line_new_fields.py b/saleor/checkout/migrations/0044_fulfill_checkout_line_new_fields.pynew file mode 100644index 0000000000..895f64bede--- /dev/null+++ b/saleor/checkout/migrations/0044_fulfill_checkout_line_new_fields.py@@ -0,0 +1,68 @@+# Generated by Django 3.2.13 on 2022-04-29 08:51++from django.db import migrations+from django.db.models import F+import uuid++BATCH_SIZE = 10000+++def set_checkout_line_token_and_created_at(apps, _schema_editor):+ CheckoutLine = apps.get_model("checkout", "CheckoutLine")+ Checkout = apps.get_model("checkout", "Checkout")++ queryset = CheckoutLine.objects.filter(token__isnull=True)+ for batch_pks in queryset_in_batches(queryset):+ checkout_lines = CheckoutLine.objects.filter(pk__in=batch_pks)+ checkout_in_bulk = Checkout.objects.in_bulk(+ checkout_lines.values_list("checkout_id", flat=True)+ )+ for checkout_line in checkout_lines:+ checkout = checkout_in_bulk.get(checkout_line.checkout_id)+ checkout_line.created_at = checkout.created_at.replace(+ second=checkout_line.pk+ )+ checkout_line.token = uuid.uuid4()++ CheckoutLine.objects.bulk_update(checkout_lines, ["token", "created_at"])+++def queryset_in_batches(queryset):+ """Slice a queryset into batches.++ Input queryset should be sorted be pk.+ """+ start_pk = 0++ while True:+ qs = queryset.order_by("pk").filter(pk__gt=start_pk)[:BATCH_SIZE]+ pks = list(qs.values_list("pk", flat=True))++ if not pks:+ break++ yield pks++ start_pk = pks[-1]+++def set_checkout_line_old_id(apps, schema_editor):+ CheckoutLine = apps.get_model("checkout", "CheckoutLine")+ CheckoutLine.objects.all().update(old_id=F("id"))+++class Migration(migrations.Migration):++ dependencies = [+ ("checkout", "0043_add_token_old_id_created_at_to_checkout_line"),+ ]++ operations = [+ migrations.RunPython(+ set_checkout_line_token_and_created_at,+ migrations.RunPython.noop,+ ),+ migrations.RunPython(+ set_checkout_line_old_id, reverse_code=migrations.RunPython.noop+ ),+ ]diff --git a/saleor/checkout/migrations/0045_alter_checkoutline_created_at.py b/saleor/checkout/migrations/0045_alter_checkoutline_created_at.pynew file mode 100644index 0000000000..e22003db4a--- /dev/null+++ b/saleor/checkout/migrations/0045_alter_checkoutline_created_at.py@@ -0,0 +1,18 @@+# Generated by Django 3.2.13 on 2022-04-29 09:01++from django.db import migrations, models+++class Migration(migrations.Migration):++ dependencies = [+ ("checkout", "0044_fulfill_checkout_line_new_fields"),+ ]++ operations = [+ migrations.AlterField(+ model_name="checkoutline",+ name="created_at",+ field=models.DateTimeField(auto_now_add=True),+ ),+ ]diff --git a/saleor/checkout/migrations/0046_alter_checkout_line_pk.py b/saleor/checkout/migrations/0046_alter_checkout_line_pk.pynew file mode 100644index 0000000000..119aeb1bd6--- /dev/null+++ b/saleor/checkout/migrations/0046_alter_checkout_line_pk.py@@ -0,0 +1,39 @@+# Generated by Django 3.2.13 on 2022-04-29 09:44++from django.db import migrations, models+import uuid+++class Migration(migrations.Migration):++ dependencies = [+ ("checkout", "0045_alter_checkoutline_created_at"),+ ("warehouse", "0027_alter_reservation_models_checkout_line"),+ ]++ operations = [+ migrations.AlterModelOptions(+ name="checkoutline",+ options={"ordering": ("created_at",)},+ ),+ migrations.RemoveField(+ model_name="checkoutline",+ name="id",+ ),+ migrations.AlterField(+ model_name="checkoutline",+ name="token",+ field=models.UUIDField(+ default=uuid.uuid4,+ editable=False,+ primary_key=True,+ serialize=False,+ unique=True,+ ),+ ),+ migrations.RenameField(+ model_name="checkoutline",+ old_name="token",+ new_name="id",+ ),+ ]diff --git a/saleor/checkout/models.py b/saleor/checkout/models.pyindex 74aa3b9da2..7ed17ac44f 100644--- a/saleor/checkout/models.py+++ b/saleor/checkout/models.py@@ -178,6 +178,9 @@ class CheckoutLine(models.Model):their `data` field is different."""+ id = models.UUIDField(primary_key=True, editable=False, unique=True, default=uuid4)+ old_id = models.PositiveIntegerField(unique=True, null=True, blank=True)+ created_at = models.DateTimeField(auto_now_add=True)checkout = models.ForeignKey(Checkout, related_name="lines", on_delete=models.CASCADE)@@ -193,7 +196,7 @@ class CheckoutLine(models.Model):)class Meta:- ordering = ("id",)+ ordering = ("created_at",)def __str__(self):return smart_str(self.variant)diff --git a/saleor/graphql/checkout/tests/deprecated/test_checkout_line_delete.py b/saleor/graphql/checkout/tests/deprecated/test_checkout_line_delete.pyindex 142e0fd236..1a95c4baf5 100644--- a/saleor/graphql/checkout/tests/deprecated/test_checkout_line_delete.py+++ b/saleor/graphql/checkout/tests/deprecated/test_checkout_line_delete.py@@ -99,3 +99,37 @@ def test_checkout_line_delete_both_token_and_id_given(assert len(data["errors"]) == 1assert not data["checkout"]assert data["errors"][0]["code"] == CheckoutErrorCode.GRAPHQL_ERROR.name+++ "saleor.graphql.checkout.mutations.checkout_line_delete."+ "update_checkout_shipping_method_if_invalid",+ wraps=update_checkout_shipping_method_if_invalid,+)+def test_checkout_line_delete_by_old_line_id(+ mocked_update_shipping_method, user_api_client, checkout_with_item+):+ checkout = checkout_with_item+ lines, _ = fetch_checkout_lines(checkout)+ assert calculate_checkout_quantity(lines) == 3+ assert checkout.lines.count() == 1+ line = checkout.lines.first()+ line.old_id = 1+ line.save(update_fields=["old_id"])+ assert line.quantity == 3++ line_id = graphene.Node.to_global_id("CheckoutLine", line.pk)++ variables = {"token": checkout.token, "lineId": line_id}+ response = user_api_client.post_graphql(MUTATION_CHECKOUT_LINES_DELETE, variables)+ content = get_graphql_content(response)++ data = content["data"]["checkoutLineDelete"]+ assert not data["errors"]+ checkout.refresh_from_db()+ lines, _ = fetch_checkout_lines(checkout)+ assert checkout.lines.count() == 0+ assert calculate_checkout_quantity(lines) == 0+ manager = get_plugins_manager()+ checkout_info = fetch_checkout_info(checkout, lines, [], manager)+ mocked_update_shipping_method.assert_called_once_with(checkout_info, lines)diff --git a/saleor/graphql/checkout/tests/deprecated/test_checkout_lines_add.py b/saleor/graphql/checkout/tests/deprecated/test_checkout_lines_add.pyindex 2c5c91082e..3ad48162f2 100644--- a/saleor/graphql/checkout/tests/deprecated/test_checkout_lines_add.py+++ b/saleor/graphql/checkout/tests/deprecated/test_checkout_lines_add.py@@ -60,7 +60,7 @@ def test_checkout_lines_add_by_checkout_id(assert not data["errors"]checkout.refresh_from_db()lines, _ = fetch_checkout_lines(checkout)- line = checkout.lines.latest("pk")+ line = checkout.lines.last()assert line.variant == variantassert line.quantity == 1assert calculate_checkout_quantity(lines) == 4diff --git a/saleor/graphql/checkout/tests/test_checkout_lines.py b/saleor/graphql/checkout/tests/test_checkout_lines.pyindex 7b59b2aa22..c899b8c16b 100644--- a/saleor/graphql/checkout/tests/test_checkout_lines.py+++ b/saleor/graphql/checkout/tests/test_checkout_lines.py@@ -75,7 +75,7 @@ def test_checkout_lines_add(assert not data["errors"]checkout.refresh_from_db()lines, _ = fetch_checkout_lines(checkout)- line = checkout.lines.latest("pk")+ line = checkout.lines.last()assert line.variant == variantassert line.quantity == 1assert calculate_checkout_quantity(lines) == 4@@ -109,7 +109,7 @@ def test_checkout_lines_add_with_reservations(assert not data["errors"]checkout.refresh_from_db()lines, _ = fetch_checkout_lines(checkout)- line = checkout.lines.latest("pk")+ line = checkout.lines.last()assert line.variant == variantassert line.quantity == 1assert calculate_checkout_quantity(lines) == 4@@ -144,7 +144,7 @@ def test_checkout_lines_add_updates_reservation(assert not data["errors"]checkout.refresh_from_db()lines, _ = fetch_checkout_lines(checkout)- line = checkout.lines.latest("pk")+ line = checkout.lines.last()assert line.variant == variantassert line.quantity == 3assert calculate_checkout_quantity(lines) == 3@@ -232,7 +232,7 @@ def test_checkout_lines_add_existing_variant(user_api_client, checkout_with_itemdata = content["data"]["checkoutLinesAdd"]assert not data["errors"]checkout.refresh_from_db()- line = checkout.lines.latest("pk")+ line = checkout.lines.last()assert line.quantity == 10assert checkout.last_change != previous_last_change@@ -258,7 +258,7 @@ def test_checkout_lines_add_custom_price(data = content["data"]["checkoutLinesAdd"]assert not data["errors"]checkout.refresh_from_db()- line = checkout.lines.latest("pk")+ line = checkout.lines.last()assert line.variant == variantassert line.quantity == 1assert line.price_override == price@@ -286,7 +286,7 @@ def test_checkout_lines_add_existing_variant_with_custom_price(data = content["data"]["checkoutLinesAdd"]assert not data["errors"]checkout.refresh_from_db()- line = checkout.lines.latest("pk")+ line = checkout.lines.last()assert line.quantity == 10assert line.price_override == price@@ -316,7 +316,7 @@ def test_checkout_lines_add_existing_variant_override_previous_custom_price(data = content["data"]["checkoutLinesAdd"]assert not data["errors"]checkout.refresh_from_db()- line = checkout.lines.latest("pk")+ line = checkout.lines.last()assert line.quantity == 10assert line.price_override == pricediff --git a/saleor/graphql/core/types/common.py b/saleor/graphql/core/types/common.pyindex 54f6888d4b..48ce1af661 100644--- a/saleor/graphql/core/types/common.py+++ b/saleor/graphql/core/types/common.py@@ -49,7 +49,7 @@ from .money import VAT# deprecated - this is temporary constant that contains the graphql types# which has double id available - uuid and old int id-TYPES_WITH_DOUBLE_ID_AVAILABLE = ["Order", "OrderLine"]+TYPES_WITH_DOUBLE_ID_AVAILABLE = ["Order", "OrderLine", "CheckoutLine"]class NonNullList(graphene.List):diff --git a/saleor/warehouse/migrations/0025_add_checkout_line_token.py b/saleor/warehouse/migrations/0025_add_checkout_line_token.pynew file mode 100644index 0000000000..7ce239fb64--- /dev/null+++ b/saleor/warehouse/migrations/0025_add_checkout_line_token.py@@ -0,0 +1,23 @@+# Generated by Django 3.2.13 on 2022-04-29 09:07++from django.db import migrations, models+++class Migration(migrations.Migration):++ dependencies = [+ ("warehouse", "0024_rewrite_order_line_relations"),+ ]++ operations = [+ migrations.AddField(+ model_name="preorderreservation",+ name="checkout_line_token",+ field=models.UUIDField(null=True),+ ),+ migrations.AddField(+ model_name="reservation",+ name="checkout_line_token",+ field=models.UUIDField(null=True),+ ),+ ]diff --git a/saleor/warehouse/migrations/0026_fulfill_checkout_line_token_fields.py b/saleor/warehouse/migrations/0026_fulfill_checkout_line_token_fields.pynew file mode 100644index 0000000000..99bc6cdf07--- /dev/null+++ b/saleor/warehouse/migrations/0026_fulfill_checkout_line_token_fields.py@@ -0,0 +1,44 @@+# Generated by Django 3.2.13 on 2022-04-29 09:35++from django.db import migrations+++class Migration(migrations.Migration):++ dependencies = [+ ("warehouse", "0025_add_checkout_line_token"),+ ("checkout", "0044_fulfill_checkout_line_new_fields"),+ ]++ operations = [+ migrations.RunSQL(+ """+ UPDATE warehouse_preorderreservation+ SET checkout_line_token = (+ SELECT token+ FROM checkout_checkoutline+ WHERE (+ warehouse_preorderreservation.checkout_line_id+ = checkout_checkoutline.id+ )+ )+ WHERE checkout_line_id IS NOT NULL;+ """,+ reverse_sql=migrations.RunSQL.noop,+ ),+ migrations.RunSQL(+ """+ UPDATE warehouse_reservation+ SET checkout_line_token = (+ SELECT token+ FROM checkout_checkoutline+ WHERE (+ warehouse_reservation.checkout_line_id+ = checkout_checkoutline.id+ )+ )+ WHERE checkout_line_id IS NOT NULL;+ """,+ reverse_sql=migrations.RunSQL.noop,+ ),+ ]diff --git a/saleor/warehouse/migrations/0027_alter_reservation_models_checkout_line.py b/saleor/warehouse/migrations/0027_alter_reservation_models_checkout_line.pynew file mode 100644index 0000000000..5f28eaa0c9--- /dev/null+++ b/saleor/warehouse/migrations/0027_alter_reservation_models_checkout_line.py@@ -0,0 +1,34 @@+# Generated by Django 3.2.13 on 2022-04-29 09:46++from django.db import migrations, models+import django.db.models.deletion+++class Migration(migrations.Migration):++ dependencies = [+ ("warehouse", "0026_fulfill_checkout_line_token_fields"),+ ]++ operations = [+ migrations.AlterField(+ model_name="preorderreservation",+ name="checkout_line",+ field=models.ForeignKey(+ on_delete=django.db.models.deletion.CASCADE,+ related_name="preorder_reservations",+ to="checkout.checkoutline",+ to_field="old_id",+ ),+ ),+ migrations.AlterField(+ model_name="reservation",+ name="checkout_line",+ field=models.ForeignKey(+ on_delete=django.db.models.deletion.CASCADE,+ related_name="reservations",+ to="checkout.checkoutline",+ to_field="old_id",+ ),+ ),+ ]diff --git a/saleor/warehouse/migrations/0028_rewrite_checkouline_relations.py b/saleor/warehouse/migrations/0028_rewrite_checkouline_relations.pynew file mode 100644index 0000000000..c98c46b380--- /dev/null+++ b/saleor/warehouse/migrations/0028_rewrite_checkouline_relations.py@@ -0,0 +1,65 @@+# Generated by Django 3.2.13 on 2022-04-29 09:47++from django.db import migrations, models+import django.db.models.deletion+++class Migration(migrations.Migration):++ dependencies = [+ ("checkout", "0046_alter_checkout_line_pk"),+ ("warehouse", "0027_alter_reservation_models_checkout_line"),+ ]++ operations = [+ migrations.AlterField(+ model_name="preorderreservation",+ name="checkout_line_token",+ field=models.ForeignKey(+ on_delete=django.db.models.deletion.CASCADE, to="checkout.checkoutline"+ ),+ ),+ migrations.AlterField(+ model_name="reservation",+ name="checkout_line_token",+ field=models.ForeignKey(+ on_delete=django.db.models.deletion.CASCADE, to="checkout.checkoutline"+ ),+ ),+ migrations.RemoveField(+ model_name="preorderreservation",+ name="checkout_line",+ ),+ migrations.RemoveField(+ model_name="reservation",+ name="checkout_line",+ ),+ migrations.RenameField(+ model_name="preorderreservation",+ old_name="checkout_line_token",+ new_name="checkout_line",+ ),+ migrations.RenameField(+ model_name="reservation",+ old_name="checkout_line_token",+ new_name="checkout_line",+ ),+ migrations.AlterField(+ model_name="preorderreservation",+ name="checkout_line",+ field=models.ForeignKey(+ on_delete=django.db.models.deletion.CASCADE,+ related_name="preorder_reservations",+ to="checkout.checkoutline",+ ),+ ),+ migrations.AlterField(+ model_name="reservation",+ name="checkout_line",+ field=models.ForeignKey(+ on_delete=django.db.models.deletion.CASCADE,+ related_name="reservations",+ to="checkout.checkoutline",+ ),+ ),+ ]