From c35dd27f01497c22bb379695ae09f63bf7a31356 Mon Sep 17 00:00:00 2001 From: Mathias Kresin Date: Sun, 13 Dec 2015 15:16:47 +0100 Subject: [PATCH] remove all group from feeds before deleting Main purpose is to prevent orphaned groups. It's not possible to use the ON DELETE CASCADE trigger here, to remove the group together with the last feed that is assigned to this group. The ON DELETE CASCADE trigger will raise an foreign key violation error in cases where the removed feed is not the last feed associated to a group. The purge_groups() call has been moved to the remove group functions, since it's the only way to create an orphaned group. --- models/feed.php | 2 ++ models/group.php | 33 +++++++++++++++++++++++++++++---- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/models/feed.php b/models/feed.php index 0e2dc6e..6482c91 100644 --- a/models/feed.php +++ b/models/feed.php @@ -435,6 +435,8 @@ function update_cache($feed_id, $last_modified, $etag) function remove($feed_id) { delete_favicon($feed_id); + Group\remove_all($feed_id); + // Items are removed by a sql constraint return Database::getInstance('db')->table('feeds')->eq('id', $feed_id)->remove(); } diff --git a/models/group.php b/models/group.php index d010edc..31d254a 100644 --- a/models/group.php +++ b/models/group.php @@ -151,11 +151,39 @@ function add($feed_id, $group_ids) */ function remove($feed_id, $group_ids) { - return Database::getInstance('db') + $result = Database::getInstance('db') ->table('feeds_groups') ->eq('feed_id', $feed_id) ->in('group_id', $group_ids) ->remove(); + + // remove empty groups + if ($result) { + purge_groups(); + } + + return $result; +} + +/** + * Remove all groups from feed + * + * @param integer $feed_id id of the feed + * @return boolean true on success, false on error + */ +function remove_all($feed_id) +{ + $result = Database::getInstance('db') + ->table('feeds_groups') + ->eq('feed_id', $feed_id) + ->remove(); + + // remove empty groups + if ($result) { + purge_groups(); + } + + return $result; } /** @@ -213,8 +241,5 @@ function update_feed_groups($feed_id, $group_ids, $create_group = '') return false; } - // cleanup - purge_groups(); - return true; }