added a trial option to dbpatch
[cxgn-corelibs.git] / lib / CXGN / Metadata / Dbpatch.pm
blob4d3c2daa6587f14dfde1f8329cd918afb8ea5127
1 =head1 NAME
3 CXGN::Metadata::Dbpatch
5 =head1 SYNOPSIS
7 my $dbpatch = CXGN::Metadata::Dbpatch->new()
9 =head1 DESCRIPTION
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.
15 Options:
16 -D <dbname> (mandatory)
17 dbname to load into
19 -H <dbhost> (mandatory)
20 dbhost to load into
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
33 using -F
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
41 package MyDbpatch;
43 use Moose;
44 extends 'CXGN::Metadata::Dbpatch';
47 #now override init_patch() and patch()
48 sub init_patch {
49 my $self=shift;
50 # You can name your patch any way you want,
51 # but it is easiest just to name it with the package name:
53 my $name=__PACKAGE__;
54 my $description = 'my dbpatch description';
55 my @prev_patches = (); # list any prerequisites of other patches
57 # now set the above 3 params
58 $self->name($name);
59 $self->description($description);
60 $self->prereq(\@prev_patches);
64 sub patch {
65 my $self=shift;
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
83 =head1 AUTHORS
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.
97 =cut
100 package CXGN::Metadata::Dbpatch;
102 use strict;
103 use warnings;
105 use CXGN::DB::InsertDBH;
106 use CXGN::Metadata::Schema;
107 use CXGN::Metadata::Dbversion;
108 use Moose;
110 with 'MooseX::Getopt';
111 with 'MooseX::Runnable';
113 has "md_schema" => (
114 is => 'rw',
115 isa => 'CXGN::Metadata::Schema',
116 required => 0,
117 traits => ['NoGetopt'],
120 has "dbh" => (
121 is => 'rw',
122 isa => 'Ref',
123 traits => ['NoGetopt'],
124 required => 0,
127 has "dbhost" => (
128 is => 'rw',
129 isa => 'Str',
130 required => 1,
131 traits => ['Getopt'],
132 cmd_aliases => 'H',
133 documentation => 'required, database host',
136 has "dbname" => (
137 is => 'rw',
138 isa => 'Str',
139 required => 1,
140 traits => ['Getopt'],
141 documentation => 'required, database name',
142 cmd_aliases => 'D',
145 has "name" => (
146 is => 'rw',
147 isa => 'Str',
148 traits => ['NoGetopt'],
149 required => 0,
152 has "description" => (
153 is => 'rw',
154 isa => 'Str',
155 required => 0,
156 traits => ['NoGetopt'],
159 has "username" => (
160 is => 'rw',
161 isa => 'Str',
162 required => 1,
163 traits => ['Getopt'],
164 documentation => 'required, your SGN site login name',
165 cmd_aliases => 'u'
168 has "prereq" => (
169 is => 'rw',
170 isa => 'ArrayRef',
171 required => 0,
172 traits => ['NoGetopt'],
173 default => sub { [] }
177 has 'force' => (
178 is => 'rw',
179 isa => 'Bool',
180 required => 0,
181 default => 0,
182 traits => ['Getopt'],
183 cmd_aliases => 'F',
184 documentation =>
185 'force apply, ignoring prereqs and possible duplicate application',
188 has 'trial' => (
189 is => 'rw',
190 isa => 'Bool',
191 required => 0,
192 default => 0,
193 traits => ['Getopt'],
194 cmd_aliases => 't',
195 documentation =>
196 'Test run. Rollback the transaction.',
199 sub run {
200 my $self = shift;
202 ##override this in the parent class
203 $self->init_patch;
206 my $dbh = CXGN::DB::InsertDBH->new(
208 dbname =>$self->dbname,
209 dbhost => $self->dbhost,
211 )->get_actual_dbh();
213 $self->dbh($dbh);
216 my $metadata_schema = CXGN::Metadata::Schema->connect(
217 sub { $dbh },
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;
241 if ($error ne '1') {
242 print "Failed! Rolling back! \n $error \n ";
243 $dbh->rollback();
244 exit();
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; }
258 sub init_patch {
259 my $self=shift;
260 warn "You have to override init_patch in your sub-class!";
264 sub patch {
265 my $self=shift;
266 warn "You have to override patch in your sub-class!";