refactor(db)!: finalize pooled WNDB v2 migration
This commit is contained in:
@@ -16,7 +16,7 @@ from app.infra.db.metadb.repositories.metadata_repository import (
|
||||
MetadataRepository,
|
||||
ProjectDbRouting,
|
||||
)
|
||||
from app.infra.db.project_routing import ActiveProjectRouting
|
||||
from app.infra.db.project_routing import ActiveProjectRouting, activate_project_routing
|
||||
|
||||
DB_ROLE_BIZ_DATA = "biz_data"
|
||||
DB_ROLE_IOT_DATA = "iot_data"
|
||||
@@ -107,6 +107,14 @@ async def get_project_business_routing(
|
||||
return await resolve_project_business_routing(ctx, metadata_repo)
|
||||
|
||||
|
||||
async def use_project_business_routing(
|
||||
routing: ActiveProjectRouting = Depends(get_project_business_routing),
|
||||
) -> AsyncGenerator[ActiveProjectRouting, None]:
|
||||
"""Keep the routed BizDB active for an entire endpoint invocation."""
|
||||
with activate_project_routing(routing):
|
||||
yield routing
|
||||
|
||||
|
||||
async def resolve_project_business_routing(
|
||||
ctx: ProjectContext,
|
||||
metadata_repo: MetadataRepository,
|
||||
@@ -189,31 +197,6 @@ async def _get_project_routing(
|
||||
return routing
|
||||
|
||||
|
||||
async def get_project_pg_session(
|
||||
ctx: ProjectContext = Depends(get_project_context),
|
||||
metadata_repo: MetadataRepository = Depends(get_metadata_repository),
|
||||
) -> AsyncGenerator[AsyncSession, None]:
|
||||
routing = await _get_project_routing(
|
||||
metadata_repo,
|
||||
ctx.project_id,
|
||||
DB_ROLE_BIZ_DATA,
|
||||
DB_TYPE_POSTGRES,
|
||||
"PostgreSQL",
|
||||
)
|
||||
|
||||
pool_min_size = routing.pool_min_size or settings.PROJECT_PG_POOL_SIZE
|
||||
pool_max_size = routing.pool_max_size or settings.PROJECT_PG_POOL_SIZE
|
||||
sessionmaker = await project_connection_manager.get_pg_sessionmaker(
|
||||
ctx.project_id,
|
||||
DB_ROLE_BIZ_DATA,
|
||||
routing.dsn,
|
||||
pool_min_size,
|
||||
pool_max_size,
|
||||
)
|
||||
async with sessionmaker() as session:
|
||||
yield session
|
||||
|
||||
|
||||
async def get_project_pg_connection(
|
||||
ctx: ProjectContext = Depends(get_project_context),
|
||||
metadata_repo: MetadataRepository = Depends(get_metadata_repository),
|
||||
@@ -226,16 +209,23 @@ async def get_project_pg_connection(
|
||||
"PostgreSQL",
|
||||
)
|
||||
|
||||
pool_min_size = routing.pool_min_size or settings.PROJECT_PG_POOL_SIZE
|
||||
pool_max_size = routing.pool_max_size or settings.PROJECT_PG_POOL_SIZE
|
||||
pool = await project_connection_manager.get_pg_pool(
|
||||
pool_min_size = (
|
||||
routing.pool_min_size
|
||||
if routing.pool_min_size is not None
|
||||
else settings.PROJECT_PG_POOL_MIN_SIZE
|
||||
)
|
||||
pool_max_size = (
|
||||
routing.pool_max_size
|
||||
if routing.pool_max_size is not None
|
||||
else settings.PROJECT_PG_POOL_SIZE
|
||||
)
|
||||
async with project_connection_manager.pg_connection(
|
||||
ctx.project_id,
|
||||
DB_ROLE_BIZ_DATA,
|
||||
routing.dsn,
|
||||
pool_min_size,
|
||||
pool_max_size,
|
||||
)
|
||||
async with pool.connection() as conn:
|
||||
) as conn:
|
||||
yield conn
|
||||
|
||||
|
||||
@@ -251,14 +241,21 @@ async def get_project_timescale_connection(
|
||||
"TimescaleDB",
|
||||
)
|
||||
|
||||
pool_min_size = routing.pool_min_size or settings.PROJECT_TS_POOL_MIN_SIZE
|
||||
pool_max_size = routing.pool_max_size or settings.PROJECT_TS_POOL_MAX_SIZE
|
||||
pool = await project_connection_manager.get_timescale_pool(
|
||||
pool_min_size = (
|
||||
routing.pool_min_size
|
||||
if routing.pool_min_size is not None
|
||||
else settings.PROJECT_TS_POOL_MIN_SIZE
|
||||
)
|
||||
pool_max_size = (
|
||||
routing.pool_max_size
|
||||
if routing.pool_max_size is not None
|
||||
else settings.PROJECT_TS_POOL_MAX_SIZE
|
||||
)
|
||||
async with project_connection_manager.timescale_connection(
|
||||
ctx.project_id,
|
||||
DB_ROLE_IOT_DATA,
|
||||
routing.dsn,
|
||||
pool_min_size,
|
||||
pool_max_size,
|
||||
)
|
||||
async with pool.connection() as conn:
|
||||
) as conn:
|
||||
yield conn
|
||||
|
||||
Reference in New Issue
Block a user