From 64be6114d94ef5e8bf7056e135a0d4d8c1e7b308 Mon Sep 17 00:00:00 2001 From: "lpsolit%gmail.com" <> Date: Wed, 6 Apr 2005 07:19:51 +0000 Subject: Bug 86328: Deleting bugs doesn't delete dependent records properly - Patch by Frederic Buclin r=wurblzap a=justdave --- Bugzilla/Bug.pm | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) (limited to 'Bugzilla/Bug.pm') diff --git a/Bugzilla/Bug.pm b/Bugzilla/Bug.pm index 87bf96e25..5017196b3 100755 --- a/Bugzilla/Bug.pm +++ b/Bugzilla/Bug.pm @@ -23,6 +23,7 @@ # Bradley Baetz # Dave Miller # Max Kanat-Alexander +# Frédéric Buclin package Bugzilla::Bug; @@ -217,6 +218,60 @@ sub initBug { return $self; } +# This is the correct way to delete bugs from the DB. +# No bug should be deleted from anywhere else except from here. +# +sub remove_from_db { + my ($self) = @_; + my $dbh = Bugzilla->dbh; + + if ($self->{'error'}) { + ThrowCodeError("bug_error", { bug => $self }); + } + + my $bug_id = $self->{'bug_id'}; + + # tables having 'bugs.bug_id' as a foreign key: + # - attachments + # - bug_group_map + # - bugs + # - bugs_activity + # - cc + # - dependencies + # - duplicates + # - flags + # - keywords + # - longdescs + # - votes + + $dbh->bz_lock_tables('attachments WRITE', 'bug_group_map WRITE', + 'bugs WRITE', 'bugs_activity WRITE', 'cc WRITE', + 'dependencies WRITE', 'duplicates WRITE', + 'flags WRITE', 'keywords WRITE', + 'longdescs WRITE', 'votes WRITE'); + + $dbh->do("DELETE FROM bug_group_map WHERE bug_id = ?", undef, $bug_id); + $dbh->do("DELETE FROM bugs_activity WHERE bug_id = ?", undef, $bug_id); + $dbh->do("DELETE FROM cc WHERE bug_id = ?", undef, $bug_id); + $dbh->do("DELETE FROM dependencies WHERE blocked = ? OR dependson = ?", + undef, ($bug_id, $bug_id)); + $dbh->do("DELETE FROM duplicates WHERE dupe = ? OR dupe_of = ?", + undef, ($bug_id, $bug_id)); + $dbh->do("DELETE FROM flags WHERE bug_id = ?", undef, $bug_id); + $dbh->do("DELETE FROM keywords WHERE bug_id = ?", undef, $bug_id); + $dbh->do("DELETE FROM longdescs WHERE bug_id = ?", undef, $bug_id); + $dbh->do("DELETE FROM votes WHERE bug_id = ?", undef, $bug_id); + # Several of the previous tables also depend on attach_id. + $dbh->do("DELETE FROM attachments WHERE bug_id = ?", undef, $bug_id); + $dbh->do("DELETE FROM bugs WHERE bug_id = ?", undef, $bug_id); + + $dbh->bz_unlock_tables(); + + # Now this bug no longer exists + $self->DESTROY; + return $self; +} + ##################################################################### # Accessors ##################################################################### -- cgit v1.2.3-24-g4f1b