From 4e314bb780d0ebefe24b9259a28ca202b3fb944d Mon Sep 17 00:00:00 2001
From: Ian Longden <ianl@sanger.ac.uk>
Date: Tue, 20 Sep 2005 10:18:25 +0000
Subject: [PATCH] version checking routines

---
 modules/Bio/EnsEMBL/DBSQL/MetaContainer.pm  | 31 +++++++++++++++++++++
 modules/Bio/EnsEMBL/Registry.pm             |  5 +++-
 modules/Bio/EnsEMBL/Utils/ConfigRegistry.pm | 29 ++++++++++++++++++-
 3 files changed, 63 insertions(+), 2 deletions(-)

diff --git a/modules/Bio/EnsEMBL/DBSQL/MetaContainer.pm b/modules/Bio/EnsEMBL/DBSQL/MetaContainer.pm
index c281b9ba70..2a2017c5d2 100644
--- a/modules/Bio/EnsEMBL/DBSQL/MetaContainer.pm
+++ b/modules/Bio/EnsEMBL/DBSQL/MetaContainer.pm
@@ -47,6 +47,37 @@ use Bio::Species;
 
 # add well known meta info get-functions below
 
+=head2 get_schema_version
+
+  Arg [1]    : none
+  Example    : $schema_ver = $meta_container->get_schema_version();
+  Description: Retrieves the schema version from the database meta table
+  Returntype : int
+  Exceptions : none
+  Caller     : ?
+  Status     : Medium risk
+
+=cut
+
+sub get_schema_version {
+  my $self = shift;
+
+  my $arrRef = $self->list_value_by_key( 'schema_version' );
+  
+  if( @$arrRef ) {
+    my ($ver) = ($arrRef->[0] =~ /^\s*(\d+)\s*$/);
+    if(!defined($ver)){ # old style format
+      return 0;
+    }
+    return $ver;
+  } else {
+    $self->warn("Please insert meta_key 'schema_version' " .
+		"in meta table at core db.\n");
+  }
+  return 0;
+}
+
+
 =head2 get_Species
 
   Arg [1]    : none
diff --git a/modules/Bio/EnsEMBL/Registry.pm b/modules/Bio/EnsEMBL/Registry.pm
index 68a321d27f..52b3682709 100644
--- a/modules/Bio/EnsEMBL/Registry.pm
+++ b/modules/Bio/EnsEMBL/Registry.pm
@@ -146,6 +146,9 @@ sub load_all{
       # other wise it gets done again by the web initialisation stuff
       delete $INC{$conf_file}; 
     }
+    else{ #error message
+      print STDERR "File passed (".$conf_file.") does not exist therefore no configuration loaded\n";
+    }
   }
   elsif(defined($ENV{ENSEMBL_REGISTRY}) and -e $ENV{ENSEMBL_REGISTRY}){
     my $file = $ENV{ENSEMBL_REGISTRY};
@@ -847,7 +850,7 @@ sub load_registry_from_db{
   for my $db (@dbnames){
     if($db =~ /^([a-z]+_[a-z]+_[a-z]+)_(\d+)_(\d+[a-z]*)/){
       if(defined($temp{$1})){
-	my ($r1,$r2) = split($temp{$1},"_");
+	my ($r1,$r2) = split("_",$temp{$1});
 	if($r1 < $2){
 	  $temp{$1} = $2."_".$3;
 	}
diff --git a/modules/Bio/EnsEMBL/Utils/ConfigRegistry.pm b/modules/Bio/EnsEMBL/Utils/ConfigRegistry.pm
index 5a18bf82ab..f774be6322 100644
--- a/modules/Bio/EnsEMBL/Utils/ConfigRegistry.pm
+++ b/modules/Bio/EnsEMBL/Utils/ConfigRegistry.pm
@@ -45,6 +45,8 @@ use Bio::EnsEMBL::DBSQL::DBAdaptor;
 
 use Bio::EnsEMBL::Utils::Exception qw(warning throw  deprecate stack_trace_dump);
 
+my $SOFTWARE_VERSION = 34;
+
 =head2 load_core, load_estgene, load_vega, load_compara, load_pipeline, load_SNP, load_lite
   Arg [1]    : DBAdaptor with DBConnection alredy attached
   Returntype : DBAdaptor;
@@ -244,8 +246,33 @@ sub load_and_attach_dnadb_to_core{
 
 # those that do not need to attach to core:-#
 
+sub check_version{
+  my ($dba) = @_;
+
+  # Check the datbase and versions match
+  # give warning if they do not.
+  my $mca = $dba->get_MetaContainerAdaptor();
+  my $database_version = $mca->get_schema_version();
+  
+  if($database_version == 0){
+    #try to work out the version
+    if($dba->dbc->dbname() =~ /\S+_\S+_\S+_(\d+)_\S+/){
+      $database_version = $1;
+    }
+    else{
+      warn("No database version for database ".$dba->dbc->dbname().". You must be using as pre version 34 database with version 34 or later code. You need to update your database or use the appropriate ensembl software release to ensure your script does not crash\n");
+    }
+  }
+  if($database_version != $SOFTWARE_VERSION){
+    warn("For ".$dba->dbc->dbname()." there is a difference in the software release (".$SOFTWARE_VERSION.") and the database release (".$database_version."). You should change one of these to ensure your script does not crash.\n");
+  }
+}
+
 sub load_core{
-  load_adaptors(@_);
+  my ($dba) = @_;
+
+  load_adaptors($dba);
+  check_version($dba);
 }
 
 sub load_compara{
-- 
GitLab