Merge pull request #2091 from solgenomics/topic/update_fixture_dump
[sgn.git] / db / 00059 / LinkPlantEntriesToProject.pm
blob6e29ae6d685cbf9e3a1445e713ccac08f1819df0
1 #!/usr/bin/env perl
3 =head1 NAME
5 LinkPlantEntriesToProject.pm
7 =head1 SYNOPSIS
9 mx-run FixTrialTypes [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 updates the way that plant entries were being created and stored. Previously, plant entries were not linked directly to the project they are in. This made uploading of phenotypes slow because of additional searches to go from plant to plot to project. Now the connnection of plant to project is available.
18 =head1 AUTHOR
20 Nicolas Morales<nm529@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 LinkPlantEntriesToProject;
34 use Moose;
35 use Bio::Chado::Schema;
36 use Try::Tiny;
37 extends 'CXGN::Metadata::Dbpatch';
38 use SGN::Model::Cvterm;
40 has '+description' => ( default => <<'' );
41 This patch updates the way that plant entries were being created and stored. Previously, plant entries were not linked directly to the project they are in. This made uploading of phenotypes slow because of additional searches to go from plant to plot to project. Now the connnection of plant to project is available.
43 sub patch {
44 my $self=shift;
46 print STDOUT "Executing the patch:\n " . $self->name . ".\n\nDescription:\n ". $self->description . ".\n\nExecuted by:\n " . $self->username . " .";
48 print STDOUT "\nChecking if this db_patch was executed before or if previous db_patches have been executed.\n";
50 print STDOUT "\nExecuting the SQL commands.\n";
52 my $schema = Bio::Chado::Schema->connect( sub { $self->dbh->clone } );
54 my $coderef = sub {
56 my $field_layout_cvterm = SGN::Model::Cvterm->get_cvterm_row($schema, 'field_layout', 'experiment_type')->cvterm_id();
57 my $plot_of_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($schema, 'plant_of', 'stock_relationship')->cvterm_id();
59 my $plots_of_plants = $schema->resultset("Stock::StockRelationship")->search({ type_id=>$plot_of_cvterm_id });
61 while (my $p = $plots_of_plants->next() ) {
62 #Get project of plant from plot
63 my $plant = $p->object();
64 my $plot_of_plant = $p->subject();
65 my $field_layout_experiment = $plot_of_plant
66 ->search_related('nd_experiment_stocks')
67 ->search_related('nd_experiment')
68 ->find({'type.cvterm_id' => $field_layout_cvterm },
69 { join => 'type' });
70 my $project = $field_layout_experiment->nd_experiment_projects->single ; #there should be one project linked with the field experiment
71 my $project_id = $project->project_id;
73 #store nd_experiment_stock entry in same way as it is done when plant entries are created now.
74 $field_layout_experiment = $schema->resultset("Project::Project")->search( { 'me.project_id' => $project_id }, {select=>['nd_experiment.nd_experiment_id']})->search_related('nd_experiment_projects')->search_related('nd_experiment', { type_id => $field_layout_cvterm })->single();
75 my $plant_nd_experiment_stock = $schema->resultset("NaturalDiversity::NdExperimentStock")->create({
76 nd_experiment_id => $field_layout_experiment->nd_experiment_id(),
77 type_id => $field_layout_cvterm,
78 stock_id => $plant->stock_id(),
79 });
83 try {
84 $schema->txn_do($coderef);
85 } catch {
86 die "Patch failed! Transaction exited." . $_ . "\n" ;
89 print "You're done!\n";
93 ####
94 1; #
95 ####