Skip to content

Commit

Permalink
Format ALTER TABLE more dynamically
Browse files Browse the repository at this point in the history
  • Loading branch information
nene committed Jan 19, 2024
1 parent a3a84e5 commit bac1d9e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
13 changes: 7 additions & 6 deletions src/syntax/alter_table.ts
Original file line number Diff line number Diff line change
@@ -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<AlterTableStmt | AllAlterActionNodes> =
{
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"]),
Expand Down
9 changes: 3 additions & 6 deletions test/canonical_syntax.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ describe("with sqlCanonicalSyntax enabled", () => {
sqlCanonicalSyntax: true,
}),
).toBe(dedent`
ALTER TABLE foo
RENAME TO bar
ALTER TABLE foo RENAME TO bar
`);
});

Expand All @@ -57,8 +56,7 @@ describe("with sqlCanonicalSyntax enabled", () => {
sqlCanonicalSyntax: true,
}),
).toBe(dedent`
ALTER TABLE foo
RENAME TO bar
ALTER TABLE foo RENAME TO bar
`);
});

Expand Down Expand Up @@ -106,8 +104,7 @@ describe("with sqlCanonicalSyntax enabled", () => {
sqlCanonicalSyntax: true,
}),
).toBe(dedent`
DELETE FROM client
WHERE id = 10
DELETE FROM client WHERE id = 10
`);
});

Expand Down
8 changes: 7 additions & 1 deletion test/ddl/alter_table.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit bac1d9e

Please sign in to comment.