From bb84ca40164b57281f1a58d7a06e28a421fe6fbb Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sun, 17 Jan 2016 08:28:56 +0000 Subject: mlib: fix foreign-key ordering issues with delete We need to ensure children and comments are all deleted before parents are. --- lib/dtas/mlib.rb | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'lib/dtas') diff --git a/lib/dtas/mlib.rb b/lib/dtas/mlib.rb index fddf8ad..1f61369 100644 --- a/lib/dtas/mlib.rb +++ b/lib/dtas/mlib.rb @@ -430,15 +430,25 @@ class DTAS::Mlib # :nodoc: node_id = node[:id] q = { parent_id: node_id } nodes = @db[:nodes] + comments = @db[:comments] + + # remove children, step 1 nodes.where(q).each do |nd| - next if nd[:id] == root_id + nd_id = nd[:id] + next if nd_id == root_id case nd[:tlen] when DM_DIR, DM_IGN remove_entry(nd) + else + comments.where(node_id: nd_id).delete end end + + # remove children, step 2 nodes.where(q).delete - @db[:comments].where(node_id: node_id).delete + + # finally remove ourselves + comments.where(node_id: node_id).delete nodes.where(id: node_id).delete end -- cgit v1.2.3-24-ge0c7