6 0001_rename_sgn_organismprop.pl
10 this_script.pl [options]
14 -D <dbname> (mandatory)
17 -H <dbhost> (mandatory)
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
33 For some reason I named sgn.organismprop table wrong. It should be sgn.common_nameprop
35 This db patch will rename the table. Some code refactoring is required accordingly (of course I will take care of that along with committing the db_patch)
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.
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);
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";
77 ## Declaration of the name of the script and the description
79 my $patch_name = '0001_rename_sgn_organismprop.pl';
80 my $patch_descr = 'This script renames sgn_organismprop to sgn.common_nameprop';
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
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";
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(
111 print STDOUT
"\nCreating the Metadata Schema object.\n";
113 my $metadata_schema = CXGN
::Metadata
::Schema
->connect(
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,
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()
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";
157 ALTER TABLE sgn.organismprop RENAME TO common_nameprop;
159 --rename the PK column
160 ALTER TABLE sgn.common_nameprop rename column organismprop_id to common_nameprop_id;
163 alter table sgn.common_nameprop drop CONSTRAINT organismprop_pkey;
164 alter table sgn.common_nameprop add primary key (common_nameprop_id);
166 --rename the sequence
167 alter SEQUENCE sgn.organismprop_organismprop_id_seq rename to common_nameprop_common_nameprop_id_seq ;
173 ## Now it will add this new patch information to the md_version table. It did the dbversion object before and
174 ## set the patch_name and the patch_description, so it only need to store it.
177 $dbversion->store($metadata);
179 print STDOUT
"DONE!\n";