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 Mar 27, 2023
1 parent 03fe217 commit 5d54a30
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions warehouse/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.exc import IntegrityError, OperationalError
from sqlalchemy.ext.declarative import declarative_base # type: ignore
from sqlalchemy.orm import sessionmaker
from sqlalchemy.orm import sessionmaker, Session as BaseSession, raiseload

from warehouse.metrics import IMetricsService
from warehouse.utils.attrs import make_repr
Expand Down Expand Up @@ -92,10 +92,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 5d54a30

Please sign in to comment.