TaskPipe/Tool/Command_ClearTables.pm

filter: Statementstmt 66.3% 81 / 122 Branchbran 33.3% 8 / 24 Conditioncond 33.3% 3 / 9 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 90.4% 19 / 21 Podpod 0.0% 0 / 4 totaltotal 60.2% 112 / 186 CC 19 SCAR 21.9
CC
19
Cov
59%
get_group_monikers
24.4
execute
12.8
filter_monikers
12.0
CRAP 8.9
« TaskPipe/OpenProxyManager.pm ↑ Summary TaskPipe/OpenProxyManager/IPList.pm »
1 package TaskPipe::Tool::Command_ClearTables;
2  
3 39 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 39 ▶use MooseX::ClassAttribute;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 39 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 39 ▶use TaskPipe::SchemaManager;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 39 ▶use TaskPipe::JobManager;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 39 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 39 ▶use TryCatch;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 39 ▶use Log::Log4perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 39 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 39 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 39 ▶use DBIx::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 39 extends 'TaskPipe::Tool::Command';
15 39 with 'MooseX::ConfigCascade';
16 39 with 'TaskPipe::Role::MooseType_ScopeMode';
17  
18  
19 has option_specs => (is => 'ro', isa => 'ArrayRef', default => sub{[{
20 1708 ▶ module => __PACKAGE__,
Subroutine __ANON__: called CC=1 SCAR=0.0
21 is_config => 0
22 39 }]});
23  
24 1708 ▶has monikers => (is => 'rw', isa => 'ArrayRef', default => sub{[]});
Subroutine __ANON__: called CC=1 SCAR=0.0
25  
26 has schema_manager => (is => 'rw', isa => 'TaskPipe::SchemaManager', lazy => 1, default => sub{
27 427 ▶ my $sm = TaskPipe::SchemaManager->new;
Subroutine __ANON__: called CC=1 SCAR=0.0
28 427 $sm->connect_schema;
29 427 return $sm;
30 39 });
31  
32  
33 subtype 'TableGroup',
34 as 'Str',
35 where {
36 2562 ▶ $_ eq 'cache'
Subroutine __ANON__: called CC=3 SCAR=11.1
Branch: 1 branch
BranchTrueFalseTotal
$_ eq "cache" or $_ eq "plan" or $_ eq "project" 854 1708 2562
Condition66%2/3
$_ eq "cache" or $_ eq "plan"
l!l&&r!l&&!r
17080854
MC/DC50%1/2
$_ eq "cache" or $_ eq "plan"
$_ eq "cache" $_ eq "plan"
Truth table66%2/3
A or B
A= $_ eq "cache"B= $_ eq "plan"
ABresult
1X1
011
000
37 || $_ eq 'plan'
38 || $_ eq 'project'
39 39 };
40  
41  
42 sub cache_table_names{
43 0 ▶ my $self = shift;
Subroutine cache_table_names: not called CC=2 SCAR=17.9
Pod undocumented
44  
45 0 my $sm = TaskPipe::SchemaManager->new;
46 0 my $schema = $sm->connect_schema('TaskPipe::SchemaTemplate_Project');
47 0 my @monikers = $schema->sources;
48  
49 0 my $cache_names = {};
50  
51 0 foreach my $moniker (@monikers){
52  
53 0 my $source = $schema->source( $moniker );
54 0 my $table_name = $sm->settings->table_prefix.$source->name;
55 0 $cache_names->{ $table_name } = 1;
56  
57 }
58  
59 0 return $cache_names;
60 }
61  
62  
63  
64 sub get_group_monikers{
65 427 ▶ my $self = shift;
Subroutine get_group_monikers: called CC=7 SCAR=32.0
Pod undocumented
66  
67 427 my @all_monikers = $self->schema_manager->schema->sources;
68
69 427 ▶ if ( $self->scope eq 'global' || $self->group eq 'project' ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->scope eq "global" or $self->group eq "project") 427 0 427
Condition33%1/3
$self->scope eq "global" or $self->group eq "project"
l!l&&r!l&&!r
04270
MC/DC0%0/2
$self->scope eq "global" or $self->group eq "project"
$self->scope eq "global" $self->group eq "project"
Truth table33%1/3
A or B
A= $self->scope eq "global"B= $self->group eq "project"
ABresult
1X1
011
000
70  
71 427 $self->monikers( \@all_monikers );
72 427 return;
73 }
74  
75 0 my @monikers = ();
76  
77 0 my $cache_names = $self->cache_table_names;
78  
79 0 foreach my $moniker (@all_monikers){
80 0 my $source = $self->schema_manager->schema->source( $moniker );
81  
82 0 ▶ if ( $cache_names->{ $source->name } ){
Branch: 1 branch
BranchTrueFalseTotal
if ($$cache_names{$source->name}) { } 0 0 0
83  
84 0 ▶ push @monikers, $moniker if $self->group eq 'cache';
Branch: 1 branch
BranchTrueFalseTotal
if $self->group eq "cache" 0 0 0
85  
86 } else {
87  
88 0 ▶ push @monikers, $moniker if $self->group eq 'plan';
Branch: 1 branch
BranchTrueFalseTotal
if $self->group eq "plan" 0 0 0
89  
90 }
91 }
92  
93 0 $self->monikers( \@monikers );
94 }
95  
96  
97  
98 sub filter_monikers{ # input list of monikers for group, output list
99 # filtered by tables list
100  
101 0 ▶ my $self = shift;
Subroutine filter_monikers: not called CC=3 SCAR=24.8
Pod undocumented
102  
103 0 my $filtered = [];
104  
105 0 foreach my $name ( @{$self->tables } ){
106  
107 0 my ($moniker) = grep { $self->schema->source( $_ )->name eq $name } @{$self->monikers};
108  
109 0 ▶ if ( $moniker ){
Branch: 1 branch
BranchTrueFalseTotal
if ($moniker) { } 0 0 0
110  
111 0 push @$filtered, $moniker;
112  
113 } else {
114  
115 0 confess "Table '$name' not found";
116  
117 }
118  
119 }
120  
121 0 $self->monikers( $filtered );
122 }
123  
124  
125  
126  
127  
128 sub execute{
129 427 ▶ my ($self) = @_;
Subroutine execute: called CC=8 SCAR=25.5
Pod undocumented
130
131 427 $self->run_info->scope( $self->scope );
132  
133 427 my $logger = Log::Log4perl->get_logger;
134  
135 427 my $guard = $self->schema_manager->schema->txn_scope_guard;
136  
137 427 $self->get_group_monikers;
138 427 ▶ $self->filter_monikers if $self->tables;
Branch: 1 branch
BranchTrueFalseTotal
if $self->tables 0 427 427
139  
140 427 my %failed;
141  
142 427 ▶ while( @{$self->monikers} ){
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->monikers;}) 4697 427 5124
143  
144 4697 my $moniker = pop( @{$self->monikers} );
145  
146 4697 my $err;
147 39 ▶ try {
Subroutine BEGIN: called CC=1 SCAR=0.0
148  
149 4697 $self->schema_manager->schema->resultset( $moniker )->delete;
150  
151 39 ▶ } catch ( DBIx::Error $err where { $_->state =~ /^23/ }){
Subroutine BEGIN: called CC=1 SCAR=3.1
Branch: 2 branches
BranchTrueFalseTotal
if ($TryCatch::Error) 0 4697 4697
if ('TryCatch'->check_tc('DBIx::Error') and do { local $_ = $TryCatch::Error; $_->state =~ /^23/ }) { } 0 0 0
Condition0%0/3
"TryCatch"->check_tc("DBIx::Error") and do {
!ll&&!rl&&r
000
MC/DC0%0/2
"TryCatch"->check_tc("DBIx::Error") and do { local $_ = $TryCatch::Error; $_->state =~ /^23/ }
"TryCatch"->check_tc("DBIx::Error") do { local $_ = $TryCatch::Error; $_->state =~ /^23/ }
Truth table0%0/3
A and B
A= "TryCatch"->check_tc("DBIx::Error")B= do { local $_ = $TryCatch::Error; $_->state =~ /^23/ }
ABresult
0X0
100
111
152 0 unshift @{$self->monikers},$moniker;
153 0 $failed{ $moniker } = 1;
154 0 ▶ confess "Unable to clear tables of group '".$self->group."': Foreign key references prevent record deletion. Last error message was $err" if scalar(keys %failed) == @{$self->monikers};
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %failed == @{$self->monikers;} 0 0 0
155  
156 0 };
157  
158 }
159  
160 427 $guard->commit;
161  
162 427 ▶ if ( $self->scope eq 'global'){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->scope eq 'global') { } 0 427 427
163 0 $logger->info("Successfully cleared global tables");
164 } else {
165 427 $logger->info("Successfully cleared ".$self->group." tables for project "
166 .$self->schema_manager->path_settings->project_name);
167 }
168  
169 }
170  
171 =head1 NAME
172  
173 TaskPipe::Tool::Command_ClearTables - command to clear tables
174  
175 =head1 PURPOSE
176  
177 A convenience function for clearing a group of tables in the database.
178  
179 =head1 DESCRIPTION
180  
181 The C<clear tables> command is intended for use in early development when mistakes are frequent and it is desirable to run plans over an empty database each time. The default behaviour is to clear only the C<cache> tables. You can clear only C<plan> tables by specifying C<--group=plan> or both C<cache> and C<plan> tables (ie the whole project) using C<--group=project>. Note that clearing all tables in the project would mean e.g. any source tables you were using would also be emptied. Be careful! The default is set for a reason...
182  
183 It is also possible to clear C<global> tables (instead of project specific tables) by specifying C<--scope=global> (if you do so, the C<--group> parameter will be ignored). However, this should not normally be necessary. Use with caution and only if you understand the consequences.
184  
185 =head1 OPTIONS
186  
187 =over
188  
189 =item group
190  
191 Use C<group> to identify which table group to clear. Note C<group> only applies when C<--scope=project>. If C<--scope=global> then this option is meaningless and will be ignored. Valid groups are C<cache>, C<plan> and C<project>. C<project> will clear both cache and plan groups (ie, all project related tables.)
192  
193 =over
194  
195 =item *
196  
197 C<cache> - clear only the project tables taskpipe uses for caching
198  
199 =item *
200  
201 C<plan> - clear the tables specific to your project without affecting the cache
202  
203 =item *
204  
205 C<project> - clear all project tables (both cache and plan)
206  
207 =back
208  
209 =cut
210  
211 39 has group => (is => 'rw', isa => 'TableGroup', default => 'cache');
212  
213  
214 =item tables
215  
216 A comma delimited list of table names to clear. The list should be a subset of the tables in C<--group>. If --tables is omitted, all tables that match C<--group> will be cleared
217  
218 =cut
219  
220 39 has tables => (is => 'rw', isa => 'StrArrayRef');
221  
222 =item scope
223  
224 Specify C<project> to clear cache and/or plan tables (use in conjunction with C<group>) - or C<global> to clear global tables (use with caution)
225  
226 =cut
227  
228 39 has scope => (is => 'rw', isa => 'ScopeMode', default => 'project');
229  
230 =back
231  
232 =head1 AUTHOR
233  
234 Tom Gracey <tomgracey@gmail.com>
235  
236 =head1 COPYRIGHT AND LICENSE
237  
238 Copyright (c) Tom Gracey 2018
239  
240 TaskPipe is free software, licensed under
241  
242 The GNU Public License Version 3
243  
244 =cut
245  
246 39 __PACKAGE__->meta->make_immutable;
247 39 1;
248  
249  
250  
« TaskPipe/OpenProxyManager.pm ↑ Summary TaskPipe/OpenProxyManager/IPList.pm »