17 This is a
generic RunnableDB module
for testing the size of the resultset of any SQL query.
19 The query is passed by the parameter
'inputquery' (param substituted)
20 The expected size is passed by the parameter
'expected_size' as a
string "CONDITION VALUE" (CONDITION defaults to equality, VALUE defaults to 0).
21 Currently, CONDITION is one of: = == < <= > >= <> !=
23 TODO: implement a
"expected_value" test
27 Copyright [1999-2015] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
28 Copyright [2016-2024] EMBL-European Bioinformatics Institute
30 Licensed under the Apache License,
Version 2.0 (the
"License"); you may not use
this file except in compliance with the License.
31 You may obtain a copy of the License at
35 Unless required by applicable law or agreed to in writing, software distributed under the License
36 is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
37 See the License
for the specific language governing permissions and limitations under the License.
41 Please subscribe to the
Hive mailing list: http:
46 package Bio::EnsEMBL::Hive::RunnableDB::SqlHealthcheck;
51 use base (
'Bio::EnsEMBL::Hive::Process');
56 'description' =>
'/no description/',
57 'expected_size' => undef,
58 'inputquery' => undef,
66 Description : Implements fetch_input()
interface method of
Bio::EnsEMBL::Hive::Process that is used to read in parameters and load data.
73 $self->param(
'inputquery') && warn
"'inputquery' is deprecated in SqlHealthcheck. Use 'query' instead\n";
76 description => $self->param(
'description'),
77 query => $self->param(
'inputquery') || $self->param_required(
'query'),
78 expected_size => $self->param(
'expected_size'),
81 $self->param(
'tests', [$test]);
82 $self->_validate_tests;
86 =head2 _validate_tests
88 Description : Checks that the tests are properly defined, and parses the
"expected_size"
95 foreach my $test (@{$self->param(
'tests')}) {
96 die
"The SQL query must be provided" unless $test->{query};
97 die
"The description must be provided" unless $test->{description};
98 $test->{subst_query} = $self->param_substitute($test->{query});
99 my $expected_size = $self->param_substitute($test->{expected_size} ||
'');
100 unless ($expected_size =~ /^\s*(=|==|>|>=|<|<=|<>|!=|)\s*(\d*)\s*$/) {
101 die
"Cannot interpret the 'expected_size' parameter: '$expected_size'";
103 $test->{logical_test} = $1 ||
'=';
104 $test->{reference_size} = $2 ||
'0';
110 Description : Implements
run() interface method of
Bio::
EnsEMBL::Hive::Process
111 Iterate through the tests and
run them all. Report the failed tests at the end
119 foreach my $test (@{$self->param(
'tests')}) {
120 push @failures, $test unless $self->_run_test($test);
123 # Transient errors like lost access to the database, etc, tend to
124 # make the job die in _run_test(). If we've passed this point, the
125 # test geneuinely failed, and it will fail again anyway
126 $self->input_job->transient_error(0);
127 die
"The following tests have failed:\n".join(
'',
map {sprintf(
" - %s\n > %s\n", $_->{description}, $_->{subst_query})} @failures);
134 Description : Runs a single test, defined in a hash with the following keys:
135 description, query, reference_size, logical_test
143 my $description = $test->{description};
144 my $query = $test->{subst_query};
145 my $reference_size = $test->{reference_size};
146 my $logical_test = $test->{logical_test};
148 # Final semicolons are removed if present
149 if ($query =~ /(;\s*$)/) {
153 $self->say_with_header(
"Test description: $description" );
154 $self->say_with_header(
"Checking whether the number of rows $logical_test $reference_size" );
156 # This could benefit from 'switch' once we move to a more recent version of Perl
157 my $maxrow = $reference_size;
158 $maxrow++
if grep {$_ eq $logical_test} qw(= == > <= <> !=);
160 $query .=
" LIMIT $maxrow" unless $query =~ /LIMIT/i;
161 $self->say_with_header(
"Query: $query" );
163 my $sth = $self->data_dbc()->prepare( $query,
164 ($self->data_dbc->driver eq
'mysql') ? {
'mysql_use_result' => 1 } : undef );
168 while (defined $sth->fetchrow_arrayref()) {
173 $self->say_with_header(
"$nrow rows returned".($nrow == $maxrow ?
" (test aborted, there could be more rows)" :
"") );
175 # This could benefit from 'switch' once we move to a more recent version of Perl
177 if ($logical_test eq
'=' or $logical_test eq
'==') {
178 $success = 1
if $nrow == $reference_size;
180 } elsif ($logical_test eq
'<' or $logical_test eq
'<=') {
181 $success = 1
if $nrow < $maxrow;
183 } elsif ($logical_test eq
'>' or $logical_test eq
'>=') {
184 $success = 1
if $nrow >= $maxrow;
186 } elsif ($logical_test eq
'<>' or $logical_test eq
'!=') {
187 $success = 1
if $nrow != $reference_size;
190 die
"This should not happen. A logical test is not checked";
192 $self->say_with_header( $success ?
"Success\n" :
"Failure\n",
'important');