diff --git a/crates/catalog/src/journal.rs b/crates/catalog/src/journal.rs index 015e4da4ab..a969caf505 100644 --- a/crates/catalog/src/journal.rs +++ b/crates/catalog/src/journal.rs @@ -512,9 +512,9 @@ impl Journal { report.stale += 1; } Some((seq, op)) if j.seq.checked_add(1) == Some(seq) => { - // (a batch that was only partly taken back when it was applied is logged as it was, and fails - // the same way again, leaving the same changes: that is the log being replayed, not damage) - if catalog.apply(op).is_err_and(|e| !matches!(e, crate::CatalogError::PartlyApplied { .. })) { + // (a batch that was only partly taken back when it was applied is logged as it was: replaying it is + // not damage, see `Catalog::replay_op`) + if catalog.replay_op(op).is_err() { report.failed += 1; } else { report.replayed += 1; diff --git a/crates/catalog/src/lib.rs b/crates/catalog/src/lib.rs index 442732d274..af05cd6ef6 100644 --- a/crates/catalog/src/lib.rs +++ b/crates/catalog/src/lib.rs @@ -1370,7 +1370,7 @@ impl Catalog { for (i, line) in lines.iter().enumerate() { match serde_json::from_str::(line) { Ok(op) => { - self.apply(op)?; + self.replay_op(op)?; n += 1; } Err(e) if i + 1 == lines.len() => { @@ -1383,6 +1383,17 @@ impl Catalog { Ok(n) } + /// Apply one op of a saved log: the one place that says which failures of a logged op are the log being replayed, not + /// damage (`Journal::open` and [`Self::replay`] both go through it). A batch that was only partly taken back when it + /// was applied is logged as it was ([`CatalogError::PartlyApplied`]) and fails the same way again, leaving the same + /// changes, so replaying it counts as done; any other error is the caller's to handle. + pub(crate) fn replay_op(&mut self, op: Op) -> Result<()> { + match self.apply(op) { + Ok(_) | Err(CatalogError::PartlyApplied { .. }) => Ok(()), + Err(e) => Err(e), + } + } + pub fn op_to_log_line(op: &Op) -> String { let mut s = serde_json::to_string(op).unwrap_or_default(); s.push('\n'); diff --git a/crates/catalog/src/tests.rs b/crates/catalog/src/tests.rs index 22c54eb7b1..2f1efc531d 100644 --- a/crates/catalog/src/tests.rs +++ b/crates/catalog/src/tests.rs @@ -1199,3 +1199,35 @@ fn the_inverse_of_a_nested_batch_is_taken_back_op_by_op() { c.apply(*remaining).unwrap(); assert_eq!(c.to_snapshot(), before); } + +/// A batch that was only partly taken back is logged as it was and leaves the same changes when the log is replayed: the +/// replay counts it and goes on with the records after it (the journal's open does the same). +#[test] +fn replaying_a_log_goes_on_after_a_partly_applied_batch() { + let mut c = Catalog::default(); + let mut ids = Vec::new(); + for name in ["a.jpg", "b.jpg"] { + let id = c.alloc_photo_id(); + let photo = Photo::new(id, Source::File { path: format!("/p/{name}") }, name, "JPEG", 60, 40, "2026-01-01T10:00:00"); + c.apply(Op::AddPhoto { photo: Box::new(photo) }).unwrap(); + ids.push(id); + } + let stack = c.alloc_stack_id(); + c.apply(Op::AddStack { stack: Stack { id: stack, photos: ids.clone(), collapsed: false } }).unwrap(); + let start = c.clone(); + // the photo goes, then its stack, then an op fails: putting the stack back needs the photo, which comes later + let torn = + Op::Batch { ops: vec![Op::RemovePhoto { id: ids[0] }, Op::RemoveStack { id: stack }, Op::SetRating { id: PhotoId(9_999), rating: 1 }] }; + let rate = Op::SetRating { id: ids[1], rating: 4 }; + assert!(matches!(c.apply(torn.clone()), Err(CatalogError::PartlyApplied { .. }))); + c.apply(rate.clone()).unwrap(); + + let log = format!("{}{}", Catalog::op_to_log_line(&torn), Catalog::op_to_log_line(&rate)); + let mut replayed = start; + assert_eq!(replayed.replay(&log).unwrap(), 2, "both records were replayed"); + assert_eq!(replayed.to_snapshot(), c.to_snapshot(), "the log gives what the session had"); + + // an op that fails plainly still stops the replay + let bad = Op::SetRating { id: PhotoId(9_999), rating: 1 }; + assert_eq!(replayed.replay(&Catalog::op_to_log_line(&bad)), Err(CatalogError::NoPhoto(PhotoId(9_999)))); +} diff --git a/docs/whats-new.md b/docs/whats-new.md index e1889be7ab..8de30811f8 100644 --- a/docs/whats-new.md +++ b/docs/whats-new.md @@ -484,7 +484,8 @@ - When a change made of several steps (such as deleting photos that sit in stacks) failed part-way and LightCraft could not put everything back, what it had changed stayed in the open library but was neither saved nor undoable, so a restart brought back the old state and Undo could not finish the job. Such a change is now saved as it - stands, the error says it was only partly taken back, and Undo finishes taking it back. + stands, the error says it was only partly taken back, and Undo finishes taking it back once the file error that + stopped the rollback is gone. While that error remains, Undo reports it and keeps the rest of the change. ### Relative-date filters and smart albums follow the clock - A current view or source count using “in the last” rules now refreshes when the session clock moves into a new