blib/lib/DBIx/Changeset/App/BaseCommand.pm

filter: Statementstmt 33.3% 35 / 105 Branchbran 11.5% 3 / 26 Conditioncond 33.3% 5 / 15 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 91.6% 11 / 12 Podpod 100% 3 / 3 totaltotal 34.1% 57 / 167 CC 19 SCAR 39.3
CC
19
Cov
29%
determine_outstanding
132.0
opt_spec
8.5
validate_args
5.6
CRAP 51.1
« blib/lib/DBIx/Changeset/App/Command/create.pm ↑ Summary blib/lib/DBIx/Changeset/Loader/Pg.pm »
1 package DBIx::Changeset::App::BaseCommand;
2  
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use base qw/App::Cmd::Command/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Digest::MD5;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Term::Report;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Term::Prompt;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 ▶use vars qw{$VERSION};
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 $VERSION = '1.11';
15 }
16  
17 =head1 NAME
18  
19 DBIx::Changeset::App::BaseCommand - Base module for the command line app commands
20  
21 =head1 SYNOPSIS
22  
23 Base module for the command line app commands
24  
25 use as a base for an App::Cmd::Command:
26  
27 use base qw/DBIx::Changeset::App::BaseCommand/;
28  
29 =head1 METHODS
30  
31 =head2 opt_spec
32  
33 Override opt_spec to provide the defaults from config:
34
35 commands implement the options method to add there own options
36  
37 =cut
38  
39 15 ▶sub opt_spec {
Subroutine opt_spec: called CC=6 SCAR=21.4
Pod documented
40 15 my ( $class, $app ) = @_;
41 15 my ( $name ) = $class->command_names;
42 my @options = (
43 [ 'help' => "This usage screen" ],
44 [ 'prompt' => "Prompt where optional", { default => $app->{'config'}->{'prompt'} || $app->{'config'}->{$name}->{'prompt'} || 1 } ],
45 15 ▶ [ 'debug' => "Show debug output", { default => $app->{'config'}->{'debug'} || $app->{'config'}->{$name}->{'debug'} || undef } ],
Condition40%4/10
$app->{'config'}{'prompt'} || $app->{'config'}{$name}{'prompt'} || 1
l!l
015
$app->{"config"}{"prompt"} or $app->{"config"}{$name}{"prompt"}
l!l&&r!l&&!r
0015
$app->{'config'}{'debug'} || $app->{'config'}{$name}{'debug'} || undef
l!l
015
$app->{"config"}{"debug"} or $app->{"config"}{$name}{"debug"}
l!l&&r!l&&!r
0015
MC/DC0%0/2
$app->{'config'}{'prompt'} || $app->{'config'}{$name}{'prompt'} || 1
$app->{"config"}{"prompt"} $app->{"config"}{$name}{"prompt"}
MC/DC0%0/2
$app->{'config'}{'debug'} || $app->{'config'}{$name}{'debug'} || undef
$app->{"config"}{"debug"} $app->{"config"}{$name}{"debug"}
Truth table33%1/3
A or B
A= $app->{"config"}{"prompt"}B= $app->{"config"}{$name}{"prompt"}
ABresult
1X1
011
000
Truth table33%1/3
A or B
A= $app->{"config"}{"debug"}B= $app->{"config"}{$name}{"debug"}
ABresult
1X1
011
000
46 );
47 15 ▶ push @options, $class->options($app) if $class->can('options');
Branch: 1 branch
BranchTrueFalseTotal
if $class->can("options") 15 0 15
48 15 return @options;
49 }
50  
51 =head2 validate_args
52  
53 =cut
54  
55 11 ▶sub validate_args {
Subroutine validate_args: called CC=4 SCAR=17.2
Pod documented
56 11 my ( $self, $opt, $args ) = @_;
57 11 ▶ print $self->_usage_text and exit if $opt->{'help'};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"help"} 0 11 11
Condition20%1/5
$opt->{'help'} && (print($self->_usage_text) && exit)
!ll
1100
print $self->_usage_text and exit
!ll
00
MC/DC0%0/2
$opt->{'help'} && (print($self->_usage_text) && exit)
$opt->{'help'} print $self->_usage_text
Truth table33%1/3
A && B
A= $opt->{'help'}B= print $self->_usage_text
ABresult
0X0
100
111
58 11 ▶ $self->validate( $opt, $args ) if $self->can('validate');
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("validate") 11 0 11
59 3 return;
60 }
61  
62 =head2 determine_outstanding
63  
64 Loop to determine the outstanding records in a collection put here as its used
65 by both compare and update. also prompts to fix records
66  
67 =cut
68 0 ▶sub determine_outstanding {
Subroutine determine_outstanding: not called CC=11 SCAR=48.8
Pod documented
69 0 my ($self, $opt, $coll) = @_;
70  
71 0 my $rep = Term::Report->new(
72 startRow => 4,
73 numFormat => 1,
74 statusBar => [
75 label => 'Changeset Checking:',
76 subText => 'determining outstanding changesets',
77 subTextAlign => 'center',
78 showTime => 1,
79 ],
80 );
81  
82 ### deal with status bar
83 0 $rep->{'statusBar'}->setItems($coll->total);
84 0 $rep->{'statusBar'}->start;
85  
86 0 $rep->savePoint('total', "Total changesets: ", 1);
87 0 $rep->savePoint('outstanding', "\nOutstanding changesets: ", 1);
88 0 $rep->savePoint('skipped', "\nSkipped changesets: ", 1);
89 0 $rep->savePoint('fixed', "\nFixed changesets: ", 1);
90 0 $rep->savePoint('invmd5', "\nChangesets with differing md5: ", 1);
91  
92  
93 0 my $count = 0;
94 0 my $outstanding = 0;
95 0 my $skipped = 0;
96 0 my $fixed = 0;
97 0 my $invmd5 = 0;
98 0 $rep->finePrint('fixed', 0, $fixed);
99 0 $rep->finePrint('skipped', 0, $skipped);
100 0 $rep->finePrint('invmd5', 0, $fixed);
101 0 ▶ while ( my $record = $coll->next ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $record = $coll->next) 0 0 0
102 0 $rep->{'statusBar'}->subText($record->uri);
103  
104 ### check the record is valid
105 0 ▶ unless ( $record->valid ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($record->valid) 0 0 0
106 0 ▶ if ( $opt->{'prompt'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{'prompt'}) { } 0 0 0
107 0 $rep->clear();
108 0 my $value = &prompt('m', {
109 prompt => "choice:",
110 title => sprintf("Record: %s is invalid should I:", $record->uri),
111 items => [ qw(repair skip) ],
112 cols => 1,
113 accept_multiple_selections => 0,
114 accept_empty_selection => 0,
115 }, "", 1);
116 0 $rep->clear();
117 ### redraw report screen
118 0 $rep->{'statusBar'}->start;
119 0 $rep->savePoint('total', "Total changesets: ", 1);
120 0 $rep->savePoint('outstanding', "\nOutstanding changesets: ", 1);
121 0 $rep->savePoint('skipped', "\nSkipped changesets: ", 1);
122 0 $rep->savePoint('fixed', "\nFixed changesets: ", 1);
123  
124 0 ▶ if ( $value == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value == 0) { } 0 0 0
125 0 $record->generate_uid();
126 0 $rep->finePrint('fixed', 0, ++$fixed);
127 0 $rep->finePrint('skipped', 0, $skipped);
128 } else {
129 0 $rep->{'statusBar'}->update;
130 0 $record->skipped(1);
131 0 $rep->finePrint('fixed', 0, $fixed);
132 0 $rep->finePrint('skipped', 0, ++$skipped);
133 0 next;
134 }
135  
136 } else {
137 0 $rep->subText('Skipping invalid record');
138 0 $rep->{'statusBar'}->update;
139 0 $record->skipped(1);
140 0 $rep->finePrint('skipped', 0, ++$skipped);
141 0 next;
142 }
143 }
144  
145 0 $rep->finePrint('total', 0, ++$count);
146  
147 ### does the record have a historyrecord
148 my $hrec = DBIx::Changeset::HistoryRecord->new({
149 history_db_dsn => $opt->{'history_db_dsn'},
150 history_db_user => $opt->{'history_db_user'},
151 0 history_db_password => $opt->{'history_db_password'},
152 });
153 0 eval { $hrec->read($record->id); };
154
155 0 my $e;
156  
157 0 ▶ if ( $e = Exception::Class->caught('DBIx::Changeset::Exception::ReadHistoryRecordException') ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($e = 'Exception::Class'->caught('DBIx::Changeset::Exception::ReadHistoryRecordException')) { } 0 0 0
elsif ($e = 'Exception::Class'->caught) { } 0 0 0
158 ### ok no history record so must be outstanding
159 0 $rep->finePrint('outstanding', 0, ++$outstanding);
160 0 $record->outstanding(1);
161 } elsif ( $e = Exception::Class->caught() ) {
162 0 $rep->clear();
163 0 $rep->finish();
164 0 warn $e->error, "\n";
165 0 ▶ warn $e->trace->as_string, "\n" if defined $opt->{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt->{"debug"} 0 0 0
166 0 exit;
167 } else {
168 ### existing record so is the MD5 the same
169 0 my $md5 = $record->md5();
170 0 ▶ if ( $md5 ne $hrec->md5 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($md5 ne $hrec->md5) 0 0 0
171  
172 ### md5's dont match so outstanding
173 0 $rep->finePrint('outstanding', 0, ++$outstanding);
174 0 $record->outstanding(1);
175 0 $rep->finePrint('invmd5', 0, ++$invmd5);
176
177 }
178 }
179
180 0 $rep->{'statusBar'}->update;
181 }
182  
183 $rep->printBarReport(
184 0 "\n\n\n\n Outstanding Changeset Summary [" . $opt->{'history_db_dsn'} . "]\n\n",
185 {
186 " Total: " => $count,
187 " Outstanding: " => $outstanding,
188 " Invalid md5: " => $invmd5,
189 " Skipped: " => $skipped,
190 " Fixed: " => $fixed,
191 }
192 );
193  
194 0 $rep->finish();
195 0 $coll->reset();
196 0 my $out_count = 1;
197 0 ▶ while ( my $record = $coll->next_outstanding ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $record = $coll->next_outstanding) 0 0 0
198 0 ▶ printf STDERR "%d.\t%s\t%s %s\n", $out_count, $record->uri, $record->id, $record->valid ? '' : '[ INVALID ]';
Branch: 1 branch
BranchTrueFalseTotal
$record->valid ? : 0 0 0
199 0 $out_count++;
200 }
201  
202 0 return;
203 }
204  
205 =head1 COPYRIGHT & LICENSE
206  
207 Copyright 2004-2008 Grox Pty Ltd.
208  
209 This program is free software; you can redistribute it and/or modify it
210 under the same terms as Perl itself.
211  
212 The full text of the license can be found in the LICENSE file included with this module.
213  
214 =cut
215  
216 3 1; # End of DBIx::Changeset
« blib/lib/DBIx/Changeset/App/Command/create.pm ↑ Summary blib/lib/DBIx/Changeset/Loader/Pg.pm »