From 538af492204f044fe2e1a3a49b3fb324933bc191 Mon Sep 17 00:00:00 2001 From: Ericson Soares Date: Thu, 24 Oct 2024 01:31:42 -0300 Subject: [PATCH] Properly sync file_path deletion when deleting directories --- core/src/location/mod.rs | 38 ++++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/core/src/location/mod.rs b/core/src/location/mod.rs index d3baf8532..e89639285 100644 --- a/core/src/location/mod.rs +++ b/core/src/location/mod.rs @@ -880,11 +880,9 @@ pub async fn delete_directory( library: &Library, location_id: location::id::Type, parent_iso_file_path: Option<&IsolatedFilePathData<'_>>, -) -> Result<(), QueryError> { +) -> Result<(), sd_core_sync::Error> { let Library { db, .. } = library; - // This is NOT sync-compatible! - // Sync requires having sync ids available. let children_params = sd_utils::chain_optional_iter( [file_path::location_id::equals(Some(location_id))], [parent_iso_file_path.and_then(|parent| { @@ -899,7 +897,39 @@ pub async fn delete_directory( })], ); - db.file_path().delete_many(children_params).exec().await?; + let pub_ids = library + .db + .file_path() + .find_many(children_params.clone()) + .select(file_path::select!({ pub_id })) + .exec() + .await? + .into_iter() + .map(|fp| fp.pub_id) + .collect::>(); + + if pub_ids.is_empty() { + debug!("No file paths to delete"); + return Ok(()); + } + + library + .sync + .write_ops( + &library.db, + ( + pub_ids + .into_iter() + .map(|pub_id| { + library + .sync + .shared_delete(prisma_sync::file_path::SyncId { pub_id }) + }) + .collect(), + db.file_path().delete_many(children_params), + ), + ) + .await?; // library.orphan_remover.invoke().await;