Merge pull request #5163 from solgenomics/audit-error-checking
[sgn.git] / db / 00080 / UpdateProjectDateProps.pm
blob10e494aa2d5a7789d81c412c36ee0a907c8af5d0
1 #!/usr/bin/env perl
4 =head1 NAME
6 UpdateProjectDateProps
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 deletes resundant project_property 'project harvest date' and 'project planting date' , adds 'project_' prefix to the existing cvterms, and updates the rows in teh projectprop table to point to the correct cvterm
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 UpdateProjectDateProps;
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 cvterm name of
46 harvest_date -> project_harvest_date
47 planting_date -> project_planting_date
48 and remove the redundant terms
49 project harvest date
50 project planting date
51 Rows in projectprop that point to the redundant cvterms will be updated to point to the correct ones
53 has '+prereq' => (
54 default => sub {
55 [],
59 sub patch {
60 my $self=shift;
62 print STDOUT "Executing the patch:\n " . $self->name . ".\n\nDescription:\n ". $self->description . ".\n\nExecuted by:\n " . $self->username . " .";
64 print STDOUT "\nChecking if this db_patch was executed before or if previous db_patches have been executed.\n";
66 print STDOUT "\nExecuting the SQL commands.\n";
67 my $schema = Bio::Chado::Schema->connect( sub { $self->dbh->clone } );
69 my $coderef = sub {
70 my $cvterm_rs = $schema->resultset("Cv::Cvterm");
71 my $cv_rs = $schema->resultset("Cv::Cv");
73 my $project_property_cv = $cv_rs->find( { name => 'project_property' });
75 my $harvest_date_cvterm = $cvterm_rs->search(
76 { name => 'harvest_date', } )->single;
78 print "Updating existing cvterm 'harvest_date' \n";
79 if ($harvest_date_cvterm) {
80 $harvest_date_cvterm->update( { name => 'project_harvest_date' }, );
81 } else {
82 $harvest_date_cvterm = $cvterm_rs->search( { name => 'project_harvest_date' } )->single;
84 my $harvest_date_cvterm_id = $harvest_date_cvterm->cvterm_id;
87 my $planting_date_cvterm = $cvterm_rs->search(
88 { name => 'planting_date', } )->single;
90 print "Updating existing cvterm 'planting_date' \n";
91 if ($planting_date_cvterm) {
92 $planting_date_cvterm->update( { name => 'project_planting_date'},);
93 } else {
94 $planting_date_cvterm = $cvterm_rs->search( { name => 'project_planting_date' } )->single;
96 my $planting_date_cvterm_id = $planting_date_cvterm->cvterm_id;
97 ####
99 #redundant cvterms that should nopt be used
100 my $old_harvest_cvterm = $cvterm_rs->search(
101 { name => 'project harvest date', } )->single;
102 my $old_planting_cvterm = $cvterm_rs->search(
103 { name => 'project planting date' , })->single;
105 #update type_ids in projecprop table
106 if ($old_harvest_cvterm ) {
107 my $harvest_projectprops = $schema->resultset("Project::Projectprop")->search(
108 { type_id => $old_harvest_cvterm->cvterm_id , } );
109 $harvest_projectprops->update( { type_id => $harvest_date_cvterm_id , });
110 $old_harvest_cvterm->delete;
112 if ($old_planting_cvterm) {
113 my $planting_projectprops = $schema->resultset("Project::Projectprop")->search(
114 { type_id => $old_planting_cvterm->cvterm_id , } );
115 $planting_projectprops->update( { type_id => $planting_date_cvterm_id , });
117 #delete redundant cvterms
118 $old_planting_cvterm->delete;
120 ##############
122 if ($self->trial) {
123 print "Trial mode! Rolling back transaction\n\n";
124 $schema->txn_rollback;
125 return 0;
127 return 1;
130 try {
131 $schema->txn_do($coderef);
133 } catch {
134 die "Load failed! " . $_ . "\n" ;
138 print "You're done!\n";
142 ####
143 1; #
144 ####