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`
This commit is contained in:
Philip Korsholm
2024-02-13 16:07:34 +08:00
committed by GitHub
parent aa9f66e16a
commit a86c87fe14
3 changed files with 10 additions and 8 deletions

View File

@@ -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 :'(

View File

@@ -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}`
)
})
})

View File

@@ -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}`
}