Skip to content

Commit

Permalink
fix: lookup in same tx
Browse files Browse the repository at this point in the history
  • Loading branch information
bodymindarts committed Jun 7, 2024
1 parent 7efeaf2 commit f2f6bdd
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
4 changes: 2 additions & 2 deletions cala-ledger/src/account_set/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ impl AccountSets {
let member = member.into();
let (time, parents, account_set, member_id) = match member {
AccountSetMember::Account(id) => {
let set = self.repo.find(account_set_id).await?;
let set = self.repo.find_in_tx(op.tx(), account_set_id).await?;
let (time, parents) = self
.repo
.add_member_account_and_return_parents(op.tx(), account_set_id, id)
Expand All @@ -116,7 +116,7 @@ impl AccountSets {
AccountSetMember::AccountSet(id) => {
let mut accounts = self
.repo
.find_all::<AccountSet>(&[account_set_id, id])
.find_all_in_tx::<AccountSet>(op.tx(), &[account_set_id, id])
.await?;
let target = accounts
.remove(&account_set_id)
Expand Down
19 changes: 16 additions & 3 deletions cala-ledger/src/account_set/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,11 @@ impl AccountSetRepo {
Ok((time.expect("time not set"), ret))
}

pub async fn find(&self, account_set_id: AccountSetId) -> Result<AccountSet, AccountSetError> {
pub async fn find_in_tx(
&self,
db: &mut Transaction<'_, Postgres>,
account_set_id: AccountSetId,
) -> Result<AccountSet, AccountSetError> {
let rows = sqlx::query_as!(
GenericEvent,
r#"SELECT a.id, e.sequence, e.event,
Expand All @@ -191,7 +195,7 @@ impl AccountSetRepo {
AND a.id = $1"#,
account_set_id as AccountSetId
)
.fetch_all(&self.pool)
.fetch_all(&mut **db)
.await?;
match EntityEvents::load_first(rows) {
Ok(account_set) => Ok(account_set),
Expand All @@ -205,6 +209,15 @@ impl AccountSetRepo {
pub(super) async fn find_all<T: From<AccountSet>>(
&self,
ids: &[AccountSetId],
) -> Result<HashMap<AccountSetId, T>, AccountSetError> {
let mut tx = self.pool.begin().await?;
self.find_all_in_tx(&mut tx, ids).await
}

pub(super) async fn find_all_in_tx<T: From<AccountSet>>(
&self,
db: &mut Transaction<'_, Postgres>,
ids: &[AccountSetId],
) -> Result<HashMap<AccountSetId, T>, AccountSetError> {
let rows = sqlx::query_as!(
GenericEvent,
Expand All @@ -219,7 +232,7 @@ impl AccountSetRepo {
ORDER BY s.id, e.sequence"#,
ids as &[AccountSetId]
)
.fetch_all(&self.pool)
.fetch_all(&mut **db)
.await?;
let n = rows.len();
let ret = EntityEvents::load_n(rows, n)?
Expand Down

0 comments on commit f2f6bdd

Please sign in to comment.