Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat(utils): psql unique index instead of constraint #6386

Merged
merged 4 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/hot-coins-itch.md
Original file line number Diff line number Diff line change
@@ -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 :'(
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
)
})
})
7 changes: 2 additions & 5 deletions packages/utils/src/common/create-psql-index-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
}
Loading