From a86c87fe1442afce9285e39255914e01012b4449 Mon Sep 17 00:00:00 2001 From: Philip Korsholm <88927411+pKorsholm@users.noreply.github.com> Date: Tue, 13 Feb 2024 16:07:34 +0800 Subject: [PATCH] Feat(utils): psql unique index instead of constraint (#6386) **What** - always return an index expression, also for unique constraints **why** - constraints can't be partial, meaning `UNIQUE ... WHERE` is not possible with constraints - constraints are indicies under the hood so it doesn't change the behavior of the system when we're not using constraint specific features but just using them for `UNIQUE` --- .changeset/hot-coins-itch.md | 5 +++++ .../utils/src/common/__tests__/create-psql-index-helper.ts | 6 +++--- packages/utils/src/common/create-psql-index-helper.ts | 7 ++----- 3 files changed, 10 insertions(+), 8 deletions(-) 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 0000000000..4f4a08dbf5 --- /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 :'( 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 ec2c70178e..321e87c913 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}` ) }) }) diff --git a/packages/utils/src/common/create-psql-index-helper.ts b/packages/utils/src/common/create-psql-index-helper.ts index 8d7529d582..f482bd3606 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}` }