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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
|
import sqlalchemy as sa
from sqlalchemy import Computed
from sqlalchemy import event
from sqlalchemy import Identity
from sqlalchemy import Integer
from sqlalchemy import String
from sqlalchemy import testing
from sqlalchemy.testing import eq_
from sqlalchemy.testing import fixtures
from sqlalchemy.testing.assertsql import assert_engine
from sqlalchemy.testing.assertsql import CompiledSQL
from sqlalchemy.testing.assertsql import Conditional
from sqlalchemy.testing.assertsql import RegexSQL
from sqlalchemy.testing.fixtures import fixture_session
from sqlalchemy.testing.schema import Column
from sqlalchemy.testing.schema import Table
class TriggerDefaultsTest(fixtures.MappedTest):
__requires__ = ("row_triggers",)
__backend__ = True
@classmethod
def define_tables(cls, metadata):
dt = Table(
"dt",
metadata,
Column(
"id", Integer, primary_key=True, test_needs_autoincrement=True
),
Column("col1", String(20)),
Column(
"col2", String(20), server_default=sa.schema.FetchedValue()
),
Column(
"col3", String(20), sa.schema.FetchedValue(for_update=True)
),
Column(
"col4",
String(20),
sa.schema.FetchedValue(),
sa.schema.FetchedValue(for_update=True),
),
implicit_returning=False,
)
dialect_name = testing.db.dialect.name
for ins in (
sa.DDL(
"CREATE TRIGGER dt_ins AFTER INSERT ON dt "
"FOR EACH ROW BEGIN "
"UPDATE dt SET col2='ins', col4='ins' "
"WHERE dt.id = NEW.id; END"
).execute_if(dialect="sqlite"),
sa.DDL(
"CREATE TRIGGER dt_ins ON dt AFTER INSERT AS "
"UPDATE dt SET col2='ins', col4='ins' "
"WHERE dt.id IN (SELECT id FROM inserted);"
).execute_if(dialect="mssql"),
sa.DDL(
"CREATE TRIGGER dt_ins BEFORE INSERT "
"ON dt "
"FOR EACH ROW "
"BEGIN "
":NEW.col2 := 'ins'; :NEW.col4 := 'ins'; END;"
).execute_if(dialect="oracle"),
sa.DDL(
"CREATE TRIGGER dt_ins BEFORE INSERT "
"ON dt "
"FOR EACH ROW "
"EXECUTE PROCEDURE my_func_ins();"
).execute_if(dialect="postgresql"),
sa.DDL(
"CREATE TRIGGER dt_ins BEFORE INSERT ON dt "
"FOR EACH ROW BEGIN "
"SET NEW.col2='ins'; SET NEW.col4='ins'; END"
).execute_if(
callable_=lambda ddl, target, bind, **kw: bind.engine.name
not in ("oracle", "mssql", "sqlite", "postgresql")
),
):
my_func_ins = sa.DDL(
"CREATE OR REPLACE FUNCTION my_func_ins() "
"RETURNS TRIGGER AS $$ "
"BEGIN "
"NEW.col2 := 'ins'; NEW.col4 := 'ins'; "
"RETURN NEW; "
"END; $$ LANGUAGE PLPGSQL"
).execute_if(dialect="postgresql")
event.listen(dt, "after_create", my_func_ins)
event.listen(dt, "after_create", ins)
if dialect_name == "postgresql":
event.listen(
dt, "before_drop", sa.DDL("DROP TRIGGER dt_ins ON dt")
)
else:
event.listen(dt, "before_drop", sa.DDL("DROP TRIGGER dt_ins"))
for up in (
sa.DDL(
"CREATE TRIGGER dt_up AFTER UPDATE ON dt "
"FOR EACH ROW BEGIN "
"UPDATE dt SET col3='up', col4='up' "
"WHERE dt.id = OLD.id; END"
).execute_if(dialect="sqlite"),
sa.DDL(
"CREATE TRIGGER dt_up ON dt AFTER UPDATE AS "
"UPDATE dt SET col3='up', col4='up' "
"WHERE dt.id IN (SELECT id FROM deleted);"
).execute_if(dialect="mssql"),
sa.DDL(
"CREATE TRIGGER dt_up BEFORE UPDATE ON dt "
"FOR EACH ROW BEGIN "
":NEW.col3 := 'up'; :NEW.col4 := 'up'; END;"
).execute_if(dialect="oracle"),
sa.DDL(
"CREATE TRIGGER dt_up BEFORE UPDATE ON dt "
"FOR EACH ROW "
"EXECUTE PROCEDURE my_func_up();"
).execute_if(dialect="postgresql"),
sa.DDL(
"CREATE TRIGGER dt_up BEFORE UPDATE ON dt "
"FOR EACH ROW BEGIN "
"SET NEW.col3='up'; SET NEW.col4='up'; END"
).execute_if(
callable_=lambda ddl, target, bind, **kw: bind.engine.name
not in ("oracle", "mssql", "sqlite", "postgresql")
),
):
my_func_up = sa.DDL(
"CREATE OR REPLACE FUNCTION my_func_up() "
"RETURNS TRIGGER AS $$ "
"BEGIN "
"NEW.col3 := 'up'; NEW.col4 := 'up'; "
"RETURN NEW; "
"END; $$ LANGUAGE PLPGSQL"
).execute_if(dialect="postgresql")
event.listen(dt, "after_create", my_func_up)
event.listen(dt, "after_create", up)
if dialect_name == "postgresql":
event.listen(dt, "before_drop", sa.DDL("DROP TRIGGER dt_up ON dt"))
else:
event.listen(dt, "before_drop", sa.DDL("DROP TRIGGER dt_up"))
@classmethod
def setup_classes(cls):
class Default(cls.Comparable):
pass
@classmethod
def setup_mappers(cls):
Default, dt = cls.classes.Default, cls.tables.dt
cls.mapper_registry.map_imperatively(Default, dt)
def test_insert(self):
Default = self.classes.Default
d1 = Default(id=1)
eq_(d1.col1, None)
eq_(d1.col2, None)
eq_(d1.col3, None)
eq_(d1.col4, None)
session = fixture_session()
session.add(d1)
session.flush()
eq_(d1.col1, None)
eq_(d1.col2, "ins")
eq_(d1.col3, None)
# don't care which trigger fired
assert d1.col4 in ("ins", "up")
def test_update(self):
Default = self.classes.Default
d1 = Default(id=1)
session = fixture_session()
session.add(d1)
session.flush()
d1.col1 = "set"
session.flush()
eq_(d1.col1, "set")
eq_(d1.col2, "ins")
eq_(d1.col3, "up")
eq_(d1.col4, "up")
class ExcludedDefaultsTest(fixtures.MappedTest):
@classmethod
def define_tables(cls, metadata):
Table(
"dt",
metadata,
Column(
"id", Integer, primary_key=True, test_needs_autoincrement=True
),
Column("col1", String(20), default="hello"),
)
def test_exclude(self):
dt = self.tables.dt
class Foo(fixtures.BasicEntity):
pass
self.mapper_registry.map_imperatively(
Foo, dt, exclude_properties=("col1",)
)
f1 = Foo()
sess = fixture_session()
sess.add(f1)
sess.flush()
eq_(sess.connection().execute(dt.select()).fetchall(), [(1, "hello")])
class ComputedDefaultsOnUpdateTest(fixtures.MappedTest):
"""test that computed columns are recognized as server
oninsert/onupdate defaults."""
__backend__ = True
__requires__ = ("computed_columns",)
@classmethod
def define_tables(cls, metadata):
Table(
"test",
metadata,
Column("id", Integer, primary_key=True),
Column("foo", Integer),
Column("bar", Integer, Computed("foo + 42")),
)
@classmethod
def setup_classes(cls):
class Thing(cls.Basic):
pass
class ThingNoEager(cls.Basic):
pass
@classmethod
def setup_mappers(cls):
Thing = cls.classes.Thing
cls.mapper_registry.map_imperatively(
Thing, cls.tables.test, eager_defaults=True
)
ThingNoEager = cls.classes.ThingNoEager
cls.mapper_registry.map_imperatively(
ThingNoEager, cls.tables.test, eager_defaults=False
)
@testing.combinations(("eager", True), ("noneager", False), id_="ia")
def test_insert_computed(self, eager):
if eager:
Thing = self.classes.Thing
else:
Thing = self.classes.ThingNoEager
s = fixture_session()
t1, t2 = (Thing(id=1, foo=5), Thing(id=2, foo=10))
s.add_all([t1, t2])
with assert_engine(testing.db) as asserter:
s.flush()
eq_(t1.bar, 5 + 42)
eq_(t2.bar, 10 + 42)
asserter.assert_(
Conditional(
eager and testing.db.dialect.insert_returning,
[
Conditional(
testing.db.dialect.insert_executemany_returning,
[
RegexSQL(
r"INSERT INTO test \(id, foo\) .*"
r"VALUES \(.*\) .*"
r"RETURNING test.bar, test.id",
[{"foo": 5, "id": 1}, {"foo": 10, "id": 2}],
dialect="postgresql",
),
],
[
RegexSQL(
r"INSERT INTO test \(id, foo\) .*"
r"VALUES \(.*\) .*"
r"RETURNING test.bar, test.id",
[{"foo": 5, "id": 1}],
dialect="postgresql",
),
RegexSQL(
r"INSERT INTO test \(id, foo\) .*"
r"VALUES \(.*\) .*"
r"RETURNING test.bar, test.id",
[{"foo": 10, "id": 2}],
dialect="postgresql",
),
],
)
],
[
CompiledSQL(
"INSERT INTO test (id, foo) VALUES (:id, :foo)",
[{"foo": 5, "id": 1}, {"foo": 10, "id": 2}],
),
CompiledSQL(
"SELECT test.bar AS test_bar FROM test "
"WHERE test.id = :pk_1",
[{"pk_1": 1}],
),
CompiledSQL(
"SELECT test.bar AS test_bar FROM test "
"WHERE test.id = :pk_1",
[{"pk_1": 2}],
),
],
)
)
@testing.combinations(
(
"eagerload",
True,
testing.requires.computed_columns_on_update_returning,
),
(
"noneagerload",
False,
),
id_="ia",
)
def test_update_computed(self, eager):
if eager:
Thing = self.classes.Thing
else:
Thing = self.classes.ThingNoEager
s = fixture_session()
t1, t2 = (Thing(id=1, foo=1), Thing(id=2, foo=2))
s.add_all([t1, t2])
s.flush()
t1.foo = 5
t2.foo = 6
with assert_engine(testing.db) as asserter:
s.flush()
eq_(t1.bar, 5 + 42)
eq_(t2.bar, 6 + 42)
if eager and testing.db.dialect.update_returning:
asserter.assert_(
CompiledSQL(
"UPDATE test SET foo=%(foo)s "
"WHERE test.id = %(test_id)s "
"RETURNING test.bar",
[{"foo": 5, "test_id": 1}],
dialect="postgresql",
),
CompiledSQL(
"UPDATE test SET foo=%(foo)s "
"WHERE test.id = %(test_id)s "
"RETURNING test.bar",
[{"foo": 6, "test_id": 2}],
dialect="postgresql",
),
)
elif eager:
asserter.assert_(
CompiledSQL(
"UPDATE test SET foo=:foo WHERE test.id = :test_id",
[{"foo": 5, "test_id": 1}, {"foo": 6, "test_id": 2}],
enable_returning=False,
),
CompiledSQL(
"SELECT test.bar AS test_bar FROM test "
"WHERE test.id = :pk_1",
[{"pk_1": 1}],
enable_returning=False,
),
CompiledSQL(
"SELECT test.bar AS test_bar FROM test "
"WHERE test.id = :pk_1",
[{"pk_1": 2}],
enable_returning=False,
),
)
else:
asserter.assert_(
CompiledSQL(
"UPDATE test SET foo=:foo WHERE test.id = :test_id",
[{"foo": 5, "test_id": 1}, {"foo": 6, "test_id": 2}],
),
CompiledSQL(
"SELECT test.bar AS test_bar FROM test "
"WHERE test.id = :pk_1",
[{"pk_1": 1}],
),
CompiledSQL(
"SELECT test.bar AS test_bar FROM test "
"WHERE test.id = :pk_1",
[{"pk_1": 2}],
),
)
class IdentityDefaultsOnUpdateTest(fixtures.MappedTest):
"""test that computed columns are recognized as server
oninsert/onupdate defaults."""
__backend__ = True
__requires__ = ("identity_columns",)
run_create_tables = "each"
@classmethod
def define_tables(cls, metadata):
Table(
"test",
metadata,
Column("id", Integer, Identity(), primary_key=True),
Column("foo", Integer),
)
@classmethod
def setup_classes(cls):
class Thing(cls.Basic):
pass
@classmethod
def setup_mappers(cls):
Thing = cls.classes.Thing
cls.mapper_registry.map_imperatively(Thing, cls.tables.test)
def test_insert_identity(self):
Thing = self.classes.Thing
s = fixture_session()
t1, t2 = (Thing(foo=5), Thing(foo=10))
s.add_all([t1, t2])
with assert_engine(testing.db) as asserter:
s.flush()
eq_(t1.id, 1)
eq_(t2.id, 2)
asserter.assert_(
Conditional(
testing.db.dialect.insert_returning,
[
Conditional(
testing.db.dialect.insert_executemany_returning,
[
RegexSQL(
r"INSERT INTO test \(foo\).*VALUES (.*).* "
r"RETURNING test.id, test.id AS id__1",
[{"foo": 5}, {"foo": 10}],
dialect="postgresql",
),
],
[
RegexSQL(
r"INSERT INTO test \(foo\).*VALUES (.*).* "
r"RETURNING test.id, test.id AS id__1",
[{"foo": 5}],
dialect="postgresql",
),
RegexSQL(
r"INSERT INTO test \(foo\).*VALUES (.*).* "
r"RETURNING test.id, test.id AS id__1",
[{"foo": 10}],
dialect="postgresql",
),
],
)
],
[
CompiledSQL(
"INSERT INTO test (foo) VALUES (:foo)",
[{"foo": 5}],
),
CompiledSQL(
"INSERT INTO test (foo) VALUES (:foo)",
[{"foo": 10}],
),
],
)
)
|