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:
5
.changeset/hot-coins-itch.md
Normal file
5
.changeset/hot-coins-itch.md
Normal 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 :'(
|
||||
@@ -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}`
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -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}`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user