Merge pull request #5163 from solgenomics/audit-error-checking
[sgn.git] / db / 00115 / UpdateStoredNdProtocolpropGenotypeStorageCvterms.pm
blob8a53cc68d51d4df3306de1da0cdc9b20b921a0b3
1 #!/usr/bin/env perl
4 =head1 NAME
6 UpdateStoredNdProtocolpropGenotypeStorageCvterms
8 =head1 SYNOPSIS
10 mx-run UpdateStoredNdProtocolpropGenotypeStorageCvterms [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 converts the old single nd_protocolprop storage into the new three value nd_protocolprop genotype storage: one for each of the cvterms: vcf_map_details, vcf_map_details_markers, vcf_map_details_markers_array
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 UpdateStoredNdProtocolpropGenotypeStorageCvterms;
34 use Moose;
35 use Bio::Chado::Schema;
36 use Try::Tiny;
37 use SGN::Model::Cvterm;
38 use JSON;
39 use Data::Dumper;
40 extends 'CXGN::Metadata::Dbpatch';
43 has '+description' => ( default => <<'' );
44 This patch converts the old single nd_protocolprop storage into the new three value nd_protocolprop genotype storage: one for each of the cvterms: vcf_map_details, vcf_map_details_markers, vcf_map_details_markers_array
46 has '+prereq' => (
47 default => sub {
48 ['AddNdProtocolpropGenotypeStorageCvterms'],
53 sub patch {
54 my $self=shift;
56 print STDOUT "Executing the patch:\n " . $self->name . ".\n\nDescription:\n ". $self->description . ".\n\nExecuted by:\n " . $self->username . " .";
58 print STDOUT "\nChecking if this db_patch was executed before or if previous db_patches have been executed.\n";
60 print STDOUT "\nExecuting the SQL commands.\n";
61 my $schema = Bio::Chado::Schema->connect( sub { $self->dbh->clone } );
63 my $vcf_map_details_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($schema, 'vcf_map_details', 'protocol_property')->cvterm_id();
64 my $vcf_map_details_markers_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($schema, 'vcf_map_details_markers', 'protocol_property')->cvterm_id();
65 my $vcf_map_details_markers_array_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($schema, 'vcf_map_details_markers_array', 'protocol_property')->cvterm_id();
67 my $q = "SELECT nd_protocolprop_id, nd_protocol_id, value FROM nd_protocolprop WHERE type_id = $vcf_map_details_cvterm_id;";
68 my $h = $schema->storage->dbh()->prepare($q);
70 my $update_protocolprop_sql = "UPDATE nd_protocolprop SET value = ? WHERE nd_protocolprop_id = ?;";
71 my $new_protocolprop_sql = "INSERT INTO nd_protocolprop (nd_protocol_id, type_id, value) VALUES (?, ?, ?);";
72 my $h_protocolprop_update = $schema->storage->dbh()->prepare($update_protocolprop_sql);
73 my $h_protocolprop = $schema->storage->dbh()->prepare($new_protocolprop_sql);
75 $h->execute();
76 while (my ($nd_protocolprop_id, $nd_protocol_id, $old_value) = $h->fetchrow_array()) {
77 my $old_value = decode_json $old_value;
78 print STDERR Dumper $old_value;
79 my $new_vcf_map_details_markers = $old_value->{markers};
80 my $new_vcf_map_details_markers_array = $old_value->{markers_array};
82 if ($new_vcf_map_details_markers) {
83 my $nd_protocolprop_markers_json_string = encode_json $new_vcf_map_details_markers;
84 $h_protocolprop->execute($nd_protocol_id, $vcf_map_details_markers_cvterm_id, $nd_protocolprop_markers_json_string);
86 if ($new_vcf_map_details_markers_array) {
87 my $nd_protocolprop_markers_array_json_string = encode_json $new_vcf_map_details_markers_array;
88 $h_protocolprop->execute($nd_protocol_id, $vcf_map_details_markers_array_cvterm_id, $nd_protocolprop_markers_array_json_string);
91 delete($old_value->{markers});
92 delete($old_value->{markers_array});
94 my $nd_protocol_json_string = encode_json $old_value;
95 $h_protocolprop_update->execute($nd_protocol_json_string, $nd_protocolprop_id);
98 print "You're done!\n";
102 ####
103 1; #
104 ####