Merge pull request #13 from solgenomics/topic/TomDelDb
[phenome.git] / db / 0002_remove_genbank_taxon_id.pl
blob4cf46b870f7a74ba81a1b934ec51a72f7af932b8
1 #!/usr/bin/env perl
4 =head1 NAME
6 0002_remove_genbank_taxon_id.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 no need for genbank_taxon_id column in the organism table anymore.
36 =head1 AUTHOR
38 Naama Menda
39 nm249@cornell.edu
41 =head1 COPYRIGHT & LICENSE
43 Copyright 2010 Boyce Thompson Institute for Plant Research
45 This program is free software; you can redistribute it and/or modify
46 it under the same terms as Perl itself.
48 =cut
51 use strict;
52 use warnings;
54 use Pod::Usage;
55 use Getopt::Std;
56 use CXGN::DB::InsertDBH;
57 use CXGN::Metadata::Dbversion; ### Module to interact with the metadata.md_dbversion table
60 ## Declaration of the parameters used to run the script
62 our ($opt_H, $opt_D, $opt_p, $opt_F, $opt_h);
63 getopts("H:D:p:Fh");
65 ## If is used -h <help> or none parameters is detailed print pod
67 if (!$opt_H && !$opt_D && !$opt_p && !$opt_F && !$opt_h) {
68 print STDOUT "No optionas passed. Printing help\n\n";
69 pod2usage(1);
71 elsif ($opt_h) {
72 pod2usage(1);
76 ## Declaration of the name of the script and the description
78 my $patch_name = '0002_remove_genbank_taxon.pl';
79 my $patch_descr = 'This script removes the genbank_taxon_id column from the public organism table.';
81 print STDOUT "\n+--------------------------------------------------------------------------------------------------+\n";
82 print STDOUT "Executing the patch:\n $patch_name.\n\nDescription:\n $patch_descr.\n\nExecuted by:\n $opt_p.";
83 print STDOUT "\n+--------------------------------------------------------------------------------------------------+\n\n";
85 ## And the requeriments if you want not use all
87 my @previous_requested_patches = ( ## ADD HERE
88 );
90 ## Specify the mandatory parameters
92 if (!$opt_H || !$opt_D) {
93 print STDOUT "\nMANDATORY PARAMETER ERROR: -D <db_name> or/and -H <db_host> parameters has not been specified for $patch_name.\n";
96 if (!$opt_p) {
97 print STDOUT "\nMANDATORY PARAMETER ERROR: -p <script_executor_user> parameter has not been specified for $patch_name.\n";
100 ## Create the $schema object for the db_version object
101 ## This should be replace for CXGN::DB::DBICFactory as soon as it can use CXGN::DB::InsertDBH
103 my $dbh = CXGN::DB::InsertDBH->new(
105 dbname => $opt_D,
106 dbhost => $opt_H
108 )->get_actual_dbh();
110 print STDOUT "\nCreating the Metadata Schema object.\n";
112 my $metadata_schema = CXGN::Metadata::Schema->connect(
113 sub { $dbh },
114 { on_connect_do => ['SET search_path TO metadata;'] },
117 print STDOUT "\nChecking if this db_patch was executed before or if previous db_patches have been executed.\n";
119 ### Now it will check if you have runned this patch or the previous patches
121 my $dbversion = CXGN::Metadata::Dbversion->new($metadata_schema)
122 ->complete_checking( {
123 patch_name => $patch_name,
124 patch_descr => $patch_descr,
125 prepatch_req => \@previous_requested_patches,
126 force => $opt_F
131 ### CREATE AN METADATA OBJECT and a new metadata_id in the database for this data
133 my $metadata = CXGN::Metadata::Metadbdata->new($metadata_schema, $opt_p);
135 ### Get a new metadata_id (if you are using store function you only need to supply $metadbdata object)
137 my $metadata_id = $metadata->store()
138 ->get_metadata_id();
140 ### Now you can insert the data using different options:
142 ## 1- By sql queryes using $dbh->do(<<EOSQL); and detailing in the tag the queries
144 ## 2- Using objects with the store function
146 ## 3- Using DBIx::Class first level objects
149 ## In this case we will use the SQL tag
151 print STDERR "\nExecuting the SQL commands.\n";
153 $dbh->do(<<EOSQL);
155 ALTER TABLE public.organism DROP COLUMN genbank_taxon_id;
158 EOSQL
160 ## Now it will add this new patch information to the md_version table. It did the dbversion object before and
161 ## set the patch_name and the patch_description, so it only need to store it.
164 $dbversion->store($metadata);
166 print STDOUT "DONE!\n";
168 $dbh->commit;
170 __END__