|
15 | 15 | UPDATE {version_table_name}
|
16 | 16 | SET {update_values}
|
17 | 17 | WHERE
|
18 |
| - {transaction_column} = ( |
19 |
| - SELECT MAX(id) FROM {transaction_table_name} |
20 |
| - WHERE native_tx_id = txid_current() |
21 |
| - ) |
| 18 | + {transaction_column} = transaction_id_value |
22 | 19 | AND
|
23 | 20 | {primary_key_criteria}
|
24 | 21 | RETURNING *
|
|
37 | 34 |
|
38 | 35 | procedure_sql = """
|
39 | 36 | CREATE OR REPLACE FUNCTION {procedure_name}() RETURNS TRIGGER AS $$
|
| 37 | +DECLARE transaction_id_value INT; |
40 | 38 | BEGIN
|
| 39 | + transaction_id_value = ( |
| 40 | + SELECT MAX(id) FROM {transaction_table_name} |
| 41 | + WHERE native_tx_id = txid_current() |
| 42 | + ); |
| 43 | + IF (transaction_id_value IS NULL) THEN |
| 44 | + INSERT INTO transaction (native_tx_id) |
| 45 | + VALUES (txid_current()) RETURNING id INTO transaction_id_value; |
| 46 | + END IF; |
| 47 | +
|
41 | 48 | IF (TG_OP = 'INSERT') THEN
|
42 | 49 | {after_insert}
|
43 | 50 | {upsert_insert}
|
|
61 | 68 |
|
62 | 69 | validity_sql = """
|
63 | 70 | UPDATE {version_table_name}
|
64 |
| -SET {end_transaction_column} = ( |
65 |
| - SELECT MAX(id) FROM {transaction_table_name} |
66 |
| - WHERE native_tx_id = txid_current() |
67 |
| -) |
| 71 | +SET {end_transaction_column} = transaction_id_value |
68 | 72 | WHERE
|
69 | 73 | {transaction_column} = (
|
70 | 74 | SELECT MIN({transaction_column}) FROM {version_table_name}
|
@@ -351,6 +355,7 @@ def __str__(self):
|
351 | 355 | excluded_columns=', '.join(
|
352 | 356 | "'%s'" % c for c in self.excluded_columns
|
353 | 357 | ),
|
| 358 | + transaction_table_name=self.transaction_table_name, |
354 | 359 | after_insert=after_insert,
|
355 | 360 | after_update=after_update,
|
356 | 361 | after_delete=after_delete,
|
|
0 commit comments