| 1 | package TaskPipe::SchemaManager; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 38 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 38 | ▶ | use DBIx::Class::Schema::Loader "make_schema_at"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 38 | ▶ | use DBIx::Error; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 38 | ▶ | use Module::Runtime qw(require_module); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 38 | ▶ | use Moose::Util::TypeConstraints; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 38 | ▶ | use TaskPipe::SchemaManager::Settings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 38 | ▶ | use TaskPipe::PathSettings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 38 | ▶ | use TaskPipe::RunInfo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 38 | ▶ | use File::Spec; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 38 | ▶ | use File::Path 'rmtree'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 38 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 38 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 38 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 38 | with 'TaskPipe::Role::MooseType_ScopeMode'; | |||||||||||||||||||||||||||
| 17 | 38 | with 'MooseX::ConfigCascade'; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | has scope => (is => 'rw', isa => 'ScopeMode', lazy => 1, default => sub{ | ||||||||||||||||||||||||||||
| 20 | 708 | ▶ | $_[0]->run_info->scope; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 38 | }); | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | has run_info => (is => 'ro', isa => 'TaskPipe::RunInfo', default => sub{ | ||||||||||||||||||||||||||||
| 24 | 1809 | ▶ | TaskPipe::RunInfo->new; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 38 | }); | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 38 | has schema => (is => 'rw', isa => 'DBIx::Class::Schema'); | |||||||||||||||||||||||||||
| 28 | has settings => (is => 'rw', isa => 'TaskPipe::SchemaManager::Settings', lazy => 1, default => sub{ | ||||||||||||||||||||||||||||
| 29 | 1796 | ▶ | my $module = __PACKAGE__.'::Settings_'.ucfirst( $_[0]->scope ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | 1796 | require_module( $module ); | |||||||||||||||||||||||||||
| 31 | 1796 | $module->new; | |||||||||||||||||||||||||||
| 32 | 38 | }); | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | has path_settings => (is => 'ro', isa => 'TaskPipe::PathSettings', lazy => 1, default => sub{ | ||||||||||||||||||||||||||||
| 35 | 708 | ▶ | TaskPipe::PathSettings->new( scope => $_[0]->scope ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | 38 | }); | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | has monikers => (is => 'rw', isa => 'HashRef', lazy => 1, default => sub{ | ||||||||||||||||||||||||||||
| 39 | 1084 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 1084 | my @source_names; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | try { | ||||||||||||||||||||||||||||
| 44 | 1084 | ▶ | @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 | 1084 | }; | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 1084 | my $m = {}; | |||||||||||||||||||||||||||
| 50 | 1084 | foreach my $source_name (@source_names){ | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 7424 | my $source = $self->schema->source( $source_name ); | |||||||||||||||||||||||||||
| 53 | 7424 | $m->{ $source->name } = $source_name; | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 1084 | return $m; | |||||||||||||||||||||||||||
| 58 | 38 | }); | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | sub db_string{ | ||||||||||||||||||||||||||||
| 63 | 1796 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
db_string: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | # my @sp = (); | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 38 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 68 | 38 | ▶ | use MooseX::ConfigCascade::Util; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 1796 | my @frags = qw(method type database host port); | |||||||||||||||||||||||||||
| 71 | 1796 | my $db_string = ''; | |||||||||||||||||||||||||||
| 72 | 1796 | foreach my $frag ( @frags ){ | |||||||||||||||||||||||||||
| 73 | 8980 | ▶ | if ( $self->settings->$frag ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 5388 | ▶ | $db_string.=':' if $db_string; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 5388 | $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 | 1796 | return $db_string; | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | sub connect_schema{ | ||||||||||||||||||||||||||||
| 90 | 1796 | ▶ | my ($self,$module) = @_; | ||||||||||||||||||||||||||
|
Subroutine
connect_schema: called CC=3 SCAR=12.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 1796 | ▶ | $module ||= $self->settings->module; | ||||||||||||||||||||||||||
|
Condition33%1/3
$module ||= $self->settings->module
MC/DC0%0/2
$module ||= $self->settings->module
$module
$self->settings->module
Truth table33%1/3
A ||= B
A= $moduleB= $self->settings->module
| |||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 1796 | ▶ | if ( ! $module ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | 1796 | ▶ | 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 | 1796 | }; | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | my $schema = $module->connect( | ||||||||||||||||||||||||||||
| 111 | $self->db_string, | ||||||||||||||||||||||||||||
| 112 | $self->settings->{username}, | ||||||||||||||||||||||||||||
| 113 | $self->settings->{password}, | ||||||||||||||||||||||||||||
| 114 | 1796 | { HandleError => DBIx::Error->HandleError, | |||||||||||||||||||||||||||
| 115 | unsafe => 1, | ||||||||||||||||||||||||||||
| 116 | ShowErrorStatement => 1 } | ||||||||||||||||||||||||||||
| 117 | ); | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 1796 | $self->schema( $schema ); | |||||||||||||||||||||||||||
| 120 | 1796 | 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 | 1266937 | ▶ | my ($self, $table_base, $type) = @_; | ||||||||||||||||||||||||||
|
Subroutine
table: called CC=6 SCAR=18.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 1266937 | my $table_prefix = ''; | |||||||||||||||||||||||||||
| 172 | 1266937 | ▶ | if ( ! $type || $type ne 'plan' ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $type or $type ne "plan"
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"
| |||||||||||||||||||||||||||||
| 173 | 1264997 | ▶ | $table_prefix = $self->settings->table_prefix || ''; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->settings->table_prefix || ''
MC/DC100%1/1
$self->settings->table_prefix || ''
$self->settings->table_prefix
Truth table100%2/2
A
A= $self->settings->table_prefix
| |||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 1266937 | my $moniker = $self->monikers->{ $table_prefix.$table_base }; | |||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 1266937 | my $rs; | |||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 1266937 | ▶ | confess "no moniker. table base: $table_base table_prefix $table_prefix monikers ".Dumper( $self->monikers ) unless $moniker; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 1266937 | eval{ $rs = $self->schema->resultset( $moniker ); }; | |||||||||||||||||||||||||||
| 183 | 1266937 | ▶ | confess $@ if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 1266937 | 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 | 38 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 216 | 38 | 1; | |||||||||||||||||||||||||||
| 217 | __END__ | ||||||||||||||||||||||||||||