Skip to content

Commit c99405d

Browse files
committed
utils
1 parent a0ced19 commit c99405d

File tree

14 files changed

+15361
-6154
lines changed

14 files changed

+15361
-6154
lines changed

.github/workflows/run-tests.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,8 @@ jobs:
2222
- name: proto-parser
2323
run: cd ./packages/proto-parser && yarn test
2424

25+
- name: utils
26+
run: cd ./packages/utils && yarn test
27+
2528
- name: transform
2629
run: cd ./packages/transform && yarn test

__fixtures__/generated/generated.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21148,6 +21148,8 @@
2114821148
"misc/cascades-23.sql": "DROP TRANSFORM IF EXISTS FOR some_type LANGUAGE plpgsql CASCADE",
2114921149
"misc/cascades-24.sql": "ALTER TABLE some_table DROP COLUMN some_column CASCADE",
2115021150
"misc/cascades-25.sql": "ALTER TABLE some_table DROP CONSTRAINT some_constraint CASCADE",
21151+
"misc/booleans-cast-1.sql": "SELECT * FROM myschema.mytable WHERE a = TRUE",
21152+
"misc/booleans-cast-2.sql": "SELECT * FROM myschema.mytable WHERE a = CAST('t' AS boolean)",
2115121153
"latest/postgres/create_view-1.sql": "CREATE FUNCTION interpt_pp(path, path)\n RETURNS point\n AS 'regresslib'\n LANGUAGE C STRICT",
2115221154
"latest/postgres/create_view-2.sql": "CREATE TABLE real_city (\n\tpop\t\t\tint4,\n\tcname\t\ttext,\n\toutline \tpath\n)",
2115321155
"latest/postgres/create_view-3.sql": "COPY real_city FROM 'filename'",

__fixtures__/generated/upstream-diff.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,6 +1071,18 @@
10711071
"original": "DROP SUBSCRIPTION IF EXISTS some_sub CASCADE",
10721072
"upstream": "DROP SUBSCRIPTION IF EXISTS some_sub"
10731073
},
1074+
"misc/booleans-cast-1.sql": {
1075+
"original": "SELECT * FROM myschema.mytable WHERE a = TRUE",
1076+
"upstream": "SELECT * FROM myschema.mytable WHERE a = false"
1077+
},
1078+
"misc/booleans-cast-2.sql": {
1079+
"original": "SELECT * FROM myschema.mytable WHERE a = CAST('t' AS boolean)",
1080+
"upstream": "SELECT * FROM myschema.mytable WHERE a = true"
1081+
},
1082+
"misc/booleans-cast-3.sql": {
1083+
"original": "SELECT * FROM myschema.mytable WHERE a = 't'::boolean",
1084+
"upstream": "SELECT * FROM myschema.mytable WHERE a = true"
1085+
},
10741086
"latest/postgres/create_view-1.sql": {
10751087
"original": "CREATE FUNCTION interpt_pp(path, path)\n RETURNS point\n AS 'regresslib'\n LANGUAGE C STRICT",
10761088
"upstream": "CREATE FUNCTION interpt_pp(path, path) RETURNS point AS $$regresslib$$ LANGUAGE c CALLED ON NULL INPUT"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
SELECT * FROM myschema.mytable WHERE a = TRUE;
2+
SELECT * FROM myschema.mytable WHERE a = CAST('t' AS boolean);
3+
SELECT * FROM myschema.mytable WHERE a = 't'::boolean;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
import { FixtureTestUtils } from '../../test-utils';
3+
const fixtures = new FixtureTestUtils();
4+
5+
it('misc-booleans-cast', async () => {
6+
await fixtures.runFixtureTests([
7+
"misc/booleans-cast-1.sql",
8+
"misc/booleans-cast-2.sql"
9+
]);
10+
});

packages/utils/__test__/__snapshots__/utils.test.ts.snap

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,6 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`dynamic creation of tables 1`] = `
4-
"CREATE TABLE users (
5-
id int PRIMARY KEY,
6-
username text,
7-
email text UNIQUE,
8-
created_at timestamp NOT NULL
9-
)"
10-
`;
11-
12-
exports[`getEnumValue snapshots 1`] = `"AEXPR_OP"`;
13-
14-
exports[`getEnumValue snapshots 2`] = `"AEXPR_OP_ANY"`;
15-
16-
exports[`getEnumValue snapshots 3`] = `"AEXPR_OP_ALL"`;
17-
18-
exports[`getEnumValue snapshots 4`] = `"AEXPR_DISTINCT"`;
19-
20-
exports[`getEnumValue snapshots 5`] = `"AEXPR_NOT_DISTINCT"`;
21-
22-
exports[`getEnumValue snapshots 6`] = `"AEXPR_NULLIF"`;
3+
exports[`dynamic creation of tables 1`] = `"CREATE TABLE users (id int PRIMARY KEY, username text, email text UNIQUE, created_at timestamp NOT NULL)"`;
234

245
exports[`simple SelectStmt 1`] = `
256
{

packages/utils/__test__/utils.test.ts.no.run renamed to packages/utils/__test__/utils.test.ts

Lines changed: 32 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,18 @@
1-
import * as u from '../src';
2-
import ast, { SelectStmt, RangeVar } from '../src';
1+
import * as t from '../src';
2+
import { RangeVar, SelectStmt } from '@pgsql/types';
33
import { deparse } from 'pgsql-deparser';
44

5-
it('getEnumValue snapshots', () => {
6-
for (let i = 0; i <= 5; i++) {
7-
expect(u.getEnumValue('A_Expr_Kind', i)).toMatchSnapshot();
8-
}
9-
});
10-
115
it('simple SelectStmt', () => {
12-
const stmt: SelectStmt = ast.selectStmt({
6+
const stmt: { SelectStmt: SelectStmt } = t.nodes.selectStmt({
137
targetList: [
14-
ast.resTarget({
15-
val: ast.columnRef({
16-
fields: [ast.aStar()]
8+
t.nodes.resTarget({
9+
val: t.nodes.columnRef({
10+
fields: [t.nodes.aStar()]
1711
})
1812
})
1913
],
2014
fromClause: [
21-
ast.rangeVar({
15+
t.nodes.rangeVar({
2216
relname: 'some_table',
2317
inh: true,
2418
relpersistence: 'p'
@@ -28,44 +22,43 @@ it('simple SelectStmt', () => {
2822
op: 'SETOP_NONE'
2923
});
3024

31-
// @ts-ignore (because of optional args)
32-
stmt.SelectStmt.fromClause[0].RangeVar.relname = 'another_table';
25+
(stmt.SelectStmt.fromClause[0] as {RangeVar: RangeVar}).RangeVar.relname = 'another_table';
3326

3427
expect(stmt).toMatchSnapshot();
3528
expect(deparse(stmt, {})).toMatchSnapshot();
3629
});
3730

3831
it('SelectStmt with WHERE clause', () => {
39-
const selectStmt: SelectStmt = ast.selectStmt({
32+
const selectStmt: { SelectStmt: SelectStmt } = t.nodes.selectStmt({
4033
targetList: [
41-
ast.resTarget({
42-
val: ast.columnRef({
43-
fields: [ast.aStar()]
34+
t.nodes.resTarget({
35+
val: t.nodes.columnRef({
36+
fields: [t.nodes.aStar()]
4437
})
4538
})
4639
],
4740
fromClause: [
48-
ast.rangeVar({
41+
t.nodes.rangeVar({
4942
schemaname: 'myschema',
5043
relname: 'mytable',
5144
inh: true,
5245
relpersistence: 'p'
5346
})
5447
],
55-
whereClause: ast.aExpr({
48+
whereClause: t.nodes.aExpr({
5649
kind: 'AEXPR_OP',
57-
name: [ast.string({ str: '=' })],
58-
lexpr: ast.columnRef({
59-
fields: [ast.string({ str: 'a' })]
50+
name: [t.nodes.string({ sval: '=' })],
51+
lexpr: t.nodes.columnRef({
52+
fields: [t.nodes.string({ sval: 'a' })]
6053
}),
61-
rexpr: ast.typeCast({
62-
arg: ast.aConst({
63-
val: ast.string({ str: 't' })
54+
rexpr: t.nodes.typeCast({
55+
arg: t.nodes.aConst({
56+
sval: t.ast.string({ sval: 't' })
6457
}),
65-
typeName: ast.typeName({
58+
typeName: t.ast.typeName({
6659
names: [
67-
ast.string({ str: 'pg_catalog' }),
68-
ast.string({ str: 'bool' })
60+
t.nodes.string({ sval: 'pg_catalog' }),
61+
t.nodes.string({ sval: 'bool' })
6962
],
7063
typemod: -1
7164
})
@@ -74,7 +67,8 @@ it('SelectStmt with WHERE clause', () => {
7467
limitOption: 'LIMIT_OPTION_DEFAULT',
7568
op: 'SETOP_NONE'
7669
});
77-
expect(deparse(selectStmt, {})).toEqual('SELECT * FROM myschema.mytable WHERE a = TRUE');
70+
71+
expect(deparse(selectStmt, {})).toEqual(`SELECT * FROM myschema.mytable WHERE a = CAST('t' AS boolean)`);
7872
});
7973

8074
it('dynamic creation of tables', () => {
@@ -90,19 +84,19 @@ it('dynamic creation of tables', () => {
9084
};
9185

9286
// Construct the CREATE TABLE statement
93-
const createStmt = ast.createStmt({
94-
relation: ast.rangeVar({
87+
const createStmt = t.nodes.createStmt({
88+
relation: t.ast.rangeVar({
9589
relname: schema.tableName,
9690
inh: true,
9791
relpersistence: 'p'
98-
}).RangeVar as RangeVar, // special case due to PG AST
99-
tableElts: schema.columns.map(column => ast.columnDef({
92+
}),
93+
tableElts: schema.columns.map(column => t.nodes.columnDef({
10094
colname: column.name,
101-
typeName: ast.typeName({
102-
names: [ast.string({ str: column.type })]
95+
typeName: t.ast.typeName({
96+
names: [t.nodes.string({ sval: column.type })]
10397
}),
10498
constraints: column.constraints?.map(constraint =>
105-
ast.constraint({
99+
t.nodes.constraint({
106100
contype: constraint === "PRIMARY KEY" ? "CONSTR_PRIMARY" : constraint === "UNIQUE" ? "CONSTR_UNIQUE" : "CONSTR_NOTNULL"
107101
})
108102
)

packages/utils/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
"test:watch": "jest --watch"
3232
},
3333
"devDependencies": {
34-
"pg-proto-parser": "^1.28.2"
34+
"pg-proto-parser": "^1.28.2",
35+
"pgsql-deparser": "^17.6.2"
3536
},
3637
"dependencies": {
3738
"@pgsql/types": "^17.5.3",

packages/utils/scripts/pg-proto-parser.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,26 @@ const options: PgProtoParserOptions = {
99
types: {
1010
enabled: false
1111
},
12+
enums: {
13+
enabled: false,
14+
},
1215
utils: {
16+
enums: {
17+
enabled: false,
18+
},
1319
astHelpers: {
1420
enabled: true,
15-
typesSource: '@pgsql/types'
21+
typesSource: '@pgsql/types',
22+
},
23+
wrappedAstHelpers: {
24+
enabled: true,
25+
filename: 'wrapped.ts'
1626
}
27+
},
28+
runtimeSchema: {
29+
enabled: true,
30+
filename: 'runtime-schema.ts',
31+
format: 'typescript'
1732
}
1833
};
1934
const parser = new PgProtoParser(inFile, options);

0 commit comments

Comments
 (0)