Skip to content

Commit d36e29a

Browse files
committed
change 'user_engine' to a 'WriteSession' instead, so the master db connection is used for writes only
1 parent daabcfc commit d36e29a

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

src/server/_db.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010

1111

1212
engine: Engine = create_engine(SQLALCHEMY_DATABASE_URI, **SQLALCHEMY_ENGINE_OPTIONS)
13+
metadata = MetaData(bind=engine)
14+
Session = sessionmaker(bind=engine)
1315

1416
if SQLALCHEMY_DATABASE_URI_PRIMARY:
15-
user_engine: Engine = create_engine(SQLALCHEMY_DATABASE_URI_PRIMARY, **SQLALCHEMY_ENGINE_OPTIONS)
17+
write_engine: Engine = create_engine(SQLALCHEMY_DATABASE_URI_PRIMARY, **SQLALCHEMY_ENGINE_OPTIONS)
18+
write_metadata = MetaData(bind=write_engine)
19+
WriteSession = sessionmaker(bind=write_engine)
1620
else:
17-
user_engine: Engine = engine
18-
19-
metadata = MetaData(bind=user_engine)
20-
21-
Session = sessionmaker(bind=user_engine)
22-
23-
21+
write_engine: Engine = engine
22+
write_metadata = metadata
23+
WriteSession = Session

src/server/admin/models.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from sqlalchemy.orm import relationship
44
from copy import deepcopy
55

6-
from .._db import Session
6+
from .._db import Session, WriteSession
77
from delphi.epidata.common.logger import get_structured_logger
88

99
from typing import Set, Optional, List
@@ -89,7 +89,7 @@ def find_user(*, # asterisk forces explicit naming of all arguments when calling
8989
@staticmethod
9090
def create_user(api_key: str, email: str, user_roles: Optional[Set[str]] = None) -> "User":
9191
get_structured_logger("api_user_models").info("creating user", api_key=api_key)
92-
with Session() as session:
92+
with WriteSession() as session:
9393
new_user = User(api_key=api_key, email=email)
9494
# TODO: we may need to populate 'created' field/column here, if the default
9595
# specified above gets bound to the time of when that line of python was evaluated.
@@ -107,7 +107,7 @@ def update_user(
107107
roles: Optional[Set[str]]
108108
) -> "User":
109109
get_structured_logger("api_user_models").info("updating user", user_id=user.id, new_api_key=api_key)
110-
with Session() as session:
110+
with WriteSession() as session:
111111
user = User.find_user(user_id=user.id)
112112
if user:
113113
update_stmt = (
@@ -123,7 +123,7 @@ def update_user(
123123
@staticmethod
124124
def delete_user(user_id: int) -> None:
125125
get_structured_logger("api_user_models").info("deleting user", user_id=user_id)
126-
with Session() as session:
126+
with WriteSession() as session:
127127
session.execute(delete(User).where(User.id == user_id))
128128
session.commit()
129129

@@ -136,7 +136,7 @@ class UserRole(Base):
136136
@staticmethod
137137
def create_role(name: str) -> None:
138138
get_structured_logger("api_user_models").info("creating user role", role=name)
139-
with Session() as session:
139+
with WriteSession() as session:
140140
session.execute(
141141
f"""
142142
INSERT INTO user_role (name)

0 commit comments

Comments
 (0)