Skip to content

Commit 9f10c01

Browse files
committed
fixed error ranges and add trigger tests
1 parent 4280771 commit 9f10c01

File tree

6 files changed

+413
-25
lines changed

6 files changed

+413
-25
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
CREATE TABLE users_1 (
2+
id integer not null PRIMARY KEY,
3+
updated_at timestamp with time zone not null DEFAULT now()
4+
);
5+
CREATE TABLE users_2 (
6+
id integer not null PRIMARY KEY
7+
);
8+
CREATE TABLE users_3 (
9+
id integer not null PRIMARY KEY
10+
);
11+
12+
create or replace function update_updated_at_column ()
13+
returns trigger
14+
language plpgsql
15+
as $function$
16+
begin
17+
new.updated_at = NOW();
18+
return new;
19+
end;
20+
$function$;
21+
22+
create trigger update_users_3_modtime -- should raise error
23+
before update on users_3 for each row
24+
execute function update_updated_at_column ();
25+
26+
create trigger update_users_1_modtime
27+
before update on users_1 for each row
28+
execute function update_updated_at_column ();
29+
30+
create trigger update_users_2_modtime -- should raise error
31+
before update on users_2 for each row
32+
execute function update_updated_at_column ();
Lines changed: 328 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,328 @@
1+
[
2+
{
3+
"RawStmt": {
4+
"stmt": {
5+
"CreateStmt": {
6+
"relation": {
7+
"relname": "users_1",
8+
"inh": true,
9+
"relpersistence": "p"
10+
},
11+
"tableElts": [
12+
{
13+
"ColumnDef": {
14+
"colname": "id",
15+
"typeName": {
16+
"names": [
17+
{
18+
"String": {
19+
"str": "pg_catalog"
20+
}
21+
},
22+
{
23+
"String": {
24+
"str": "int4"
25+
}
26+
}
27+
],
28+
"typemod": -1
29+
},
30+
"is_local": true,
31+
"constraints": [
32+
{
33+
"Constraint": {
34+
"contype": "CONSTR_NOTNULL"
35+
}
36+
},
37+
{
38+
"Constraint": {
39+
"contype": "CONSTR_PRIMARY"
40+
}
41+
}
42+
]
43+
}
44+
},
45+
{
46+
"ColumnDef": {
47+
"colname": "updated_at",
48+
"typeName": {
49+
"names": [
50+
{
51+
"String": {
52+
"str": "pg_catalog"
53+
}
54+
},
55+
{
56+
"String": {
57+
"str": "timestamptz"
58+
}
59+
}
60+
],
61+
"typemod": -1
62+
},
63+
"is_local": true,
64+
"constraints": [
65+
{
66+
"Constraint": {
67+
"contype": "CONSTR_NOTNULL"
68+
}
69+
},
70+
{
71+
"Constraint": {
72+
"contype": "CONSTR_DEFAULT",
73+
"raw_expr": {
74+
"FuncCall": {
75+
"funcname": [
76+
{
77+
"String": {
78+
"str": "now"
79+
}
80+
}
81+
]
82+
}
83+
}
84+
}
85+
}
86+
]
87+
}
88+
}
89+
],
90+
"oncommit": "ONCOMMIT_NOOP"
91+
}
92+
},
93+
"stmt_len": 120
94+
}
95+
},
96+
{
97+
"RawStmt": {
98+
"stmt": {
99+
"CreateStmt": {
100+
"relation": {
101+
"relname": "users_2",
102+
"inh": true,
103+
"relpersistence": "p"
104+
},
105+
"tableElts": [
106+
{
107+
"ColumnDef": {
108+
"colname": "id",
109+
"typeName": {
110+
"names": [
111+
{
112+
"String": {
113+
"str": "pg_catalog"
114+
}
115+
},
116+
{
117+
"String": {
118+
"str": "int4"
119+
}
120+
}
121+
],
122+
"typemod": -1
123+
},
124+
"is_local": true,
125+
"constraints": [
126+
{
127+
"Constraint": {
128+
"contype": "CONSTR_NOTNULL"
129+
}
130+
},
131+
{
132+
"Constraint": {
133+
"contype": "CONSTR_PRIMARY"
134+
}
135+
}
136+
]
137+
}
138+
}
139+
],
140+
"oncommit": "ONCOMMIT_NOOP"
141+
}
142+
},
143+
"stmt_len": 59
144+
}
145+
},
146+
{
147+
"RawStmt": {
148+
"stmt": {
149+
"CreateStmt": {
150+
"relation": {
151+
"relname": "users_3",
152+
"inh": true,
153+
"relpersistence": "p"
154+
},
155+
"tableElts": [
156+
{
157+
"ColumnDef": {
158+
"colname": "id",
159+
"typeName": {
160+
"names": [
161+
{
162+
"String": {
163+
"str": "pg_catalog"
164+
}
165+
},
166+
{
167+
"String": {
168+
"str": "int4"
169+
}
170+
}
171+
],
172+
"typemod": -1
173+
},
174+
"is_local": true,
175+
"constraints": [
176+
{
177+
"Constraint": {
178+
"contype": "CONSTR_NOTNULL"
179+
}
180+
},
181+
{
182+
"Constraint": {
183+
"contype": "CONSTR_PRIMARY"
184+
}
185+
}
186+
]
187+
}
188+
}
189+
],
190+
"oncommit": "ONCOMMIT_NOOP"
191+
}
192+
},
193+
"stmt_len": 59
194+
}
195+
},
196+
{
197+
"RawStmt": {
198+
"stmt": {
199+
"CreateFunctionStmt": {
200+
"replace": true,
201+
"funcname": [
202+
{
203+
"String": {
204+
"str": "update_updated_at_column"
205+
}
206+
}
207+
],
208+
"returnType": {
209+
"names": [
210+
{
211+
"String": {
212+
"str": "trigger"
213+
}
214+
}
215+
],
216+
"typemod": -1
217+
},
218+
"options": [
219+
{
220+
"DefElem": {
221+
"defname": "language",
222+
"arg": {
223+
"String": {
224+
"str": "plpgsql"
225+
}
226+
},
227+
"defaction": "DEFELEM_UNSPEC"
228+
}
229+
},
230+
{
231+
"DefElem": {
232+
"defname": "as",
233+
"arg": {
234+
"List": {
235+
"items": [
236+
{
237+
"String": {
238+
"str": "\nbegin\n new.updated_at = NOW();\n return new;\nend;\n"
239+
}
240+
}
241+
]
242+
}
243+
},
244+
"defaction": "DEFELEM_UNSPEC"
245+
}
246+
}
247+
]
248+
}
249+
},
250+
"stmt_len": 171
251+
}
252+
},
253+
{
254+
"RawStmt": {
255+
"stmt": {
256+
"CreateTrigStmt": {
257+
"trigname": "update_users_3_modtime",
258+
"relation": {
259+
"relname": "users_3",
260+
"inh": true,
261+
"relpersistence": "p"
262+
},
263+
"funcname": [
264+
{
265+
"String": {
266+
"str": "update_updated_at_column"
267+
}
268+
}
269+
],
270+
"row": true,
271+
"timing": 2,
272+
"events": 16
273+
}
274+
},
275+
"stmt_len": 148
276+
}
277+
},
278+
{
279+
"RawStmt": {
280+
"stmt": {
281+
"CreateTrigStmt": {
282+
"trigname": "update_users_1_modtime",
283+
"relation": {
284+
"relname": "users_1",
285+
"inh": true,
286+
"relpersistence": "p"
287+
},
288+
"funcname": [
289+
{
290+
"String": {
291+
"str": "update_updated_at_column"
292+
}
293+
}
294+
],
295+
"row": true,
296+
"timing": 2,
297+
"events": 16
298+
}
299+
},
300+
"stmt_len": 126
301+
}
302+
},
303+
{
304+
"RawStmt": {
305+
"stmt": {
306+
"CreateTrigStmt": {
307+
"trigname": "update_users_2_modtime",
308+
"relation": {
309+
"relname": "users_2",
310+
"inh": true,
311+
"relpersistence": "p"
312+
},
313+
"funcname": [
314+
{
315+
"String": {
316+
"str": "update_updated_at_column"
317+
}
318+
}
319+
],
320+
"row": true,
321+
"timing": 2,
322+
"events": 16
323+
}
324+
},
325+
"stmt_len": 148
326+
}
327+
}
328+
]

server/src/postgres/parsers/parseFunctions.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export interface TriggerInfo {
1616
functionName: string,
1717
relname: string,
1818
stmtLocation?: number,
19+
stmtLen: number,
1920
}
2021

2122
export async function parseFunctions(
@@ -138,7 +139,8 @@ function getCreateTriggers(
138139
{
139140
functionName,
140141
relname,
141-
stmtLocation: statement?.stmt_location,
142+
stmtLocation: statement.stmt_location,
143+
stmtLen: statement.stmt_len,
142144
},
143145
]
144146
},

0 commit comments

Comments
 (0)