From db93f24899e7038086cb054e48701f8745f0590f Mon Sep 17 00:00:00 2001 From: francoisgn <32835330+francoisgn@users.noreply.github.com> Date: Fri, 23 Oct 2026 14:31:34 +0300 Subject: [PATCH] LRC-LIB-CATALOG-IMPORT: a backup finds collections keyed on the live catalog's path (#2606) Collections imported by an earlier release are keyed on the live `.lrcat` path, with no catalog id. A backup zip reports the zip as its source, so neither its id key nor its path key matched them and every collection came in a second time. CatalogImport now carries `catalog_path` (where the catalog was backed up from) and the lookup tries that path's key too. Test `a_backup_finds_collections_an_earlier_release_keyed_on_the_live_catalog_path` fails without the extra key. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/lightroom_archive.rs | 2 ++ crates/engine/src/lightroom_catalog.rs | 35 ++++++++++++++++++++++++-- crates/engine/src/lightroom_job.rs | 2 ++ crates/ui-egui/src/lightroom_import.rs | 1 + 4 files changed, 38 insertions(+), 2 deletions(-) diff --git a/crates/engine/src/lightroom_archive.rs b/crates/engine/src/lightroom_archive.rs index df6ba5d5aa..511f81f308 100644 --- a/crates/engine/src/lightroom_archive.rs +++ b/crates/engine/src/lightroom_archive.rs @@ -520,6 +520,7 @@ mod tests { CatalogImport { source, catalog_id: None, + catalog_path: None, photos: Vec::new(), collections: Vec::new(), members: Vec::new(), @@ -545,6 +546,7 @@ mod tests { CatalogImport { source: "source.lrcat".into(), catalog_id: None, + catalog_path: None, photos: vec![photo], collections: vec![BTreeMap::from([("name".into(), serde_json::json!("Keepers"))])], members: vec![BTreeMap::from([("image".into(), serde_json::json!(7))])], diff --git a/crates/engine/src/lightroom_catalog.rs b/crates/engine/src/lightroom_catalog.rs index be9b381632..8f6aee4c0f 100644 --- a/crates/engine/src/lightroom_catalog.rs +++ b/crates/engine/src/lightroom_catalog.rs @@ -190,6 +190,11 @@ pub struct CatalogImport { /// of the catalog (issue #2592). `None` when the catalog has none. #[serde(default)] pub catalog_id: Option, + /// Where the catalog was when it isn't `source`: for a backup zip, the `.lrcat` it was backed up + /// from. Collections imported by earlier releases are keyed on that path, so a backup still finds + /// them. `None` when the catalog was read where it is. + #[serde(default)] + pub catalog_path: Option, pub photos: Vec, pub collections: Vec, pub members: Vec, @@ -595,6 +600,7 @@ fn read_file(path: &Path, document: &Path, source: &Path, cancel: &AtomicBool, d let mut out = CatalogImport { source: source.to_string_lossy().into(), catalog_id: reader.catalog_id(), + catalog_path: (document != source).then(|| document.to_string_lossy().into()), photos: Vec::new(), collections: optional_table("AgLibraryCollection", &mut warnings)?, members: optional_table("AgLibraryCollectionImage", &mut warnings)?, @@ -1075,11 +1081,14 @@ pub(crate) fn apply_prepared(s: &mut crate::Session, mut data: CatalogImport, co continue; } // keyed on the catalog's own id (the same in its backups); imports made before that was - // read are keyed on the catalog's path, and are still found by it + // read are keyed on the catalog's path, and are still found by it, also from a backup (the + // path of the catalog it was backed up from) let path_key = format!("{}:{}", key(&data.source), number(collection, "id_local")); + let original_key = data.catalog_path.as_ref().map(|p| format!("{}:{}", key(p), number(collection, "id_local"))); let collection_key = data.catalog_id.as_ref().map_or_else(|| path_key.clone(), |c| format!("lrcat:{c}:{}", number(collection, "id_local"))); - let id = [&collection_key, &path_key] + let id = [Some(&collection_key), Some(&path_key), original_key.as_ref()] .into_iter() + .flatten() .filter_map(|k| index.collections.get(k).copied()) .map(lightcraft_catalog::AlbumId) .find(|id| s.catalog.album(*id).is_some()) @@ -1176,6 +1185,7 @@ mod tests { CatalogImport { source: "test.lrcat".into(), catalog_id: None, + catalog_path: None, photos: vec![copy, photo], collections: vec![ row(json!({"id_local":9,"name":"Travel","creationId":"collection.group"})), @@ -1649,6 +1659,27 @@ mod tests { let _ = std::fs::remove_dir_all(&root); } + /// A backup of a catalog that an earlier release imported (collections keyed on the live + /// `.lrcat` path, no catalog id) finds those albums through the path it was backed up from. + #[test] + fn a_backup_finds_collections_an_earlier_release_keyed_on_the_live_catalog_path() { + let root = std::env::temp_dir().join(format!("lightcraft-lrbackup-legacy-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&root); + let (live, zip) = backup_fixture(&root, "968AF29B-EEE2-481E-8AF9-55DD82965F49"); + let mut s = crate::Session::new(); + s.open_library(root.join("Library"), false).unwrap(); + // an earlier release: the live catalog imported without its id (path keys only) + let mut old = read(&live).unwrap(); + old.catalog_id = None; + apply(&mut s, old, false).unwrap(); + assert_eq!(albums(&s), 1); + let backup = read(&zip).unwrap(); + assert_eq!(backup.catalog_path.as_deref(), Some(live.to_string_lossy().as_ref()), "where the zip's catalog was"); + apply(&mut s, backup, false).unwrap(); + assert_eq!(albums(&s), 1, "the backup finds the albums keyed on the live catalog's path"); + let _ = std::fs::remove_dir_all(&root); + } + /// Libraries imported by earlier releases (path keys) still find their albums. #[test] fn collections_imported_before_the_catalog_id_was_read_are_still_matched_by_path() { diff --git a/crates/engine/src/lightroom_job.rs b/crates/engine/src/lightroom_job.rs index 0a2eac60df..defdb850f0 100644 --- a/crates/engine/src/lightroom_job.rs +++ b/crates/engine/src/lightroom_job.rs @@ -357,6 +357,7 @@ mod tests { CatalogImport { source: "fixture.lrcat".into(), catalog_id: None, + catalog_path: None, photos: Vec::new(), collections: Vec::new(), members: Vec::new(), @@ -392,6 +393,7 @@ mod tests { data: CatalogImport { source: "fixture.lrcat".into(), catalog_id: None, + catalog_path: None, photos: vec![lightroom_catalog::CatalogPhoto { source_id: 1, uuid: "fixture-photo".into(), diff --git a/crates/ui-egui/src/lightroom_import.rs b/crates/ui-egui/src/lightroom_import.rs index 6a33e2e728..db6d5f3323 100644 --- a/crates/ui-egui/src/lightroom_import.rs +++ b/crates/ui-egui/src/lightroom_import.rs @@ -724,6 +724,7 @@ mod tests { let report = inspect_report(CatalogImport { source: "synthetic.lrcat".into(), catalog_id: None, + catalog_path: None, photos: Vec::new(), collections: Vec::new(), members: Vec::new(),