function for returning the second level object of locusgroup members
[phenome.git] / db / 0004_grant_cvterm_permissions.pl
blobcd4a0b8057d668bed60e50af84299a828a40ccc9
1 #!/usr/bin/env perl
4 =head1 NAME
6 [ this script name ].pl
8 =head1 SYNOPSIS
10 this_script.pl [options]
12 Options:
14 -D <dbname> (mandatory)
15 dbname to load into
17 -H <dbhost> (mandatory)
18 dbhost to load into
20 -p <script_executor_user> (mandatory)
21 username to run the script
23 -F force to run this script and don't stop it by
24 missing previous db_patches
26 Note: If the first time that you run this script, obviously
27 you have no previous dbversion row in the md_dbversion
28 table, so you need to force the execution of this script
29 using -F
31 =head1 DESCRIPTION
33 we now allow submitters to load new organismprops (such as 'sol100' for organisms to be included in the sequencing project)
35 This db patch will grant web_usr with insert permissions to the organismprop table to allow calling $organism->create_organismprops (L<Bio::Chado::Schema::Organism::Orgnaism> ) from the web site (sol100.pl now allows this, and soon sybmitters will be able to add more props)
37 =head1 AUTHOR
39 Naama Menda
40 nm249@cornell.edu
42 =head1 COPYRIGHT & LICENSE
44 Copyright 2010 Boyce Thompson Institute for Plant Research
46 This program is free software; you can redistribute it and/or modify
47 it under the same terms as Perl itself.
49 =cut
52 use strict;
53 use warnings;
55 use Pod::Usage;
56 use Getopt::Std;
57 use CXGN::DB::InsertDBH;
58 use CXGN::Metadata::Dbversion; ### Module to interact with the metadata.md_dbversion table
61 ## Declaration of the parameters used to run the script
63 our ($opt_H, $opt_D, $opt_p, $opt_F, $opt_h);
64 getopts("H:D:p:Fh");
66 ## If is used -h <help> or none parameters is detailed print pod
68 if (!$opt_H && !$opt_D && !$opt_p && !$opt_F && !$opt_h) {
69 print STDOUT "No optionas passed. Printing help\n\n";
70 pod2usage(1);
72 elsif ($opt_h) {
73 pod2usage(1);
77 ## Declaration of the name of the script and the description
79 my $patch_name = '0003_grant_organismprop_permissions.pl';
80 my $patch_descr = 'This script grants web_user with insert permissions on cvterm ';
82 print STDOUT "\n+--------------------------------------------------------------------------------------------------+\n";
83 print STDOUT "Executing the patch:\n $patch_name.\n\nDescription:\n $patch_descr.\n\nExecuted by:\n $opt_p.";
84 print STDOUT "\n+--------------------------------------------------------------------------------------------------+\n\n";
86 ## And the requeriments if you want not use all
88 my @previous_requested_patches = ( ## ADD HERE
89 );
91 ## Specify the mandatory parameters
93 if (!$opt_H || !$opt_D) {
94 print STDOUT "\nMANDATORY PARAMETER ERROR: -D <db_name> or/and -H <db_host> parameters has not been specified for $patch_name.\n";
97 if (!$opt_p) {
98 print STDOUT "\nMANDATORY PARAMETER ERROR: -p <script_executor_user> parameter has not been specified for $patch_name.\n";
101 ## Create the $schema object for the db_version object
102 ## This should be replace for CXGN::DB::DBICFactory as soon as it can use CXGN::DB::InsertDBH
104 my $dbh = CXGN::DB::InsertDBH->new(
106 dbname => $opt_D,
107 dbhost => $opt_H
109 )->get_actual_dbh();
111 print STDOUT "\nCreating the Metadata Schema object.\n";
113 my $metadata_schema = CXGN::Metadata::Schema->connect(
114 sub { $dbh },
115 { on_connect_do => ['SET search_path TO metadata;'] },
118 print STDOUT "\nChecking if this db_patch was executed before or if previous db_patches have been executed.\n";
120 ### Now it will check if you have runned this patch or the previous patches
122 my $dbversion = CXGN::Metadata::Dbversion->new($metadata_schema)
123 ->complete_checking( {
124 patch_name => $patch_name,
125 patch_descr => $patch_descr,
126 prepatch_req => \@previous_requested_patches,
127 force => $opt_F
132 ### CREATE AN METADATA OBJECT and a new metadata_id in the database for this data
134 my $metadata = CXGN::Metadata::Metadbdata->new($metadata_schema, $opt_p);
136 ### Get a new metadata_id (if you are using store function you only need to supply $metadbdata object)
138 my $metadata_id = $metadata->store()
139 ->get_metadata_id();
141 ### Now you can insert the data using different options:
143 ## 1- By sql queryes using $dbh->do(<<EOSQL); and detailing in the tag the queries
145 ## 2- Using objects with the store function
147 ## 3- Using DBIx::Class first level objects
150 ## In this case we will use the SQL tag
152 print STDERR "\nExecuting the SQL commands.\n";
154 $dbh->do(<<EOSQL);
156 GRANT insert,select ON public.cvterm TO web_usr;
157 GRANT usage,select,update ON public.cvterm_cvterm_id_seq TO web_usr;
159 EOSQL
161 ## Now it will add this new patch information to the md_version table. It did the dbversion object before and
162 ## set the patch_name and the patch_description, so it only need to store it.
165 $dbversion->store($metadata);
167 print STDOUT "DONE!\n";
169 $dbh->commit;
171 __END__