bug: fix constraint on customer table to allow soft-deletes (#6631)
This commit is contained in:
5
.changeset/eighty-eagles-reply.md
Normal file
5
.changeset/eighty-eagles-reply.md
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"@medusajs/medusa": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
bug: fix constraint on customer table to allow soft-deletes
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
import { MigrationInterface, QueryRunner } from "typeorm"
|
||||||
|
|
||||||
|
export class AlterCustomerUniqueConstraint1709888477798 implements MigrationInterface {
|
||||||
|
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||||
|
await queryRunner.query(`
|
||||||
|
ALTER TABLE "customer" DROP CONSTRAINT "UQ_unique_email_for_guests_and_customer_accounts";
|
||||||
|
|
||||||
|
CREATE UNIQUE INDEX "IDX_unique_email_for_guests_and_customer_accounts" ON "customer" ("email", "has_account") WHERE "deleted_at" IS NULL;
|
||||||
|
`)
|
||||||
|
}
|
||||||
|
|
||||||
|
public async down(queryRunner: QueryRunner): Promise<void> {
|
||||||
|
// ensure we dont have duplicated email,has_account to avoid violation of the previous unique constraint
|
||||||
|
await queryRunner.query(`
|
||||||
|
WITH RankedCustomers AS (
|
||||||
|
SELECT
|
||||||
|
id,
|
||||||
|
ROW_NUMBER() OVER (
|
||||||
|
PARTITION BY email, has_account
|
||||||
|
ORDER BY CASE WHEN deleted_at IS NULL THEN 0 ELSE 1 END, id
|
||||||
|
) AS rn
|
||||||
|
FROM
|
||||||
|
customer
|
||||||
|
)
|
||||||
|
DELETE FROM customer
|
||||||
|
WHERE id IN (
|
||||||
|
SELECT id FROM RankedCustomers WHERE rn > 1
|
||||||
|
);
|
||||||
|
`);
|
||||||
|
|
||||||
|
await queryRunner.query(`
|
||||||
|
DROP INDEX "IDX_unique_email_for_guests_and_customer_accounts";
|
||||||
|
|
||||||
|
ALTER TABLE "customer" ADD CONSTRAINT "UQ_unique_email_for_guests_and_customer_accounts" UNIQUE ("email", "has_account");
|
||||||
|
`)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -19,7 +19,7 @@ import { SoftDeletableEntity } from "../interfaces/models/soft-deletable-entity"
|
|||||||
import { generateEntityId } from "../utils/generate-entity-id"
|
import { generateEntityId } from "../utils/generate-entity-id"
|
||||||
|
|
||||||
@Entity()
|
@Entity()
|
||||||
@Unique(["email", "has_account"])
|
@Index(["email", "has_account"], { unique: true, where: "deleted_at IS NULL" })
|
||||||
export class Customer extends SoftDeletableEntity {
|
export class Customer extends SoftDeletableEntity {
|
||||||
@Index()
|
@Index()
|
||||||
@Column()
|
@Column()
|
||||||
|
|||||||
Reference in New Issue
Block a user