mirror of
https://github.com/d3vyce/fastapi-toolsets.git
synced 2026-08-04 23:54:09 +00:00
Version 4.0.0 (#263)
* fix: lock_tables acquires dedicated session to enforce RAII lock boundaries (#261) * fix: make lock_tables generic over session type (#270) * docs: fix zensical warnings * Version 4.0.0 * docs: add v4 migration + fix lock_tables documentation
This commit is contained in:
@@ -21,4 +21,4 @@ Example usage:
|
||||
return Response(data={"user": user.username}, message="Success")
|
||||
"""
|
||||
|
||||
__version__ = "3.1.1"
|
||||
__version__ = "4.0.0"
|
||||
|
||||
+26
-21
@@ -151,52 +151,57 @@ class LockMode(str, Enum):
|
||||
ACCESS_EXCLUSIVE = "ACCESS EXCLUSIVE"
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def lock_tables(
|
||||
session: AsyncSession,
|
||||
def lock_tables(
|
||||
session_maker: async_sessionmaker[_SessionT],
|
||||
tables: list[type[DeclarativeBase]],
|
||||
*,
|
||||
mode: LockMode = LockMode.SHARE_UPDATE_EXCLUSIVE,
|
||||
timeout: str = "5s",
|
||||
) -> AsyncGenerator[AsyncSession, None]:
|
||||
) -> AbstractAsyncContextManager[_SessionT]:
|
||||
"""Lock PostgreSQL tables for the duration of a transaction.
|
||||
|
||||
Acquires table-level locks that are held until the transaction ends.
|
||||
Useful for preventing concurrent modifications during critical operations.
|
||||
|
||||
Args:
|
||||
session: AsyncSession instance
|
||||
tables: List of SQLAlchemy model classes to lock
|
||||
mode: Lock mode (default: SHARE UPDATE EXCLUSIVE)
|
||||
timeout: Lock timeout (default: "5s")
|
||||
session_maker: Async session factory used to create the dedicated
|
||||
session.
|
||||
tables: List of SQLAlchemy model classes to lock.
|
||||
mode: Lock mode (default: SHARE UPDATE EXCLUSIVE).
|
||||
timeout: Lock timeout (default: "5s").
|
||||
|
||||
Yields:
|
||||
The session with locked tables
|
||||
The dedicated session, open within the locked transaction.
|
||||
|
||||
Raises:
|
||||
SQLAlchemyError: If lock cannot be acquired within timeout
|
||||
SQLAlchemyError: If the lock cannot be acquired within *timeout*.
|
||||
|
||||
Example:
|
||||
```python
|
||||
from fastapi_toolsets.db import lock_tables, LockMode
|
||||
|
||||
async with lock_tables(session, [User, Account]):
|
||||
# Tables are locked with SHARE UPDATE EXCLUSIVE mode
|
||||
async with lock_tables(session_maker, [User, Account]) as session:
|
||||
# Tables are locked; changes are committed when the context exits.
|
||||
user = await UserCrud.get(session, [User.id == 1])
|
||||
user.balance += 100
|
||||
|
||||
# With custom lock mode
|
||||
async with lock_tables(session, [Order], mode=LockMode.EXCLUSIVE):
|
||||
# Exclusive lock - no other transactions can access
|
||||
async with lock_tables(session_maker, [Order], mode=LockMode.EXCLUSIVE) as session:
|
||||
await process_order(session, order_id)
|
||||
```
|
||||
"""
|
||||
table_names = ",".join(table.__tablename__ for table in tables)
|
||||
|
||||
async with get_transaction(session):
|
||||
await session.execute(text(f"SET LOCAL lock_timeout='{timeout}'"))
|
||||
await session.execute(text(f"LOCK {table_names} IN {mode.value} MODE"))
|
||||
yield session
|
||||
@asynccontextmanager
|
||||
async def _lock() -> AsyncGenerator[_SessionT, None]:
|
||||
async with session_maker() as session:
|
||||
try:
|
||||
await session.execute(text(f"SET LOCAL lock_timeout='{timeout}'"))
|
||||
await session.execute(text(f"LOCK {table_names} IN {mode.value} MODE"))
|
||||
yield session
|
||||
await session.commit()
|
||||
except BaseException:
|
||||
await session.rollback()
|
||||
raise
|
||||
|
||||
return _lock()
|
||||
|
||||
|
||||
async def create_database(
|
||||
|
||||
Reference in New Issue
Block a user