remove variable attribute section for traits.
[sgn.git] / db / 00155 / FixOutOfSyncNdGeolocations.pm
blob29c044c0da512f1c6732628478d4792bc93c2f6a
1 #!/usr/bin/env perl
4 =head1 NAME
6 FixOutOfSyncNdGeolocations
8 =head1 SYNOPSIS
10 mx-run FixOutOfSyncNdGeolocations [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 updates nd_experiment.nd_geolocation_ids that are out of sync with the location specified in linked projectprop.values
17 This subclass uses L<Moose>. The parent class uses L<MooseX::Runnable>
19 =head1 AUTHOR
21 Bryan Ellerbrock<bje24@cornell.edu>
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 FixOutOfSyncNdGeolocations;
35 use Moose;
36 use Bio::Chado::Schema;
37 use Try::Tiny;
38 use SGN::Model::Cvterm;
39 use JSON;
40 use Data::Dumper;
42 extends 'CXGN::Metadata::Dbpatch';
45 has '+description' => ( default => <<'' );
46 This patch updates nd_experiment.nd_geolocation_ids that are out of sync with the location specified in linked projectprop.values
48 has '+prereq' => (
49 default => sub {
50 [],
55 sub patch {
56 my $self=shift;
58 print STDOUT "Executing the patch:\n " . $self->name . ".\n\nDescription:\n ". $self->description . ".\n\nExecuted by:\n " . $self->username . " .";
60 print STDOUT "\nChecking if this db_patch was executed before or if previous db_patches have been executed.\n";
62 print STDOUT "\nExecuting the SQL commands.\n";
64 my $schema = Bio::Chado::Schema->connect( sub { $self->dbh->clone } );
65 my $project_location_id = SGN::Model::Cvterm->get_cvterm_row($schema, 'project location', 'project_property')->cvterm_id();
67 my $query = "UPDATE nd_experiment
68 SET nd_geolocation_id = projectprop.value::int
69 FROM nd_experiment_project
70 JOIN projectprop ON(
71 nd_experiment_project.project_id = projectprop.project_id AND
72 projectprop.type_id = ?
74 WHERE nd_experiment_project.nd_experiment_id = nd_experiment.nd_experiment_id
75 AND nd_experiment_project.nd_experiment_id IN (
76 SELECT ne.nd_experiment_id
77 FROM nd_experiment_project nep
78 JOIN nd_experiment ne USING(nd_experiment_id)
79 EXCEPT
80 SELECT ne.nd_experiment_id
81 FROM project p
82 JOIN projectprop pp ON(
83 p.project_id=pp.project_id AND
84 pp.type_id = (
85 SELECT cvterm_id FROM cvterm WHERE name = 'project location'
88 JOIN nd_experiment_project nep ON(p.project_id = nep.project_id)
89 JOIN nd_experiment ne ON(
90 nep.nd_experiment_id = ne.nd_experiment_id AND
91 ne.nd_geolocation_id = pp.value::int
93 JOIN cvterm c ON(ne.type_id=c.cvterm_id)
94 );";
96 # print STDERR Dumper $q;
97 my $h = $schema->storage->dbh()->prepare($query);
98 $h->execute($project_location_id);
100 print "You're done!\n";
104 ####
105 1; #
106 ####