Commit a2458fa0 authored by Leo Gordon's avatar Leo Gordon
Browse files

a cleaner re-write of db_version.pl that differentiates between different types of errors

parent e8782c7e
......@@ -16,7 +16,6 @@
See the License for the specific language governing permissions and
limitations under the License.
=cut
......@@ -25,81 +24,79 @@ use warnings;
## The servers should have already set the PERL5LIB to point to the latest hive API in versions
use Bio::EnsEMBL::Hive::DBSQL::DBAdaptor;
use Bio::EnsEMBL::Hive::DBSQL::SqlSchemaAdaptor;
use Bio::EnsEMBL::Hive::Utils::URL;
use JSON;
use lib ("../lib");
use msg;
my $json_url = shift @ARGV || '{"url":["mysql://ensro@127.0.0.1:2914/mp12_compara_nctrees_74sheep"]}';
my $response = msg->new();
# Input data
my $url = decode_json($json_url)->{url}->[0];
main();
my $response = msg->new();
sub main {
my $json_input_with_url = shift @ARGV
or report_failure_and_exit( 'This script needs one argument formatted in JSON like so: {"url":["mysql://username:password@hostname:portnumber/database_name"]}');
# Initialization
my $dbConn;
eval {
$dbConn = Bio::EnsEMBL::Hive::DBSQL::DBAdaptor->new( -no_sql_schema_version_check => 1, -url => $url );
};
my $url = decode_json($json_input_with_url)->{url}->[0];
if ($@) {
$response->err_msg($@);
$response->status("FAILED");
}
if (defined $dbConn) {
## Check db_connection
my $hive_db_version;
eval {
$hive_db_version = get_hive_db_version($dbConn);
};
if ($@) {
$response->err_msg($@);
$response->status("FAILED");
print $response->toJSON;
exit(0);
}
my $hive_dba;
eval { # ------------------------- can we create the DBAdaptor?
$hive_dba = Bio::EnsEMBL::Hive::DBSQL::DBAdaptor->new( -no_sql_schema_version_check => 1, -url => $url );
} or do {
report_failure_and_exit( $@ || "Could not create a valid Hive DBAdaptor from URL: $url" );
};
eval { # ------------------------- can we connect this DBAdaptor?
$hive_dba->dbc->connect;
1;
} or do {
report_failure_and_exit( "Could not connect using URL: $url" );
};
my $hive_db_version;
eval { # ------------------------- can we come up with a valid version?
$hive_db_version = get_hive_db_version($hive_dba);
} or do {
report_failure_and_exit( $@ || "No version found in the database accessible via URL: $url" );
};
if (defined $hive_db_version) {
my $parsed_url = Bio::EnsEMBL::Hive::Utils::URL::parse($url);
my $json_obj = { 'user' => $parsed_url->{user},
'dbname' => $parsed_url->{dbname},
'host' => $parsed_url->{host},
'port' => $parsed_url->{port},
'passwd' => $parsed_url->{pass},
'db_version' => $hive_db_version,
};
my $json_obj = {
'user' => $parsed_url->{user},
'passwd' => $parsed_url->{pass},
'host' => $parsed_url->{host},
'port' => $parsed_url->{port},
'dbname' => $parsed_url->{dbname},
'db_version' => $hive_db_version,
};
$response->out_msg($json_obj);
} else {
$response->err_msg("No version found for db");
print $response->toJSON();
}
sub report_failure_and_exit {
my ($error_message) = @_;
$response->err_msg( $error_message );
$response->status("FAILED");
}
} else {
$response->err_msg("The provided URL seems to be invalid. Please check the URL and try again\n") unless($response->err_msg);
$response->status("FAILED");
print $response->toJSON();
exit 0;
}
print $response->toJSON();
#######################
# This method is also defined in scripts/db_connect.pl
sub get_hive_db_version {
my ($dbConn) = @_;
my $metaAdaptor = $dbConn->get_MetaAdaptor;
my $db_sql_schema_version;
eval {
if ($metaAdaptor->can('fetch_value_by_key')) {
$db_sql_schema_version = $metaAdaptor->fetch_value_by_key( 'hive_sql_schema_version' );
} elsif ($metaAdaptor->can('get_value_by_key')) {
$db_sql_schema_version = $metaAdaptor->get_value_by_key( 'hive_sql_schema_version' );
} else {
$db_sql_schema_version = eval { $metaAdaptor->fetch_by_meta_key( 'hive_sql_schema_version' )->{'meta_value'}; };
}
};
return $db_sql_schema_version;
my ($hive_dba) = @_;
my $metaAdaptor = $hive_dba->get_MetaAdaptor;
my $db_sql_schema_version;
eval {
$db_sql_schema_version =
$metaAdaptor->can('fetch_value_by_key') ? $metaAdaptor->fetch_value_by_key( 'hive_sql_schema_version' )
: $metaAdaptor->can('get_value_by_key') ? $metaAdaptor->get_value_by_key( 'hive_sql_schema_version' )
: $metaAdaptor->fetch_by_meta_key( 'hive_sql_schema_version' )->{'meta_value'};
};
return $db_sql_schema_version;
}
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