From becad6a4a39e16103e765d5b438d63047ee41ccb Mon Sep 17 00:00:00 2001
From: Andrew Yates <ayates@ebi.ac.uk>
Date: Wed, 7 Dec 2011 16:19:39 +0000
Subject: [PATCH] Bugfixing the reactivaition of aliases and making sure we
 only do it if we need to

---
 .../import_species_names_from_cores.pl        | 28 ++++++++++++++-----
 .../import_species_names_from_taxonomy.pl     | 19 +++++++++++--
 2 files changed, 37 insertions(+), 10 deletions(-)

diff --git a/misc-scripts/production_database/scripts/import_species_names_from_cores.pl b/misc-scripts/production_database/scripts/import_species_names_from_cores.pl
index c4f371b3fe..c8d401f87b 100644
--- a/misc-scripts/production_database/scripts/import_species_names_from_cores.pl
+++ b/misc-scripts/production_database/scripts/import_species_names_from_cores.pl
@@ -35,6 +35,7 @@ sub args {
     pass|p=s
     database|d=s
     pattern=s
+    retirealiases!
     verbose|v!
     help
     man
@@ -177,7 +178,7 @@ sub _get_production {
   );
   my $sql = 'select alias from species_alias where species_id =? and is_current = ?';
   $hash->{active_aliases} = $target_dbc->sql_helper()->execute_simple(-SQL => $sql, -PARAMS => [$hash->{species_id}, 1]);
-  $hash->{inactive_aliases} = $target_dbc->sql_helper()->execute_simple(-SQL => $sql, -PARAMS => [$hash->{species_id}, 1]);
+  $hash->{inactive_aliases} = $target_dbc->sql_helper()->execute_simple(-SQL => $sql, -PARAMS => [$hash->{species_id}, 0]);
   return $hash;
 }
 
@@ -244,21 +245,29 @@ sub _update_attributes {
 sub _update_species_aliases {
   my ($self, $source_meta, $production) = @_;
   my $target_dbc = $self->_target_dbc();
+  my $h = $target_dbc->sql_helper();
 
   my $aliases = $self->_decipher_aliases($source_meta, $production);
   my $species_id = $production->{species_id};
   
   foreach my $alias (@{$aliases->{new}}) {
     my $sql = 'insert into species_alias (species_id, alias, is_current, created_by, created_at) values (?,?,?,?,now())';
-    $target_dbc->sql_helper()->execute_update(-SQL => $sql, -PARAMS => [$species_id, $alias, 1, undef]);
+    $h->execute_update(-SQL => $sql, -PARAMS => [$species_id, $alias, 1, undef]);
   }
+  $self->v('Added %d aliases', scalar(@{$aliases->{new}}));
+  
   foreach my $alias (@{$aliases->{reactivated}}) {
-    my $sql = 'update species_alias set is_current =?, updated_by =?, updated_at =now() where species_id =? and alias =?';
-    $target_dbc->sql_helper()->execute_update(-SQL => $sql, -PARAMS => [1, undef, $species_id, $alias]);
+    my $sql = 'update species_alias set is_current =?, modified_by =?, modified_at =now() where species_id =? and alias =?';
+    $h->execute_update(-SQL => $sql, -PARAMS => [1, undef, $species_id, $alias]);
   }
-  foreach my $alias (@{$aliases->{retired}}) {
-    my $sql = 'update species_alias set is_current =?, updated_by =?, updated_at =now() where species_id =? and alias =?';
-    $target_dbc->sql_helper()->execute_update(-SQL => $sql, -PARAMS => [0, undef, $species_id, $alias]);
+  $self->v('Reactivated %d aliases', scalar(@{$aliases->{reactivated}}));
+  
+  if($self->{opts}->{retirealiases}){
+    foreach my $alias (@{$aliases->{retired}}) {
+      my $sql = 'update species_alias set is_current =?, modified_by =?, modified_at =now() where species_id =? and alias =?';
+      $h->execute_update(-SQL => $sql, -PARAMS => [0, undef, $species_id, $alias]);
+    }
+    $self->v('Retired %d aliases', scalar(@{$aliases->{retired}}));
   }
   
   return;  
@@ -375,6 +384,11 @@ Database name to search for. Can be a SQL like statement
   --database="homo_sapiens_core_65_37"
   --database="%core_65%"
 
+=item B<--retirealiases>
+
+If specified this will retire aliases if they were not an active name in the
+core database meta aliases.
+
 =item B<--verbose>
 
 Make the script chatty
diff --git a/misc-scripts/production_database/scripts/import_species_names_from_taxonomy.pl b/misc-scripts/production_database/scripts/import_species_names_from_taxonomy.pl
index 6d580c84e9..adbb57c93c 100644
--- a/misc-scripts/production_database/scripts/import_species_names_from_taxonomy.pl
+++ b/misc-scripts/production_database/scripts/import_species_names_from_taxonomy.pl
@@ -35,6 +35,7 @@ sub args {
     pass|p=s
     database|d=s
     taxon=i@
+    retirealiases!
     verbose|v!
     help
     man
@@ -228,13 +229,20 @@ sub _update_species_aliases {
     my $sql = 'insert into species_alias (species_id, alias, is_current, created_by, created_at) values (?,?,?,?,now())';
     $h->execute_update(-SQL => $sql, -PARAMS => [$species_id, $alias, 1, undef]);
   }
+  $self->v('Added %d aliases', scalar(@{$aliases->{new}}));
+  
   foreach my $alias (@{$aliases->{reactivated}}) {
     my $sql = 'update species_alias set is_current =?, modified_by =?, modified_at =now() where species_id =? and alias =?';
     $h->execute_update(-SQL => $sql, -PARAMS => [1, undef, $species_id, $alias]);
   }
-  foreach my $alias (@{$aliases->{retired}}) {
-    my $sql = 'update species_alias set is_current =?, modified_by =?, modified_at =now() where species_id =? and alias =?';
-    $h->execute_update(-SQL => $sql, -PARAMS => [0, undef, $species_id, $alias]);
+  $self->v('Reactivated %d aliases', scalar(@{$aliases->{reactivated}}));
+  
+  if($self->{opts}->{retirealiases}){
+    foreach my $alias (@{$aliases->{retired}}) {
+      my $sql = 'update species_alias set is_current =?, modified_by =?, modified_at =now() where species_id =? and alias =?';
+      $h->execute_update(-SQL => $sql, -PARAMS => [0, undef, $species_id, $alias]);
+    }
+    $self->v('Retired %d aliases', scalar(@{$aliases->{retired}}));
   }
   
   return;  
@@ -333,6 +341,11 @@ Pass for the production database
 Allows for the update of a single species by specifying its taxonomy id. This
 is useful for new species updates.
 
+=item B<--retirealiases>
+
+If specified this will retire aliases if they were not an active name in the
+taxonomy database. 
+
 =item B<--verbose>
 
 Make the script chatty
-- 
GitLab