Skip to content

Commit 0120cdf

Browse files
committed
feat(formatter): upgrade sql-formatter from 2 to 4
1 parent 7e46b20 commit 0120cdf

File tree

8 files changed

+940
-601
lines changed

8 files changed

+940
-601
lines changed

.eslintrc

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
"oclif",
44
"oclif-typescript",
55
"plugin:prettier/recommended",
6-
"prettier/standard",
7-
"prettier/@typescript-eslint",
86
"plugin:jest/recommended"
97
]
108
}

.prettierignore

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,3 @@ coverage
88

99
# Misc
1010
te?mp
11-
12-
# Yarn
13-
.yarn
14-
.pnp.*

.yarn/install-state.gz

-1.16 MB
Binary file not shown.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
A CLI tool to convert query from PostgreSQL to BigQuery
44

5+
[![deepcode](https://www.deepcode.ai/api/gh/badge?key=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJwbGF0Zm9ybTEiOiJnaCIsIm93bmVyMSI6Imhja2hhbmgiLCJyZXBvMSI6InBnMmJpZ3F1ZXJ5IiwiaW5jbHVkZUxpbnQiOmZhbHNlLCJhdXRob3JJZCI6Mjg0MzQsImlhdCI6MTYxNzQ5NzY2MX0.RKs5KwUX_1aAQz-K1LyWD7HvjGi3zcIHhb-CVdZckE4)](https://www.deepcode.ai/app/gh/hckhanh/pg2bigquery/_/dashboard?utm_content=gh%2Fhckhanh%2Fpg2bigquery)
56
![Release](https://github.com/hckhanh/pg2bigquery/workflows/Release/badge.svg)
67
[![Language grade: JavaScript](https://img.shields.io/lgtm/grade/javascript/g/hckhanh/pg2bigquery.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/hckhanh/pg2bigquery/context:javascript)
78
[![oclif](https://img.shields.io/badge/cli-oclif-brightgreen.svg)](https://oclif.io)

__tests__/core.ts

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import sqlFormatter from "sql-formatter";
1+
import { format } from "sql-formatter";
22
import { postgres2Bigquery } from "../src/core";
33
import { getTableRegexes } from "../src/utils";
44

@@ -25,24 +25,28 @@ describe("core", () => {
2525
`;
2626

2727
const bQuery = postgres2Bigquery(query, "demo_dataset", tableRegexes);
28-
expect(bQuery).toBe(sqlFormatter.format(expectedQuery));
28+
expect(bQuery).toBe(format(expectedQuery));
2929
});
3030

3131
it("should add prefix dataset to tables", async () => {
3232
const query = `
3333
SELECT created_at AS start_date, role, id
34-
FROM users, roles
35-
WHERE users.id = roles.user_id and role = 'admin';
34+
FROM users,
35+
roles
36+
WHERE users.id = roles.user_id
37+
and role = 'admin';
3638
`;
3739

3840
const expectedQuery = `
3941
SELECT created_at AS start_date, role, id
40-
FROM demo_dataset.users, demo_dataset.roles
41-
WHERE users.id = roles.user_id and role = 'admin';
42+
FROM demo_dataset.users,
43+
demo_dataset.roles
44+
WHERE users.id = roles.user_id
45+
and role = 'admin';
4246
`;
4347

4448
const bQuery = postgres2Bigquery(query, "demo_dataset", tableRegexes);
45-
expect(bQuery).toBe(sqlFormatter.format(expectedQuery));
49+
expect(bQuery).toBe(format(expectedQuery));
4650
});
4751

4852
it("should convert time calculations", async () => {
@@ -55,11 +59,12 @@ describe("core", () => {
5559
const expectedQuery = `
5660
SELECT *
5761
FROM demo_dataset.users
58-
WHERE created_at > (TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 30 DAY)) and created_at <= (TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 180 DAY));
62+
WHERE created_at > (TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 30 DAY))
63+
and created_at <= (TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 180 DAY));
5964
`;
6065

6166
const bQuery = postgres2Bigquery(query, "demo_dataset", tableRegexes);
62-
expect(bQuery).toBe(sqlFormatter.format(expectedQuery));
67+
expect(bQuery).toBe(format(expectedQuery));
6368
});
6469

6570
it("should find products with product id array", async () => {
@@ -76,7 +81,7 @@ describe("core", () => {
7681
`;
7782

7883
const bQuery = postgres2Bigquery(query, "demo_dataset", tableRegexes);
79-
expect(bQuery).toBe(sqlFormatter.format(expectedQuery));
84+
expect(bQuery).toBe(format(expectedQuery));
8085
});
8186

8287
it("should find products not in product id array", async () => {
@@ -93,24 +98,26 @@ describe("core", () => {
9398
`;
9499

95100
const bQuery = postgres2Bigquery(query, "demo_dataset", tableRegexes);
96-
expect(bQuery).toBe(sqlFormatter.format(expectedQuery));
101+
expect(bQuery).toBe(format(expectedQuery));
97102
});
98103

99104
it("should remove quotes for all numbers", async () => {
100105
const query = `
101106
SELECT *
102107
FROM products
103-
WHERE id = '9' or id = 10;
108+
WHERE id = '9'
109+
or id = 10;
104110
`;
105111

106112
const expectedQuery = `
107113
SELECT *
108114
FROM demo_dataset.products
109-
WHERE id = 9 or id = 10;
115+
WHERE id = 9
116+
or id = 10;
110117
`;
111118

112119
const bQuery = postgres2Bigquery(query, "demo_dataset", tableRegexes);
113-
expect(bQuery).toBe(sqlFormatter.format(expectedQuery));
120+
expect(bQuery).toBe(format(expectedQuery));
114121
});
115122

116123
it("should convert date_part function", async () => {
@@ -125,37 +132,37 @@ describe("core", () => {
125132
`;
126133

127134
const bQuery = postgres2Bigquery(query, "demo_dataset", tableRegexes);
128-
expect(bQuery).toBe(sqlFormatter.format(expectedQuery));
135+
expect(bQuery).toBe(format(expectedQuery));
129136
});
130137

131138
it("should cast else case of date_part to string", async () => {
132139
const query = `
133-
SELECT
134-
id,
135-
name,
136-
date_part('day'::text, created_at) AS year,
140+
SELECT id,
141+
name,
142+
date_part('day'::text, created_at) AS year,
137143
CASE
138144
WHEN date_part('month', created_at) < 10
139145
THEN concat('0', date_part('month', created_at))
140146
ELSE date_part('month', created_at)
141-
END AS month
147+
END
148+
AS month
142149
FROM products;
143150
`;
144151

145152
const expectedQuery = `
146-
SELECT
147-
id,
148-
name,
149-
EXTRACT(DAY FROM created_at) AS year,
153+
SELECT id,
154+
name,
155+
EXTRACT(DAY FROM created_at) AS year,
150156
CASE
151157
WHEN EXTRACT(MONTH FROM created_at) < 10 THEN concat(0, EXTRACT(MONTH FROM created_at))
152158
ELSE CAST(EXTRACT(MONTH FROM created_at) AS STRING)
153-
END AS month
159+
END
160+
AS month
154161
FROM
155162
demo_dataset.products;
156163
`;
157164

158165
const bQuery = postgres2Bigquery(query, "demo_dataset", tableRegexes);
159-
expect(bQuery).toBe(sqlFormatter.format(expectedQuery));
166+
expect(bQuery).toBe(format(expectedQuery));
160167
});
161168
});

package.json

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,33 @@
1010
"dependencies": {
1111
"@oclif/command": "^1.8.0",
1212
"@oclif/config": "^1.17.0",
13-
"@oclif/plugin-help": "^3.2.0",
13+
"@oclif/plugin-help": "^3.2.2",
1414
"cli-ux": "^5.5.1",
15-
"sql-formatter": "^2.3.3",
15+
"sql-formatter": "^4.0.2",
1616
"tslib": "^2.0.3"
1717
},
1818
"devDependencies": {
19-
"@babel/core": "^7.12.10",
20-
"@babel/preset-env": "^7.12.10",
21-
"@babel/preset-typescript": "^7.12.7",
19+
"@babel/core": "^7.13.14",
20+
"@babel/preset-env": "^7.13.12",
21+
"@babel/preset-typescript": "^7.13.0",
2222
"@oclif/dev-cli": "^1.26.0",
23-
"@types/jest": "^26.0.19",
24-
"@types/node": "^14.14.12",
25-
"@types/sql-formatter": "^2.3.0",
23+
"@types/jest": "^26",
24+
"@types/node": "^14",
25+
"@types/sql-formatter": "^2",
2626
"babel-jest": "^26.6.3",
27-
"eslint": "^7.15.0",
27+
"eslint": "^7.23.0",
2828
"eslint-config-oclif": "^3.1",
2929
"eslint-config-oclif-typescript": "^0.2.0",
30-
"eslint-config-prettier": "^7.0.0",
31-
"eslint-plugin-jest": "^24.1.3",
32-
"eslint-plugin-prettier": "^3.2.0",
30+
"eslint-config-prettier": "^8.1.0",
31+
"eslint-plugin-jest": "^24.3.3",
32+
"eslint-plugin-prettier": "^3.3.1",
3333
"jest": "^26.6.3",
3434
"jest-get-type": "^26.3.0",
3535
"prettier": "2.2.1",
3636
"rimraf": "^3.0.2",
37-
"semantic-release": "^17.3.0",
37+
"semantic-release": "^17.4.2",
3838
"ts-node": "^9.1.1",
39-
"typescript": "4.1.3"
39+
"typescript": "4.2.3"
4040
},
4141
"engines": {
4242
"node": ">=12"

src/core.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import sqlFormatter from "sql-formatter";
1+
import { format } from "sql-formatter";
22
import { dataTypeRegexes } from "./postgres";
33

44
const timeOperators: { [operator: string]: string } = {
@@ -105,7 +105,7 @@ export function postgres2Bigquery(
105105
dataset: string,
106106
tableRegexes: RegExp[]
107107
): string {
108-
let bQuery = sqlFormatter.format(pgQuery);
108+
let bQuery = format(pgQuery, { language: "postgresql" });
109109

110110
bQuery = removeTypeCasts(bQuery);
111111
bQuery = addPrefixDatasetToTables(tableRegexes, bQuery, dataset);
@@ -116,5 +116,5 @@ export function postgres2Bigquery(
116116
bQuery = convertDatePartFunction(bQuery);
117117
bQuery = castElseCaseToString(bQuery);
118118

119-
return sqlFormatter.format(bQuery);
119+
return format(bQuery);
120120
}

0 commit comments

Comments
 (0)