Skip to content

Commit 697374b

Browse files
committed
Gracefully skip unsupported AST analysis
1 parent b32028b commit 697374b

5 files changed

Lines changed: 35 additions & 6 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# main
22

3+
- Gracefully skip optional AST-based query analysis when `pgsql-ast-parser` cannot parse valid PostgreSQL syntax, so generation can continue with database-derived types.
4+
35
# 3.0.0
46

57
- BREAKING: Require ReScript `>=12.0.0`.

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/query/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "pgtyped-rescript-query",
3-
"version": "3.0.0",
3+
"version": "3.0.1",
44
"type": "module",
55
"exports": {
66
".": {

packages/query/src/actions.test.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { generateHash, reduceTypeRows } from './actions.js';
1+
import {
2+
generateHash,
3+
parseQueryForAnalysis,
4+
reduceTypeRows,
5+
} from './actions.js';
26

37
test('test postgres md5 hash generation', () => {
48
const salt = [0x81, 0xcc, 0x95, 0x8b];
@@ -195,3 +199,10 @@ test('reduce type rows to MappableTypes', () => {
195199
]),
196200
).toMatchSnapshot();
197201
});
202+
203+
test('query analysis gracefully degrades for unsupported PostgreSQL syntax', () => {
204+
expect(
205+
parseQueryForAnalysis('with a as materialized (select 1) select * from a'),
206+
).toBeNull();
207+
expect(parseQueryForAnalysis('select from')).toBeNull();
208+
});

packages/query/src/actions.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,18 @@ export function getAliasedLiterals(
723723
return map;
724724
}
725725

726+
export function parseQueryForAnalysis(query: string): Statement[] | null {
727+
try {
728+
return parse(query);
729+
} catch (err) {
730+
debugQuery(
731+
'Skipping AST-based query analysis because pgsql-ast-parser failed: %o',
732+
err,
733+
);
734+
return null;
735+
}
736+
}
737+
726738
async function extraParameterInfo(query: Statement[]) {
727739
const paramsInfo = new Map<
728740
number,
@@ -789,9 +801,13 @@ export async function getTypes(
789801
const commentRows = await getComments(fields, queue);
790802
const checkRows = await getCheckConstraints(fields, queue);
791803
const typeMap = reduceTypeRows(typeRows);
792-
const parsedQuery = parse(queryData.query);
793-
const aliasedLiterals = getAliasedLiterals(parsedQuery);
794-
const paramsInfo = await extraParameterInfo(parsedQuery);
804+
const parsedQuery = parseQueryForAnalysis(queryData.query);
805+
const aliasedLiterals =
806+
parsedQuery == null
807+
? new Map<string, ConstraintValue[]>()
808+
: getAliasedLiterals(parsedQuery);
809+
const paramsInfo =
810+
parsedQuery == null ? new Map() : await extraParameterInfo(parsedQuery);
795811

796812
const attrMatcher = ({
797813
tableOID,

0 commit comments

Comments
 (0)