sub new {
my $class = shift @_;
my %flags = @_;
my ($dbc, $url, $reg_conf, $reg_type, $reg_alias, $species, $no_sql_schema_version_check)
= @flags{qw(-dbconn -url -reg_conf -reg_type -reg_alias -species -no_sql_schema_version_check)};
$url .= ';nosqlvc=1' if($url && $no_sql_schema_version_check);
if($reg_conf or $reg_alias) { # need to initialize Registry even if $reg_conf is not really given
require Bio::EnsEMBL::Registry;
Bio::EnsEMBL::Registry->load_all($reg_conf); # if undefined, default reg_conf will be used
}
my $self;
if($url) {
or die "Unable to create a DBC using url='$url'";
} elsif($reg_alias) {
$reg_type ||= 'hive';
$self = Bio::EnsEMBL::Registry->get_DBAdaptor($reg_alias, $reg_type)
or die "Unable to connect to DBA using reg_conf='$reg_conf', reg_type='$reg_type', reg_alias='$reg_alias'\n";
if($reg_type ne 'hive') { # ensure we are getting a Hive adaptor even from a non-Hive Registry entry:
$dbc = $self->dbc;
$self = undef;
}
}
if($dbc && !$self) {
$self = bless {}, $class;
$self->dbc( $dbc );
}
unless($no_sql_schema_version_check) {
my $dbc = $self->dbc();
$url ||= $dbc->url();
|| die "DB($url) Could not establish code_sql_schema_version, please check that 'EHIVE_ROOT_DIR' environment variable is set correctly";
my $db_sql_schema_version = eval { $self->get_MetaAdaptor->get_value_by_key( 'hive_sql_schema_version' ); };
if($@) {
if($@ =~ /hive_meta.*doesn't exist/) {
die "\nDB($url) The 'hive_meta' table does not seem to exist in the database yet.\nPlease patch the database up to sql_schema_version '$code_sql_schema_version' and try again.\n";
} else {
die "DB($url) $@";
}
} elsif(!$db_sql_schema_version) {
die "\nDB($url) The 'hive_meta' table does not contain 'hive_sql_schema_version' entry.\nPlease investigate.\n";
} elsif($db_sql_schema_version < $code_sql_schema_version) {
my $new_patches = Bio::EnsEMBL::Hive::DBSQL::SqlSchemaAdaptor->get_sql_schema_patches( $db_sql_schema_version, $dbc->driver )
|| die "DB($url) sql_schema_version mismatch: the database's version is '$db_sql_schema_version' but the code is already '$code_sql_schema_version'.\n"
."Unfortunately we cannot patch the database; you may have to create a new database or agree to run older code\n";
my $patcher_command = "$ENV{'EHIVE_ROOT_DIR'}/scripts/db_cmd.pl -url $url";
die "DB($url) sql_schema_version mismatch: the database's version is '$db_sql_schema_version' but the code is already '$code_sql_schema_version'.\n"
."Please upgrade the database by applying the following patches:\n\n".join("\n", map { "\t$patcher_command < $_" } @$new_patches)."\n\nand try again.\n";
} elsif($code_sql_schema_version < $db_sql_schema_version) {
die "DB($url) sql_schema_version mismatch: the database's version is '$db_sql_schema_version', but your code is still '$code_sql_schema_version'.\n"
."Please update the code and try again.\n";
}
}
if($species) { # [compatibility with core code] store the DBAdaptor in Registry:
require Bio::EnsEMBL::Registry;
Bio::EnsEMBL::Registry->add_DBAdaptor( $species, 'hive', $self );
}
return $self;
}