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:
1 parent
aa9f66e16a
commit
a86c87fe14
3 files changed
+10
-8
No files matched your search
@@ -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)
|
const indexStatement = createPsqlIndexStatementHelper(options)
|
||||||
expect(indexStatement).toEqual(
|
expect(indexStatement).toEqual(
|
||||||
`ALTER TABLE IF EXISTS "${options.tableName}" ADD CONSTRAINT "${
|
`CREATE UNIQUE INDEX IF NOT EXISTS "${options.name}" ON "${
|
||||||
options.name
|
options.tableName
|
||||||
}" UNIQUE (${options.columns.join(", ")}) WHERE ${options.where}`
|
}" (${options.columns.join(", ")}) WHERE ${options.where}`
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@@ -45,10 +45,7 @@ export function createPsqlIndexStatementHelper({
|
|||||||
columns = Array.isArray(columns) ? columns.join(", ") : columns
|
columns = Array.isArray(columns) ? columns.join(", ") : columns
|
||||||
const typeStr = type ? ` USING ${type}` : ""
|
const typeStr = type ? ` USING ${type}` : ""
|
||||||
const optionsStr = where ? ` WHERE ${where}` : ""
|
const optionsStr = where ? ` WHERE ${where}` : ""
|
||||||
|
const uniqueStr = unique ? "UNIQUE " : ""
|
||||||
|
|
||||||
if (!unique) {
|
return `CREATE ${uniqueStr}INDEX IF NOT EXISTS "${name}" ON "${tableName}"${typeStr} (${columns})${optionsStr}`
|
||||||
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}`
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user