From 8c78708174aef9d5c9adccd5149c83e81f3a86e9 Mon Sep 17 00:00:00 2001 From: olivermrbl Date: Mon, 22 Jan 2024 13:51:20 +0100 Subject: [PATCH 1/5] feat(cart): Migration file --- .../src/migrations/.snapshot-medusa-cart.json | 1308 +++++++++++++++++ .../CartModuleSetup20240122122952.ts | 98 ++ 2 files changed, 1406 insertions(+) create mode 100644 packages/cart/src/migrations/.snapshot-medusa-cart.json create mode 100644 packages/cart/src/migrations/CartModuleSetup20240122122952.ts diff --git a/packages/cart/src/migrations/.snapshot-medusa-cart.json b/packages/cart/src/migrations/.snapshot-medusa-cart.json new file mode 100644 index 0000000000000..1a4401ec62e4f --- /dev/null +++ b/packages/cart/src/migrations/.snapshot-medusa-cart.json @@ -0,0 +1,1308 @@ +{ + "namespaces": [ + "public" + ], + "name": "public", + "tables": [ + { + "columns": { + "id": { + "name": "id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "customer_id": { + "name": "customer_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "company": { + "name": "company", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "first_name": { + "name": "first_name", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "last_name": { + "name": "last_name", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "address_1": { + "name": "address_1", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "address_2": { + "name": "address_2", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "city": { + "name": "city", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "country_code": { + "name": "country_code", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "province": { + "name": "province", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "postal_code": { + "name": "postal_code", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "phone": { + "name": "phone", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "metadata": { + "name": "metadata", + "type": "jsonb", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "json" + }, + "created_at": { + "name": "created_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "length": 6, + "default": "now()", + "mappedType": "datetime" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "length": 6, + "default": "now()", + "mappedType": "datetime" + } + }, + "name": "cart_address", + "schema": "public", + "indexes": [ + { + "keyName": "cart_address_pkey", + "columnNames": [ + "id" + ], + "composite": false, + "primary": true, + "unique": true + } + ], + "checks": [], + "foreignKeys": {} + }, + { + "columns": { + "id": { + "name": "id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "region_id": { + "name": "region_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "customer_id": { + "name": "customer_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "sales_channel_id": { + "name": "sales_channel_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "email": { + "name": "email", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "currency_code": { + "name": "currency_code", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "shipping_address_id": { + "name": "shipping_address_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "billing_address_id": { + "name": "billing_address_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "metadata": { + "name": "metadata", + "type": "jsonb", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "json" + }, + "created_at": { + "name": "created_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "length": 6, + "default": "now()", + "mappedType": "datetime" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "length": 6, + "default": "now()", + "mappedType": "datetime" + }, + "deleted_at": { + "name": "deleted_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "length": 6, + "mappedType": "datetime" + } + }, + "name": "cart", + "schema": "public", + "indexes": [ + { + "columnNames": [ + "customer_id" + ], + "composite": false, + "keyName": "IDX_cart_customer_id", + "primary": false, + "unique": false + }, + { + "columnNames": [ + "shipping_address_id" + ], + "composite": false, + "keyName": "IDX_cart_shipping_address_id", + "primary": false, + "unique": false + }, + { + "columnNames": [ + "billing_address_id" + ], + "composite": false, + "keyName": "IDX_cart_billing_address_id", + "primary": false, + "unique": false + }, + { + "keyName": "cart_pkey", + "columnNames": [ + "id" + ], + "composite": false, + "primary": true, + "unique": true + } + ], + "checks": [], + "foreignKeys": { + "cart_shipping_address_id_foreign": { + "constraintName": "cart_shipping_address_id_foreign", + "columnNames": [ + "shipping_address_id" + ], + "localTableName": "public.cart", + "referencedColumnNames": [ + "id" + ], + "referencedTableName": "public.cart_address", + "deleteRule": "set null", + "updateRule": "cascade" + }, + "cart_billing_address_id_foreign": { + "constraintName": "cart_billing_address_id_foreign", + "columnNames": [ + "billing_address_id" + ], + "localTableName": "public.cart", + "referencedColumnNames": [ + "id" + ], + "referencedTableName": "public.cart_address", + "deleteRule": "set null", + "updateRule": "cascade" + } + } + }, + { + "columns": { + "id": { + "name": "id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "cart_id": { + "name": "cart_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "title": { + "name": "title", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "subtitle": { + "name": "subtitle", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "thumbnail": { + "name": "thumbnail", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "quantity": { + "name": "quantity", + "type": "integer", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "integer" + }, + "variant_id": { + "name": "variant_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "product_id": { + "name": "product_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "product_title": { + "name": "product_title", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "product_description": { + "name": "product_description", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "product_subtitle": { + "name": "product_subtitle", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "product_type": { + "name": "product_type", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "product_collection": { + "name": "product_collection", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "product_handle": { + "name": "product_handle", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "variant_sku": { + "name": "variant_sku", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "variant_barcode": { + "name": "variant_barcode", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "variant_title": { + "name": "variant_title", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "variant_option_values": { + "name": "variant_option_values", + "type": "jsonb", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "json" + }, + "requires_shipping": { + "name": "requires_shipping", + "type": "boolean", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "default": "true", + "mappedType": "boolean" + }, + "is_discountable": { + "name": "is_discountable", + "type": "boolean", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "default": "true", + "mappedType": "boolean" + }, + "is_tax_inclusive": { + "name": "is_tax_inclusive", + "type": "boolean", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "default": "false", + "mappedType": "boolean" + }, + "compare_at_unit_price": { + "name": "compare_at_unit_price", + "type": "numeric", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "decimal" + }, + "unit_price": { + "name": "unit_price", + "type": "numeric", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "decimal" + }, + "created_at": { + "name": "created_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "length": 6, + "default": "now()", + "mappedType": "datetime" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "length": 6, + "default": "now()", + "mappedType": "datetime" + } + }, + "name": "cart_line_item", + "schema": "public", + "indexes": [ + { + "columnNames": [ + "cart_id" + ], + "composite": false, + "keyName": "IDX_line_item_cart_id", + "primary": false, + "unique": false + }, + { + "columnNames": [ + "variant_id" + ], + "composite": false, + "keyName": "IDX_line_item_variant_id", + "primary": false, + "unique": false + }, + { + "keyName": "cart_line_item_pkey", + "columnNames": [ + "id" + ], + "composite": false, + "primary": true, + "unique": true + } + ], + "checks": [ + { + "name": "cart_line_item_unit_price_check", + "expression": "unit_price >= 0", + "definition": "check ((unit_price >= 0))", + "columnName": "unit_price" + } + ], + "foreignKeys": { + "cart_line_item_cart_id_foreign": { + "constraintName": "cart_line_item_cart_id_foreign", + "columnNames": [ + "cart_id" + ], + "localTableName": "public.cart_line_item", + "referencedColumnNames": [ + "id" + ], + "referencedTableName": "public.cart", + "deleteRule": "cascade", + "updateRule": "cascade" + } + } + }, + { + "columns": { + "id": { + "name": "id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "description": { + "name": "description", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "promotion_id": { + "name": "promotion_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "code": { + "name": "code", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "amount": { + "name": "amount", + "type": "numeric", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "decimal" + }, + "provider_id": { + "name": "provider_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "created_at": { + "name": "created_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "length": 6, + "default": "now()", + "mappedType": "datetime" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "length": 6, + "default": "now()", + "mappedType": "datetime" + }, + "item_id": { + "name": "item_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + } + }, + "name": "cart_line_item_adjustment", + "schema": "public", + "indexes": [ + { + "columnNames": [ + "item_id" + ], + "composite": false, + "keyName": "IDX_adjustment_item_id", + "primary": false, + "unique": false + }, + { + "keyName": "cart_line_item_adjustment_pkey", + "columnNames": [ + "id" + ], + "composite": false, + "primary": true, + "unique": true + } + ], + "checks": [ + { + "name": "cart_line_item_adjustment_check", + "expression": "amount >= 0", + "definition": "check ((amount >= 0))" + } + ], + "foreignKeys": { + "cart_line_item_adjustment_item_id_foreign": { + "constraintName": "cart_line_item_adjustment_item_id_foreign", + "columnNames": [ + "item_id" + ], + "localTableName": "public.cart_line_item_adjustment", + "referencedColumnNames": [ + "id" + ], + "referencedTableName": "public.cart_line_item", + "deleteRule": "cascade", + "updateRule": "cascade" + } + } + }, + { + "columns": { + "id": { + "name": "id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "description": { + "name": "description", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "tax_rate_id": { + "name": "tax_rate_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "code": { + "name": "code", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "rate": { + "name": "rate", + "type": "numeric", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "decimal" + }, + "provider_id": { + "name": "provider_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "created_at": { + "name": "created_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "length": 6, + "default": "now()", + "mappedType": "datetime" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "length": 6, + "default": "now()", + "mappedType": "datetime" + }, + "item_id": { + "name": "item_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + } + }, + "name": "cart_line_item_tax_line", + "schema": "public", + "indexes": [ + { + "columnNames": [ + "item_id" + ], + "composite": false, + "keyName": "IDX_tax_line_item_id", + "primary": false, + "unique": false + }, + { + "keyName": "cart_line_item_tax_line_pkey", + "columnNames": [ + "id" + ], + "composite": false, + "primary": true, + "unique": true + } + ], + "checks": [], + "foreignKeys": { + "cart_line_item_tax_line_item_id_foreign": { + "constraintName": "cart_line_item_tax_line_item_id_foreign", + "columnNames": [ + "item_id" + ], + "localTableName": "public.cart_line_item_tax_line", + "referencedColumnNames": [ + "id" + ], + "referencedTableName": "public.cart_line_item", + "deleteRule": "cascade", + "updateRule": "cascade" + } + } + }, + { + "columns": { + "id": { + "name": "id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "cart_id": { + "name": "cart_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "name": { + "name": "name", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "description": { + "name": "description", + "type": "jsonb", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "json" + }, + "amount": { + "name": "amount", + "type": "numeric", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "decimal" + }, + "is_tax_inclusive": { + "name": "is_tax_inclusive", + "type": "boolean", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "default": "false", + "mappedType": "boolean" + }, + "shipping_option_id": { + "name": "shipping_option_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "data": { + "name": "data", + "type": "jsonb", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "json" + }, + "metadata": { + "name": "metadata", + "type": "jsonb", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "json" + }, + "created_at": { + "name": "created_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "length": 6, + "default": "now()", + "mappedType": "datetime" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "length": 6, + "default": "now()", + "mappedType": "datetime" + } + }, + "name": "cart_shipping_method", + "schema": "public", + "indexes": [ + { + "columnNames": [ + "cart_id" + ], + "composite": false, + "keyName": "IDX_shipping_method_cart_id", + "primary": false, + "unique": false + }, + { + "keyName": "cart_shipping_method_pkey", + "columnNames": [ + "id" + ], + "composite": false, + "primary": true, + "unique": true + } + ], + "checks": [ + { + "name": "cart_shipping_method_check", + "expression": "amount >= 0", + "definition": "check ((amount >= 0))" + } + ], + "foreignKeys": { + "cart_shipping_method_cart_id_foreign": { + "constraintName": "cart_shipping_method_cart_id_foreign", + "columnNames": [ + "cart_id" + ], + "localTableName": "public.cart_shipping_method", + "referencedColumnNames": [ + "id" + ], + "referencedTableName": "public.cart", + "deleteRule": "cascade", + "updateRule": "cascade" + } + } + }, + { + "columns": { + "id": { + "name": "id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "description": { + "name": "description", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "promotion_id": { + "name": "promotion_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "code": { + "name": "code", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "amount": { + "name": "amount", + "type": "numeric", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "decimal" + }, + "provider_id": { + "name": "provider_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "created_at": { + "name": "created_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "length": 6, + "default": "now()", + "mappedType": "datetime" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "length": 6, + "default": "now()", + "mappedType": "datetime" + }, + "shipping_method_id": { + "name": "shipping_method_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + } + }, + "name": "cart_shipping_method_adjustment", + "schema": "public", + "indexes": [ + { + "columnNames": [ + "shipping_method_id" + ], + "composite": false, + "keyName": "IDX_adjustment_shipping_method_id", + "primary": false, + "unique": false + }, + { + "keyName": "cart_shipping_method_adjustment_pkey", + "columnNames": [ + "id" + ], + "composite": false, + "primary": true, + "unique": true + } + ], + "checks": [], + "foreignKeys": { + "cart_shipping_method_adjustment_shipping_method_id_foreign": { + "constraintName": "cart_shipping_method_adjustment_shipping_method_id_foreign", + "columnNames": [ + "shipping_method_id" + ], + "localTableName": "public.cart_shipping_method_adjustment", + "referencedColumnNames": [ + "id" + ], + "referencedTableName": "public.cart_shipping_method", + "deleteRule": "cascade", + "updateRule": "cascade" + } + } + }, + { + "columns": { + "id": { + "name": "id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "description": { + "name": "description", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "tax_rate_id": { + "name": "tax_rate_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "code": { + "name": "code", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "rate": { + "name": "rate", + "type": "numeric", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "decimal" + }, + "provider_id": { + "name": "provider_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "created_at": { + "name": "created_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "length": 6, + "default": "now()", + "mappedType": "datetime" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "length": 6, + "default": "now()", + "mappedType": "datetime" + }, + "shipping_method_id": { + "name": "shipping_method_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + } + }, + "name": "cart_shipping_method_tax_line", + "schema": "public", + "indexes": [ + { + "columnNames": [ + "shipping_method_id" + ], + "composite": false, + "keyName": "IDX_tax_line_shipping_method_id", + "primary": false, + "unique": false + }, + { + "keyName": "cart_shipping_method_tax_line_pkey", + "columnNames": [ + "id" + ], + "composite": false, + "primary": true, + "unique": true + } + ], + "checks": [], + "foreignKeys": { + "cart_shipping_method_tax_line_shipping_method_id_foreign": { + "constraintName": "cart_shipping_method_tax_line_shipping_method_id_foreign", + "columnNames": [ + "shipping_method_id" + ], + "localTableName": "public.cart_shipping_method_tax_line", + "referencedColumnNames": [ + "id" + ], + "referencedTableName": "public.cart_shipping_method", + "deleteRule": "cascade", + "updateRule": "cascade" + } + } + } + ] +} diff --git a/packages/cart/src/migrations/CartModuleSetup20240122122952.ts b/packages/cart/src/migrations/CartModuleSetup20240122122952.ts new file mode 100644 index 0000000000000..6cbe7bb62b08f --- /dev/null +++ b/packages/cart/src/migrations/CartModuleSetup20240122122952.ts @@ -0,0 +1,98 @@ +import { Migration } from "@mikro-orm/migrations" + +export class CartModuleSetup20240122122952 extends Migration { + async up(): Promise { + this.addSql( + 'create table "cart_address" ("id" text not null, "customer_id" text null, "company" text null, "first_name" text null, "last_name" text null, "address_1" text null, "address_2" text null, "city" text null, "country_code" text null, "province" text null, "postal_code" text null, "phone" text null, "metadata" jsonb null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), constraint "cart_address_pkey" primary key ("id"));' + ) + + this.addSql( + 'create table "cart" ("id" text not null, "region_id" text null, "customer_id" text null, "sales_channel_id" text null, "email" text null, "currency_code" text not null, "shipping_address_id" text null, "billing_address_id" text null, "metadata" jsonb null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "cart_pkey" primary key ("id"));' + ) + this.addSql( + 'create index "IDX_cart_customer_id" on "cart" ("customer_id");' + ) + this.addSql( + 'create index "IDX_cart_shipping_address_id" on "cart" ("shipping_address_id");' + ) + this.addSql( + 'create index "IDX_cart_billing_address_id" on "cart" ("billing_address_id");' + ) + + this.addSql( + 'create table "cart_line_item" ("id" text not null, "cart_id" text not null, "title" text not null, "subtitle" text null, "thumbnail" text null, "quantity" integer not null, "variant_id" text null, "product_id" text null, "product_title" text null, "product_description" text null, "product_subtitle" text null, "product_type" text null, "product_collection" text null, "product_handle" text null, "variant_sku" text null, "variant_barcode" text null, "variant_title" text null, "variant_option_values" jsonb null, "requires_shipping" boolean not null default true, "is_discountable" boolean not null default true, "is_tax_inclusive" boolean not null default false, "compare_at_unit_price" numeric null, "unit_price" numeric not null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), constraint "cart_line_item_pkey" primary key ("id"), constraint cart_line_item_unit_price_check check (unit_price >= 0));' + ) + this.addSql( + 'create index "IDX_line_item_cart_id" on "cart_line_item" ("cart_id");' + ) + this.addSql( + 'create index "IDX_line_item_variant_id" on "cart_line_item" ("variant_id");' + ) + + this.addSql( + 'create table "cart_line_item_adjustment" ("id" text not null, "description" text null, "promotion_id" text null, "code" text null, "amount" numeric not null, "provider_id" text null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "item_id" text null, constraint "cart_line_item_adjustment_pkey" primary key ("id"), constraint cart_line_item_adjustment_check check (amount >= 0));' + ) + this.addSql( + 'create index "IDX_adjustment_item_id" on "cart_line_item_adjustment" ("item_id");' + ) + + this.addSql( + 'create table "cart_line_item_tax_line" ("id" text not null, "description" text null, "tax_rate_id" text null, "code" text not null, "rate" numeric not null, "provider_id" text null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "item_id" text null, constraint "cart_line_item_tax_line_pkey" primary key ("id"));' + ) + this.addSql( + 'create index "IDX_tax_line_item_id" on "cart_line_item_tax_line" ("item_id");' + ) + + this.addSql( + 'create table "cart_shipping_method" ("id" text not null, "cart_id" text not null, "name" text not null, "description" jsonb null, "amount" numeric not null, "is_tax_inclusive" boolean not null default false, "shipping_option_id" text null, "data" jsonb null, "metadata" jsonb null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), constraint "cart_shipping_method_pkey" primary key ("id"), constraint cart_shipping_method_check check (amount >= 0));' + ) + this.addSql( + 'create index "IDX_shipping_method_cart_id" on "cart_shipping_method" ("cart_id");' + ) + + this.addSql( + 'create table "cart_shipping_method_adjustment" ("id" text not null, "description" text null, "promotion_id" text null, "code" text null, "amount" numeric not null, "provider_id" text null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "shipping_method_id" text null, constraint "cart_shipping_method_adjustment_pkey" primary key ("id"));' + ) + this.addSql( + 'create index "IDX_adjustment_shipping_method_id" on "cart_shipping_method_adjustment" ("shipping_method_id");' + ) + + this.addSql( + 'create table "cart_shipping_method_tax_line" ("id" text not null, "description" text null, "tax_rate_id" text null, "code" text not null, "rate" numeric not null, "provider_id" text null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "shipping_method_id" text null, constraint "cart_shipping_method_tax_line_pkey" primary key ("id"));' + ) + this.addSql( + 'create index "IDX_tax_line_shipping_method_id" on "cart_shipping_method_tax_line" ("shipping_method_id");' + ) + + this.addSql( + 'alter table "cart" add constraint "cart_shipping_address_id_foreign" foreign key ("shipping_address_id") references "cart_address" ("id") on update cascade on delete set null;' + ) + this.addSql( + 'alter table "cart" add constraint "cart_billing_address_id_foreign" foreign key ("billing_address_id") references "cart_address" ("id") on update cascade on delete set null;' + ) + + this.addSql( + 'alter table "cart_line_item" add constraint "cart_line_item_cart_id_foreign" foreign key ("cart_id") references "cart" ("id") on update cascade on delete cascade;' + ) + + this.addSql( + 'alter table "cart_line_item_adjustment" add constraint "cart_line_item_adjustment_item_id_foreign" foreign key ("item_id") references "cart_line_item" ("id") on update cascade on delete cascade;' + ) + + this.addSql( + 'alter table "cart_line_item_tax_line" add constraint "cart_line_item_tax_line_item_id_foreign" foreign key ("item_id") references "cart_line_item" ("id") on update cascade on delete cascade;' + ) + + this.addSql( + 'alter table "cart_shipping_method" add constraint "cart_shipping_method_cart_id_foreign" foreign key ("cart_id") references "cart" ("id") on update cascade on delete cascade;' + ) + + this.addSql( + 'alter table "cart_shipping_method_adjustment" add constraint "cart_shipping_method_adjustment_shipping_method_id_foreign" foreign key ("shipping_method_id") references "cart_shipping_method" ("id") on update cascade on delete cascade;' + ) + + this.addSql( + 'alter table "cart_shipping_method_tax_line" add constraint "cart_shipping_method_tax_line_shipping_method_id_foreign" foreign key ("shipping_method_id") references "cart_shipping_method" ("id") on update cascade on delete cascade;' + ) + } +} From d081a62b4bb08345dba7c39c86bd0b7e4afed99a Mon Sep 17 00:00:00 2001 From: olivermrbl Date: Fri, 26 Jan 2024 15:39:06 +0100 Subject: [PATCH 2/5] fixup migration --- .../CartModuleSetup20240122122952.ts | 253 ++++++++++++------ 1 file changed, 168 insertions(+), 85 deletions(-) diff --git a/packages/cart/src/migrations/CartModuleSetup20240122122952.ts b/packages/cart/src/migrations/CartModuleSetup20240122122952.ts index 6cbe7bb62b08f..dab87b844ed05 100644 --- a/packages/cart/src/migrations/CartModuleSetup20240122122952.ts +++ b/packages/cart/src/migrations/CartModuleSetup20240122122952.ts @@ -3,96 +3,179 @@ import { Migration } from "@mikro-orm/migrations" export class CartModuleSetup20240122122952 extends Migration { async up(): Promise { this.addSql( - 'create table "cart_address" ("id" text not null, "customer_id" text null, "company" text null, "first_name" text null, "last_name" text null, "address_1" text null, "address_2" text null, "city" text null, "country_code" text null, "province" text null, "postal_code" text null, "phone" text null, "metadata" jsonb null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), constraint "cart_address_pkey" primary key ("id"));' - ) + ` + CREATE TABLE IF NOT EXISTS "cart" ( + "id" TEXT NOT NULL, + "region_id" TEXT NULL, + "customer_id" TEXT NULL, + "sales_channel_id" TEXT NULL, + "email" TEXT NULL, + "currency_code" TEXT NOT NULL, + "shipping_address_id" TEXT NULL, + "billing_address_id" TEXT NULL, + "metadata" JSONB NULL, + "created_at" TIMESTAMPTZ NOT NULL DEFAULT NOW(), + "updated_at" TIMESTAMPTZ NOT NULL DEFAULT NOW(), + "deleted_at" TIMESTAMPTZ NULL, + CONSTRAINT "cart_pkey" PRIMARY KEY ("id") + ); + + ALTER TABLE "cart" ADD COLUMN IF NOT EXISTS "currency_code" TEXT NOT NULL; + + ALTER TABLE "cart" DROP CONSTRAINT IF EXISTS "FK_242205c81c1152fab1b6e848470"; + ALTER TABLE "cart" DROP CONSTRAINT IF EXISTS "FK_484c329f4783be4e18e5e2ff090"; + ALTER TABLE "cart" DROP CONSTRAINT IF EXISTS "FK_6b9c66b5e36f7c827dfaa092f94"; + ALTER TABLE "cart" DROP CONSTRAINT IF EXISTS "FK_9d1a161434c610aae7c3df2dc7e"; + ALTER TABLE "cart" DROP CONSTRAINT IF EXISTS "FK_a2bd3c26f42e754b9249ba78fd6"; + ALTER TABLE "cart" DROP CONSTRAINT IF EXISTS "FK_ced15a9a695d2b5db9dabce763d"; + + CREATE INDEX IF NOT EXISTS "IDX_cart_customer_id" ON "cart" ("customer_id"); + CREATE INDEX IF NOT EXISTS "IDX_cart_shipping_address_id" ON "cart" ("shipping_address_id"); + CREATE INDEX IF NOT EXISTS "IDX_cart_billing_address_id" ON "cart" ("billing_address_id"); + + CREATE TABLE IF NOT EXISTS "cart_address" ( + "id" TEXT NOT NULL, + "customer_id" TEXT NULL, + "company" TEXT NULL, + "first_name" TEXT NULL, + "last_name" TEXT NULL, + "address_1" TEXT NULL, + "address_2" TEXT NULL, + "city" TEXT NULL, + "country_code" TEXT NULL, + "province" TEXT NULL, + "postal_code" TEXT NULL, + "phone" TEXT NULL, + "metadata" JSONB NULL, + "created_at" TIMESTAMPTZ NOT NULL DEFAULT NOW(), + "updated_at" TIMESTAMPTZ NOT NULL DEFAULT NOW(), + CONSTRAINT "cart_address_pkey" PRIMARY KEY ("id") + ); + + CREATE TABLE IF NOT EXISTS "cart_line_item" ( + "id" TEXT NOT NULL, + "cart_id" TEXT NOT NULL, + "title" TEXT NOT NULL, + "subtitle" TEXT NULL, + "thumbnail" TEXT NULL, + "quantity" INTEGER NOT NULL, + "variant_id" TEXT NULL, + "product_id" TEXT NULL, + "product_title" TEXT NULL, + "product_description" TEXT NULL, + "product_subtitle" TEXT NULL, + "product_type" TEXT NULL, + "product_collection" TEXT NULL, + "product_handle" TEXT NULL, + "variant_sku" TEXT NULL, + "variant_barcode" TEXT NULL, + "variant_title" TEXT NULL, + "variant_option_values" JSONB NULL, + "requires_shipping" BOOLEAN NOT NULL DEFAULT TRUE, + "is_discountable" BOOLEAN NOT NULL DEFAULT TRUE, + "is_tax_inclusive" BOOLEAN NOT NULL DEFAULT FALSE, + "compare_at_unit_price" NUMERIC NULL, + "unit_price" NUMERIC NOT NULL, + "created_at" TIMESTAMPTZ NOT NULL DEFAULT NOW(), + "updated_at" TIMESTAMPTZ NOT NULL DEFAULT NOW(), + CONSTRAINT "cart_line_item_pkey" PRIMARY KEY ("id"), + CONSTRAINT cart_line_item_unit_price_check CHECK (unit_price >= 0) + ); + + ALTER TABLE "cart" ADD CONSTRAINT "cart_shipping_address_id_foreign" FOREIGN KEY ("shipping_address_id") REFERENCES "cart_address" ("id") ON UPDATE CASCADE ON DELETE SET NULL; + ALTER TABLE "cart" ADD CONSTRAINT "cart_billing_address_id_foreign" FOREIGN KEY ("billing_address_id") REFERENCES "cart_address" ("id") ON UPDATE CASCADE ON DELETE SET NULL; + + CREATE INDEX IF NOT EXISTS "IDX_line_item_cart_id" ON "cart_line_item" ("cart_id"); + + CREATE INDEX IF NOT EXISTS "IDX_line_item_variant_id" ON "cart_line_item" ("variant_id"); + + + CREATE TABLE IF NOT EXISTS "cart_line_item_adjustment" ( + "id" TEXT NOT NULL, + "description" TEXT NULL, + "promotion_id" TEXT NULL, + "code" TEXT NULL, + "amount" NUMERIC NOT NULL, + "provider_id" TEXT NULL, + "created_at" TIMESTAMPTZ NOT NULL DEFAULT NOW(), + "updated_at" TIMESTAMPTZ NOT NULL DEFAULT NOW(), + "item_id" TEXT NULL, + CONSTRAINT "cart_line_item_adjustment_pkey" PRIMARY KEY ("id"), + CONSTRAINT cart_line_item_adjustment_check CHECK (amount >= 0) + ); + + CREATE INDEX IF NOT EXISTS "IDX_adjustment_item_id" ON "cart_line_item_adjustment" ("item_id"); + + CREATE TABLE IF NOT EXISTS "cart_line_item_tax_line" ( + "id" TEXT NOT NULL, + "description" TEXT NULL, + "tax_rate_id" TEXT NULL, + "code" TEXT NOT NULL, + "rate" NUMERIC NOT NULL, + "provider_id" TEXT NULL, + "created_at" TIMESTAMPTZ NOT NULL DEFAULT NOW(), + "updated_at" TIMESTAMPTZ NOT NULL DEFAULT NOW(), + "item_id" TEXT NULL, + CONSTRAINT "cart_line_item_tax_line_pkey" PRIMARY KEY ("id") + ); + + CREATE INDEX IF NOT EXISTS "IDX_tax_line_item_id" ON "cart_line_item_tax_line" ("item_id"); + + CREATE TABLE IF NOT EXISTS "cart_shipping_method" ( + "id" TEXT NOT NULL, + "cart_id" TEXT NOT NULL, + "name" TEXT NOT NULL, + "description" JSONB NULL, + "amount" NUMERIC NOT NULL, + "is_tax_inclusive" BOOLEAN NOT NULL DEFAULT FALSE, + "shipping_option_id" TEXT NULL, + "data" JSONB NULL, + "metadata" JSONB NULL, + "created_at" TIMESTAMPTZ NOT NULL DEFAULT NOW(), + "updated_at" TIMESTAMPTZ NOT NULL DEFAULT NOW(), + CONSTRAINT "cart_shipping_method_pkey" PRIMARY KEY ("id"), + CONSTRAINT cart_shipping_method_check CHECK (amount >= 0) + ); + + CREATE INDEX IF NOT EXISTS "IDX_shipping_method_cart_id" ON "cart_shipping_method" ("cart_id"); - this.addSql( - 'create table "cart" ("id" text not null, "region_id" text null, "customer_id" text null, "sales_channel_id" text null, "email" text null, "currency_code" text not null, "shipping_address_id" text null, "billing_address_id" text null, "metadata" jsonb null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "cart_pkey" primary key ("id"));' - ) - this.addSql( - 'create index "IDX_cart_customer_id" on "cart" ("customer_id");' - ) - this.addSql( - 'create index "IDX_cart_shipping_address_id" on "cart" ("shipping_address_id");' - ) - this.addSql( - 'create index "IDX_cart_billing_address_id" on "cart" ("billing_address_id");' - ) - - this.addSql( - 'create table "cart_line_item" ("id" text not null, "cart_id" text not null, "title" text not null, "subtitle" text null, "thumbnail" text null, "quantity" integer not null, "variant_id" text null, "product_id" text null, "product_title" text null, "product_description" text null, "product_subtitle" text null, "product_type" text null, "product_collection" text null, "product_handle" text null, "variant_sku" text null, "variant_barcode" text null, "variant_title" text null, "variant_option_values" jsonb null, "requires_shipping" boolean not null default true, "is_discountable" boolean not null default true, "is_tax_inclusive" boolean not null default false, "compare_at_unit_price" numeric null, "unit_price" numeric not null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), constraint "cart_line_item_pkey" primary key ("id"), constraint cart_line_item_unit_price_check check (unit_price >= 0));' - ) - this.addSql( - 'create index "IDX_line_item_cart_id" on "cart_line_item" ("cart_id");' - ) - this.addSql( - 'create index "IDX_line_item_variant_id" on "cart_line_item" ("variant_id");' - ) + CREATE TABLE IF NOT EXISTS "cart_shipping_method_adjustment" ( + "id" TEXT NOT NULL, + "description" TEXT NULL, + "promotion_id" TEXT NULL, + "code" TEXT NULL, + "amount" NUMERIC NOT NULL, + "provider_id" TEXT NULL, + "created_at" TIMESTAMPTZ NOT NULL DEFAULT NOW(), + "updated_at" TIMESTAMPTZ NOT NULL DEFAULT NOW(), + "shipping_method_id" TEXT NULL, + CONSTRAINT "cart_shipping_method_adjustment_pkey" PRIMARY KEY ("id") + ); - this.addSql( - 'create table "cart_line_item_adjustment" ("id" text not null, "description" text null, "promotion_id" text null, "code" text null, "amount" numeric not null, "provider_id" text null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "item_id" text null, constraint "cart_line_item_adjustment_pkey" primary key ("id"), constraint cart_line_item_adjustment_check check (amount >= 0));' - ) - this.addSql( - 'create index "IDX_adjustment_item_id" on "cart_line_item_adjustment" ("item_id");' - ) + CREATE INDEX IF NOT EXISTS "IDX_adjustment_shipping_method_id" ON "cart_shipping_method_adjustment" ("shipping_method_id"); - this.addSql( - 'create table "cart_line_item_tax_line" ("id" text not null, "description" text null, "tax_rate_id" text null, "code" text not null, "rate" numeric not null, "provider_id" text null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "item_id" text null, constraint "cart_line_item_tax_line_pkey" primary key ("id"));' - ) - this.addSql( - 'create index "IDX_tax_line_item_id" on "cart_line_item_tax_line" ("item_id");' - ) + CREATE TABLE IF NOT EXISTS "cart_shipping_method_tax_line" ( + "id" TEXT NOT NULL, + "description" TEXT NULL, + "tax_rate_id" TEXT NULL, + "code" TEXT NOT NULL, + "rate" NUMERIC NOT NULL, + "provider_id" TEXT NULL, + "created_at" TIMESTAMPTZ NOT NULL DEFAULT NOW(), + "updated_at" TIMESTAMPTZ NOT NULL DEFAULT NOW(), + "shipping_method_id" TEXT NULL, + CONSTRAINT "cart_shipping_method_tax_line_pkey" PRIMARY KEY ("id") + ); - this.addSql( - 'create table "cart_shipping_method" ("id" text not null, "cart_id" text not null, "name" text not null, "description" jsonb null, "amount" numeric not null, "is_tax_inclusive" boolean not null default false, "shipping_option_id" text null, "data" jsonb null, "metadata" jsonb null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), constraint "cart_shipping_method_pkey" primary key ("id"), constraint cart_shipping_method_check check (amount >= 0));' - ) - this.addSql( - 'create index "IDX_shipping_method_cart_id" on "cart_shipping_method" ("cart_id");' - ) + CREATE INDEX IF NOT EXISTS "IDX_tax_line_shipping_method_id" ON "cart_shipping_method_tax_line" ("shipping_method_id"); - this.addSql( - 'create table "cart_shipping_method_adjustment" ("id" text not null, "description" text null, "promotion_id" text null, "code" text null, "amount" numeric not null, "provider_id" text null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "shipping_method_id" text null, constraint "cart_shipping_method_adjustment_pkey" primary key ("id"));' - ) - this.addSql( - 'create index "IDX_adjustment_shipping_method_id" on "cart_shipping_method_adjustment" ("shipping_method_id");' - ) - - this.addSql( - 'create table "cart_shipping_method_tax_line" ("id" text not null, "description" text null, "tax_rate_id" text null, "code" text not null, "rate" numeric not null, "provider_id" text null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "shipping_method_id" text null, constraint "cart_shipping_method_tax_line_pkey" primary key ("id"));' - ) - this.addSql( - 'create index "IDX_tax_line_shipping_method_id" on "cart_shipping_method_tax_line" ("shipping_method_id");' - ) - - this.addSql( - 'alter table "cart" add constraint "cart_shipping_address_id_foreign" foreign key ("shipping_address_id") references "cart_address" ("id") on update cascade on delete set null;' - ) - this.addSql( - 'alter table "cart" add constraint "cart_billing_address_id_foreign" foreign key ("billing_address_id") references "cart_address" ("id") on update cascade on delete set null;' - ) - - this.addSql( - 'alter table "cart_line_item" add constraint "cart_line_item_cart_id_foreign" foreign key ("cart_id") references "cart" ("id") on update cascade on delete cascade;' - ) - - this.addSql( - 'alter table "cart_line_item_adjustment" add constraint "cart_line_item_adjustment_item_id_foreign" foreign key ("item_id") references "cart_line_item" ("id") on update cascade on delete cascade;' - ) - - this.addSql( - 'alter table "cart_line_item_tax_line" add constraint "cart_line_item_tax_line_item_id_foreign" foreign key ("item_id") references "cart_line_item" ("id") on update cascade on delete cascade;' - ) - - this.addSql( - 'alter table "cart_shipping_method" add constraint "cart_shipping_method_cart_id_foreign" foreign key ("cart_id") references "cart" ("id") on update cascade on delete cascade;' - ) - - this.addSql( - 'alter table "cart_shipping_method_adjustment" add constraint "cart_shipping_method_adjustment_shipping_method_id_foreign" foreign key ("shipping_method_id") references "cart_shipping_method" ("id") on update cascade on delete cascade;' - ) - - this.addSql( - 'alter table "cart_shipping_method_tax_line" add constraint "cart_shipping_method_tax_line_shipping_method_id_foreign" foreign key ("shipping_method_id") references "cart_shipping_method" ("id") on update cascade on delete cascade;' + ALTER TABLE "cart_line_item" ADD CONSTRAINT "cart_line_item_cart_id_foreign" FOREIGN KEY ("cart_id") REFERENCES "cart" ("id") ON UPDATE CASCADE ON DELETE CASCADE; + ALTER TABLE "cart_line_item_adjustment" ADD CONSTRAINT "cart_line_item_adjustment_item_id_foreign" FOREIGN KEY ("item_id") REFERENCES "cart_line_item" ("id") ON UPDATE CASCADE ON DELETE CASCADE; + ALTER TABLE "cart_line_item_tax_line" ADD CONSTRAINT "cart_line_item_tax_line_item_id_foreign" FOREIGN KEY ("item_id") REFERENCES "cart_line_item" ("id") ON UPDATE CASCADE ON DELETE CASCADE; + ALTER TABLE "cart_shipping_method" ADD CONSTRAINT "cart_shipping_method_cart_id_foreign" FOREIGN KEY ("cart_id") REFERENCES "cart" ("id") ON UPDATE CASCADE ON DELETE CASCADE; + ALTER TABLE "cart_shipping_method_adjustment" ADD CONSTRAINT "cart_shipping_method_adjustment_shipping_method_id_foreign" FOREIGN KEY ("shipping_method_id") REFERENCES "cart_shipping_method" ("id") ON UPDATE CASCADE ON DELETE CASCADE; + ALTER TABLE "cart_shipping_method_tax_line" ADD CONSTRAINT "cart_shipping_method_tax_line_shipping_method_id_foreign" FOREIGN KEY ("shipping_method_id") REFERENCES "cart_shipping_method" ("id") ON UPDATE CASCADE ON DELETE CASCADE; + ` ) } } From ff67a8e9f7ea0fcab4ea5b7f9b7588517d12e834 Mon Sep 17 00:00:00 2001 From: olivermrbl Date: Fri, 26 Jan 2024 16:00:19 +0100 Subject: [PATCH 3/5] Add indexes on cart table for currency, region, and sales channel --- .../migrations/CartModuleSetup20240122122952.ts | 3 +++ packages/cart/src/models/cart.ts | 16 ++++++++++++---- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/packages/cart/src/migrations/CartModuleSetup20240122122952.ts b/packages/cart/src/migrations/CartModuleSetup20240122122952.ts index dab87b844ed05..ab7c71c508c63 100644 --- a/packages/cart/src/migrations/CartModuleSetup20240122122952.ts +++ b/packages/cart/src/migrations/CartModuleSetup20240122122952.ts @@ -32,6 +32,9 @@ export class CartModuleSetup20240122122952 extends Migration { CREATE INDEX IF NOT EXISTS "IDX_cart_customer_id" ON "cart" ("customer_id"); CREATE INDEX IF NOT EXISTS "IDX_cart_shipping_address_id" ON "cart" ("shipping_address_id"); CREATE INDEX IF NOT EXISTS "IDX_cart_billing_address_id" ON "cart" ("billing_address_id"); + CREATE INDEX IF NOT EXISTS "IDX_cart_region_id" ON "cart" ("region_id"); + CREATE INDEX IF NOT EXISTS "IDX_cart_sales_channel_id" ON "cart" ("sales_channel_id"); + CREATE INDEX IF NOT EXISTS "IDX_cart_currency_code" ON "cart" ("currency_code"); CREATE TABLE IF NOT EXISTS "cart_address" ( "id" TEXT NOT NULL, diff --git a/packages/cart/src/models/cart.ts b/packages/cart/src/models/cart.ts index 168f9216bae84..41de7b29820ec 100644 --- a/packages/cart/src/models/cart.ts +++ b/packages/cart/src/models/cart.ts @@ -11,7 +11,7 @@ import { OneToMany, OptionalProps, PrimaryKey, - Property + Property, } from "@mikro-orm/core" import Address from "./address" import LineItem from "./line-item" @@ -29,7 +29,11 @@ export default class Cart { @PrimaryKey({ columnType: "text" }) id: string - @Property({ columnType: "text", nullable: true }) + @Property({ + columnType: "text", + nullable: true, + index: "IDX_cart_customer_id", + }) region_id?: string | null @Property({ @@ -39,13 +43,17 @@ export default class Cart { }) customer_id?: string | null - @Property({ columnType: "text", nullable: true }) + @Property({ + columnType: "text", + nullable: true, + index: "IDX_cart_customer_id", + }) sales_channel_id?: string | null @Property({ columnType: "text", nullable: true }) email?: string | null - @Property({ columnType: "text" }) + @Property({ columnType: "text", index: "IDX_cart_curency_code" }) currency_code: string @Index({ name: "IDX_cart_shipping_address_id" }) From 8654cc573bc7c19078347b9cd7492c684dbb1eb7 Mon Sep 17 00:00:00 2001 From: olivermrbl Date: Sun, 28 Jan 2024 11:00:08 +0100 Subject: [PATCH 4/5] fix indexes --- .../__tests__/services/address/index.spec.ts | 128 ------------------ .../services/cart-module/index.spec.ts | 6 +- .../__tests__/services/cart/index.spec.ts | 122 ----------------- packages/cart/src/models/cart.ts | 4 +- 4 files changed, 5 insertions(+), 255 deletions(-) delete mode 100644 packages/cart/integration-tests/__tests__/services/address/index.spec.ts delete mode 100644 packages/cart/integration-tests/__tests__/services/cart/index.spec.ts diff --git a/packages/cart/integration-tests/__tests__/services/address/index.spec.ts b/packages/cart/integration-tests/__tests__/services/address/index.spec.ts deleted file mode 100644 index e7008793f32b1..0000000000000 --- a/packages/cart/integration-tests/__tests__/services/address/index.spec.ts +++ /dev/null @@ -1,128 +0,0 @@ -import { SqlEntityManager } from "@mikro-orm/postgresql" -import { AddressService } from "../../../../src/services" -import { MikroOrmWrapper } from "../../../utils" -import { createMedusaContainer } from "@medusajs/utils" -import { asValue } from "awilix" -import ContainerLoader from "../../../../src/loaders/container" - -jest.setTimeout(30000) - -describe("Address Service", () => { - let service: AddressService - let testManager: SqlEntityManager - let repositoryManager: SqlEntityManager - - beforeEach(async () => { - await MikroOrmWrapper.setupDatabase() - repositoryManager = await MikroOrmWrapper.forkManager() - testManager = await MikroOrmWrapper.forkManager() - - const container = createMedusaContainer() - container.register("manager", asValue(repositoryManager)) - - await ContainerLoader({ container }) - - service = container.resolve("addressService") - }) - - afterEach(async () => { - await MikroOrmWrapper.clearDatabase() - }) - - describe("create", () => { - it("should create an address successfully", async () => { - const [createdAddress] = await service.create([ - { - first_name: "John", - last_name: "Doe", - address_1: "Test street 1", - city: "Test city", - country_code: "US", - postal_code: "12345", - }, - ]) - - const [address] = await service.list({ id: [createdAddress.id] }) - - expect(address).toEqual( - expect.objectContaining({ - id: createdAddress.id, - first_name: "John", - last_name: "Doe", - address_1: "Test street 1", - city: "Test city", - country_code: "US", - postal_code: "12345", - }) - ) - }) - }) - - describe("update", () => { - it("should throw an error if address does not exist", async () => { - const error = await service - .update([ - { - id: "none-existing", - }, - ]) - .catch((e) => e) - - expect(error.message).toContain( - 'Address with id "none-existing" not found' - ) - }) - - it("should update an address successfully", async () => { - const [createdAddress] = await service.create([ - { - first_name: "Jane", - last_name: "Doe", - address_1: "Test street 1", - city: "Test city", - country_code: "US", - postal_code: "12345", - }, - ]) - - await service.update([ - { - id: createdAddress.id, - address_1: "Test street 2", - city: "Test city 2", - }, - ]) - - const [address] = await service.list({ id: [createdAddress.id] }) - - expect(address).toEqual( - expect.objectContaining({ - id: createdAddress.id, - first_name: "Jane", - last_name: "Doe", - address_1: "Test street 2", - city: "Test city 2", - country_code: "US", - postal_code: "12345", - }) - ) - }) - }) - - describe("delete", () => { - it("should delete a cart successfully", async () => { - const [createdAddress] = await service.create([ - { - first_name: "Jane", - last_name: "Doe", - }, - ]) - - await service.delete([createdAddress.id]) - - const carts = await service.list({ id: [createdAddress.id] }) - - expect(carts.length).toEqual(0) - }) - }) -}) diff --git a/packages/cart/integration-tests/__tests__/services/cart-module/index.spec.ts b/packages/cart/integration-tests/__tests__/services/cart-module/index.spec.ts index e7c1b602a5bea..314047fe71954 100644 --- a/packages/cart/integration-tests/__tests__/services/cart-module/index.spec.ts +++ b/packages/cart/integration-tests/__tests__/services/cart-module/index.spec.ts @@ -1,9 +1,9 @@ +import { Modules } from "@medusajs/modules-sdk" import { ICartModuleService } from "@medusajs/types" import { CheckConstraintViolationException } from "@mikro-orm/core" +import { initModules } from "medusa-test-utils" import { MikroOrmWrapper } from "../../../utils" -import { Modules } from "@medusajs/modules-sdk" import { getInitModuleConfig } from "../../../utils/get-init-module-config" -import { initModules } from "medusa-test-utils" jest.setTimeout(30000) @@ -34,7 +34,7 @@ describe("Cart Module Service", () => { }) describe("create", () => { - it("should throw an error when required params are not passed", async () => { + it.only("should throw an error when required params are not passed", async () => { const error = await service .create([ { diff --git a/packages/cart/integration-tests/__tests__/services/cart/index.spec.ts b/packages/cart/integration-tests/__tests__/services/cart/index.spec.ts deleted file mode 100644 index de63177a9d87f..0000000000000 --- a/packages/cart/integration-tests/__tests__/services/cart/index.spec.ts +++ /dev/null @@ -1,122 +0,0 @@ -import { SqlEntityManager } from "@mikro-orm/postgresql" -import { CartService } from "../../../../src/services" -import { createCarts } from "../../../__fixtures__/cart" -import { MikroOrmWrapper } from "../../../utils" -import { createMedusaContainer } from "@medusajs/utils" -import { asValue } from "awilix" -import ContainerLoader from "../../../../src/loaders/container" - -jest.setTimeout(30000) - -describe("Cart Service", () => { - let service: CartService - let testManager: SqlEntityManager - let repositoryManager: SqlEntityManager - - beforeEach(async () => { - await MikroOrmWrapper.setupDatabase() - repositoryManager = await MikroOrmWrapper.forkManager() - testManager = await MikroOrmWrapper.forkManager() - - const container = createMedusaContainer() - container.register("manager", asValue(repositoryManager)) - - await ContainerLoader({ container }) - - service = container.resolve("cartService") - - await createCarts(testManager) - }) - - afterEach(async () => { - await MikroOrmWrapper.clearDatabase() - }) - - describe("create", () => { - it("should throw an error when required params are not passed", async () => { - const error = await service - .create([ - { - email: "test@email.com", - } as any, - ]) - .catch((e) => e) - - expect(error.message).toContain( - "Value for Cart.currency_code is required, 'undefined' found" - ) - }) - - it("should create a cart successfully", async () => { - const [createdCart] = await service.create([ - { - currency_code: "eur", - }, - ]) - - const [cart] = await service.list({ id: [createdCart.id] }) - - expect(cart).toEqual( - expect.objectContaining({ - id: createdCart.id, - currency_code: "eur", - }) - ) - }) - }) - - describe("update", () => { - it("should throw an error if cart does not exist", async () => { - const error = await service - .update([ - { - id: "none-existing", - }, - ]) - .catch((e) => e) - - expect(error.message).toContain('Cart with id "none-existing" not found') - }) - - it("should update a cart successfully", async () => { - const [createdCart] = await service.create([ - { - currency_code: "eur", - }, - ]) - - const [updatedCart] = await service.update([ - { - id: createdCart.id, - email: "test@email.com", - }, - ]) - - const [cart] = await service.list({ id: [createdCart.id] }) - - expect(cart).toEqual( - expect.objectContaining({ - id: createdCart.id, - currency_code: "eur", - email: updatedCart.email, - }) - ) - }) - }) - - describe("delete", () => { - it("should delete a cart successfully", async () => { - const [createdCart] = await service.create([ - { - currency_code: "eur", - }, - ]) - - await service.delete([createdCart.id]) - - const carts = await service.list({ id: [createdCart.id] }) - - expect(carts.length).toEqual(0) - }) - }) -}) diff --git a/packages/cart/src/models/cart.ts b/packages/cart/src/models/cart.ts index 41de7b29820ec..b83d1b534e33a 100644 --- a/packages/cart/src/models/cart.ts +++ b/packages/cart/src/models/cart.ts @@ -32,7 +32,7 @@ export default class Cart { @Property({ columnType: "text", nullable: true, - index: "IDX_cart_customer_id", + index: "IDX_cart_region_id", }) region_id?: string | null @@ -46,7 +46,7 @@ export default class Cart { @Property({ columnType: "text", nullable: true, - index: "IDX_cart_customer_id", + index: "IDX_cart_sales_channel_id", }) sales_channel_id?: string | null From 763c1a0dd0d6fc9e95e96db5a386cfea60d2128a Mon Sep 17 00:00:00 2001 From: olivermrbl Date: Sun, 28 Jan 2024 21:02:17 +0100 Subject: [PATCH 5/5] address PR comments --- .../__tests__/services/cart-module/index.spec.ts | 2 +- .../cart/src/migrations/CartModuleSetup20240122122952.ts | 7 ++++++- packages/cart/src/models/adjustment-line.ts | 5 ++++- packages/cart/src/models/line-item.ts | 6 +++++- packages/cart/src/models/shipping-method.ts | 6 +++++- packages/cart/src/models/tax-line.ts | 5 ++++- 6 files changed, 25 insertions(+), 6 deletions(-) diff --git a/packages/cart/integration-tests/__tests__/services/cart-module/index.spec.ts b/packages/cart/integration-tests/__tests__/services/cart-module/index.spec.ts index 314047fe71954..2529a00824b44 100644 --- a/packages/cart/integration-tests/__tests__/services/cart-module/index.spec.ts +++ b/packages/cart/integration-tests/__tests__/services/cart-module/index.spec.ts @@ -34,7 +34,7 @@ describe("Cart Module Service", () => { }) describe("create", () => { - it.only("should throw an error when required params are not passed", async () => { + it("should throw an error when required params are not passed", async () => { const error = await service .create([ { diff --git a/packages/cart/src/migrations/CartModuleSetup20240122122952.ts b/packages/cart/src/migrations/CartModuleSetup20240122122952.ts index ab7c71c508c63..a81894789b3c6 100644 --- a/packages/cart/src/migrations/CartModuleSetup20240122122952.ts +++ b/packages/cart/src/migrations/CartModuleSetup20240122122952.ts @@ -89,7 +89,7 @@ export class CartModuleSetup20240122122952 extends Migration { ALTER TABLE "cart" ADD CONSTRAINT "cart_billing_address_id_foreign" FOREIGN KEY ("billing_address_id") REFERENCES "cart_address" ("id") ON UPDATE CASCADE ON DELETE SET NULL; CREATE INDEX IF NOT EXISTS "IDX_line_item_cart_id" ON "cart_line_item" ("cart_id"); - + CREATE INDEX IF NOT EXISTS "IDX_line_item_product_id" ON "cart_line_item" ("product_id"); CREATE INDEX IF NOT EXISTS "IDX_line_item_variant_id" ON "cart_line_item" ("variant_id"); @@ -108,6 +108,7 @@ export class CartModuleSetup20240122122952 extends Migration { ); CREATE INDEX IF NOT EXISTS "IDX_adjustment_item_id" ON "cart_line_item_adjustment" ("item_id"); + CREATE INDEX IF NOT EXISTS "IDX_line_item_adjustment_promotion_id" ON "cart_line_item_adjustment" ("promotion_id"); CREATE TABLE IF NOT EXISTS "cart_line_item_tax_line" ( "id" TEXT NOT NULL, @@ -123,6 +124,7 @@ export class CartModuleSetup20240122122952 extends Migration { ); CREATE INDEX IF NOT EXISTS "IDX_tax_line_item_id" ON "cart_line_item_tax_line" ("item_id"); + CREATE INDEX IF NOT EXISTS "IDX_line_item_tax_line_tax_rate_id" ON "cart_line_item_tax_line" ("tax_rate_id"); CREATE TABLE IF NOT EXISTS "cart_shipping_method" ( "id" TEXT NOT NULL, @@ -141,6 +143,7 @@ export class CartModuleSetup20240122122952 extends Migration { ); CREATE INDEX IF NOT EXISTS "IDX_shipping_method_cart_id" ON "cart_shipping_method" ("cart_id"); + CREATE INDEX IF NOT EXISTS "IDX_shipping_method_option_id" ON "cart_shipping_method" ("shipping_option_id"); CREATE TABLE IF NOT EXISTS "cart_shipping_method_adjustment" ( "id" TEXT NOT NULL, @@ -156,6 +159,7 @@ export class CartModuleSetup20240122122952 extends Migration { ); CREATE INDEX IF NOT EXISTS "IDX_adjustment_shipping_method_id" ON "cart_shipping_method_adjustment" ("shipping_method_id"); + CREATE INDEX IF NOT EXISTS "IDX_shipping_method_adjustment_promotion_id" ON "cart_shipping_method_adjustment" ("promotion_id"); CREATE TABLE IF NOT EXISTS "cart_shipping_method_tax_line" ( "id" TEXT NOT NULL, @@ -171,6 +175,7 @@ export class CartModuleSetup20240122122952 extends Migration { ); CREATE INDEX IF NOT EXISTS "IDX_tax_line_shipping_method_id" ON "cart_shipping_method_tax_line" ("shipping_method_id"); + CREATE INDEX IF NOT EXISTS "IDX_shipping_method_tax_line_tax_rate_id" ON "cart_shipping_method_tax_line" ("tax_rate_id"); ALTER TABLE "cart_line_item" ADD CONSTRAINT "cart_line_item_cart_id_foreign" FOREIGN KEY ("cart_id") REFERENCES "cart" ("id") ON UPDATE CASCADE ON DELETE CASCADE; ALTER TABLE "cart_line_item_adjustment" ADD CONSTRAINT "cart_line_item_adjustment_item_id_foreign" FOREIGN KEY ("item_id") REFERENCES "cart_line_item" ("id") ON UPDATE CASCADE ON DELETE CASCADE; diff --git a/packages/cart/src/models/adjustment-line.ts b/packages/cart/src/models/adjustment-line.ts index e7c0961ac1ac5..02401fef94a46 100644 --- a/packages/cart/src/models/adjustment-line.ts +++ b/packages/cart/src/models/adjustment-line.ts @@ -16,7 +16,10 @@ export default abstract class AdjustmentLine { @Property({ columnType: "text", nullable: true }) description: string | null = null - @Property({ columnType: "text", nullable: true }) + @Property({ + columnType: "text", + nullable: true, + }) promotion_id: string | null = null @Property({ columnType: "text", nullable: true }) diff --git a/packages/cart/src/models/line-item.ts b/packages/cart/src/models/line-item.ts index ada1d29e3f782..19a152d070c87 100644 --- a/packages/cart/src/models/line-item.ts +++ b/packages/cart/src/models/line-item.ts @@ -61,7 +61,11 @@ export default class LineItem { }) variant_id?: string | null - @Property({ columnType: "text", nullable: true }) + @Property({ + columnType: "text", + nullable: true, + index: "IDX_line_item_product_id", + }) product_id?: string | null @Property({ columnType: "text", nullable: true }) diff --git a/packages/cart/src/models/shipping-method.ts b/packages/cart/src/models/shipping-method.ts index a46bbcc6b635f..ec41b17b91a32 100644 --- a/packages/cart/src/models/shipping-method.ts +++ b/packages/cart/src/models/shipping-method.ts @@ -43,7 +43,11 @@ export default class ShippingMethod { @Property({ columnType: "boolean" }) is_tax_inclusive = false - @Property({ columnType: "text", nullable: true }) + @Property({ + columnType: "text", + nullable: true, + index: "IDX_shipping_method_option_id", + }) shipping_option_id?: string | null @Property({ columnType: "jsonb", nullable: true }) diff --git a/packages/cart/src/models/tax-line.ts b/packages/cart/src/models/tax-line.ts index 5425d2c84bb21..5ba302e305c58 100644 --- a/packages/cart/src/models/tax-line.ts +++ b/packages/cart/src/models/tax-line.ts @@ -11,7 +11,10 @@ export default abstract class TaxLine { @Property({ columnType: "text", nullable: true }) description?: string | null - @Property({ columnType: "text", nullable: true }) + @Property({ + columnType: "text", + nullable: true, + }) tax_rate_id?: string | null @Property({ columnType: "text" })