From: Frédéric Buclin Date: Wed, 28 Dec 2011 11:09:29 +0000 (+0100) Subject: Bug 713144: The SQL query to remove older searches from the profile_search table... X-Git-Tag: bugzilla-4.2rc1~5 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5deff30099c7ae0c9cce9c32e52f4bd1dc380926;p=thirdparty%2Fbugzilla.git Bug 713144: The SQL query to remove older searches from the profile_search table should be more robust r=dkl a=LpSolit --- diff --git a/Bugzilla/Search/Recent.pm b/Bugzilla/Search/Recent.pm index 89d9cf6ff9..ccd4a0f090 100644 --- a/Bugzilla/Search/Recent.pm +++ b/Bugzilla/Search/Recent.pm @@ -57,14 +57,16 @@ sub create { my $class = shift; my $dbh = Bugzilla->dbh; $dbh->bz_start_transaction(); - my $search = $class->SUPER::create(@_); + my $search = $class->SUPER::create(@_); + my $user_id = $search->user_id; # Enforce there only being SAVE_NUM_SEARCHES per user. - my ($num_searches, $min_id) = $dbh->selectrow_array( - 'SELECT COUNT(*), MIN(id) FROM profile_search WHERE user_id = ?', - undef, $search->user_id); - if ($num_searches > SAVE_NUM_SEARCHES) { - $dbh->do('DELETE FROM profile_search WHERE id = ?', undef, $min_id); + my $min_id = $dbh->selectrow_array( + 'SELECT id FROM profile_search WHERE user_id = ? ORDER BY id DESC ' + . $dbh->sql_limit(1, SAVE_NUM_SEARCHES), undef, $user_id); + if ($min_id) { + $dbh->do('DELETE FROM profile_search WHERE user_id = ? AND id <= ?', + undef, ($user_id, $min_id)); } $dbh->bz_commit_transaction(); return $search;