Skip to content

Commit 3eef9aa

Browse files
committed
Use raiseload option for all queries
1 parent 03fe217 commit 3eef9aa

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

warehouse/db.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from sqlalchemy.dialects.postgresql import UUID
2424
from sqlalchemy.exc import IntegrityError, OperationalError
2525
from sqlalchemy.ext.declarative import declarative_base # type: ignore
26-
from sqlalchemy.orm import sessionmaker
26+
from sqlalchemy.orm import Session as BaseSession, raiseload, sessionmaker
2727

2828
from warehouse.metrics import IMetricsService
2929
from warehouse.utils.attrs import make_repr
@@ -92,10 +92,18 @@ class Model(ModelBase):
9292
)
9393

9494

95+
# Custom Session to prevent lazy-loading
96+
class StrictSession(BaseSession):
97+
def query(self, *entities, **kwargs):
98+
query = super().query(*entities)
99+
query = query.options(raiseload("*"))
100+
return query
101+
102+
95103
# Create our session class here, this will stay stateless as we'll bind the
96104
# engine to each new state we create instead of binding it to the session
97105
# class.
98-
Session = sessionmaker()
106+
Session = sessionmaker(class_=StrictSession)
99107

100108

101109
def listens_for(target, identifier, *args, **kwargs):

0 commit comments

Comments
 (0)