Commit 388fa19e authored by lpsolit%gmail.com's avatar lpsolit%gmail.com

Bug 303692: Eliminate deprecated Bugzilla::DB routines from sanitycheck.cgi -…

Bug 303692: Eliminate deprecated Bugzilla::DB routines from sanitycheck.cgi - Patch by Gabriel Sales de Oliveira <gabriel@async.com.br> r=LpSolit a=justdave
parent 03dd95d6
...@@ -103,16 +103,15 @@ $template->put_header("Bugzilla Sanity Check"); ...@@ -103,16 +103,15 @@ $template->put_header("Bugzilla Sanity Check");
if (defined $cgi->param('rebuildvotecache')) { if (defined $cgi->param('rebuildvotecache')) {
Status("OK, now rebuilding vote cache."); Status("OK, now rebuilding vote cache.");
$dbh->bz_lock_tables('bugs WRITE', 'votes READ'); $dbh->bz_lock_tables('bugs WRITE', 'votes READ');
SendSQL("UPDATE bugs SET votes = 0"); $dbh->do(q{UPDATE bugs SET votes = 0});
SendSQL("SELECT bug_id, SUM(vote_count) FROM votes " . my $sth_update = $dbh->prepare(q{UPDATE bugs
$dbh->sql_group_by('bug_id')); SET votes = ?
my %votes; WHERE bug_id = ?});
while (@row = FetchSQLData()) { my $sth = $dbh->prepare(q{SELECT bug_id, SUM(vote_count)
my ($id, $v) = (@row); FROM votes }. $dbh->sql_group_by('bug_id'));
$votes{$id} = $v; $sth->execute();
} while (my ($id, $v) = $sth->fetchrow_array) {
foreach my $id (keys %votes) { $sth_update->execute($v, $id);
SendSQL("UPDATE bugs SET votes = $votes{$id} WHERE bug_id = $id");
} }
$dbh->bz_unlock_tables(); $dbh->bz_unlock_tables();
Status("Vote cache has been rebuilt."); Status("Vote cache has been rebuilt.");
...@@ -222,22 +221,23 @@ if (defined $cgi->param('rescanallBugMail')) { ...@@ -222,22 +221,23 @@ if (defined $cgi->param('rescanallBugMail')) {
require Bugzilla::BugMail; require Bugzilla::BugMail;
Status("OK, now attempting to send unsent mail"); Status("OK, now attempting to send unsent mail");
SendSQL("SELECT bug_id FROM bugs my $time = $dbh->sql_interval(30, 'MINUTE');
WHERE (lastdiffed IS NULL OR lastdiffed < delta_ts) AND
delta_ts < now() - " . $dbh->sql_interval(30, 'MINUTE') . my $list = $dbh->selectcol_arrayref(qq{
" ORDER BY bug_id"); SELECT bug_id
my @list; FROM bugs
while (MoreSQLData()) { WHERE (lastdiffed IS NULL
push (@list, FetchOneColumn()); OR lastdiffed < delta_ts)
} AND delta_ts < now() - $time
ORDER BY bug_id});
Status(scalar(@list) . ' bugs found with possibly unsent mail.');
Status(scalar(@$list) . ' bugs found with possibly unsent mail.');
foreach my $bugid (@list) {
foreach my $bugid (@$list) {
Bugzilla::BugMail::Send($bugid); Bugzilla::BugMail::Send($bugid);
} }
if (scalar(@list) > 0) { if (scalar(@$list) > 0) {
Status("Unsent mail has been sent."); Status("Unsent mail has been sent.");
} }
...@@ -319,29 +319,32 @@ sub CrossCheck { ...@@ -319,29 +319,32 @@ sub CrossCheck {
my %exceptions = map { $_ => 1 } @$exceptions; my %exceptions = map { $_ => 1 } @$exceptions;
Status("... from $refertable.$referfield"); Status("... from $refertable.$referfield");
SendSQL("SELECT DISTINCT $refertable.$referfield" . ($keyname ? ", $refertable.$keyname" : '') . " " . my $query = qq{SELECT DISTINCT $refertable.$referfield} .
"FROM $refertable LEFT JOIN $table " . ($keyname ? qq{, $refertable.$keyname } : q{}) .
" ON $refertable.$referfield = $table.$field " . qq{ FROM $refertable
"WHERE $table.$field IS NULL " . LEFT JOIN $table
" AND $refertable.$referfield IS NOT NULL"); ON $refertable.$referfield = $table.$field
WHERE $table.$field IS NULL
AND $refertable.$referfield IS NOT NULL};
my $sth = $dbh->prepare($query);
$sth->execute;
my $has_bad_references = 0; my $has_bad_references = 0;
while (MoreSQLData()) {
my ($value, $key) = FetchSQLData(); while (my ($value, $key) = $sth->fetchrow_array) {
if (!$exceptions{$value}) { next if $exceptions{$value};
my $alert = "Bad value &quot;$value&quot; found in $refertable.$referfield"; my $alert = "Bad value &quot;$value&quot; found in $refertable.$referfield";
if ($keyname) { if ($keyname) {
if ($keyname eq 'bug_id') { if ($keyname eq 'bug_id') {
$alert .= ' (bug ' . BugLink($key) . ')'; $alert .= ' (bug ' . BugLink($key) . ')';
} } else {
else { $alert .= " ($keyname == '$key')";
$alert .= " ($keyname == '$key')";
}
} }
Alert($alert);
$has_bad_references = 1;
} }
Alert($alert);
$has_bad_references = 1;
} }
# References to non existent bugs can be safely removed, bug 288461 # References to non existent bugs can be safely removed, bug 288461
if ($table eq 'bugs' && $has_bad_references) { if ($table eq 'bugs' && $has_bad_references) {
...@@ -480,19 +483,22 @@ sub DoubleCrossCheck { ...@@ -480,19 +483,22 @@ sub DoubleCrossCheck {
my ($refertable, $referfield1, $referfield2, $keyname) = @$ref; my ($refertable, $referfield1, $referfield2, $keyname) = @$ref;
Status("... from $refertable.$referfield1 / $refertable.$referfield2"); Status("... from $refertable.$referfield1 / $refertable.$referfield2");
SendSQL("SELECT DISTINCT $refertable.$referfield1, $refertable.$referfield2" . ($keyname ? ", $refertable.$keyname" : '') . " " . my $d_cross_check = $dbh->selectall_arrayref(qq{
"FROM $refertable LEFT JOIN $table " . SELECT DISTINCT $refertable.$referfield1,
" ON $refertable.$referfield1 = $table.$field1 " . $refertable.$referfield2 } .
" AND $refertable.$referfield2 = $table.$field2 " . ($keyname ? qq{, $refertable.$keyname } : q{}) .
"WHERE $table.$field1 IS NULL " . qq{ FROM $refertable
" AND $table.$field2 IS NULL " . LEFT JOIN $table
" AND $refertable.$referfield1 IS NOT NULL " . ON $refertable.$referfield1 = $table.$field1
" AND $refertable.$referfield2 IS NOT NULL"); AND $refertable.$referfield2 = $table.$field2
WHERE $table.$field1 IS NULL
while (MoreSQLData()) { AND $table.$field2 IS NULL
my ($value1, $value2, $key) = FetchSQLData(); AND $refertable.$referfield1 IS NOT NULL
AND $refertable.$referfield2 IS NOT NULL});
foreach my $check (@$d_cross_check) {
my ($value1, $value2, $key) = @$check;
my $alert = "Bad values &quot;$value1&quot;, &quot;$value2&quot; found in " . my $alert = "Bad values &quot;$value1&quot;, &quot;$value2&quot; found in " .
"$refertable.$referfield1 / $refertable.$referfield2"; "$refertable.$referfield1 / $refertable.$referfield2";
if ($keyname) { if ($keyname) {
...@@ -530,9 +536,10 @@ DoubleCrossCheck("milestones", "product_id", "value", ...@@ -530,9 +536,10 @@ DoubleCrossCheck("milestones", "product_id", "value",
Status("Checking profile logins"); Status("Checking profile logins");
SendSQL("SELECT userid, login_name FROM profiles"); my $sth = $dbh->prepare(q{SELECT userid, login_name FROM profiles});
$sth->execute;
while (my ($id,$email) = (FetchSQLData())) { while (my ($id, $email) = $sth->fetchrow_array) {
validate_email_syntax($email) validate_email_syntax($email)
|| Alert "Bad profile email address, id=$id, &lt;$email&gt;."; || Alert "Bad profile email address, id=$id, &lt;$email&gt;.";
} }
...@@ -549,15 +556,17 @@ sub AlertBadVoteCache { ...@@ -549,15 +556,17 @@ sub AlertBadVoteCache {
$offervotecacherebuild = 1; $offervotecacherebuild = 1;
} }
SendSQL("SELECT bug_id, votes, keywords FROM bugs " . $sth = $dbh->prepare(q{SELECT bug_id, votes, keywords
"WHERE votes != 0 OR keywords != ''"); FROM bugs
WHERE votes != 0
OR keywords != ''});
$sth->execute;
my %votes; my %votes;
my %bugid; my %bugid;
my %keyword; my %keyword;
while (@row = FetchSQLData()) { while (my ($id, $v, $k) = $sth->fetchrow_array) {
my($id, $v, $k) = (@row);
if ($v != 0) { if ($v != 0) {
$votes{$id} = $v; $votes{$id} = $v;
} }
...@@ -567,11 +576,12 @@ while (@row = FetchSQLData()) { ...@@ -567,11 +576,12 @@ while (@row = FetchSQLData()) {
} }
Status("Checking cached vote counts"); Status("Checking cached vote counts");
SendSQL("SELECT bug_id, SUM(vote_count) FROM votes " . $sth = $dbh->prepare(q{SELECT bug_id, SUM(vote_count)
$dbh->sql_group_by('bug_id')); FROM votes }.
$dbh->sql_group_by('bug_id'));
$sth->execute;
while (@row = FetchSQLData()) { while (my ($id, $v) = $sth->fetchrow_array) {
my ($id, $v) = (@row);
if ($v <= 0) { if ($v <= 0) {
Alert("Bad vote sum for bug $id"); Alert("Bad vote sum for bug $id");
} else { } else {
...@@ -593,9 +603,12 @@ if ($offervotecacherebuild) { ...@@ -593,9 +603,12 @@ if ($offervotecacherebuild) {
Status("Checking keywords table"); Status("Checking keywords table");
my %keywordids; my %keywordids;
SendSQL("SELECT id, name FROM keyworddefs");
while (@row = FetchSQLData()) { my $keywords = $dbh->selectall_arrayref(q{SELECT id, name
my ($id, $name) = (@row); FROM keyworddefs});
foreach my $keyword (@$keywords) {
my ($id, $name) = @$keyword;
if ($keywordids{$id}) { if ($keywordids{$id}) {
Alert("Duplicate entry in keyworddefs for id $id"); Alert("Duplicate entry in keyworddefs for id $id");
} }
...@@ -605,12 +618,13 @@ while (@row = FetchSQLData()) { ...@@ -605,12 +618,13 @@ while (@row = FetchSQLData()) {
} }
} }
$sth = $dbh->prepare(q{SELECT bug_id, keywordid
SendSQL("SELECT bug_id, keywordid FROM keywords ORDER BY bug_id, keywordid"); FROM keywords
ORDER BY bug_id, keywordid});
$sth->execute;
my $lastid; my $lastid;
my $lastk; my $lastk;
while (@row = FetchSQLData()) { while (my ($id, $k) = $sth->fetchrow_array) {
my ($id, $k) = (@row);
if (!$keywordids{$k}) { if (!$keywordids{$k}) {
Alert("Bogus keywordids $k found in keywords table"); Alert("Bogus keywordids $k found in keywords table");
} }
...@@ -630,18 +644,21 @@ if (defined $cgi->param('rebuildkeywordcache')) { ...@@ -630,18 +644,21 @@ if (defined $cgi->param('rebuildkeywordcache')) {
'keyworddefs read'); 'keyworddefs read');
} }
SendSQL("SELECT keywords.bug_id, keyworddefs.name " . my $query = q{SELECT keywords.bug_id, keyworddefs.name
"FROM keywords " . FROM keywords
"INNER JOIN keyworddefs " . INNER JOIN keyworddefs
" ON keyworddefs.id = keywords.keywordid " . ON keyworddefs.id = keywords.keywordid
"INNER JOIN bugs " . INNER JOIN bugs
" ON keywords.bug_id = bugs.bug_id " . ON keywords.bug_id = bugs.bug_id
"ORDER BY keywords.bug_id, keyworddefs.name"); ORDER BY keywords.bug_id, keyworddefs.name};
$sth = $dbh->prepare($query);
$sth->execute;
my $lastb = 0; my $lastb = 0;
my @list; my @list;
while (1) { while (1) {
my ($b, $k) = FetchSQLData(); my ($b, $k) = $sth->fetchrow_array;
if (!defined $b || $b != $lastb) { if (!defined $b || $b != $lastb) {
if (@list) { if (@list) {
$realk{$lastb} = join(', ', @list); $realk{$lastb} = join(', ', @list);
...@@ -671,6 +688,11 @@ if (@badbugs) { ...@@ -671,6 +688,11 @@ if (@badbugs) {
@badbugs = sort {$a <=> $b} @badbugs; @badbugs = sort {$a <=> $b} @badbugs;
Alert(scalar(@badbugs) . " bug(s) found with incorrect keyword cache: " . Alert(scalar(@badbugs) . " bug(s) found with incorrect keyword cache: " .
BugListLinks(@badbugs)); BugListLinks(@badbugs));
my $sth_update = $dbh->prepare(q{UPDATE bugs
SET keywords = ?
WHERE bug_id = ?});
if (defined $cgi->param('rebuildkeywordcache')) { if (defined $cgi->param('rebuildkeywordcache')) {
Status("OK, now fixing keyword cache."); Status("OK, now fixing keyword cache.");
foreach my $b (@badbugs) { foreach my $b (@badbugs) {
...@@ -678,8 +700,7 @@ if (@badbugs) { ...@@ -678,8 +700,7 @@ if (@badbugs) {
if (exists($realk{$b})) { if (exists($realk{$b})) {
$k = $realk{$b}; $k = $realk{$b};
} }
SendSQL("UPDATE bugs SET keywords = " . SqlQuote($k) . $sth_update->execute($k, $b);
" WHERE bug_id = $b");
} }
Status("Keyword cache fixed."); Status("Keyword cache fixed.");
} else { } else {
...@@ -697,20 +718,13 @@ if (defined $cgi->param('rebuildkeywordcache')) { ...@@ -697,20 +718,13 @@ if (defined $cgi->param('rebuildkeywordcache')) {
sub BugCheck { sub BugCheck {
my ($middlesql, $errortext, $repairparam, $repairtext) = @_; my ($middlesql, $errortext, $repairparam, $repairtext) = @_;
SendSQL("SELECT DISTINCT bugs.bug_id " . my $badbugs = $dbh->selectcol_arrayref(qq{SELECT DISTINCT bugs.bug_id
"FROM $middlesql " . FROM $middlesql
"ORDER BY bugs.bug_id"); ORDER BY bugs.bug_id});
my @badbugs = ();
while (@row = FetchSQLData()) {
my ($id) = (@row);
push (@badbugs, $id);
}
if (@badbugs) { if (scalar(@$badbugs)) {
Alert("$errortext: " . BugListLinks(@badbugs)); Alert("$errortext: " . BugListLinks(@$badbugs));
if ($repairparam) { if ($repairparam) {
$repairtext ||= 'Repair these bugs'; $repairtext ||= 'Repair these bugs';
print qq{<a href="sanitycheck.cgi?$repairparam=1">$repairtext</a>.}, print qq{<a href="sanitycheck.cgi?$repairparam=1">$repairtext</a>.},
...@@ -770,8 +784,10 @@ sub DateCheck { ...@@ -770,8 +784,10 @@ sub DateCheck {
my $table = shift @_; my $table = shift @_;
my $field = shift @_; my $field = shift @_;
Status("Checking dates in $table.$field"); Status("Checking dates in $table.$field");
SendSQL("SELECT COUNT( $field ) FROM $table WHERE $field > NOW()"); my $c = $dbh->selectrow_array(qq{SELECT COUNT($field)
my $c = FetchOneColumn(); FROM $table
WHERE $field > NOW()});
if ($c) { if ($c) {
Alert("Found $c dates in future"); Alert("Found $c dates in future");
} }
...@@ -787,18 +803,19 @@ DateCheck("profiles", "refreshed_when"); ...@@ -787,18 +803,19 @@ DateCheck("profiles", "refreshed_when");
# Checks for values that are invalid OR # Checks for values that are invalid OR
# not among the 9 valid combinations # not among the 9 valid combinations
Status("Checking for bad values in group_control_map"); Status("Checking for bad values in group_control_map");
SendSQL("SELECT COUNT(product_id) FROM group_control_map WHERE " . my $groups = join(", ", (CONTROLMAPNA, CONTROLMAPSHOWN, CONTROLMAPDEFAULT,
"membercontrol NOT IN(" . CONTROLMAPNA . "," . CONTROLMAPSHOWN . CONTROLMAPMANDATORY));
"," . CONTROLMAPDEFAULT . "," . CONTROLMAPMANDATORY . ")" . $query = qq{
" OR " . SELECT COUNT(product_id)
"othercontrol NOT IN(" . CONTROLMAPNA . "," . CONTROLMAPSHOWN . FROM group_control_map
"," . CONTROLMAPDEFAULT . "," . CONTROLMAPMANDATORY . ")" . WHERE membercontrol NOT IN( $groups )
" OR " . OR othercontrol NOT IN( $groups )
"( (membercontrol != othercontrol) " . OR ((membercontrol != othercontrol)
"AND (membercontrol != " . CONTROLMAPSHOWN . ") " . AND (membercontrol != } . CONTROLMAPSHOWN . q{)
"AND ((membercontrol != " . CONTROLMAPDEFAULT . ") " . AND ((membercontrol != } . CONTROLMAPDEFAULT . q{)
"OR (othercontrol = " . CONTROLMAPSHOWN . ")))"); OR (othercontrol = } . CONTROLMAPSHOWN . q{)))};
my $c = FetchOneColumn();
my $c = $dbh->selectrow_array($query);
if ($c) { if ($c) {
Alert("Found $c bad group_control_map entries"); Alert("Found $c bad group_control_map entries");
} }
...@@ -837,21 +854,18 @@ BugCheck("bugs ...@@ -837,21 +854,18 @@ BugCheck("bugs
Status("Checking for unsent mail"); Status("Checking for unsent mail");
@badbugs = (); my $time = $dbh->sql_interval(30, 'MINUTE');
my $badbugs = $dbh->selectcol_arrayref(qq{
SELECT bug_id
FROM bugs
WHERE (lastdiffed IS NULL OR lastdiffed < delta_ts)
AND delta_ts < now() - $time
ORDER BY bug_id});
SendSQL("SELECT bug_id " .
"FROM bugs WHERE (lastdiffed IS NULL OR lastdiffed < delta_ts) AND " .
"delta_ts < now() - " . $dbh->sql_interval(30, 'MINUTE') .
" ORDER BY bug_id");
while (@row = FetchSQLData()) { if (scalar(@$badbugs > 0)) {
my ($id) = (@row);
push(@badbugs, $id);
}
if (@badbugs > 0) {
Alert("Bugs that have changes but no mail sent for at least half an hour: " . Alert("Bugs that have changes but no mail sent for at least half an hour: " .
BugListLinks(@badbugs)); BugListLinks(@$badbugs));
print qq{<a href="sanitycheck.cgi?rescanallBugMail=1">Send these mails</a>.<p>\n}; print qq{<a href="sanitycheck.cgi?rescanallBugMail=1">Send these mails</a>.<p>\n};
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment