From 85435de01f6880d8a2fa4b66449913295b7f09e8 Mon Sep 17 00:00:00 2001 From: THEtheChad <880190+THEtheChad@users.noreply.github.com> Date: Tue, 10 Nov 2026 20:30:00 -0600 Subject: [PATCH] fix(lightroom): a collection member whose collection is not an id joins no album stage_collections grouped AgLibraryCollectionImage rows by number(member, "collection"), which reads a missing, NULL, text or non-finite value as 0 and truncates 1.5 to 1, so such a row's photo joined the collection with that id. The column is now read as a whole number (id_of); a member that names no collection joins no album, with one warning, and stays in the recovery archive. The warning for a collection that shares an earlier one's id no longer counts these rows as photos listed under that id. --- crates/engine/src/lightroom_catalog.rs | 26 +++++- crates/engine/src/lightroom_catalog/tests.rs | 94 ++++++++++++++++++++ docs/lightroom-catalog-import.md | 4 +- 3 files changed, 121 insertions(+), 3 deletions(-) diff --git a/crates/engine/src/lightroom_catalog.rs b/crates/engine/src/lightroom_catalog.rs index 9a84b37ddbe..97e9e0e9b45 100644 --- a/crates/engine/src/lightroom_catalog.rs +++ b/crates/engine/src/lightroom_catalog.rs @@ -26,6 +26,20 @@ fn error(why: impl Into) -> crate::EngineError { fn text<'a>(r: &'a Row, k: &str) -> &'a str { r.get(k).and_then(Value::as_str).unwrap_or("") } +/// The record a row's `k` column names: its value as a whole number, or `None` when that is missing, NULL, text, a +/// fraction or out of `i64` range. [`number`] reads a missing, NULL or text value as 0, naming the record whose `id_local` is 0, and +/// truncates a fraction (1.5 names record 1). +fn id_of(r: &Row, k: &str) -> Option { + let n = r.get(k)?.as_number()?; + let f = n.as_f64()?; + // An integer goes through f64 as [`number`] takes the `id_local` of the record it names, so both get the same key + // past 2^53 too. + if n.is_i64() { + return Some(f as i64); + } + // (2^63 is the first value past i64::MAX; a cast would saturate onto a real id) + (f.fract() == 0.0 && f >= i64::MIN as f64 && f < 9_223_372_036_854_775_808.0).then_some(f as i64) +} fn number(r: &Row, k: &str) -> i64 { r.get(k).and_then(Value::as_f64).filter(|n| n.is_finite()).unwrap_or(0.0) as i64 } @@ -1042,7 +1056,7 @@ fn first_of_each_collection<'a>(data: &'a CatalogImport, warnings: &mut Vec = rows.iter().map(|r| (number(r, "id_local"), number(r, "parent"))).collect(); let collections: HashMap<_, _> = rows.iter().map(|r| (number(r, "id_local"), *r)).collect(); let mut grouped_members: HashMap> = HashMap::new(); + // read strictly: a NULL, text or fractional `collection` names no collection, not collection 0 (or 1 for 1.5) for member in &data.members { - grouped_members.entry(number(member, "collection")).or_default().push(member); + if let Some(collection) = id_of(member, "collection") { + grouped_members.entry(collection).or_default().push(member); + } + } + match data.members.iter().filter(|m| id_of(m, "collection").is_none()).count() { + 0 => {} + 1 => warnings.push("AgLibraryCollectionImage: 1 record added to no album; its collection is not a collection id".into()), + n => warnings.push(format!("AgLibraryCollectionImage: {n} records added to no album; their collection is not a collection id")), } for members in grouped_members.values_mut() { members.sort_by(|a, b| text(a, "positionInCollection").cmp(text(b, "positionInCollection"))); diff --git a/crates/engine/src/lightroom_catalog/tests.rs b/crates/engine/src/lightroom_catalog/tests.rs index 837db27c906..a46a3110042 100644 --- a/crates/engine/src/lightroom_catalog/tests.rs +++ b/crates/engine/src/lightroom_catalog/tests.rs @@ -620,6 +620,50 @@ fn a_collection_listed_twice_imports_once_and_a_conflicting_one_is_skipped_with_ ); assert_eq!(s.undo.len(), 1); } +/// A collection member whose `collection` is missing, NULL, text or a fraction names no collection. It used to read as +/// collection 0 (and 11.5 as 11), so the collection with that id took in photos that aren't its own; now such members +/// join no album, with one warning. A whole number stored as a REAL (11.0) still names its collection. +#[test] +fn a_member_whose_collection_is_not_a_whole_number_joins_no_album() { + let mut s = crate::Session::new(); + let mut data = sample(); + data.collections.push(row(json!({"id_local":0,"name":"Zero","creationId":"collection"}))); + data.collections.push(row(json!({"id_local":11,"name":"Eleven","creationId":"collection"}))); + data.members.extend([ + row(json!({"collection":0,"image":1,"positionInCollection":"a"})), + row(json!({"collection":11,"image":2,"positionInCollection":"a"})), + row(json!({"collection":11.0,"image":1,"positionInCollection":"b"})), + row(json!({"collection":null,"image":2,"positionInCollection":"c"})), + row(json!({"collection":"x","image":2,"positionInCollection":"d"})), + row(json!({"image":2,"positionInCollection":"e"})), + row(json!({"collection":11.5,"image":1,"positionInCollection":"f"})), + ]); + let r = apply(&mut s, data, false).unwrap(); + let id = |source: &str| PhotoId(r["mapping"][source].as_u64().unwrap()); + assert_eq!(album_named(&s, "Zero").photos, [id("1")], "{r}"); + assert_eq!(album_named(&s, "Eleven").photos, [id("2"), id("1")], "{r}"); + assert_eq!(album_named(&s, "Keepers").photos, [id("2"), id("1")], "{r}"); + assert_eq!(r["warnings"], json!(["AgLibraryCollectionImage: 4 records added to no album; their collection is not a collection id"])); +} +/// A collection left out for sharing collection 0's id says the photos under that id go to the first only when a +/// member names it: a member whose `collection` is NULL is not one of them. +#[test] +fn a_member_naming_no_collection_is_not_listed_under_id_0() { + let mut s = crate::Session::new(); + let mut data = sample(); + data.collections.push(row(json!({"id_local":0,"name":"Zero","creationId":"collection"}))); + data.collections.push(row(json!({"id_local":0,"name":"Also zero","creationId":"collection"}))); + data.members.push(row(json!({"collection":null,"image":1,"positionInCollection":"a"}))); + let r = apply(&mut s, data, false).unwrap(); + assert!(album_named(&s, "Zero").photos.is_empty(), "{r}"); + assert_eq!( + r["warnings"], + json!([ + "collection “Also zero”: not imported, it has the id of collection “Zero”", + "AgLibraryCollectionImage: 1 record added to no album; its collection is not a collection id" + ]) + ); +} /// The search for the nearest set ends on a parent loop that got past `validate` (A in B in A, a collection in /// itself) and on a chain of plain collections deeper than 64: the album goes to the top level, flagged as moved. #[test] @@ -1321,6 +1365,56 @@ fn record(values: &[SqlValue]) -> Vec { out.extend(body); out } +#[test] +fn a_reference_names_a_record_only_by_a_whole_number() { + let r = row(json!({ + "int": 7, "zero": 0, "negative": -3, "real": 7.0, "fraction": 7.5, "text": "7", "null": null, "nan_like": "NaN", + "past_f64": 9_007_199_254_740_993_i64, "max": i64::MAX, "edge": 9_223_372_036_854_775_808.0, + "low_edge": -9_223_372_036_854_775_808.0, "too_big": 1e19, "too_small": -1e19 + })); + let got: Vec<_> = [ + "int", + "zero", + "negative", + "real", + "fraction", + "text", + "null", + "nan_like", + "missing", + "past_f64", + "max", + "edge", + "low_edge", + "too_big", + "too_small", + ] + .iter() + .map(|k| (*k, id_of(&r, k))) + .collect(); + assert_eq!( + got, + [ + ("int", Some(7)), + ("zero", Some(0)), + ("negative", Some(-3)), + ("real", Some(7)), + ("fraction", None), + ("text", None), + ("null", None), + ("nan_like", None), + ("missing", None), + // an integer rounds through f64 as `number` reads the `id_local` it names + ("past_f64", Some(number(&r, "past_f64"))), + ("max", Some(number(&r, "max"))), + // 2^63, the first value past i64::MAX: a cast would saturate onto record i64::MAX + ("edge", None), + ("low_edge", Some(i64::MIN)), + ("too_big", None), + ("too_small", None), + ] + ); +} fn leaf(page: &mut [u8], offset: usize, rows: &[Vec]) { page[offset] = 13; page[offset + 3..offset + 5].copy_from_slice(&(rows.len() as u16).to_be_bytes()); diff --git a/docs/lightroom-catalog-import.md b/docs/lightroom-catalog-import.md index d2316d17bc9..e783e580a8b 100644 --- a/docs/lightroom-catalog-import.md +++ b/docs/lightroom-catalog-import.md @@ -119,7 +119,9 @@ such as a print saved inside a plain collection, or one whose parent is system-o nearest collection set above it, or the top level, and the result names it under warnings (issues #2165, #2532). Quick Collection and unsaved creations are system-only and not imported; publish collections live in tables the importer doesn't read. A collection the catalog lists twice imports once; a different collection with the id of an -earlier one is left out with a warning naming both, and the photos listed under that id go to the first. Archived +earlier one is left out with a warning naming both, and the photos listed under that id go to the first. A +collection member whose `collection` is not a whole number (missing, NULL, text or a fraction) joins no album, with +one warning; it does not join the collection whose id is 0 (or 11 for 11.5). Archived history and snapshots are source data, not native LightCraft history yet. A Custom white balance on a raw LightCraft develops relative to its as-shot look (ARW, NEF, RW2, RAF, CR3, CR2, PEF, SRW; Lightroom names them all `RAW`) can't be taken as Kelvin: the catalog keeps no