mirror of
https://github.com/d3vyce/fastapi-toolsets.git
synced 2026-08-07 17:04:09 +00:00
fix: make lock_tables generic over session type (#270)
This commit is contained in:
@@ -151,14 +151,13 @@ class LockMode(str, Enum):
|
|||||||
ACCESS_EXCLUSIVE = "ACCESS EXCLUSIVE"
|
ACCESS_EXCLUSIVE = "ACCESS EXCLUSIVE"
|
||||||
|
|
||||||
|
|
||||||
@asynccontextmanager
|
def lock_tables(
|
||||||
async def lock_tables(
|
session_maker: async_sessionmaker[_SessionT],
|
||||||
session_maker: async_sessionmaker[AsyncSession],
|
|
||||||
tables: list[type[DeclarativeBase]],
|
tables: list[type[DeclarativeBase]],
|
||||||
*,
|
*,
|
||||||
mode: LockMode = LockMode.SHARE_UPDATE_EXCLUSIVE,
|
mode: LockMode = LockMode.SHARE_UPDATE_EXCLUSIVE,
|
||||||
timeout: str = "5s",
|
timeout: str = "5s",
|
||||||
) -> AsyncGenerator[AsyncSession, None]:
|
) -> AbstractAsyncContextManager[_SessionT]:
|
||||||
"""Lock PostgreSQL tables for the duration of a transaction.
|
"""Lock PostgreSQL tables for the duration of a transaction.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
@@ -190,6 +189,8 @@ async def lock_tables(
|
|||||||
"""
|
"""
|
||||||
table_names = ",".join(table.__tablename__ for table in tables)
|
table_names = ",".join(table.__tablename__ for table in tables)
|
||||||
|
|
||||||
|
@asynccontextmanager
|
||||||
|
async def _lock() -> AsyncGenerator[_SessionT, None]:
|
||||||
async with session_maker() as session:
|
async with session_maker() as session:
|
||||||
try:
|
try:
|
||||||
await session.execute(text(f"SET LOCAL lock_timeout='{timeout}'"))
|
await session.execute(text(f"SET LOCAL lock_timeout='{timeout}'"))
|
||||||
@@ -200,6 +201,8 @@ async def lock_tables(
|
|||||||
await session.rollback()
|
await session.rollback()
|
||||||
raise
|
raise
|
||||||
|
|
||||||
|
return _lock()
|
||||||
|
|
||||||
|
|
||||||
async def create_database(
|
async def create_database(
|
||||||
db_name: str,
|
db_name: str,
|
||||||
|
|||||||
Reference in New Issue
Block a user