99201f48fd
Root cause: BaseHTTPMiddleware.call_next() runs the endpoint in a separate async task, which breaks SQLAlchemy's greenlet context. After session.commit(), the connection is released to the pool. When session.refresh() tries to acquire a new connection for a SELECT, it fails with MissingGreenlet because the greenlet spawn context is not available in the call_next() task. Fix (two-part): 1. Convert all 4 middleware classes from BaseHTTPMiddleware to pure ASGI middleware — eliminates call_next() entirely so the entire request chain runs in the same async context. 2. Set expire_on_commit=False on the session factory — after commit, objects retain their in-memory values instead of being expired. This removes the need for session.refresh() entirely. All session.refresh() calls removed from 11 repository files. With expire_on_commit=False, post-commit attribute access no longer triggers lazy loads that would also fail with MissingGreenlet. Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
83 lines
2.8 KiB
Python
83 lines
2.8 KiB
Python
"""Nexus — Platform & Node Repository (Async SQLAlchemy)"""
|
|
|
|
from typing import Optional, List
|
|
from sqlalchemy import select
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from server.domain.models import Platform, Node
|
|
|
|
|
|
class PlatformRepositoryImpl:
|
|
def __init__(self, session: AsyncSession):
|
|
self.session = session
|
|
|
|
async def get_by_id(self, id: int) -> Optional[Platform]:
|
|
result = await self.session.execute(select(Platform).where(Platform.id == id))
|
|
return result.scalar_one_or_none()
|
|
|
|
async def get_all(self) -> List[Platform]:
|
|
result = await self.session.execute(select(Platform).order_by(Platform.id))
|
|
return list(result.scalars().all())
|
|
|
|
async def get_by_type(self, platform_type: str) -> Optional[Platform]:
|
|
result = await self.session.execute(select(Platform).where(Platform.type == platform_type))
|
|
return result.scalar_one_or_none()
|
|
|
|
async def create(self, platform: Platform) -> Platform:
|
|
self.session.add(platform)
|
|
await self.session.commit()
|
|
return platform
|
|
|
|
async def update(self, platform: Platform) -> Platform:
|
|
await self.session.commit()
|
|
return platform
|
|
|
|
async def delete(self, id: int) -> bool:
|
|
platform = await self.get_by_id(id)
|
|
if platform:
|
|
await self.session.delete(platform)
|
|
await self.session.commit()
|
|
return True
|
|
return False
|
|
|
|
|
|
class NodeRepositoryImpl:
|
|
def __init__(self, session: AsyncSession):
|
|
self.session = session
|
|
|
|
async def get_by_id(self, id: int) -> Optional[Node]:
|
|
result = await self.session.execute(select(Node).where(Node.id == id))
|
|
return result.scalar_one_or_none()
|
|
|
|
async def get_all(self) -> List[Node]:
|
|
result = await self.session.execute(select(Node).order_by(Node.sort_order, Node.id))
|
|
return list(result.scalars().all())
|
|
|
|
async def get_children(self, parent_id: int) -> List[Node]:
|
|
result = await self.session.execute(
|
|
select(Node).where(Node.parent_id == parent_id).order_by(Node.sort_order, Node.id)
|
|
)
|
|
return list(result.scalars().all())
|
|
|
|
async def get_root_nodes(self) -> List[Node]:
|
|
result = await self.session.execute(
|
|
select(Node).where(Node.parent_id.is_(None)).order_by(Node.sort_order, Node.id)
|
|
)
|
|
return list(result.scalars().all())
|
|
|
|
async def create(self, node: Node) -> Node:
|
|
self.session.add(node)
|
|
await self.session.commit()
|
|
return node
|
|
|
|
async def update(self, node: Node) -> Node:
|
|
await self.session.commit()
|
|
return node
|
|
|
|
async def delete(self, id: int) -> bool:
|
|
node = await self.get_by_id(id)
|
|
if node:
|
|
await self.session.delete(node)
|
|
await self.session.commit()
|
|
return True
|
|
return False |