Skip to content

Commit 51316fd

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

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
@@ -87,7 +87,7 @@ def find_user(*, # asterisk forces explicit naming of all arguments when calling
8787
@staticmethod
8888
def create_user(api_key: str, email: str, user_roles: Optional[Set[str]] = None) -> "User":
8989
get_structured_logger("api_user_models").info("creating user", api_key=api_key)
90-
with Session() as session:
90+
with WriteSession() as session:
9191
new_user = User(api_key=api_key, email=email)
9292
# TODO: we may need to populate 'created' field/column here, if the default
9393
# specified above gets bound to the time of when that line of python was evaluated.
@@ -105,7 +105,7 @@ def update_user(
105105
roles: Optional[Set[str]]
106106
) -> "User":
107107
get_structured_logger("api_user_models").info("updating user", user_id=user.id, new_api_key=api_key)
108-
with Session() as session:
108+
with WriteSession() as session:
109109
user = User.find_user(user_id=user.id)
110110
if user:
111111
update_stmt = (
@@ -121,7 +121,7 @@ def update_user(
121121
@staticmethod
122122
def delete_user(user_id: int) -> None:
123123
get_structured_logger("api_user_models").info("deleting user", user_id=user_id)
124-
with Session() as session:
124+
with WriteSession() as session:
125125
session.execute(delete(User).where(User.id == user_id))
126126
session.commit()
127127

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

0 commit comments

Comments
 (0)