JESP.pm

filter: Statementstmt 95.2% 179 / 188 Branchbran 85.2% 29 / 34 Conditioncond 55.5% 5 / 9 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 100% 30 / 30 Podpod 100% 3 / 3 totaltotal 91.8% 248 / 270 CC 25 SCAR 14.2
CC
25
Cov
92%
deploy
11.1
install
5.0
_protect_select
4.1
CRAP 4.1
« JESP/Cmd/CommandJESP.pm ↑ Summary JESP/Patch.pm »
1 package App::JESP;
2 8 $App::JESP::VERSION = '0.016';
3 8 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 8 ▶use App::JESP::Plan;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use App::JESP::Colorizer;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 8 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 ▶use Class::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 ▶use DBIx::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 8 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 8 ▶use IO::Interactive;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 8 ▶use Log::Any qw/$log/;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 8 ▶use String::Truncate;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 # Settings
18 ## DB Connection attrbutes.
19 8 has 'dsn' => ( is => 'ro', isa => 'Str', required => 1 );
20 8 has 'username' => ( is => 'ro', isa => 'Maybe[Str]', required => 1);
21 8 has 'password' => ( is => 'ro', isa => 'Maybe[Str]', required => 1);
22 8 has 'home' => ( is => 'ro', isa => 'Str', required => 1 );
23  
24 ## JESP Attributes
25 8 has 'prefix' => ( is => 'ro', isa => 'Str', default => 'jesp_' );
26 8 has 'driver_class' => ( is => 'ro', isa => 'Str', lazy_build => 1);
27  
28 # Operational stuff
29 7 ▶has 'get_dbh' => ( is => 'ro', isa => 'CodeRef', default => sub{
Subroutine __ANON__: called CC=1 SCAR=0.0
30 7 my ($self) = @_;
31 3 ▶ return sub{
Subroutine __ANON__: called CC=1 SCAR=0.0
32 3 return DBI->connect( $self->dsn(), $self->username(), $self->password(),
33 { RaiseError => 1,
34 PrintError => 0,
35 AutoCommit => 1,
36 });
37 7 };
38 8 });
39  
40 8 has 'dbix_simple' => ( is => 'ro', isa => 'DBIx::Simple', lazy_build => 1);
41 8 has 'patches_table_name' => ( is => 'ro', isa => 'Str' , lazy_build => 1);
42 8 has 'meta_patches' => ( is => 'ro', isa => 'ArrayRef[HashRef]',
43 lazy_build => 1 );
44  
45  
46 8 has 'plan' => ( is => 'ro', isa => 'App::JESP::Plan', lazy_build => 1);
47 8 has 'driver' => ( is => 'ro', isa => 'App::JESP::Driver', lazy_build => 1 );
48  
49 8 has 'interactive' => ( is => 'ro' , isa => 'Bool' , lazy_build => 1 );
50 8 has 'colorizer' => ( is => 'ro', isa => 'App::JESP::Colorizer', lazy_build => 1 );
51  
52 8 has json => ( is => "ro", lazy_build => 1 );
53 3 ▶sub _build_json { JSON->new->relaxed(1) }
Subroutine _build_json: called CC=1 SCAR=0.0
54  
55 1 ▶sub _build_driver{
Subroutine _build_driver: called CC=1 SCAR=0.0
56 1 my ($self) = @_;
57 1 return $self->driver_class()->new({ jesp => $self });
58 }
59  
60 1 ▶sub _build_driver_class{
Subroutine _build_driver_class: called CC=1 SCAR=0.0
61 1 my ($self) = @_;
62 1 my $db_name = $self->dbix_simple()->dbh()->{Driver}->{Name};
63 1 my $driver_class = 'App::JESP::Driver::'.$db_name;
64 1 $log->info("Loading driver ".$driver_class);
65 1 Class::Load::load_class( $driver_class );
66 1 return $driver_class;
67 }
68  
69 4 ▶sub _build_plan{
Subroutine _build_plan: called CC=3 SCAR=11.0
70 4 my ($self) = @_;
71 4 my $file = File::Spec->catfile( $self->home(), 'plan.json' );
72 4 ▶ unless( ( -e $file ) && ( -r $file ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $file and -r $file) 1 3 4
Condition66%2/3
-e $file and -r $file
!ll&&!rl&&r
103
MC/DC50%1/2
-e $file and -r $file
-e $file -r $file
Truth table66%2/3
A and B
A= -e $fileB= -r $file
ABresult
0X0
100
111
73 1 die "File $file does not exists or is not readable\n";
74 }
75 3 return App::JESP::Plan->new({ file => $file, jesp => $self });
76 }
77  
78 3 ▶sub _build_dbix_simple{
Subroutine _build_dbix_simple: called CC=1 SCAR=0.0
79 3 my ($self) = @_;
80 3 my $dbh = $self->get_dbh()->();
81 3 my $db = DBIx::Simple->connect($dbh);
82 }
83  
84 2 ▶sub _build_patches_table_name{
Subroutine _build_patches_table_name: called CC=1 SCAR=0.0
85 2 my ($self) = @_;
86 2 return $self->prefix().'patch';
87 }
88  
89 # Building the meta patches, in SQLite compatible format.
90 2 ▶sub _build_meta_patches{
Subroutine _build_meta_patches: called CC=1 SCAR=0.0
91 2 my ($self) = @_;
92 return [
93 2 { id => $self->prefix().'meta_zero', sql => 'CREATE TABLE '.$self->patches_table_name().' ( id VARCHAR(512) NOT NULL PRIMARY KEY, applied_datetime TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP );' }
94 ];
95 }
96  
97 2 ▶sub _build_interactive{
Subroutine _build_interactive: called CC=1 SCAR=0.0
98 2 my ($self) = @_;
99 2 return IO::Interactive::is_interactive();
100 }
101  
102 2 ▶sub _build_colorizer{
Subroutine _build_colorizer: called CC=1 SCAR=0.0
103 2 my ($self) = @_;
104 2 return App::JESP::Colorizer->new({ jesp => $self });
105 }
106  
107 3 ▶sub install{
Subroutine install: called CC=5 SCAR=16.2
Pod documented
108 3 my ($self) = @_;
109  
110 # First try to select from $self->patches_table_name
111 3 my $dbh = $self->dbix_simple->dbh();
112 3 my $patches = eval{
113 $self->_protect_select(
114 3 ▶ sub{ $self->dbix_simple()->query('SELECT '.$dbh->quote_identifier('id').' FROM '.$dbh->quote_identifier($self->patches_table_name)); },
Subroutine __ANON__: called CC=1 SCAR=0.0
115 3 "CANNOT_SELECT_FROM_META");
116 };
117 3 ▶ if( my $err = $@ ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 2 1 3
118 2 ▶ unless( $err =~ /^CANNOT_SELECT_FROM_META\n/ ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($err =~ /^CANNOT_SELECT_FROM_META\n/) 0 2 2
119 0 $log->critical("Unexpected error from _protect_select. Run again in verbose mode.");
120 0 die $err;
121 }
122 2 $log->info("Innitiating meta tables");
123 2 $self->_apply_meta_patch( $self->meta_patches()->[0] );
124 }
125 3 $log->info("Uprading meta tables");
126 # Select all meta patches and make sure all of mine are applied.
127 3 my $applied_patches = { $self->dbix_simple()
128 ->select( $self->patches_table_name() , [ 'id', 'applied_datetime' ] , { id => { -like => $self->prefix().'meta_%' } } )
129 ->map_hashes('id')
130 };
131 3 foreach my $meta_patch ( @{ $self->meta_patches() } ){
132 4 ▶ if( $applied_patches->{$meta_patch->{id}} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($$applied_patches{$meta_patch->{"id"}}) 3 1 4
133 3 $log->debug("Patch ".$meta_patch->{id}." already applied on ".$applied_patches->{$meta_patch->{id}}->{applied_datetime});
134 3 next;
135 }
136 1 $self->_apply_meta_patch( $meta_patch );
137 }
138 3 $log->info("Done upgrading meta tables");
139 3 return 1;
140 }
141  
142 8 ▶sub _applied_patches{
Subroutine _applied_patches: called CC=1 SCAR=0.0
143 8 my ($self) = @_;
144  
145 8 my $db = $self->dbix_simple();
146 my $applied_patches_result = $self->_protect_select(
147 8 ▶ sub{
Subroutine __ANON__: called CC=1 SCAR=0.0
148 8 $db->select( $self->patches_table_name() , [ 'id', 'applied_datetime' ] , {} , 'applied_datetime' );
149 8 }, "ERROR querying meta schema. Did you forget to run 'install'?");
150  
151 7 return { $applied_patches_result->map_hashes('id') };
152 }
153  
154  
155 3 ▶sub status{
Subroutine status: called CC=4 SCAR=13.9
Pod documented
156 3 my ($self) = @_;
157 3 my $plan_patches = $self->plan()->patches();
158 3 $log->debug("Got ".scalar(@$plan_patches)." patches to inspect");
159  
160 3 my $applied_patches = $self->_applied_patches();
161  
162 3 foreach my $plan_patch ( @$plan_patches ){
163 11 ▶ if( my $applied_patch = delete $applied_patches->{$plan_patch->id()} ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $applied_patch = delete $$applied_patches{$plan_patch->id}) { } 7 4 11
164 7 $plan_patch->applied_datetime( $applied_patch->{applied_datetime} );
165 7 $log->info( $self->colorizer->colored('✔︎', "bold green")." ".sprintf('%-52s', "'".$plan_patch->id()."'" )." Applied on ".$plan_patch->applied_datetime() );
166 }else{
167 4 $log->info( $self->colorizer->colored('⚠', "bold yellow")." ".sprintf('%-52s', "'".$plan_patch->id()."'" )." Not applied (yet?)" );
168 }
169 }
170  
171 3 my $meta_prefix = $self->prefix().'meta';
172  
173 3 my $plan_orphans = [ grep{ $_ !~ /^$meta_prefix/ } keys %$applied_patches ];
174 3 ▶ if( @$plan_orphans ){
Branch: 1 branch
BranchTrueFalseTotal
if (@$plan_orphans) 1 2 3
175 1 $log->warn($self->colorizer()->colored('⚠︎', "bold red")." Got orphan patches (patches in meta table but not in plan): ".join(', ' , map{ "'$_'" } @$plan_orphans ) );
176 }
177  
178 return {
179 3 plan_patches => $plan_patches,
180 plan_orphans => $plan_orphans,
181 };
182 }
183  
184 5 ▶sub deploy{
Subroutine deploy: called CC=11 SCAR=24.1
Pod documented
185 5 my ($self, $options) = @_;
186  
187 5 ▶ defined($options) ||( $options = {} );
Branch: 1 branch
BranchTrueFalseTotal
defined $options or $options = {} 2 3 5
188  
189 5 $log->info("DEPLOYING DB Patches");
190  
191 5 my $db = $self->dbix_simple();
192 5 my $patches = $self->plan()->patches();
193  
194 5 ▶ if( $options->{patches} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"patches"}) 1 4 5
195 # Filter existing patches.
196 1 my @patch_ids = @{$options->{patches}};
197 1 $log->info("Applying only patches ".join(', ', map{ "'".$_."'" } @patch_ids)." in this order");
198 1 my $patches_by_id = { map{ $_->id() => $_ } @$patches };
199 1 my @new_patch_list = ();
200 1 foreach my $patch_id ( @patch_ids ){
201 1 my $patch = $patches_by_id->{$patch_id};
202 1 ▶ unless( $patch ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($patch) 0 1 1
203 0 die "Cannot find patch '$patch_id' in the plan ".$self->plan()->file().". Check the name\n";
204 }
205 1 push @new_patch_list , $patch;
206 }
207 1 $patches = \@new_patch_list;
208 }
209  
210 5 my $applied_patches = $self->_applied_patches();
211  
212 4 my $applied = 0;
213 4 foreach my $patch ( @{$patches} ){
214 13 ▶ if( my $applied_patch = $applied_patches->{$patch->id()}){
Branch: 1 branch
BranchTrueFalseTotal
if (my $applied_patch = $$applied_patches{$patch->id}) { } 9 4 13
215 9 ▶ unless( $options->{force} ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($options->{"force"}) 4 5 9
216 4 $log->debug("Patch '".$patch->id()."' has already been applied on ".$applied_patch->{applied_datetime}." skipping");
217 4 next;
218 }
219 5 $log->warn("Patch '".$patch->id()."' has already been applied on ".$applied_patch->{applied_datetime}." but forcing it.");
220 }else{
221 4 $log->info("Patch '".$patch->id()."' never applied");
222 }
223 9 eval{
224 9 $db->begin_work();
225 9 ▶ if( my $already_applied = $db->select( $self->patches_table_name(), '*',
Branch: 1 branch
BranchTrueFalseTotal
if (my $already_applied = $db->select($self->patches_table_name, '*', {'id', $patch->id})->hash) { } 5 4 9
226 { id => $patch->id() } )->hash() ){
227 5 $db->update( $self->patches_table_name(),
228 { applied_datetime => \'CURRENT_TIMESTAMP' },
229 { id => $patch->id() } );
230 } else {
231 4 $db->insert( $self->patches_table_name() , { id => $patch->id() } );
232 }
233 9 ▶ unless( $options->{logonly} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{'logonly'}) { } 4 5 9
234 5 $self->driver()->apply_patch( $patch );
235 }else{
236 4 $log->info("logonly mode. Patch not really applied");
237 }
238 9 $db->commit();
239 };
240 9 ▶ if( my $err = $@ ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 0 9 9
241 0 $log->error("Got error $err. ROLLING BACK");
242 0 $db->rollback();
243 0 die "ERROR APPLYING PATCH ".$patch->id().": $err. ".$self->colorizer()->colored("ABORTING", "bold red")."\n";
244 };
245 9 $log->info($self->colorizer()->colored("Patch '".$patch->id()."' applied successfully", "green"));
246 9 $applied++;
247 }
248 4 $log->info($self->colorizer()->colored("DONE Deploying DB Patches", "green"));
249 4 return $applied;
250 }
251  
252 # Runs the code to return a DBIx::Simple::Result
253 # or die with the given error message (for humans)
254 #
255 # Mainly this is for testing that a table exists by attemtpting to select from
256 # it. Do NOT use that in any other cases.
257 11 ▶sub _protect_select{
Subroutine _protect_select: called CC=4 SCAR=14.2
258 11 my ( $self, $code , $message) = @_;
259 11 my $result = eval{ $code->(); };
260 11 ▶ if( my $err = $@ || $result->isa('DBIx::Simple::Dummy') ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@ || $result->isa("DBIx::Simple::Dummy")) 3 8 11
Condition66%2/3
$@ || $result->isa('DBIx::Simple::Dummy')
l!l&&r!l&&!r
308
MC/DC50%1/2
$@ || $result->isa('DBIx::Simple::Dummy')
$@ $result->isa('DBIx::Simple::Dummy')
Truth table66%2/3
A || B
A= $@B= $result->isa('DBIx::Simple::Dummy')
ABresult
1X1
011
000
261 3 ▶ $log->trace("Error doing select: ".( $err || $self->dbix_simple()->error() ) );
Condition33%1/3
$err || $self->dbix_simple->error
l!l&&r!l&&!r
300
MC/DC0%0/2
$err || $self->dbix_simple->error
$err $self->dbix_simple->error
Truth table33%1/3
A || B
A= $errB= $self->dbix_simple->error
ABresult
1X1
011
000
262 3 die $message."\n";
263 }
264 8 return $result;
265 }
266  
267 3 ▶sub _apply_meta_patch{
Subroutine _apply_meta_patch: called CC=3 SCAR=11.6
268 3 my ($self, $meta_patch) = @_;
269 3 $log->debug("Applying meta patch ".$meta_patch->{id});
270  
271 3 my $sql = $meta_patch->{sql};
272 3 my $db = $self->dbix_simple();
273  
274 3 $log->debug("Doing ".$sql);
275 3 eval{
276 3 $db->begin_work();
277 3 ▶ $db->dbh->do( $sql ) or die "Cannot do '$sql':".$db->dbh->errstr()."\n";
Branch: 1 branch
BranchTrueFalseTotal
$db->dbh->do($sql) or die "Cannot do '${sql}':" . $db->dbh->errstr . "\n" 3 0 3
278 3 $db->insert( $self->patches_table_name() , { id => $meta_patch->{id} } );
279 3 $db->commit();
280 };
281 3 ▶ if( my $err = $@ ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 0 3 3
282 0 $log->error("Got error $err. ROLLING BACK");
283 0 $db->rollback();
284 0 die "ERROR APPLYING META PATCH ".$meta_patch->{id}.": $err. ABORTING\n";
285 };
286 }
287  
288 8 __PACKAGE__->meta->make_immutable();
289 8 1;
290  
291 __END__
« JESP/Cmd/CommandJESP.pm ↑ Summary JESP/Patch.pm »