Merge pull request #1890 from solgenomics/topic/UpdateBrapiSearchDBlist
[sgn.git] / db / 00063 / RenameObsoleteStocks.pm
blob1588d7eadad725803a9221642d83846bfab94c89
1 #!/usr/bin/env perl
3 =head1 NAME
5 RenameObsoleteStocks.pm
7 =head1 SYNOPSIS
9 mx-run RenameObsoleteStocks [options] -H hostname -D dbname -u username [-F]
11 this is a subclass of L<CXGN::Metadata::Dbpatch>
12 see the perldoc of parent class for more details.
14 =head1 DESCRIPTION
16 This patch renames obsolete stocks by adding _OBSOLETED and a timestamp to the end of the uniquename and name. This then allows new stocks to be added with the same name as the obsoleted stock.
18 =head1 AUTHOR
20 Bryan Ellerbrock<bje24@cornell.edu>
22 =head1 COPYRIGHT & LICENSE
24 Copyright 2010 Boyce Thompson Institute for Plant Research
26 This program is free software; you can redistribute it and/or modify
27 it under the same terms as Perl itself.
29 =cut
32 package RenameObsoleteStocks;
34 use Moose;
35 use Bio::Chado::Schema;
36 use Try::Tiny;
37 extends 'CXGN::Metadata::Dbpatch';
40 has '+description' => ( default => <<'' );
41 This patch renames obsolete stocks by adding _OBSOLETED and a timestamp to the end of the uniquename and name. This then allows new stocks to be added with the same name as the obsoleted stock.
44 sub patch {
45 my $self=shift;
47 print STDOUT "Executing the patch:\n " . $self->name . ".\n\nDescription:\n ". $self->description . ".\n\nExecuted by:\n " . $self->username . " .";
49 print STDOUT "\nChecking if this db_patch was executed before or if previous db_patches have been executed.\n";
51 print STDOUT "\nExecuting the SQL commands.\n";
53 my $schema = Bio::Chado::Schema->connect( sub { $self->dbh->clone } );
55 my $coderef = sub {
56 print STDERR "Updating names of obsolete stocks . . .\n";
58 my $obsolete_stocks = $schema->resultset("Stock::Stock")->search({is_obsolete => 't'});
60 while (my $stock = $obsolete_stocks->next) {
61 my $obsolete_string = '_OBSOLETED_' . localtime();
62 my $name = $stock->name();
63 my $uniquename = $stock->uniquename();
65 unless ($name =~ /OBSOLETE/ || $uniquename =~ /OBSOLETE/) { # skip if already renamed
66 $stock->update( { name => $name . $obsolete_string, uniquename => $uniquename . $obsolete_string }, );
67 print STDERR "Set stock $name name to $name$obsolete_string and uniquename to $uniquename$obsolete_string . . .\n";
73 try {
74 $schema->txn_do($coderef);
75 } catch {
76 die "RenameObsoleteStocks patch failed! " . $_ . "\n" ;
79 print "You're done!\n";
83 ####
84 1; #
85 ####