3 CXGN::Metadata::Dbpatch
7 my $dbpatch = CXGN::Metadata::Dbpatch->new()
11 This should be a parent class of all cxgn db patches.
12 This class takes care of storing a new Metadata and Dbversion objects,
13 which help us keep track of running db patches on the database.
16 -D <dbname> (mandatory)
19 -H <dbhost> (mandatory)
22 -u <script_executor_user> (mandatory)
23 username to run the script
25 -F force to run this script and don't stop it by
26 missing previous db_patches
28 -t test run. Rollback the transaction.
30 Note: If the first time that you run this script, obviously
31 you have no previous dbversion row in the md_dbversion
32 table, so you need to force the execution of this script
36 This class has to be sub-classed by a dbpatch script. The subclass
37 script has to be named exactly the same as the package name
39 =head2 Example MyDbpatch.pm
44 extends 'CXGN::Metadata::Dbpatch';
47 #now override init_patch() and patch()
50 # You can name your patch any way you want,
51 # but it is easiest just to name it with the package name:
54 my $description = 'my dbpatch description';
55 my @prev_patches = (); # list any prerequisites of other patches
57 # now set the above 3 params
59 $self->description($description);
60 $self->prereq(\@prev_patches);
66 ### Now you can insert the data using different options:
68 ## 1- By sql queryes using $dbh->do(<<EOSQL); and detailing in the tag the queries
70 ## 2- Using objects with the store function
72 ## 3- Using DBIx::Class first level objects
79 Now you can run the db patch from the command line like this:
81 mx-run MyDbpatch -H dbhost -D dbname -u username
85 Naama Menda<nm249@cornell.edu>
86 Lukas Mueller<lam87@cornell.edu>
87 Aureliano Bombarely<ab782@cornell.edu>
89 =head1 COPYRIGHT & LICENSE
91 Copyright 2010 Boyce Thompson Institute for Plant Research
93 This program is free software; you can redistribute it and/or modify
94 it under the same terms as Perl itself.
100 package CXGN
::Metadata
::Dbpatch
;
105 use CXGN
::DB
::InsertDBH
;
106 use CXGN
::Metadata
::Schema
;
107 use CXGN
::Metadata
::Dbversion
;
110 with
'MooseX::Getopt';
111 with
'MooseX::Runnable';
115 isa
=> 'CXGN::Metadata::Schema',
117 traits
=> ['NoGetopt'],
123 traits
=> ['NoGetopt'],
131 traits
=> ['Getopt'],
133 documentation
=> 'required, database host',
140 traits
=> ['Getopt'],
141 documentation
=> 'required, database name',
148 traits
=> ['NoGetopt'],
152 has
"description" => (
156 traits
=> ['NoGetopt'],
163 traits
=> ['Getopt'],
164 documentation
=> 'required, your SGN site login name',
172 traits
=> ['NoGetopt'],
173 default => sub { [] }
182 traits
=> ['Getopt'],
185 'force apply, ignoring prereqs and possible duplicate application',
193 traits
=> ['Getopt'],
196 'Test run. Rollback the transaction.',
202 ##override this in the parent class
206 my $dbh = CXGN
::DB
::InsertDBH
->new(
208 dbname
=>$self->dbname,
209 dbhost
=> $self->dbhost,
216 my $metadata_schema = CXGN
::Metadata
::Schema
->connect(
218 { on_connect_do
=> ['SET search_path TO metadata;'] },
221 ### Now it will check if you have runned this patch or the previous patches
223 my $dbversion = CXGN
::Metadata
::Dbversion
->new($metadata_schema)
224 ->complete_checking( {
225 patch_name
=> $self->name,
226 patch_descr
=> $self->description,
227 prepatch
=> $self->prereq,
228 force
=> $self->force
233 #CREATE A METADATA OBJECT and a new metadata_id in the database for this data
235 my $metadata = CXGN
::Metadata
::Metadbdata
->new($metadata_schema, $self->username);
237 #Get a new metadata_id (if you are using store function you only need to supply $metadbdata object)
239 #override this in the sub-class
240 my $error = $self->patch;
242 print "Failed! Rolling back! \n $error \n ";
248 $metadata->get_dbh->do('set search_path to metadata');
250 my $metadata_id = $metadata->store()->get_metadata_id();
251 $dbversion->store($metadata);
252 if ($self->trial) { $dbh->rollback; }
253 else { $dbh->commit; }
260 warn "You have to override init_patch in your sub-class!";
266 warn "You have to override patch in your sub-class!";