1
Fork 0
mirror of https://github.com/thegeneralist01/archivr synced 2026-07-22 03:05:32 +02:00

fix(core): delete_entry correctly nulls FK for child entries

archive_run_items.produced_entry_id has no ON DELETE action so it must
be manually nulled before the entry row is deleted. The old query used
WHERE root_entry_id = entry_id, which finds descendants of a root but
returns nothing when entry_id IS a child (children have no sub-children,
so no row has root_entry_id = child_id). The DELETE then failed under
foreign_keys=ON.

Fix: add OR produced_entry_id = ?1 so the entry's own run_item FK is
always cleared before deletion, regardless of whether it is a root or a
child. The subtree subquery is kept for the root-deletion case where all
child run_items also need nulling.
This commit is contained in:
TheGeneralist 2026-07-21 13:37:18 +02:00
parent c800a395c7
commit d07e4d7ac9
Signed by: thegeneralist01
SSH key fingerprint: SHA256:pp9qddbCNmVNoSjevdvQvM5z0DHN7LTa8qBMbcMq/R4
3 changed files with 22 additions and 4 deletions

View file

@ -1638,12 +1638,15 @@ pub fn delete_entry(conn: &Connection, entry_uid: &str) -> Result<bool> {
// shared blobs with any subtree member, excluding every subtree ID simultaneously.
cascade_cached_bytes_after_subtree_delete(conn, &subtree_ids)?;
// Null the FK that has no ON DELETE action (covers root and all descendants).
// Null the FK that has no ON DELETE action. Must cover:
// - The entry itself (child entry: root_entry_id = playlist root, not self)
// - All descendants (root entry: children have root_entry_id = entry_id)
conn.execute(
"UPDATE archive_run_items SET produced_entry_id = NULL
WHERE produced_entry_id IN (
SELECT id FROM archived_entries WHERE root_entry_id = ?1
)",
WHERE produced_entry_id = ?1
OR produced_entry_id IN (
SELECT id FROM archived_entries WHERE root_entry_id = ?1
)",
[entry_id],
)?;