fix: patches sqlite support (#2876)
Co-authored-by: Jin Peng <jinjpeng@Jins-MacBook-Pro.local> Co-authored-by: jnjpng <jin@letta.com>
This commit is contained in:
@@ -49,6 +49,11 @@ def handle_db_timeout(func):
|
||||
return async_wrapper
|
||||
|
||||
|
||||
def is_postgresql_session(session: Session) -> bool:
|
||||
"""Check if the database session is PostgreSQL instead of SQLite for setting query options."""
|
||||
return session.bind.dialect.name == "postgresql"
|
||||
|
||||
|
||||
class AccessType(str, Enum):
|
||||
ORGANIZATION = "organization"
|
||||
USER = "user"
|
||||
@@ -494,12 +499,14 @@ class SqlalchemyBase(CommonSqlalchemyMetaMixins, Base):
|
||||
query, query_conditions = cls._read_multiple_preprocess(identifiers, actor, access, access_type, check_is_deleted, **kwargs)
|
||||
if query is None:
|
||||
raise NoResultFound(f"{cls.__name__} not found with identifier {identifier}")
|
||||
await db_session.execute(text("SET LOCAL enable_seqscan = OFF"))
|
||||
if is_postgresql_session(db_session):
|
||||
await db_session.execute(text("SET LOCAL enable_seqscan = OFF"))
|
||||
try:
|
||||
result = await db_session.execute(query)
|
||||
item = result.scalar_one_or_none()
|
||||
finally:
|
||||
await db_session.execute(text("SET LOCAL enable_seqscan = ON"))
|
||||
if is_postgresql_session(db_session):
|
||||
await db_session.execute(text("SET LOCAL enable_seqscan = ON"))
|
||||
|
||||
if item is None:
|
||||
raise NoResultFound(f"{cls.__name__} not found with {', '.join(query_conditions if query_conditions else ['no conditions'])}")
|
||||
|
||||
@@ -8,6 +8,7 @@ from letta.helpers.decorators import async_redis_cache
|
||||
from letta.log import get_logger
|
||||
from letta.orm.errors import NoResultFound
|
||||
from letta.orm.organization import Organization as OrganizationModel
|
||||
from letta.orm.sqlalchemy_base import is_postgresql_session
|
||||
from letta.orm.user import User as UserModel
|
||||
from letta.otel.tracing import trace_method
|
||||
from letta.schemas.user import User as PydanticUser
|
||||
@@ -157,13 +158,15 @@ class UserManager:
|
||||
"""Fetch a user by ID asynchronously."""
|
||||
async with db_registry.async_session() as session:
|
||||
# Turn off seqscan to force use pk index
|
||||
await session.execute(text("SET LOCAL enable_seqscan = OFF"))
|
||||
if is_postgresql_session(session):
|
||||
await session.execute(text("SET LOCAL enable_seqscan = OFF"))
|
||||
try:
|
||||
stmt = select(UserModel).where(UserModel.id == actor_id)
|
||||
result = await session.execute(stmt)
|
||||
user = result.scalar_one_or_none()
|
||||
finally:
|
||||
await session.execute(text("SET LOCAL enable_seqscan = ON"))
|
||||
if is_postgresql_session(session):
|
||||
await session.execute(text("SET LOCAL enable_seqscan = ON"))
|
||||
|
||||
if not user:
|
||||
raise NoResultFound(f"User not found with id={actor_id}")
|
||||
|
||||
Reference in New Issue
Block a user