Skip to content

Commit 6731fe3

Browse files
committed
Add Connection.transaction alias for asyncpg consistency.
1 parent 2fa852d commit 6731fe3

4 files changed

Lines changed: 27 additions & 1 deletion

File tree

postgresql/documentation/changes-v1.3.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Changes in v1.3
66

77
* Commit DB-API 2.0 ClientCannotConnect exception correction.
88
* Eliminate types-as-documentation annotations.
9+
* Add Connection.transaction alias for asyncpg consistency.
910
* Eliminate multiple inheritance in `postgresql.api` in favor of ABC registration.
1011
* Add support for PGTEST environment variable (pq-IRI) to improve test performance
1112
and to aid in cases where the target fixture is already available.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../changes-v1.3.rst

postgresql/driver/pq3.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2324,9 +2324,12 @@ def do(self, language : str, source : str,
23242324
sql = "DO " + qlit(source) + " LANGUAGE " + qid(language) + ";"
23252325
self.execute(sql)
23262326

2327-
def xact(self, isolation = None, mode = None) -> Transaction:
2327+
# Alias transaction as xact. xact is the original term, but support
2328+
# the full word for identifier consistency with asyncpg.
2329+
def transaction(self, isolation = None, mode = None) -> Transaction:
23282330
x = Transaction(self, isolation = isolation, mode = mode)
23292331
return x
2332+
xact=transaction
23302333

23312334
def prepare(self,
23322335
sql_statement_string : str,

postgresql/test/test_driver.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,27 @@ def testSelectInXact(self):
824824
with db.xact():
825825
self.select()
826826

827+
@pg_tmp
828+
def testTransactionAlias(self):
829+
self.assertEqual(db.transaction, db.xact)
830+
831+
try:
832+
with db.transaction():
833+
db.execute("CREATE TABLE t (i int);")
834+
raise Exception('some failure')
835+
except:
836+
pass
837+
else:
838+
self.fail("expected exception was not raised")
839+
840+
try:
841+
db.query("select * from t")
842+
except:
843+
# No table.
844+
pass
845+
else:
846+
self.fail("transaction abort had no effect")
847+
827848
def cursor_read(self):
828849
ps = db.prepare("SELECT i FROM generate_series(0, (2^8)::int - 1) AS g(i)")
829850
c = ps.declare()

0 commit comments

Comments
 (0)