Skip to content

Commit

Permalink
Use raiseload option for all queries
Browse files Browse the repository at this point in the history
  • Loading branch information
di committed Jul 12, 2024
1 parent 0377af2 commit ab05791
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions warehouse/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,14 @@
from sqlalchemy import event, func, inspect
from sqlalchemy.dialects.postgresql import UUID as PG_UUID
from sqlalchemy.exc import IntegrityError, OperationalError
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, sessionmaker
from sqlalchemy.orm import (
DeclarativeBase,
Mapped,
Session as BaseSession,
mapped_column,
raiseload,
sessionmaker,
)

from warehouse.metrics import IMetricsService
from warehouse.utils.attrs import make_repr
Expand Down Expand Up @@ -102,10 +109,18 @@ class Model(ModelBase):
)


# Custom Session to prevent lazy-loading
class StrictSession(BaseSession):
def query(self, *entities, **kwargs):
query = super().query(*entities)
query = query.options(raiseload("*"))
return query


# Create our session class here, this will stay stateless as we'll bind the
# engine to each new state we create instead of binding it to the session
# class.
Session = sessionmaker()
Session = sessionmaker(class_=StrictSession)


def listens_for(target, identifier, *args, **kwargs):
Expand Down

0 comments on commit ab05791

Please sign in to comment.