chore(): Fix database test utils and utils (#6383)

**What**
Fix the test utils database to trully run the migrations, the migration path is deprecated and not used anymore as umzung infer the path under the hood. It also fix the schema so that it is possible to specify different schema if needed.

The index helper can now create named constraints for uniqueness. 

extracted [from](https://github.com/medusajs/medusa/pull/6381)
This commit is contained in:
Adrien de Peretti
2024-02-12 19:07:15 +01:00
committed by GitHub
parent 1593e0b192
commit e85463b2a7
5 changed files with 90 additions and 32 deletions

View File

@@ -10,7 +10,7 @@ describe("createPsqlIndexStatementHelper", function () {
const indexStatement = createPsqlIndexStatementHelper(options)
expect(indexStatement).toEqual(
`CREATE INDEX IF NOT EXISTS ${options.name} ON ${options.tableName} (${options.columns})`
`CREATE INDEX IF NOT EXISTS "${options.name}" ON "${options.tableName}" (${options.columns})`
)
})
@@ -23,9 +23,9 @@ describe("createPsqlIndexStatementHelper", function () {
const indexStatement = createPsqlIndexStatementHelper(options)
expect(indexStatement).toEqual(
`CREATE INDEX IF NOT EXISTS ${options.name} ON ${
`CREATE INDEX IF NOT EXISTS "${options.name}" ON "${
options.tableName
} (${options.columns.join(", ")})`
}" (${options.columns.join(", ")})`
)
})
@@ -39,9 +39,9 @@ describe("createPsqlIndexStatementHelper", function () {
const indexStatement = createPsqlIndexStatementHelper(options)
expect(indexStatement).toEqual(
`CREATE INDEX IF NOT EXISTS ${options.name} ON ${
`CREATE INDEX IF NOT EXISTS "${options.name}" ON "${
options.tableName
} (${options.columns.join(", ")}) WHERE ${options.where}`
}" (${options.columns.join(", ")}) WHERE ${options.where}`
)
})
@@ -56,9 +56,26 @@ describe("createPsqlIndexStatementHelper", function () {
const indexStatement = createPsqlIndexStatementHelper(options)
expect(indexStatement).toEqual(
`CREATE INDEX IF NOT EXISTS ${options.name} ON ${
`CREATE INDEX IF NOT EXISTS "${options.name}" ON "${
options.tableName
} USING GIN (${options.columns.join(", ")}) WHERE ${options.where}`
}" USING GIN (${options.columns.join(", ")}) WHERE ${options.where}`
)
})
it("should generate unique constraint", function () {
const options = {
name: "index_name",
tableName: "table_name",
columns: ["column_name_1", "column_name_2"],
unique: true,
where: "column_name_1 IS NOT NULL",
}
const indexStatement = createPsqlIndexStatementHelper(options)
expect(indexStatement).toEqual(
`ALTER TABLE IF EXISTS "${options.tableName}" ADD CONSTRAINT "${
options.name
}" UNIQUE (${options.columns.join(", ")}) WHERE ${options.where}`
)
})
})

View File

@@ -5,6 +5,7 @@
* @param columns The columns to index
* @param type The type of index (e.g GIN, GIST, BTREE, etc)
* @param where The where clause
* @param unique If the index should be a unique index
*
* @example
* createPsqlIndexStatementHelper({
@@ -32,16 +33,22 @@ export function createPsqlIndexStatementHelper({
columns,
type,
where,
unique,
}: {
name: string
tableName: string
columns: string | string[]
type?: string
where?: string
unique?: boolean
}) {
columns = Array.isArray(columns) ? columns.join(", ") : columns
const typeStr = type ? ` USING ${type}` : ""
const optionsStr = where ? ` WHERE ${where}` : ""
return `CREATE INDEX IF NOT EXISTS ${name} ON ${tableName}${typeStr} (${columns})${optionsStr}`
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}`
}
}