fix: use database transactions for atomic operations in save and update modes

Co-authored-by: aider (openai/andrew/openrouter/qwen/qwen3-coder) <aider@aider.chat>
This commit is contained in:
Andrew Phillips
2025-08-09 23:33:53 -03:00
parent a3eb9e7056
commit cb408bafa1
2 changed files with 23 additions and 11 deletions

View File

@@ -119,6 +119,9 @@ pub fn mode_save(
db::set_item_tags(conn, item.clone(), tags)?;
// Use a transaction for database operations to ensure atomicity
let tx = conn.transaction()?;
let mut item_meta: HashMap<String, String> = get_meta_from_env();
if let Ok(hostname) = gethostname().into_string() {
@@ -132,16 +135,16 @@ pub fn mode_save(
item_meta.insert(item.key, item.value);
}
let item_id = item.id.ok_or_else(|| anyhow!("Item missing ID"))?;
for kv in item_meta.iter() {
let meta = db::Meta {
id: item.id.unwrap(),
id: item_id,
name: kv.0.to_string(),
value: kv.1.to_string(),
};
db::store_meta(conn, meta)?;
db::store_meta(&tx, meta)?;
}
let item_id = item.id.ok_or_else(|| anyhow!("Item missing ID"))?;
let mut item_path = data_path.clone();
item_path.push(item_id.to_string());
@@ -190,7 +193,7 @@ pub fn mode_save(
match meta_plugin.finalize() {
Ok(meta_value) => {
if let Err(e) = store_item_meta_value(conn, item.clone(), meta_name.clone(), meta_value) {
if let Err(e) = store_item_meta_value(&tx, item.clone(), meta_name.clone(), meta_value) {
eprintln!("Warning: Failed to store meta value for {}: {}", meta_name, e);
}
}
@@ -200,7 +203,10 @@ pub fn mode_save(
}
}
db::update_item(conn, item.clone())?;
db::update_item(&tx, item.clone())?;
// Commit the transaction
tx.commit()?;
Ok(())
}