From b227816b230029e215eec7d17d98fe8d676f01dd Mon Sep 17 00:00:00 2001 From: taozhi8833998 Date: Tue, 4 Nov 2025 10:07:46 +0800 Subject: [PATCH] fix: sqlify index_columns multiple order attributes in pg --- src/column.js | 4 ++-- src/command.js | 6 +++--- src/tables.js | 2 +- test/postgres.spec.js | 7 +++++++ 4 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/column.js b/src/column.js index f590a13f..c2d79108 100644 --- a/src/column.js +++ b/src/column.js @@ -135,8 +135,8 @@ function columnOption(definition) { } function columnOrderToSQL(columnOrder) { - const { column, collate, nulls, opclass, order_by } = columnOrder - const columnExpr = typeof column === 'string' ? { type: 'column_ref', table: columnOrder.table, column } : columnOrder + const { order_by, column, collate, nulls, opclass } = columnOrder + const columnExpr = typeof column === 'string' ? { type: 'column_ref', table: columnOrder.table, column } : ({ ...columnOrder, order_by: null }) columnExpr.collate = null const result = [ exprToSQL(columnExpr), diff --git a/src/command.js b/src/command.js index 36577dca..094480aa 100644 --- a/src/command.js +++ b/src/command.js @@ -175,8 +175,8 @@ function ifToSQL(stmt) { if (elseifExpr) { result.push( elseifExpr.map( - elseif => [toUpper(elseif.type), exprToSQL(elseif.boolean_expr), 'THEN', astToSQL(elseif.then.ast || elseif.then), elseif.semicolon].filter(hasVal).join(' ') - ).join(' ') + elseif => [toUpper(elseif.type), exprToSQL(elseif.boolean_expr), 'THEN', astToSQL(elseif.then.ast || elseif.then), elseif.semicolon].filter(hasVal).join(' '), + ).join(' '), ) } if (elseExpr) result.push('ELSE', `${astToSQL(elseExpr.ast || elseExpr)}${semicolons[1]}`) @@ -207,7 +207,7 @@ function grantAndRevokeToSQL(stmt) { case 'priv': result.push( literalToSQL(on.object_type), - on.priv_level.map(privLevel => [identifierToSql(privLevel.prefix), identifierToSql(privLevel.name)].filter(hasVal).join('.')).join(', ') + on.priv_level.map(privLevel => [identifierToSql(privLevel.prefix), identifierToSql(privLevel.name)].filter(hasVal).join('.')).join(', '), ) break case 'proxy': diff --git a/src/tables.js b/src/tables.js index 55d1747b..7c9e9a1c 100644 --- a/src/tables.js +++ b/src/tables.js @@ -13,7 +13,7 @@ function unnestToSQL(unnestExpr) { commonOptionConnector( toUpper(withOffset && withOffset.keyword), identifierToSql, - withOffset && withOffset.as + withOffset && withOffset.as, ), ] return result.filter(hasVal).join(' ') diff --git a/test/postgres.spec.js b/test/postgres.spec.js index 6ec00018..df5dad18 100644 --- a/test/postgres.spec.js +++ b/test/postgres.spec.js @@ -2360,6 +2360,13 @@ describe('Postgres', () => { 'CREATE TEMP TABLE "test" (id INT, price MONEY)' ] }, + { + title: 'create index', + sql: [ + `CREATE INDEX "test" ON "test" USING BTREE ("a" ASC, "b" ASC, "c" DESC);`, + 'CREATE INDEX "test" ON "test" USING BTREE ("a" ASC, "b" ASC, "c" DESC)' + ] + } ] neatlyNestTestedSQL(SQL_LIST) })