fix: use transaction for idx consistency checking (#2840)

This commit is contained in:
Ellie Huxtable
2025-07-24 10:08:20 +02:00
committed by GitHub
parent 576789335a
commit 0f381dd515

View File

@ -620,9 +620,12 @@ impl Database for Postgres {
const STATUS_SQL: &str =
"select host, tag, max(idx) from store where user_id = $1 group by host, tag";
// Use a transaction to ensure consistent reads from both tables
let mut tx = self.pool.begin().await.map_err(fix_error)?;
let mut res: Vec<(Uuid, String, i64)> = sqlx::query_as(STATUS_SQL)
.bind(user.id)
.fetch_all(&self.pool)
.fetch_all(&mut *tx)
.await
.map_err(fix_error)?;
res.sort();
@ -636,11 +639,13 @@ impl Database for Postgres {
let mut cached_res: Vec<(Uuid, String, i64)> =
sqlx::query_as("select host, tag, idx from store_idx_cache where user_id = $1")
.bind(user.id)
.fetch_all(&self.pool)
.fetch_all(&mut *tx)
.await
.map_err(fix_error)?;
cached_res.sort();
tx.commit().await.map_err(fix_error)?;
let mut status = RecordStatus::new();
let equal = res == cached_res;