Merge pull request #3326 from solgenomics/topic/remove_date_of
[sgn.git] / db / 00085 / AddSeedlotCurrentCountCvterm.pm
blobdc71ddf09f56fb13158a55bd5806c28959c8036d
1 #!/usr/bin/env perl
4 =head1 NAME
6 AddSeedlotCurrentCountCvterm.pm
8 =head1 SYNOPSIS
10 mx-run AddSeedlotCurrentCountCvterm [options] -H hostname -D dbname -u username [-F]
12 this is a subclass of L<CXGN::Metadata::Dbpatch>
13 see the perldoc of parent class for more details.
15 =head1 DESCRIPTION
16 This patch adds the necessary cvterms that are used for brapi germplasm, programs, and studies.
17 This subclass uses L<Moose>. The parent class uses L<MooseX::Runnable>
19 =head1 AUTHOR
21 Nick Morales
23 =head1 COPYRIGHT & LICENSE
25 Copyright 2010 Boyce Thompson Institute for Plant Research
27 This program is free software; you can redistribute it and/or modify
28 it under the same terms as Perl itself.
30 =cut
33 package AddSeedlotCurrentCountCvterm;
35 use Moose;
36 use Bio::Chado::Schema;
37 use Try::Tiny;
38 use CXGN::Stock::Seedlot;
39 use SGN::Model::Cvterm;
40 extends 'CXGN::Metadata::Dbpatch';
43 has '+description' => ( default => <<'' );
44 Adds current_count stock_property cvterm which acts as a store for the current count of a seedlot
46 has '+prereq' => (
47 default => sub {
48 [],
53 sub patch {
54 my $self=shift;
56 print STDOUT "Executing the patch:\n " . $self->name . ".\n\nDescription:\n ". $self->description . ".\n\nExecuted by:\n " . $self->username . " .";
58 print STDOUT "\nChecking if this db_patch was executed before or if previous db_patches have been executed.\n";
60 print STDOUT "\nExecuting the SQL commands.\n";
61 my $schema = Bio::Chado::Schema->connect( sub { $self->dbh->clone } );
64 print STDERR "INSERTING CV TERMS...\n";
66 my $terms = {
67 'stock_property' => [
68 'current_count',
72 foreach my $t (keys %$terms){
73 foreach (@{$terms->{$t}}){
74 $schema->resultset("Cv::Cvterm")->create_with({
75 name => $_,
76 cv => $t
77 });
81 # Applies current_count to all existing seedlots in database
82 my $seedlot_type_id = SGN::Model::Cvterm->get_cvterm_row($schema, 'seedlot', 'stock_type')->cvterm_id();
83 my $seedlot_rs = $schema->resultset("Stock::Stock")->search({
84 type_id=>$seedlot_type_id
85 });
86 while(my $r=$seedlot_rs->next){
87 my $sl = CXGN::Stock::Seedlot->new(schema => $schema, seedlot_id=>$r->stock_id);
88 $sl->set_current_count_property();
91 print "You're done!\n";
95 ####
96 1; #
97 ####