Merge pull request #5243 from solgenomics/topic/observations_upload_catch_error
[sgn.git] / db / 00051 / UpdateLocalCvterms.pm
blob7e63b20e56dac9eff5316fff58708b8ac55518d4
1 #!/usr/bin/env perl
4 =head1 NAME
6 UpdateLocalCvterms
8 =head1 SYNOPSIS
10 mx-run ThisPackageName [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
17 This patch changes the cv of local cvterms to the correct one
19 This subclass uses L<Moose>. The parent class uses L<MooseX::Runnable>
21 =head1 AUTHOR
23 Naama Menda<nm249@cornell.edu>
25 =head1 COPYRIGHT & LICENSE
27 Copyright 2010 Boyce Thompson Institute for Plant Research
29 This program is free software; you can redistribute it and/or modify
30 it under the same terms as Perl itself.
32 =cut
35 package UpdateLocalCvterms;
37 use Moose;
38 use Bio::Chado::Schema;
39 use Try::Tiny;
41 extends 'CXGN::Metadata::Dbpatch';
44 has '+description' => ( default => <<'' );
45 This patch will update the cv id of the following cvterms that currently have a local cv name
46 cross_type
47 breeding_program
48 breeding_program_trial_relationship
49 harvest_date
50 number_of_flowers
51 number_of_seeds
52 planting_date
53 this is important for making CVterms uniform and having an explicit cv name that provides the right context
55 has '+prereq' => (
56 default => sub {
57 [],
61 sub patch {
62 my $self=shift;
64 print STDOUT "Executing the patch:\n " . $self->name . ".\n\nDescription:\n ". $self->description . ".\n\nExecuted by:\n " . $self->username . " .";
66 print STDOUT "\nChecking if this db_patch was executed before or if previous db_patches have been executed.\n";
68 print STDOUT "\nExecuting the SQL commands.\n";
69 my $schema = Bio::Chado::Schema->connect( sub { $self->dbh->clone } );
71 my $coderef = sub {
72 my $cvterm_rs = $schema->resultset("Cv::Cvterm");
73 my $cv_rs = $schema->resultset("Cv::Cv");
75 my $nd_experiment_property_cv = $cv_rs->find_or_create( { name => 'nd_experiment_property' });
77 my $cross_type_cvterm = $cvterm_rs->search(
78 { name => 'cross_type', } );
80 if ( $cross_type_cvterm->count() == 0 ) {
81 print "Creating new cvterm 'cross_type' cv = 'nd_experiment_property' \n";
82 $cvterm_rs->create_with(
84 name => 'cross_type',
85 cv => 'nd_experiment_property',
86 } );
87 } else {
88 print "Updating existing cvterm 'cross_type' cv = 'nd_experiment_property' \n";
89 $cross_type_cvterm->first->update( { cv_id => $nd_experiment_property_cv->cv_id }, );
91 ####
92 my $number_of_flowers_cvterm = $cvterm_rs->search(
93 { name => 'number_of_flowers', } );
95 if ( $number_of_flowers_cvterm->count() == 0 ) {
96 print "Creating new cvterm 'number_of_flowers' cv = 'nd_experiment_property' \n";
97 $cvterm_rs->create_with(
99 name => 'number_of_flowers',
100 cv => 'nd_experiment_property',
101 } );
102 } else {
103 print "Updating existing cvterm 'number_of_flowers' cv = 'nd_experiment_property' \n";
104 $number_of_flowers_cvterm->first->update( { cv_id => $nd_experiment_property_cv->cv_id }, );
106 ##########
107 my $number_of_seeds_cvterm = $cvterm_rs->search(
108 { name => 'number_of_seeds', } );
110 if ( $number_of_seeds_cvterm->count() == 0 ) {
111 print "Creating new cvterm 'number_of_seeds' cv = 'nd_experiment_property' \n";
112 $cvterm_rs->create_with(
114 name => 'number_of_seeds',
115 cv => 'nd_experiment_property',
116 } );
117 } else {
118 print "Updating existing cvterm 'number_of_seeds' cv = 'nd_experiment_property' \n";
119 $number_of_seeds_cvterm->first->update( { cv_id => $nd_experiment_property_cv->cv_id }, );
122 #####
124 my $project_property_cv = $cv_rs->find_or_create( { name => 'project_property' });
125 my $breeding_program_cvterm = $cvterm_rs->search(
126 { name => 'breeding_program', } );
128 if ( $breeding_program_cvterm->count() == 0 ) {
129 print "Creating new cvterm 'breeding_program' cv = 'project_property' \n";
130 $cvterm_rs->create_with(
132 name => 'breeding_program',
133 cv => 'project_property',
134 } );
135 } else {
136 print "Updating existing cvterm 'breeding_program' cv = 'project_property' \n";
137 $breeding_program_cvterm->first->update( { cv_id => $project_property_cv->cv_id }, );
139 ##############
140 my $harvest_date_cvterm = $cvterm_rs->search(
141 { name => 'harvest_date', } );
143 if ( $harvest_date_cvterm->count() == 0 ) {
144 print "Creating new cvterm 'harvest_date' cv = 'project_property' \n";
145 $cvterm_rs->create_with(
147 name => 'harvest_date',
148 cv => 'project_property',
149 } );
150 } else {
151 print "Updating existing cvterm 'harvest_date' cv = 'project_property' \n";
152 $harvest_date_cvterm->first->update( { cv_id => $project_property_cv->cv_id }, );
154 ####
155 my $planting_date_cvterm = $cvterm_rs->search(
156 { name => 'planting_date', } );
158 if ( $planting_date_cvterm->count() == 0 ) {
159 print "Creating new cvterm 'planting_date' cv = 'project_property' \n";
160 $cvterm_rs->create_with(
162 name => 'planting_date',
163 cv => 'project_property',
164 } );
165 } else {
166 print "Updating existing cvterm 'planting_date' cv = 'project_property' \n";
167 $planting_date_cvterm->first->update( { cv_id => $project_property_cv->cv_id }, );
169 ###########
171 my $project_relationship_cv = $cv_rs->find_or_create( { name => 'project_relationship' });
172 my $breeding_program_trial_cvterm = $cvterm_rs->search(
173 { name => 'breeding_program_trial_relationship', } );
175 if ( $breeding_program_trial_cvterm->count() == 0 ) {
176 print "Creating new cvterm 'breeding_program_trial_relationship' cv = 'project_relationship' \n";
177 $cvterm_rs->create_with(
179 name => 'breeding_program_trial_relationship',
180 cv => 'project_relationship',
181 } );
182 } else {
183 print "Updating existing cvterm 'breeding_program_trial_relationship' cv = 'project_relationship' \n";
184 $breeding_program_trial_cvterm->first->update( { cv_id => $project_relationship_cv->cv_id }, );
187 ##############
189 if ($self->trial) {
190 print "Trial mode! Rolling back transaction\n\n";
191 $schema->txn_rollback;
192 return 0;
194 return 1;
197 try {
198 $schema->txn_do($coderef);
200 } catch {
201 die "Load failed! " . $_ . "\n" ;
205 print "You're done!\n";
209 ####
210 1; #
211 ####