-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into mai/db-async-engine
- Loading branch information
Showing
31 changed files
with
799 additions
and
477 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
packages/postgres-database/src/simcore_postgres_database/utils_repos.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import logging | ||
from collections.abc import AsyncIterator | ||
from contextlib import asynccontextmanager | ||
|
||
from sqlalchemy.ext.asyncio import AsyncConnection, AsyncEngine | ||
|
||
_logger = logging.getLogger(__name__) | ||
|
||
|
||
@asynccontextmanager | ||
async def pass_or_acquire_connection( | ||
engine: AsyncEngine, connection: AsyncConnection | None = None | ||
) -> AsyncIterator[AsyncConnection]: | ||
# NOTE: When connection is passed, the engine is actually not needed | ||
# NOTE: Creator is responsible of closing connection | ||
is_connection_created = connection is None | ||
if is_connection_created: | ||
connection = await engine.connect() | ||
try: | ||
assert connection # nosec | ||
yield connection | ||
finally: | ||
assert connection # nosec | ||
assert not connection.closed # nosec | ||
if is_connection_created and connection: | ||
await connection.close() | ||
|
||
|
||
@asynccontextmanager | ||
async def transaction_context( | ||
engine: AsyncEngine, connection: AsyncConnection | None = None | ||
): | ||
async with pass_or_acquire_connection(engine, connection) as conn: | ||
if conn.in_transaction(): | ||
async with conn.begin_nested(): # inner transaction (savepoint) | ||
yield conn | ||
else: | ||
try: | ||
async with conn.begin(): # outer transaction (savepoint) | ||
yield conn | ||
finally: | ||
assert not conn.closed # nosec | ||
assert not conn.in_transaction() # nosec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.