Skip to content

Commit 707b701

Browse files
committed
Format CREATE SUBSCRIPTION
1 parent dfd9415 commit 707b701

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

src/syntax/subscription.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { AllSubscriptionNodes } from "sql-parser-cst";
2+
import { group, indent, line, join } from "../print_utils";
3+
import { CstToDocMap } from "../CstToDocMap";
4+
5+
export const subscriptionMap: Partial<CstToDocMap<AllSubscriptionNodes>> = {
6+
create_subscription_stmt: (print, node) =>
7+
group(
8+
join(line, [
9+
print.spaced(["createSubscriptionKw", "name"]),
10+
print.spaced(["connectionKw", "connectionInfo"]),
11+
print.spaced(["publicationKw", "publications"]),
12+
...(node.with ? [print.spaced(["with"])] : []),
13+
]),
14+
),
15+
};

src/syntax/transformMap.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import { publicationMap } from "./publication";
3737
import { schemaMap } from "./schema";
3838
import { selectMap } from "./select";
3939
import { sequenceMap } from "./sequence";
40+
import { subscriptionMap } from "./subscription";
4041
import { transactionMap } from "./transaction";
4142
import { triggerMap } from "./trigger";
4243
import { truncateMap } from "./truncate";
@@ -88,6 +89,7 @@ export const transformMap: Partial<CstToDocMap<Node>> = {
8889
...schemaMap,
8990
...selectMap,
9091
...sequenceMap,
92+
...subscriptionMap,
9193
...transactionMap,
9294
...triggerMap,
9395
...truncateMap,
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import dedent from "dedent-js";
2+
import { testPostgresql } from "../test_utils";
3+
4+
describe("subscriptions", () => {
5+
describe("CREATE SUBSCRIPTION", () => {
6+
it(`formats CREATE SUBSCRIPTION to single line if fits`, async () => {
7+
await testPostgresql(dedent`
8+
CREATE SUBSCRIPTION my_sub CONNECTION 'con' PUBLICATION my_pub
9+
`);
10+
});
11+
12+
it(`formats CREATE SUBSCRIPTION to multiple lines`, async () => {
13+
await testPostgresql(dedent`
14+
CREATE SUBSCRIPTION my_subscription
15+
CONNECTION 'host=192.168.1.50 port=5432 user=foo dbname=foodb'
16+
PUBLICATION my_publication
17+
`);
18+
});
19+
20+
it(`formats CREATE SUBSCRIPTION to multiple lines`, async () => {
21+
await testPostgresql(dedent`
22+
CREATE SUBSCRIPTION my_subscription
23+
CONNECTION 'host=192.168.1.50 port=5432 user=foo dbname=foodb'
24+
PUBLICATION my_publication
25+
`);
26+
});
27+
28+
it(`formats WITH clause`, async () => {
29+
await testPostgresql(dedent`
30+
CREATE SUBSCRIPTION my_subscription
31+
CONNECTION 'host=192.168.1.50 port=5432 user=foo dbname=foodb'
32+
PUBLICATION my_publication
33+
WITH (param1 = 1, param2 = 2)
34+
`);
35+
});
36+
});
37+
});

0 commit comments

Comments
 (0)