Merge pull request #2678 from solgenomics/BBase-15
[sgn.git] / db / 00103 / FixTrialPluralTypes.pm
blob6193850e2819e0277523c1fed68b6c03e553413d
1 #!/usr/bin/env perl
4 =head1 NAME
6 FixTrialPluralTypes.pm
8 =head1 SYNOPSIS
10 mx-run FixTrialPluralTypes [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 fixes the odd "Preliminary Yield Trials" and "Advanced Yeld Trials" and "Advanced Yield Trials" terms that should be singular terms
17 This subclass uses L<Moose>. The parent class uses L<MooseX::Runnable>
19 =head1 AUTHOR
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 FixTrialPluralTypes;
34 use Moose;
35 use Bio::Chado::Schema;
36 use Try::Tiny;
37 use SGN::Model::Cvterm;
38 extends 'CXGN::Metadata::Dbpatch';
41 has '+description' => ( default => <<'' );
42 This patch fixes the odd "Preliminary Yield Trials" and "Advanced Yeld Trials" and "Advanced Yield Trials" terms that should be singular terms
44 has '+prereq' => (
45 default => sub {
46 [],
51 sub patch {
52 my $self=shift;
54 print STDOUT "Executing the patch:\n " . $self->name . ".\n\nDescription:\n ". $self->description . ".\n\nExecuted by:\n " . $self->username . " .";
56 print STDOUT "\nChecking if this db_patch was executed before or if previous db_patches have been executed.\n";
58 print STDOUT "\nExecuting the SQL commands.\n";
59 my $schema = Bio::Chado::Schema->connect( sub { $self->dbh->clone } );
62 print STDERR "INSERTING CV TERMS...\n";
64 my $correct_ayt_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($schema, 'Advanced Yield Trial', 'project_type')->cvterm_id();
65 my $correct_pyt_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($schema, 'Preliminary Yield Trial', 'project_type')->cvterm_id();
67 my $previously_saved_wrong_ayt_projectprops_sql = "SELECT projectprop_id FROM projectprop join cvterm on(type_id=cvterm_id) where name='Advanced Yeld Trials' OR name='Advanced Yield Trials';";
68 my $update_previously_saved_wrong_ayt_projectprops_sql = "UPDATE projectprop SET type_id = $correct_ayt_cvterm_id WHERE projectprop_id = ?;";
70 my $h1 = $schema->storage->dbh()->prepare($previously_saved_wrong_ayt_projectprops_sql);
71 my $h_update = $schema->storage->dbh()->prepare($update_previously_saved_wrong_ayt_projectprops_sql);
73 $h1->execute();
74 while (my ($projectprop_id) = $h1->fetchrow_array()) {
75 $h_update->execute($projectprop_id);
78 my $previously_saved_wrong_pyt_projectprops_sql = "SELECT projectprop_id FROM projectprop join cvterm on(type_id=cvterm_id) where name='Preliminary Yield Trials';";
79 my $update_previously_saved_wrong_pyt_projectprops_sql = "UPDATE projectprop SET type_id = $correct_pyt_cvterm_id WHERE projectprop_id = ?;";
81 my $h2 = $schema->storage->dbh()->prepare($previously_saved_wrong_pyt_projectprops_sql);
82 my $h2_update = $schema->storage->dbh()->prepare($update_previously_saved_wrong_pyt_projectprops_sql);
84 $h2->execute();
85 while (my ($projectprop_id) = $h2->fetchrow_array()) {
86 $h2_update->execute($projectprop_id);
89 my $wrong_trial_types_rs = $schema->resultset("Cv::Cvterm")->search({
90 name => {-in => ["Advanced Yeld Trials", "Advanced Yield Trials", "Preliminary Yield Trials"]}
91 });
92 while (my $r = $wrong_trial_types_rs->next){
93 $r->delete;
96 print "You're done!\n";
100 ####
101 1; #
102 ####