forked from python-postgres/be
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxact.sql
More file actions
205 lines (170 loc) · 4.48 KB
/
Copy pathxact.sql
File metadata and controls
205 lines (170 loc) · 4.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
SET standard_conforming_strings TO ON;
SET client_encoding TO 'UTF-8';
CREATE OR REPLACE FUNCTION committed_nothing() RETURNS VOID LANGUAGE python AS
$python$
def main():
with xact():
pass
$python$;
SELECT committed_nothing();
CREATE OR REPLACE FUNCTION aborted_nothing() RETURNS VOID LANGUAGE python AS
$python$
def main():
try:
with xact():
raise ValueError
except ValueError:
pass
$python$;
SELECT aborted_nothing();
CREATE OR REPLACE FUNCTION cant_reuse() RETURNS VOID LANGUAGE python AS
$python$
x = xact()
def main():
with x:
pass
$python$;
SELECT cant_reuse();
SELECT cant_reuse();
DROP TABLE IF EXISTS pyxact_test;
CREATE TABLE pyxact_test (i int);
-- Move everything through the SQL function to avoid
-- a dependency on Postgres.Statement [That will be checked later]
CREATE OR REPLACE FUNCTION insert_val(int) RETURNS VOID LANGUAGE SQL AS
$$
INSERT INTO pyxact_test VALUES ($1);
$$;
CREATE OR REPLACE FUNCTION delete_val(int) RETURNS VOID LANGUAGE SQL AS
$$
DELETE FROM pyxact_test WHERE i = $1;
$$;
CREATE OR REPLACE FUNCTION check_commit() RETURNS VOID LANGUAGE python AS
$python$
from Postgres import CONST, Function, Type
rp = Type(CONST['REGPROCEDUREOID'])
ins = Function(rp('insert_val(int)'))
def main():
with xact():
ins(123)
$python$;
SELECT check_commit();
SELECT i, i = 123 AS should_be_true FROM pyxact_test;
CREATE OR REPLACE FUNCTION check_delete() RETURNS VOID LANGUAGE python AS
$python$
from Postgres import CONST, Function, Type
rp = Type(CONST['REGPROCEDUREOID'])
delv = Function(rp('delete_val(int)'))
def main():
with xact():
delv(123)
$python$;
SELECT check_delete();
SELECT COUNT(i) AS should_be_zero FROM pyxact_test;
CREATE OR REPLACE FUNCTION check_abort() RETURNS VOID LANGUAGE python AS
$python$
from Postgres import CONST, Function, Type
rp = Type(CONST['REGPROCEDUREOID'])
ins = Function(rp('insert_val(int)'))
def main():
try:
with xact():
ins(321)
raise ValueError()
except ValueError:
pass
$python$;
SELECT check_abort();
SELECT COUNT(i) AS should_be_zero FROM pyxact_test;
CREATE OR REPLACE FUNCTION check_abort_from_error() RETURNS VOID LANGUAGE python AS
$python$
from Postgres import CONST, Function, Type
rp = Type(CONST['REGPROCEDUREOID'])
ins = Function(rp('insert_val(int)'))
def main():
try:
with xact():
ins(1013)
rp('foobaZZZzzz(int17)')
except:
pass
else:
raise RuntimeError("bad regprocedure did not raise exception")
$python$;
SELECT check_abort_from_error();
SELECT COUNT(i) AS should_be_zero FROM pyxact_test;
CREATE OR REPLACE FUNCTION entered_xact() RETURNS VOID LANGUAGE python AS
$python$
def main():
x = xact()
x.__enter__()
$python$;
SELECT entered_xact();
CREATE OR REPLACE FUNCTION entered_exc_xact() RETURNS VOID LANGUAGE python AS
$python$
def main():
x = xact()
x.__enter__()
raise ValueError("xact WARNING should get thrown")
$python$;
SELECT entered_exc_xact();
CREATE OR REPLACE FUNCTION exited_xact() RETURNS VOID LANGUAGE python AS
$python$
def main():
x = xact()
x.__exit__(None, None, None)
$python$;
SELECT exited_xact();
CREATE OR REPLACE FUNCTION out_of_order() RETURNS VOID LANGUAGE python AS
$python$
def main():
x1 = xact()
x2 = xact()
x1.__enter__()
x2.__enter__()
x1.__exit__(None, None, None)
$python$;
SELECT out_of_order();
-- Finally, test the pl_ist_count global's recursion safety(handler called by handler).
CREATE OR REPLACE FUNCTION xsubsub() RETURNS VOID LANGUAGE python AS
$python$
def main():
x = xact()
x.__enter__()
raise ValueError("eek")
# Should cause an error, but trap in outer and recover accordingly.
$python$;
CREATE OR REPLACE FUNCTION call_broken_but_recover() RETURNS VOID LANGUAGE python AS
$python$
from Postgres import CONST, Function, Type, Exception as pg_exc
rp = Type(CONST['REGPROCEDUREOID'])
ins = Function(rp('insert_val(int)'))
s = Function(rp('entered_exc_xact()'))
def main():
with xact():
ins(8080)
try:
with xact():
s()
except pg_exc:
pass
$python$;
SELECT call_broken_but_recover();
SELECT i FROM pyxact_test WHERE i = 8080;
-- Same as above, but do the insert in a block (might be a useless test..)
CREATE OR REPLACE FUNCTION call_broken_but_recover_noxact() RETURNS VOID LANGUAGE python AS
$python$
from Postgres import CONST, Function, Type, Exception as pg_exc
rp = Type(CONST['REGPROCEDUREOID'])
s = Function(rp('entered_exc_xact()'))
def main():
try:
with xact():
s()
except pg_exc:
pass
$python$;
BEGIN;
SELECT call_broken_but_recover_noxact();
INSERT INTO pyxact_test VALUES (2020);
COMMIT;
SELECT i FROM pyxact_test WHERE i = 2020;