fix: ensure the idx cache is cleaned on deletion, only insert if records are inserted (#2841)

This commit is contained in:
Ellie Huxtable
2025-07-24 14:29:37 +02:00
committed by GitHub
parent 0f381dd515
commit cb157f7c40

View File

@ -232,15 +232,28 @@ impl Database for Postgres {
} }
async fn delete_store(&self, user: &User) -> DbResult<()> { async fn delete_store(&self, user: &User) -> DbResult<()> {
let mut tx = self.pool.begin().await.map_err(fix_error)?;
sqlx::query( sqlx::query(
"delete from store "delete from store
where user_id = $1", where user_id = $1",
) )
.bind(user.id) .bind(user.id)
.execute(&self.pool) .execute(&mut *tx)
.await .await
.map_err(fix_error)?; .map_err(fix_error)?;
sqlx::query(
"delete from store_idx_cache
where user_id = $1",
)
.bind(user.id)
.execute(&mut *tx)
.await
.map_err(fix_error)?;
tx.commit().await.map_err(fix_error)?;
Ok(()) Ok(())
} }
@ -509,7 +522,7 @@ impl Database for Postgres {
for i in records { for i in records {
let id = atuin_common::utils::uuid_v7(); let id = atuin_common::utils::uuid_v7();
sqlx::query( let result = sqlx::query(
"insert into store "insert into store
(id, client_id, host, idx, timestamp, version, tag, data, cek, user_id) (id, client_id, host, idx, timestamp, version, tag, data, cek, user_id)
values ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) values ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
@ -530,15 +543,17 @@ impl Database for Postgres {
.await .await
.map_err(fix_error)?; .map_err(fix_error)?;
// we're already iterating sooooo // Only update heads if we actually inserted the record
heads if result.rows_affected() > 0 {
.entry((i.host.id, &i.tag)) heads
.and_modify(|e| { .entry((i.host.id, &i.tag))
if i.idx > *e { .and_modify(|e| {
*e = i.idx if i.idx > *e {
} *e = i.idx
}) }
.or_insert(i.idx); })
.or_insert(i.idx);
}
} }
// we've built the map of heads for this push, so commit it to the database // we've built the map of heads for this push, so commit it to the database
@ -644,7 +659,7 @@ impl Database for Postgres {
.map_err(fix_error)?; .map_err(fix_error)?;
cached_res.sort(); cached_res.sort();
tx.commit().await.map_err(fix_error)?; // No need to commit a read-only transaction
let mut status = RecordStatus::new(); let mut status = RecordStatus::new();