Handle embedded pg schema name inside the table name when generating indexes (#7774)
This commit is contained in:
@@ -89,4 +89,42 @@ describe("createPsqlIndexStatementHelper", function () {
|
||||
}" (${options.columns.join(", ")}) WHERE ${options.where}`
|
||||
)
|
||||
})
|
||||
|
||||
it("should generate index on an explicit pg schema", function () {
|
||||
const options = {
|
||||
name: "index_name",
|
||||
tableName: "public.table_name",
|
||||
columns: "column_name",
|
||||
}
|
||||
|
||||
const indexStatement = createPsqlIndexStatementHelper(options)
|
||||
expect(indexStatement + "").toEqual(
|
||||
`CREATE INDEX IF NOT EXISTS "${options.name}" ON "public"."table_name" (${options.columns})`
|
||||
)
|
||||
})
|
||||
|
||||
it("generate index name from table name when using explicit pg schema", function () {
|
||||
const options = {
|
||||
tableName: "public.table_name",
|
||||
columns: "column_name",
|
||||
}
|
||||
|
||||
const indexStatement = createPsqlIndexStatementHelper(options)
|
||||
expect(indexStatement + "").toEqual(
|
||||
`CREATE INDEX IF NOT EXISTS "IDX_table_name_column_name" ON "public"."table_name" (${options.columns})`
|
||||
)
|
||||
})
|
||||
|
||||
it("should generate index when table name was previously formatted to allow pg schema name", function () {
|
||||
const options = {
|
||||
name: "index_name",
|
||||
tableName: 'public"."table_name',
|
||||
columns: "column_name",
|
||||
}
|
||||
|
||||
const indexStatement = createPsqlIndexStatementHelper(options)
|
||||
expect(indexStatement + "").toEqual(
|
||||
`CREATE INDEX IF NOT EXISTS "${options.name}" ON "public"."table_name" (${options.columns})`
|
||||
)
|
||||
})
|
||||
})
|
||||
@@ -31,7 +31,7 @@ import { Index } from "@mikro-orm/core"
|
||||
*/
|
||||
export function createPsqlIndexStatementHelper({
|
||||
name,
|
||||
tableName,
|
||||
tableName: qualifiedName,
|
||||
columns,
|
||||
type,
|
||||
where,
|
||||
@@ -45,6 +45,20 @@ export function createPsqlIndexStatementHelper({
|
||||
unique?: boolean
|
||||
}) {
|
||||
const columnsName = Array.isArray(columns) ? columns.join("_") : columns
|
||||
const tokens = qualifiedName.replace(/"/g, "").split(".")
|
||||
|
||||
let pgSchemaName: string | undefined
|
||||
let tableName: string
|
||||
let tableReference: string
|
||||
|
||||
if (tokens.length > 1) {
|
||||
pgSchemaName = tokens.shift()
|
||||
tableName = tokens.join(".")
|
||||
tableReference = `"${pgSchemaName}"."${tableName}"`
|
||||
} else {
|
||||
tableName = qualifiedName
|
||||
tableReference = `"${tableName}"`
|
||||
}
|
||||
|
||||
columns = Array.isArray(columns) ? columns.join(", ") : columns
|
||||
name = name || `IDX_${tableName}_${columnsName}${unique ? "_unique" : ""}`
|
||||
@@ -53,7 +67,7 @@ export function createPsqlIndexStatementHelper({
|
||||
const optionsStr = where ? ` WHERE ${where}` : ""
|
||||
const uniqueStr = unique ? "UNIQUE " : ""
|
||||
|
||||
const expression = `CREATE ${uniqueStr}INDEX IF NOT EXISTS "${name}" ON "${tableName}"${typeStr} (${columns})${optionsStr}`
|
||||
const expression = `CREATE ${uniqueStr}INDEX IF NOT EXISTS "${name}" ON ${tableReference}${typeStr} (${columns})${optionsStr}`
|
||||
return {
|
||||
toString: () => {
|
||||
return expression
|
||||
|
||||
Reference in New Issue
Block a user