chore: snapshot workflow migration progress
This commit is contained in:
+15
-18
@@ -1,13 +1,11 @@
|
||||
from __future__ import annotations
|
||||
from typing import TYPE_CHECKING, AsyncGenerator, Optional
|
||||
from typing import AsyncGenerator, Optional
|
||||
from starlette.requests import Request
|
||||
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine, async_sessionmaker
|
||||
from sqlalchemy.orm import DeclarativeBase
|
||||
from sqlalchemy import text
|
||||
from app.config import settings
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from starlette.requests import Request
|
||||
|
||||
engine = create_async_engine(
|
||||
settings.database_url,
|
||||
echo=False,
|
||||
@@ -27,22 +25,21 @@ class Base(DeclarativeBase):
|
||||
pass
|
||||
|
||||
|
||||
async def get_db(request: "Request | None" = None) -> AsyncGenerator[AsyncSession, None]:
|
||||
async def get_db(request: Request) -> AsyncGenerator[AsyncSession, None]:
|
||||
async with AsyncSessionLocal() as session:
|
||||
# Auto-apply RLS context if TenantContextMiddleware populated request.state
|
||||
if request is not None:
|
||||
tenant_id = getattr(request.state, "tenant_id", None)
|
||||
role = getattr(request.state, "role", None)
|
||||
if tenant_id:
|
||||
# global_admin and legacy admin bypass RLS to see all tenants
|
||||
_bypass_roles = {"global_admin", "admin"}
|
||||
if role in _bypass_roles:
|
||||
await session.execute(text("SET LOCAL app.current_tenant_id = 'bypass'"))
|
||||
else:
|
||||
await session.execute(
|
||||
text("SET LOCAL app.current_tenant_id = :tid"),
|
||||
{"tid": tenant_id},
|
||||
)
|
||||
tenant_id = getattr(request.state, "tenant_id", None)
|
||||
role = getattr(request.state, "role", None)
|
||||
if tenant_id:
|
||||
# global_admin and legacy admin bypass RLS to see all tenants
|
||||
_bypass_roles = {"global_admin", "admin"}
|
||||
if role in _bypass_roles:
|
||||
await session.execute(text("SET LOCAL app.current_tenant_id = 'bypass'"))
|
||||
else:
|
||||
await session.execute(
|
||||
text("SET LOCAL app.current_tenant_id = :tid"),
|
||||
{"tid": tenant_id},
|
||||
)
|
||||
try:
|
||||
yield session
|
||||
finally:
|
||||
|
||||
Reference in New Issue
Block a user