TaskPipe/SchemaManager.pm

filter: Statementstmt 85.8% 97 / 113 Branchbran 75.0% 9 / 12 Conditioncond 62.5% 5 / 8 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 83.3% 25 / 30 Podpod 0.0% 0 / 6 totaltotal 79.3% 138 / 174 CC 12 SCAR 8.7
CC
12
Cov
83%
table
6.1
db_string
4.0
connect_schema
3.3
CRAP 2.4
« TaskPipe/RunInfo.pm ↑ Summary TaskPipe/Task/ModuleMap.pm »
1 package TaskPipe::SchemaManager;
2  
3 40 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 40 ▶use DBIx::Class::Schema::Loader "make_schema_at";
Subroutine BEGIN: called CC=1 SCAR=0.0
5 40 ▶use DBIx::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 40 ▶use Module::Runtime qw(require_module);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 40 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 40 ▶use TaskPipe::SchemaManager::Settings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 40 ▶use TaskPipe::PathSettings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 40 ▶use TaskPipe::RunInfo;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 40 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 40 ▶use File::Path 'rmtree';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 40 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 40 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 40 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 40 with 'TaskPipe::Role::MooseType_ScopeMode';
17 40 with 'MooseX::ConfigCascade';
18  
19 has scope => (is => 'rw', isa => 'ScopeMode', lazy => 1, default => sub{
20 742 ▶ $_[0]->run_info->scope;
Subroutine __ANON__: called CC=1 SCAR=0.0
21 40 });
22  
23 has run_info => (is => 'ro', isa => 'TaskPipe::RunInfo', default => sub{
24 1911 ▶ TaskPipe::RunInfo->new;
Subroutine __ANON__: called CC=1 SCAR=0.0
25 40 });
26  
27 40 has schema => (is => 'rw', isa => 'DBIx::Class::Schema');
28 has settings => (is => 'rw', isa => 'TaskPipe::SchemaManager::Settings', lazy => 1, default => sub{
29 1898 ▶ my $module = __PACKAGE__.'::Settings_'.ucfirst( $_[0]->scope );
Subroutine __ANON__: called CC=1 SCAR=0.0
30 1898 require_module( $module );
31 1898 $module->new;
32 40 });
33  
34 has path_settings => (is => 'ro', isa => 'TaskPipe::PathSettings', lazy => 1, default => sub{
35 742 ▶ TaskPipe::PathSettings->new( scope => $_[0]->scope );
Subroutine __ANON__: called CC=1 SCAR=0.0
36 40 });
37  
38 has monikers => (is => 'rw', isa => 'HashRef', lazy => 1, default => sub{
39 1152 ▶ my $self = shift;
Subroutine __ANON__: called CC=2 SCAR=7.0
40  
41 1152 my @source_names;
42  
43 try {
44 1152 ▶ @source_names = $self->schema->sources;
Subroutine __ANON__: called CC=1 SCAR=0.0
45 } catch {
46 0 ▶ confess "Could not determine source names: $_";
Subroutine __ANON__: not called CC=1 SCAR=6.9
47 1152 };
48  
49 1152 my $m = {};
50 1152 foreach my $source_name (@source_names){
51  
52 7860 my $source = $self->schema->source( $source_name );
53 7860 $m->{ $source->name } = $source_name;
54  
55 }
56  
57 1152 return $m;
58 40 });
59  
60  
61  
62 sub db_string{
63 1898 ▶ my $self = shift;
Subroutine db_string: called CC=4 SCAR=13.9
Pod undocumented
64  
65 # my @sp = ();
66  
67 40 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
68 40 ▶ use MooseX::ConfigCascade::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
69  
70 1898 my @frags = qw(method type database host port);
71 1898 my $db_string = '';
72 1898 foreach my $frag ( @frags ){
73 9490 ▶ if ( $self->settings->$frag ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->settings->$frag) 5694 3796 9490
74 5694 ▶ $db_string.=':' if $db_string;
Branch: 1 branch
BranchTrueFalseTotal
if $db_string 3796 1898 5694
75 5694 $db_string.=$self->settings->$frag;
76 }
77  
78 # confess "A database connect string was requested, but a $frag was not provided. (Check your database connection information in the ".ref( $self->settings )." section of your ".$self->run_info->scope." config. You should have definitions for the following fields: ".join(",",@frags) unless $self->settings->$frag;
79 # push @sp,$self->settings->$frag;
80  
81 }
82  
83 # my $db_string = join(':',@sp);
84 1898 return $db_string;
85  
86 }
87  
88  
89 sub connect_schema{
90 1898 ▶ my ($self,$module) = @_;
Subroutine connect_schema: called CC=3 SCAR=12.0
Pod undocumented
91  
92 1898 ▶ $module ||= $self->settings->module;
Condition33%1/3
$module ||= $self->settings->module
l!l&&r!l&&!r
018980
MC/DC0%0/2
$module ||= $self->settings->module
$module $self->settings->module
Truth table33%1/3
A ||= B
A= $moduleB= $self->settings->module
ABresult
1X1
011
000
93  
94 1898 ▶ if ( ! $module ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($module) 0 1898 1898
95  
96 0 confess "I could not find the name of a database schema module to use. Did you forget to enter the correct database settings in the ".ref( $self->settings )." section of your config?";
97  
98 }
99  
100 try {
101 1898 ▶ require_module( $module );
Subroutine __ANON__: called CC=1 SCAR=0.0
102 } catch {
103 0 ▶ confess "[B<Require module ($module) failed. (Did you forget to generate the schema? See the help for C<generate schema>:>
Subroutine __ANON__: not called CC=1 SCAR=6.9
104  
105 taskpipe help generate schema
106  
107 B<):>\n$_]";
108 1898 };
109  
110 my $schema = $module->connect(
111 $self->db_string,
112 $self->settings->{username},
113 $self->settings->{password},
114 1898 { HandleError => DBIx::Error->HandleError,
115 unsafe => 1,
116 ShowErrorStatement => 1 }
117 );
118  
119 1898 $self->schema( $schema );
120 1898 return $schema;
121 }
122  
123  
124 sub flush{
125 0 ▶ my ($self) = @_;
Subroutine flush: not called CC=1 SCAR=6.9
Pod undocumented
126  
127 0 $self->schema->storage->disconnect;
128 0 my $schema = $self->connect_schema;
129 0 $self->schema( $schema );
130  
131 }
132  
133  
134  
135 sub gen_schema{
136 0 ▶ my ($self) = @_;
Subroutine gen_schema: not called CC=1 SCAR=6.9
Pod undocumented
137  
138 0 $self->clear_schema_path;
139  
140 make_schema_at($self->settings->module, {
141 dump_directory => $self->path_settings->path('lib'),
142 components => ['InflateColumn::DateTime']
143 }, [
144 $self->db_string,
145 $self->settings->{username},
146 $self->settings->{password}
147 0 ]);
148 }
149  
150  
151 sub clear_schema_path{
152 0 ▶ my ($self) = @_;
Subroutine clear_schema_path: not called CC=1 SCAR=6.9
Pod undocumented
153  
154 0 my $module = $self->settings->module;
155 0 my $sep = File::Spec->catdir('');
156 0 $module =~ s/::/$sep/g;
157 0 my $schema_path = File::Spec->catdir(
158 $self->path_settings->path('lib'),
159 $module
160 );
161  
162 0 rmtree( $schema_path );
163 }
164  
165  
166  
167  
168 sub table{
169 1459805 ▶ my ($self, $table_base, $type) = @_;
Subroutine table: called CC=6 SCAR=18.1
Pod undocumented
170  
171 1459805 my $table_prefix = '';
172 1459805 ▶ if ( ! $type || $type ne 'plan' ){
Branch: 1 branch
BranchTrueFalseTotal
if (not $type or $type ne "plan") 1457782 2023 1459805
Condition66%2/3
not $type or $type ne "plan"
l!l&&r!l&&!r
145778202023
MC/DC50%1/2
not $type or $type ne "plan"
not $type $type ne "plan"
Truth table66%2/3
not(A) or B
A= not $typeB= $type ne "plan"
ABresult
1X1
011
000
173 1457782 ▶ $table_prefix = $self->settings->table_prefix || '';
Condition100%2/2
$self->settings->table_prefix || ''
l!l
14545063276
MC/DC100%1/1
$self->settings->table_prefix || ''
$self->settings->table_prefix
Truth table100%2/2
A
A= $self->settings->table_prefix
Aresult
11
00
174 }
175  
176 1459805 my $moniker = $self->monikers->{ $table_prefix.$table_base };
177  
178 1459805 my $rs;
179  
180 1459805 ▶ confess "no moniker. table base: $table_base table_prefix $table_prefix monikers ".Dumper( $self->monikers ) unless $moniker;
Branch: 1 branch
BranchTrueFalseTotal
unless $moniker 0 1459805 1459805
181  
182 1459805 eval{ $rs = $self->schema->resultset( $moniker ); };
183 1459805 ▶ confess $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1459805 1459805
184  
185 1459805 return $rs;
186 }
187  
188  
189 =head1 NAME
190  
191 TaskPipe::SchemaManager - manage schema connections for TaskPipe
192  
193 =head1 DESCRIPTION
194  
195 It is not recommended to use this module directly. See the general manpages for TaskPipe.
196  
197 =head1 AUTHOR
198  
199 Tom Gracey <tomgracey@gmail.com>
200  
201 =head1 COPYRIGHT AND LICENSE
202  
203 Copyright (c) Tom Gracey 2018
204  
205 TaskPipe is free software, licensed under
206  
207 The GNU Public License Version 3
208  
209 =cut
210  
211
212  
213  
214  
215 40 __PACKAGE__->meta->make_immutable;
216 40 1;
217 __END__
« TaskPipe/RunInfo.pm ↑ Summary TaskPipe/Task/ModuleMap.pm »