From bac1d9e052a7a694e48fd54a850675af7f5447f2 Mon Sep 17 00:00:00 2001 From: Rene Saarsoo Date: Fri, 19 Jan 2024 17:54:35 +0200 Subject: [PATCH] Format ALTER TABLE more dynamically --- src/syntax/alter_table.ts | 13 +++++++------ test/canonical_syntax.test.ts | 9 +++------ test/ddl/alter_table.test.ts | 8 +++++++- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/syntax/alter_table.ts b/src/syntax/alter_table.ts index f0329dd..ecb9b8f 100644 --- a/src/syntax/alter_table.ts +++ b/src/syntax/alter_table.ts @@ -1,14 +1,15 @@ import { AlterTableStmt, AllAlterActionNodes } from "sql-parser-cst"; import { CstToDocMap } from "../CstToDocMap"; -import { hardline } from "../print_utils"; +import { group, hardline } from "../print_utils"; export const alterTableMap: CstToDocMap = { - alter_table_stmt: (print) => [ - print.spaced(["alterTableKw", "ifExistsKw", "table"]), - hardline, - print("actions"), - ], + alter_table_stmt: (print) => + group([ + print.spaced(["alterTableKw", "ifExistsKw", "table"]), + print.dynamicLine(), + print("actions"), + ]), alter_action_rename_table: (print) => print.spaced(["renameKw", "newName"]), alter_action_rename_column: (print) => print.spaced(["renameKw", "ifExistsKw", "oldName", "toKw", "newName"]), diff --git a/test/canonical_syntax.test.ts b/test/canonical_syntax.test.ts index 8c3f16a..870c009 100644 --- a/test/canonical_syntax.test.ts +++ b/test/canonical_syntax.test.ts @@ -45,8 +45,7 @@ describe("with sqlCanonicalSyntax enabled", () => { sqlCanonicalSyntax: true, }), ).toBe(dedent` - ALTER TABLE foo - RENAME TO bar + ALTER TABLE foo RENAME TO bar `); }); @@ -57,8 +56,7 @@ describe("with sqlCanonicalSyntax enabled", () => { sqlCanonicalSyntax: true, }), ).toBe(dedent` - ALTER TABLE foo - RENAME TO bar + ALTER TABLE foo RENAME TO bar `); }); @@ -106,8 +104,7 @@ describe("with sqlCanonicalSyntax enabled", () => { sqlCanonicalSyntax: true, }), ).toBe(dedent` - DELETE FROM client - WHERE id = 10 + DELETE FROM client WHERE id = 10 `); }); diff --git a/test/ddl/alter_table.test.ts b/test/ddl/alter_table.test.ts index 59b16c0..f662dcd 100644 --- a/test/ddl/alter_table.test.ts +++ b/test/ddl/alter_table.test.ts @@ -2,7 +2,13 @@ import dedent from "dedent-js"; import { test, testBigquery, testMysql, testPostgresql } from "../test_utils"; describe("alter table", () => { - it(`formats ALTER TABLE..RENAME`, async () => { + it(`formats short ALTER TABLE..RENAME on a single line`, async () => { + await test(dedent` + ALTER TABLE client RENAME TO org_client + `); + }); + + it(`preserves ALTER TABLE..RENAME on muliple lines`, async () => { await test(dedent` ALTER TABLE client RENAME TO org_client