From 89ec9d09ca3b8109483a882bc5e737fb9676116b Mon Sep 17 00:00:00 2001 From: Sebastian Rindom Date: Thu, 25 Jan 2024 15:03:54 +0100 Subject: [PATCH] fix: migration --- .../migrations/.snapshot-medusa-customer.json | 600 ++++++++++++++++++ .../src/migrations/Migration20240124154000.ts | 80 +++ packages/customer/src/models/address.ts | 4 +- .../src/models/customer-group-customer.ts | 6 +- packages/customer/src/models/customer.ts | 3 + 5 files changed, 687 insertions(+), 6 deletions(-) create mode 100644 packages/customer/src/migrations/.snapshot-medusa-customer.json create mode 100644 packages/customer/src/migrations/Migration20240124154000.ts diff --git a/packages/customer/src/migrations/.snapshot-medusa-customer.json b/packages/customer/src/migrations/.snapshot-medusa-customer.json new file mode 100644 index 0000000000000..b39fc7ed7d14d --- /dev/null +++ b/packages/customer/src/migrations/.snapshot-medusa-customer.json @@ -0,0 +1,600 @@ +{ + "namespaces": [ + "public" + ], + "name": "public", + "tables": [ + { + "columns": { + "id": { + "name": "id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "company_name": { + "name": "company_name", + "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" + }, + "email": { + "name": "email", + "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" + }, + "has_account": { + "name": "has_account", + "type": "boolean", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "default": "false", + "mappedType": "boolean" + }, + "default_shipping_address_id": { + "name": "default_shipping_address_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "default_billing_address_id": { + "name": "default_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" + }, + "created_by": { + "name": "created_by", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + } + }, + "name": "customer", + "schema": "public", + "indexes": [ + { + "columnNames": [ + "default_shipping_address_id" + ], + "composite": false, + "keyName": "IDX_customer_default_shipping_address_id", + "primary": false, + "unique": false + }, + { + "columnNames": [ + "default_billing_address_id" + ], + "composite": false, + "keyName": "IDX_customer_default_billing_address_id", + "primary": false, + "unique": false + }, + { + "keyName": "customer_pkey", + "columnNames": [ + "id" + ], + "composite": false, + "primary": true, + "unique": true + } + ], + "checks": [], + "foreignKeys": { + "customer_default_shipping_address_id_foreign": { + "constraintName": "customer_default_shipping_address_id_foreign", + "columnNames": [ + "default_shipping_address_id" + ], + "localTableName": "public.customer", + "referencedColumnNames": [ + "id" + ], + "referencedTableName": "public.customer_address", + "deleteRule": "set null", + "updateRule": "cascade" + }, + "customer_default_billing_address_id_foreign": { + "constraintName": "customer_default_billing_address_id_foreign", + "columnNames": [ + "default_billing_address_id" + ], + "localTableName": "public.customer", + "referencedColumnNames": [ + "id" + ], + "referencedTableName": "public.customer_address", + "deleteRule": "set null", + "updateRule": "cascade" + } + } + }, + { + "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": false, + "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": "customer_address", + "schema": "public", + "indexes": [ + { + "columnNames": [ + "customer_id" + ], + "composite": false, + "keyName": "IDX_customer_address_customer_id", + "primary": false, + "unique": false + }, + { + "keyName": "customer_address_pkey", + "columnNames": [ + "id" + ], + "composite": false, + "primary": true, + "unique": true + } + ], + "checks": [], + "foreignKeys": { + "customer_address_customer_id_foreign": { + "constraintName": "customer_address_customer_id_foreign", + "columnNames": [ + "customer_id" + ], + "localTableName": "public.customer_address", + "referencedColumnNames": [ + "id" + ], + "referencedTableName": "public.customer", + "updateRule": "cascade" + } + } + }, + { + "columns": { + "id": { + "name": "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": true, + "mappedType": "text" + }, + "metadata": { + "name": "metadata", + "type": "jsonb", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "json" + }, + "created_by": { + "name": "created_by", + "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" + }, + "deleted_at": { + "name": "deleted_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "length": 6, + "mappedType": "datetime" + } + }, + "name": "customer_group", + "schema": "public", + "indexes": [ + { + "keyName": "customer_group_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" + }, + "customer_id": { + "name": "customer_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "customer_group_id": { + "name": "customer_group_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "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" + }, + "created_by": { + "name": "created_by", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + } + }, + "name": "customer_group_customer", + "schema": "public", + "indexes": [ + { + "columnNames": [ + "customer_group_id" + ], + "composite": false, + "keyName": "IDX_customer_group_customer_group_id", + "primary": false, + "unique": false + }, + { + "columnNames": [ + "customer_id" + ], + "composite": false, + "keyName": "IDX_customer_group_customer_customer_id", + "primary": false, + "unique": false + }, + { + "keyName": "customer_group_customer_pkey", + "columnNames": [ + "id" + ], + "composite": false, + "primary": true, + "unique": true + } + ], + "checks": [], + "foreignKeys": { + "customer_group_customer_customer_group_id_foreign": { + "constraintName": "customer_group_customer_customer_group_id_foreign", + "columnNames": [ + "customer_group_id" + ], + "localTableName": "public.customer_group_customer", + "referencedColumnNames": [ + "id" + ], + "referencedTableName": "public.customer_group", + "deleteRule": "cascade" + }, + "customer_group_customer_customer_id_foreign": { + "constraintName": "customer_group_customer_customer_id_foreign", + "columnNames": [ + "customer_id" + ], + "localTableName": "public.customer_group_customer", + "referencedColumnNames": [ + "id" + ], + "referencedTableName": "public.customer", + "deleteRule": "cascade" + } + } + } + ] +} diff --git a/packages/customer/src/migrations/Migration20240124154000.ts b/packages/customer/src/migrations/Migration20240124154000.ts new file mode 100644 index 0000000000000..53680c9c8f527 --- /dev/null +++ b/packages/customer/src/migrations/Migration20240124154000.ts @@ -0,0 +1,80 @@ +import { Migration } from "@mikro-orm/migrations" + +export class Migration20240124154000 extends Migration { + async up(): Promise { + // Customer table modifications + this.addSql( + 'create table if not exists "customer" ("id" text not null, "company_name" text null, "first_name" text null, "last_name" text null, "email" text null, "phone" text null, "has_account" boolean not null default false, "default_shipping_address_id" text null, "default_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, "created_by" text null, constraint "customer_pkey" primary key ("id"));' + ) + this.addSql( + 'alter table "customer" add column if not exists "company_name" text null;' + ) + this.addSql( + 'alter table "customer" add column if not exists "default_shipping_address_id" text null;' + ) + this.addSql( + 'alter table "customer" add column if not exists "default_billing_address_id" text null;' + ) + this.addSql( + 'alter table "customer" add column if not exists "created_by" text null;' + ) + this.addSql('drop index if exists "IDX_8abe81b9aac151ae60bf507ad1";') + this.addSql( + 'create index if not exists "IDX_customer_default_shipping_address_id" on "customer" ("default_shipping_address_id");' + ) + this.addSql( + 'create index if not exists "IDX_customer_default_billing_address_id" on "customer" ("default_billing_address_id");' + ) + + // Customer Address table + this.addSql( + 'create table if not exists "customer_address" ("id" text not null, "customer_id" text not 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 "customer_address_pkey" primary key ("id"));' + ) + this.addSql( + 'create index if not exists "IDX_customer_address_customer_id" on "customer_address" ("customer_id");' + ) + + // Customer Group table modifications + this.addSql( + 'create table if not exists "customer_group" ("id" text not null, "name" text null, "metadata" jsonb null, "created_by" text null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "customer_group_pkey" primary key ("id"));' + ) + this.addSql( + 'alter table "customer_group" add column if not exists "created_by" text null;' + ) + this.addSql('drop index if exists "IDX_c4c3a5225a7a1f0af782c40abc";') + this.addSql( + 'create unique index if not exists "IDX_customer_group_name" on "customer_group" ("name") where "deleted_at" is null;' + ) + + // Customer Group Customer table + this.addSql( + 'create table if not exists "customer_group_customer" ("id" text not null, "customer_id" text not null, "customer_group_id" text not null, "metadata" jsonb null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "created_by" text null, constraint "customer_group_customer_pkey" primary key ("id"));' + ) + this.addSql( + 'create index if not exists "IDX_customer_group_customer_group_id" on "customer_group_customer" ("customer_group_id");' + ) + this.addSql( + 'create index if not exists "IDX_customer_group_customer_customer_id" on "customer_group_customer" ("customer_id");' + ) + + // Foreign key constraints + this.addSql( + 'alter table "customer" drop constraint if exists "FK_8abe81b9aac151ae60bf507ad15";' + ) + this.addSql( + 'alter table "customer" add constraint "customer_default_shipping_address_id_foreign" foreign key ("default_shipping_address_id") references "customer_address" ("id") on update cascade on delete set null;' + ) + this.addSql( + 'alter table "customer" add constraint "customer_default_billing_address_id_foreign" foreign key ("default_billing_address_id") references "customer_address" ("id") on update cascade on delete set null;' + ) + this.addSql( + 'alter table "customer_address" add constraint "customer_address_customer_id_foreign" foreign key ("customer_id") references "customer" ("id") on update cascade;' + ) + this.addSql( + 'alter table "customer_group_customer" add constraint "customer_group_customer_customer_group_id_foreign" foreign key ("customer_group_id") references "customer_group" ("id") on delete cascade;' + ) + this.addSql( + 'alter table "customer_group_customer" add constraint "customer_group_customer_customer_id_foreign" foreign key ("customer_id") references "customer" ("id") on delete cascade;' + ) + } +} diff --git a/packages/customer/src/models/address.ts b/packages/customer/src/models/address.ts index ae641cac94cf1..2ed08e3f926ab 100644 --- a/packages/customer/src/models/address.ts +++ b/packages/customer/src/models/address.ts @@ -25,9 +25,9 @@ export default class Address { @ManyToOne(() => Customer, { fieldName: "customer_id", - nullable: true, + index: "IDX_customer_address_customer_id", }) - customer?: Customer + customer: Customer @Property({ columnType: "text", nullable: true }) company: string | null = null diff --git a/packages/customer/src/models/customer-group-customer.ts b/packages/customer/src/models/customer-group-customer.ts index 94602e6693c74..9ec649efbe2cf 100644 --- a/packages/customer/src/models/customer-group-customer.ts +++ b/packages/customer/src/models/customer-group-customer.ts @@ -32,19 +32,17 @@ export default class CustomerGroupCustomer { entity: () => Customer, fieldName: "customer_id", index: "IDX_customer_group_customer_customer_id", - nullable: true, cascade: [Cascade.REMOVE], }) - customer: Customer | null + customer: Customer @ManyToOne({ entity: () => CustomerGroup, fieldName: "customer_group_id", index: "IDX_customer_group_customer_group_id", - nullable: true, cascade: [Cascade.REMOVE], }) - customer_group: CustomerGroup | null + customer_group: CustomerGroup @Property({ columnType: "jsonb", nullable: true }) metadata: Record | null = null diff --git a/packages/customer/src/models/customer.ts b/packages/customer/src/models/customer.ts index 3a2a4c6a4acd9..0313c771dc0e8 100644 --- a/packages/customer/src/models/customer.ts +++ b/packages/customer/src/models/customer.ts @@ -49,6 +49,9 @@ export default class Customer { @Property({ columnType: "text", nullable: true }) phone: string | null = null + @Property({ columnType: "boolean", default: false }) + has_account: boolean = false + @Index({ name: "IDX_customer_default_shipping_address_id" }) @Property({ columnType: "text", nullable: true }) default_shipping_address_id: string | null = null