From a0c3275d92371aa7d647db0e3d42eb00a70b908a Mon Sep 17 00:00:00 2001 From: Philip Korsholm Date: Tue, 13 Feb 2024 15:13:16 +0800 Subject: [PATCH 1/4] update unique constraint processing for psql index expressions --- packages/utils/src/common/create-psql-index-helper.ts | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/packages/utils/src/common/create-psql-index-helper.ts b/packages/utils/src/common/create-psql-index-helper.ts index 8d7529d58224f..55e86ea5e2831 100644 --- a/packages/utils/src/common/create-psql-index-helper.ts +++ b/packages/utils/src/common/create-psql-index-helper.ts @@ -45,10 +45,7 @@ export function createPsqlIndexStatementHelper({ columns = Array.isArray(columns) ? columns.join(", ") : columns const typeStr = type ? ` USING ${type}` : "" const optionsStr = where ? ` WHERE ${where}` : "" + const uniqueStr = unique ? "UNIQUE " : "" - if (!unique) { - return `CREATE INDEX IF NOT EXISTS "${name}" ON "${tableName}"${typeStr} (${columns})${optionsStr}` - } else { - return `ALTER TABLE IF EXISTS "${tableName}" ADD CONSTRAINT "${name}" UNIQUE (${columns})${optionsStr}` - } + return `CREATE ${uniqueStr}INDEX IF NOT EXISTS ${name} ON ${tableName}${typeStr} (${columns})${optionsStr}` } From c9f08c8ee4c51177343ad4a748a5e194e81e90c2 Mon Sep 17 00:00:00 2001 From: Philip Korsholm Date: Tue, 13 Feb 2024 15:31:09 +0800 Subject: [PATCH 2/4] add changeset --- .changeset/hot-coins-itch.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/hot-coins-itch.md diff --git a/.changeset/hot-coins-itch.md b/.changeset/hot-coins-itch.md new file mode 100644 index 0000000000000..4f4a08dbf5486 --- /dev/null +++ b/.changeset/hot-coins-itch.md @@ -0,0 +1,5 @@ +--- +"@medusajs/utils": patch +--- + +feat(utils): make psql index util return index instead of constraint for unique indicies becuase partial constraints don't exist :'( From 163564a7666bb725df58951efafe74b93cd42d18 Mon Sep 17 00:00:00 2001 From: Philip Korsholm Date: Tue, 13 Feb 2024 15:37:07 +0800 Subject: [PATCH 3/4] fix quotes --- packages/utils/src/common/create-psql-index-helper.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/utils/src/common/create-psql-index-helper.ts b/packages/utils/src/common/create-psql-index-helper.ts index 55e86ea5e2831..f482bd360617a 100644 --- a/packages/utils/src/common/create-psql-index-helper.ts +++ b/packages/utils/src/common/create-psql-index-helper.ts @@ -47,5 +47,5 @@ export function createPsqlIndexStatementHelper({ const optionsStr = where ? ` WHERE ${where}` : "" const uniqueStr = unique ? "UNIQUE " : "" - return `CREATE ${uniqueStr}INDEX IF NOT EXISTS ${name} ON ${tableName}${typeStr} (${columns})${optionsStr}` + return `CREATE ${uniqueStr}INDEX IF NOT EXISTS "${name}" ON "${tableName}"${typeStr} (${columns})${optionsStr}` } From 855b54e9c813736bea6090efd9481a077e42466d Mon Sep 17 00:00:00 2001 From: Philip Korsholm Date: Tue, 13 Feb 2024 15:41:25 +0800 Subject: [PATCH 4/4] fix tests --- .../utils/src/common/__tests__/create-psql-index-helper.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/utils/src/common/__tests__/create-psql-index-helper.ts b/packages/utils/src/common/__tests__/create-psql-index-helper.ts index ec2c70178e80f..321e87c913290 100644 --- a/packages/utils/src/common/__tests__/create-psql-index-helper.ts +++ b/packages/utils/src/common/__tests__/create-psql-index-helper.ts @@ -73,9 +73,9 @@ describe("createPsqlIndexStatementHelper", function () { const indexStatement = createPsqlIndexStatementHelper(options) expect(indexStatement).toEqual( - `ALTER TABLE IF EXISTS "${options.tableName}" ADD CONSTRAINT "${ - options.name - }" UNIQUE (${options.columns.join(", ")}) WHERE ${options.where}` + `CREATE UNIQUE INDEX IF NOT EXISTS "${options.name}" ON "${ + options.tableName + }" (${options.columns.join(", ")}) WHERE ${options.where}` ) }) })