Merge pull request #2986 from solgenomics/topic/file_upload
[sgn.git] / db / 00006 / PopulateStockPub.pm
blobf43612678ed4ebe4691c001ba7086ef339ec4ce8
1 #!/usr/bin/env perl
4 =head1 NAME
6 PopulateStockPub.pm
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 is a patch for populating the stockpub table , and granting web_usr permissions
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 PopulateStockPub;
37 use Moose;
38 extends 'CXGN::Metadata::Dbpatch';
40 use Bio::Chado::Schema;
42 sub init_patch {
43 my $self=shift;
44 my $name = __PACKAGE__;
45 print "dbpatch name is ':" . $name . "\n\n";
46 my $description = 'Populating stock_pub table';
47 my @previous_requested_patches = (); #ADD HERE
48 $self->name($name);
49 $self->description($description);
50 $self->prereq(\@previous_requested_patches);
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 $stockdbxrefs = $schema->resultset("General::Db")->search( { name => 'PMID' } )
64 ->search_related('dbxrefs')->search_related('stock_dbxrefs');
65 my $result = $schema->txn_do( sub {
66 while ( my $sd = $stockdbxrefs->next ) {
67 $sd->stock->find_or_create_related('stock_pubs' , {
68 pub_id => $sd->dbxref->search_related('pub_dbxrefs')->search_related('pub')->first->pub_id
69 }, );
70 print "Added publication for stock " . $sd->stock->name . "\n";
72 if ( $self->trial ) {
73 print "Trial mode! Rolling back transaction.\n\n";
74 $schema->txn_rollback;
75 return 0;
76 } else {
77 print "Committing.\n";
78 return 1;
80 });
83 $self->dbh->do(<<EOSQL);
84 --do your SQL here
86 --grant permissions to web_usr
87 grant ALL on public.stock_pub to web_usr ;
88 grant USAGE on public.stock_pub_stock_pub_id_seq TO web_usr ;
91 EOSQL
92 print $result ? "Patch applied successfully.\n" : "Patch not applied.\n";
96 ####
97 1; #
98 ####