File Coverage

blib/lib/DBIx/Changeset/App/BaseCommand.pm
Criterion Covered Total %
statement 34 104 32.6
branch 3 22 13.6
condition 2 7 28.5
subroutine 11 12 91.6
pod 3 3 100.0
total 53 148 35.8


line stmt bran cond sub pod time code
1             package DBIx::Changeset::App::BaseCommand;
2              
3 2     2   3983 use warnings;
  2         3  
  2         57  
4 2     2   10 use strict;
  2         3  
  2         57  
5              
6 2     2   10 use base qw/App::Cmd::Command/;
  2         4  
  2         1436  
7 2     2   1868 use Digest::MD5;
  2         3  
  2         71  
8 2     2   1779 use Encode;
  2         28769  
  2         259  
9 2     2   1958 use Term::Report;
  2         3820  
  2         73  
10 2     2   1681 use Term::Prompt;
  2         41633  
  2         184  
11              
12 2     2   25 use vars qw{$VERSION};
  2         4  
  2         131  
13             BEGIN {
14 2     2   2126 $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             sub opt_spec {
40 14     14 1 548675 my ( $class, $app ) = @_;
41 14         50 my ( $name ) = $class->command_names;
42 14   50     441 my @options = (
      50        
43             [ 'help' => "This usage screen" ],
44             [ 'prompt' => "Prompt where optional", { default => $app->{'config'}->{'prompt'} || $app->{'config'}->{$name}->{'prompt'} || 1 } ],
45             [ 'debug' => "Show debug output", { default => $app->{'config'}->{'debug'} || $app->{'config'}->{$name}->{'debug'} || undef } ],
46             );
47 14 50       173 push @options, $class->options($app) if $class->can('options');
48 14         100 return @options;
49             }
50              
51             =head2 validate_args
52              
53             =cut
54              
55             sub validate_args {
56 10     10 1 20239 my ( $self, $opt, $args ) = @_;
57 10 50 0     122 print $self->_usage_text and exit if $opt->{'help'};
58 10 50       77 $self->validate( $opt, $args ) if $self->can('validate');
59 2         6 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             sub determine_outstanding {
69 0     0 1   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 ) {
102 0           $rep->{'statusBar'}->subText($record->uri);
103              
104             ### check the record is valid
105 0 0         unless ( $record->valid ) {
106 0 0         if ( $opt->{'prompt'} ) {
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 0         if ( $value == 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 0           my $hrec = DBIx::Changeset::HistoryRecord->new({
149             history_db_dsn => $opt->{'history_db_dsn'},
150             history_db_user => $opt->{'history_db_user'},
151             history_db_password => $opt->{'history_db_password'},
152             });
153 0           eval { $hrec->read($record->id); };
  0            
154            
155 0           my $e;
156              
157 0 0         if ( $e = Exception::Class->caught('DBIx::Changeset::Exception::ReadHistoryRecordException') ) {
    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 0         warn $e->trace->as_string, "\n" if defined $opt->{'debug'};
166 0           exit;
167             } else {
168             ### existing record so is the MD5 the same
169 0           my $md5 = $record->md5();
170 0 0         if ( $md5 ne $hrec->md5 ) {
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 ) {
198 0 0         printf STDERR "%d.\t%s\t%s %s\n", $out_count, $record->uri, $record->id, $record->valid ? '' : '[ INVALID ]';
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             1; # End of DBIx::Changeset