@@ -3233,13 +3233,14 @@ export class PostgresQueryRunner
32333233 const indicesSql =
32343234 `SELECT "ns"."nspname" AS "table_schema", "t"."relname" AS "table_name", "i"."relname" AS "constraint_name", "a"."attname" AS "column_name", ` +
32353235 `CASE "ix"."indisunique" WHEN 't' THEN 'TRUE' ELSE'FALSE' END AS "is_unique", pg_get_expr("ix"."indpred", "ix"."indrelid") AS "condition", ` +
3236- `"types"."typname" AS "type_name" ` +
3236+ `"types"."typname" AS "type_name", "am"."amname" AS "index_type" ` +
32373237 `FROM "pg_class" "t" ` +
32383238 `INNER JOIN "pg_index" "ix" ON "ix"."indrelid" = "t"."oid" ` +
32393239 `INNER JOIN "pg_attribute" "a" ON "a"."attrelid" = "t"."oid" AND "a"."attnum" = ANY ("ix"."indkey") ` +
32403240 `INNER JOIN "pg_namespace" "ns" ON "ns"."oid" = "t"."relnamespace" ` +
32413241 `INNER JOIN "pg_class" "i" ON "i"."oid" = "ix"."indexrelid" ` +
32423242 `INNER JOIN "pg_type" "types" ON "types"."oid" = "a"."atttypid" ` +
3243+ `INNER JOIN "pg_am" "am" ON "i"."relam" = "am"."oid" ` +
32433244 `LEFT JOIN "pg_constraint" "cnst" ON "cnst"."conname" = "i"."relname" ` +
32443245 `WHERE "t"."relkind" IN ('m') AND "cnst"."contype" IS NULL AND (${ constraintsCondition } )`
32453246
@@ -3287,13 +3288,16 @@ export class PostgresQueryRunner
32873288 constraint [ "constraint_name" ]
32883289 )
32893290 } )
3291+
32903292 return new TableIndex ( < TableIndexOptions > {
32913293 view : view ,
32923294 name : constraint [ "constraint_name" ] ,
32933295 columnNames : indices . map ( ( i ) => i [ "column_name" ] ) ,
32943296 isUnique : constraint [ "is_unique" ] === "TRUE" ,
32953297 where : constraint [ "condition" ] ,
3298+ isSpatial : constraint [ "index_type" ] === "gist" ,
32963299 isFulltext : false ,
3300+ type : constraint [ "index_type" ] ,
32973301 } )
32983302 } )
32993303 return view
@@ -4028,6 +4032,7 @@ export class PostgresQueryRunner
40284032 isUnique : constraint [ "is_unique" ] === "TRUE" ,
40294033 where : constraint [ "condition" ] ,
40304034 isSpatial : constraint [ "index_type" ] === "gist" ,
4035+ type : constraint [ "index_type" ] ,
40314036 isFulltext : false ,
40324037 } )
40334038 } )
@@ -4343,35 +4348,51 @@ export class PostgresQueryRunner
43434348 return new Query ( `DROP TYPE ${ enumName } ` )
43444349 }
43454350
4351+ /**
4352+ * Builds the SQL `USING <index_type>` clause based on the index type, prioritizing `isSpatial` as `GiST`.
4353+ */
4354+
4355+ private buildIndexTypeClause ( index : TableIndex ) {
4356+ const type = index . isSpatial ? "gist" : index . type
4357+
4358+ if ( typeof type !== "string" ) return null
4359+
4360+ return `USING ${ type } `
4361+ }
4362+
43464363 /**
43474364 * Builds create index sql.
43484365 */
43494366 protected createIndexSql ( table : Table , index : TableIndex ) : Query {
4367+ const indexTypeClause = this . buildIndexTypeClause ( index )
4368+
43504369 const columns = index . columnNames
43514370 . map ( ( columnName ) => `"${ columnName } "` )
43524371 . join ( ", " )
43534372 return new Query (
43544373 `CREATE ${ index . isUnique ? "UNIQUE " : "" } INDEX${
43554374 index . isConcurrent ? " CONCURRENTLY" : ""
43564375 } "${ index . name } " ON ${ this . escapePath ( table ) } ${
4357- index . isSpatial ? "USING GiST " : ""
4358- } (${ columns } ) ${ index . where ? "WHERE " + index . where : "" } `,
4376+ indexTypeClause ?? ""
4377+ } (${ columns } ) ${ index . where ? "WHERE " + index . where : "" } `,
43594378 )
43604379 }
43614380
43624381 /**
43634382 * Builds create view index sql.
43644383 */
43654384 protected createViewIndexSql ( view : View , index : TableIndex ) : Query {
4385+ const indexTypeClause = this . buildIndexTypeClause ( index )
4386+
43664387 const columns = index . columnNames
43674388 . map ( ( columnName ) => `"${ columnName } "` )
43684389 . join ( ", " )
43694390 return new Query (
43704391 `CREATE ${ index . isUnique ? "UNIQUE " : "" } INDEX "${
43714392 index . name
4372- } " ON ${ this . escapePath ( view ) } ( ${ columns } ) ${
4373- index . where ? "WHERE " + index . where : ""
4374- } `,
4393+ } " ON ${ this . escapePath ( view ) } ${
4394+ indexTypeClause ?? ""
4395+ } ( ${ columns } ) ${ index . where ? "WHERE " + index . where : "" } `,
43754396 )
43764397 }
43774398
0 commit comments