Skip to content
Snippets Groups Projects
Commit 0132988d authored by Leo Gordon's avatar Leo Gordon
Browse files

Allow multiple driver-dependent versions of the same patch; suggest schema...

Allow multiple driver-dependent versions of the same patch; suggest schema patching with db_conn.pl commands.
parent f93593be
No related branches found
No related tags found
No related merge requests found
......@@ -58,33 +58,43 @@ sub new {
: ($class->SUPER::new(@args) || die "Unable to connect to DBA using parameters (".join(', ', @args).")\n");
unless($no_sql_schema_version_check) {
my $code_sql_schema_version = Bio::EnsEMBL::Hive::DBSQL::SqlSchemaAdaptor->get_code_sql_schema_version();
my $dbc = $self->dbc();
$url ||= $dbc->url();
my $code_sql_schema_version = Bio::EnsEMBL::Hive::DBSQL::SqlSchemaAdaptor->get_code_sql_schema_version()
|| 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->fetch_value_by_key( 'hive_sql_schema_version' ); };
if($@) {
if($@ =~ /hive_meta.*doesn't exist/) {
die "\nThe '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";
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 "$@";
die "DB($url) $@";
}
} elsif(!$db_sql_schema_version) {
die "\nThe 'hive_meta' table does not contain 'hive_sql_schema_version' entry.\nPlease investigate.\n";
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 ) };
die "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$_" } @new_patches)."\n\nand try again.\n";
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_conn.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 "sql_schema_version mismatch: the database's version is '$db_sql_schema_version', but your code is still '$code_sql_schema_version'.\n"
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";
}
}
......
......@@ -23,25 +23,47 @@ package Bio::EnsEMBL::Hive::DBSQL::SqlSchemaAdaptor;
use strict;
sub find_all_sql_schema_patches {
sub get_sql_schema_patches {
my $after_version = pop @_ || 0;
my %all_patches = ();
if(my $hive_root_dir = $ENV{'EHIVE_ROOT_DIR'} ) {
foreach my $patch_path ( split(/\n/, `ls -1 $hive_root_dir/sql/patch_20*.*sql*`) ) {
my ($patch_name, $driver) = split(/\./, $patch_path);
$driver = 'mysql' if ($driver eq 'sql'); # for backwards compatibility
my @patches = split(/\n/, `ls -1 $hive_root_dir/sql/patch_20*.sql`);
$all_patches{$patch_name}{$driver} = $patch_path;
}
} # otherwise will sliently return an empty hash
return \%all_patches;
}
return [ @patches[$after_version..scalar(@patches)-1] ];
} else {
warn "WARNING: 'EHIVE_ROOT_DIR' environment variable has not been set, things may start crashing soon\n";
return [];
sub get_sql_schema_patches {
my ($self, $after_version, $driver) = @_;
my $all_patches = $self->find_all_sql_schema_patches();
my $code_schema_version = $self->get_code_sql_schema_version();
my @ordered_patches = ();
foreach my $patch_key ( (sort keys %$all_patches)[$after_version..$code_schema_version-1] ) {
if(my $patch_path = $all_patches->{$patch_key}{$driver}) {
push @ordered_patches, $patch_path;
} else {
return;
}
}
return \@ordered_patches;
}
sub get_code_sql_schema_version {
my ($self) = @_;
return scalar( @{ get_sql_schema_patches() } );
return scalar( keys %{ $self->find_all_sql_schema_patches() } ); # 0 probably means $ENV{'EHIVE_ROOT_DIR'} not set correctly
}
1;
......
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