Compare commits
2
Commits
6466b797d3
...
v1.1.1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3258020fa7 | ||
|
|
2fb078ac6f |
No files matched your search
@@ -10,18 +10,18 @@ a sibling of the `mongo` lib. Class is **`PsqlDB`**.
|
||||
`requirements.txt`:
|
||||
|
||||
```
|
||||
psql @ git+ssh://git@git.rethinkstudios.io/rethink-public/psql.git@v1.0.0
|
||||
psql @ git+ssh://git@git.rethinkstudios.io/rethink-public/psql.git@v1.1.1
|
||||
```
|
||||
|
||||
Direct:
|
||||
|
||||
```bash
|
||||
pip install "psql @ git+ssh://git@git.rethinkstudios.io/rethink-public/psql.git@v1.0.0"
|
||||
pip install "psql @ git+ssh://git@git.rethinkstudios.io/rethink-public/psql.git@v1.1.1"
|
||||
```
|
||||
|
||||
Pulls `asyncpg`.
|
||||
|
||||
Drop the `@v1.0.0` suffix from the line above to install the latest unpinned.
|
||||
Drop the `@v1.1.1` suffix from the line above to install the latest unpinned.
|
||||
|
||||
## The two-layer API
|
||||
|
||||
|
||||
+1
-1
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
|
||||
|
||||
[project]
|
||||
name = "psql"
|
||||
version = "1.1.0"
|
||||
version = "1.1.1"
|
||||
description = "async postgres wrapper over asyncpg: two-layer API (friendly verbs + raw escape hatch), fail-loud, config-free"
|
||||
requires-python = ">=3.10"
|
||||
dependencies = [
|
||||
|
||||
+12
-30
@@ -44,6 +44,7 @@ errors (FAIL LOUD - unlike the mongo lib's swallow-and-default):
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
from contextlib import asynccontextmanager
|
||||
from typing import Any, Dict, List, Optional, Sequence
|
||||
|
||||
import asyncpg
|
||||
@@ -236,7 +237,7 @@ class PsqlDB:
|
||||
commits on clean exit, rolls back and re-raises on any error. `conn` is a raw
|
||||
asyncpg connection ($1-placeholder execute/fetch/... directly).
|
||||
"""
|
||||
return _Transaction(self.pool)
|
||||
return _transaction(self.pool)
|
||||
|
||||
# -------------------------------------------------------------------------
|
||||
# layer 1 - friendly portable verbs (identical across psql/mysql)
|
||||
@@ -365,36 +366,17 @@ class PsqlDB:
|
||||
return _status_count(status)
|
||||
|
||||
|
||||
class _Transaction:
|
||||
"""async ctx mgr: acquire a pooled connection + open a transaction on it"""
|
||||
@asynccontextmanager
|
||||
async def _transaction(pool: asyncpg.Pool):
|
||||
"""acquire a pooled connection and run the block in a transaction on it
|
||||
|
||||
def __init__(self, pool: asyncpg.Pool):
|
||||
self._pool = pool
|
||||
self._conn = None
|
||||
self._tx = None
|
||||
|
||||
async def __aenter__(self):
|
||||
self._conn = await self._pool.acquire()
|
||||
try:
|
||||
self._tx = self._conn.transaction()
|
||||
await self._tx.start()
|
||||
except BaseException:
|
||||
# start() (or transaction()) failing after acquire would otherwise leak the
|
||||
# pooled connection - __aexit__ is not called when __aenter__ raises. release
|
||||
# it and reset so a failed transaction start never burns a pool slot.
|
||||
await self._pool.release(self._conn)
|
||||
self._conn = None
|
||||
raise
|
||||
return self._conn
|
||||
|
||||
async def __aexit__(self, exc_type, exc, tb) -> None:
|
||||
try:
|
||||
if exc_type is None:
|
||||
await self._tx.commit()
|
||||
else:
|
||||
await self._tx.rollback()
|
||||
finally:
|
||||
await self._pool.release(self._conn)
|
||||
asyncpg's own context managers do the work: the transaction commits on clean exit
|
||||
and rolls back on any error, and the acquire releases the connection on every path -
|
||||
including a failed transaction start, so a failed start never burns a pool slot
|
||||
"""
|
||||
async with pool.acquire() as conn:
|
||||
async with conn.transaction():
|
||||
yield conn
|
||||
|
||||
|
||||
def _where(conditions: Optional[Dict[str, Any]]) -> tuple:
|
||||
|
||||
Reference in new issue
Block a user