Skip to content

Commit d6af20e

Browse files
authored
Merge pull request #32 from emptybutton/use-select-for-update
fix(`infrastructure`): use `select for update` (#26)
2 parents 5039644 + 09a17f6 commit d6af20e

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

src/ttt/infrastructure/adapters/games.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ async def game_with_game_location(
2020
join_condition = (TableUser.id == game_location_user_id) & (
2121
TableUser.game_location_game_id == TableGame.id
2222
)
23-
stmt = select(TableGame).join(TableUser, join_condition)
23+
stmt = (
24+
select(TableGame).join(TableUser, join_condition).with_for_update()
25+
)
2426

2527
table_game = await self._session.scalar(stmt)
2628

src/ttt/infrastructure/adapters/users.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ async def contains_user_with_id(
1919
id_: int,
2020
/,
2121
) -> bool:
22-
stmt = select(exists(1).where(TableUser.id == id_))
22+
stmt = select(exists(1).where(TableUser.id == id_)).with_for_update()
2323

2424
return bool(await self._session.execute(stmt))
2525

@@ -50,6 +50,7 @@ async def users_with_ids(
5050
return tuple(users)
5151

5252
async def user_with_id(self, id_: int, /) -> User | None:
53-
table_user = await self._session.get(TableUser, id_)
53+
stmt = select(TableUser).where(TableUser.id == id_).with_for_update()
54+
table_user = await self._session.scalar(stmt)
5455

5556
return None if table_user is None else table_user.entity()

0 commit comments

Comments
 (0)