Usul/Schema.pm

filter: Statementstmt 29.2% 99 / 339 Branchbran 5.1% 5 / 98 Conditioncond 3.5% 2 / 57 MC/DCmcdc 0.0% 0 / 35 Subroutinesub 47.2% 26 / 55 Podpod 100% 13 / 13 totaltotal 24.2% 145 / 597 CC 83 SCAR 39.6
CC
83
Cov
21%
execute_ddl
182.0
edit_credentials
156.0
create_database
132.0
CRAP 52.2
« Usul/TraitFor/Prompting.pm ↑ Summary Usul/TraitFor/RunningMethods.pm »
1 package Class::Usul::Schema;
2  
3 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use Class::Usul::Constants qw( AS_PARA AS_PASSWORD EXCEPTION_CLASS COMMA
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 FAILED FALSE NUL OK QUOTED_RE SPC TRUE );
7 1 ▶use Class::Usul::Crypt::Util qw( encrypt_for_config );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Class::Usul::Functions qw( distname ensure_class_loaded io throw trim );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Class::Usul::Types qw( ArrayRef Bool HashRef Maybe NonEmptySimpleStr
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 PositiveInt SimpleStr Str );
11 1 ▶use Data::Record;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Unexpected::Functions qw( inflate_placeholders Unspecified );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Class::Usul::Options;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 extends q(Class::Usul::Programs);
18 1 with q(Class::Usul::TraitFor::ConnectInfo);
19  
20 # Attribute constructors
21 0 ▶my $_build_connect_options = sub {
Subroutine _build_connect_options: not called CC=2 SCAR=17.9
22 0 my $self = shift;
23 0 my $copts = { password => NUL, user => NUL, };
24 0 my $text = 'Need the database administrators id and password';
25  
26 0 $self->output( $text, AS_PARA );
27  
28 0 my $prompt = '+Database administrator id';
29 0 ▶ my $user = $self->db_admin_ids->{ lc $self->driver } || NUL;
Condition0%0/2
$self->db_admin_ids->{lc $self->driver} || ''
l!l
00
MC/DC0%0/1
$self->db_admin_ids->{lc $self->driver} || ''
$self->db_admin_ids->{lc $self->driver}
Truth table0%0/2
A
A= $self->db_admin_ids->{lc $self->driver}
Aresult
11
00
30  
31 0 $copts->{user} = $self->get_line( $prompt, $user, TRUE, 0 );
32 0 $prompt = '+Database administrator password';
33 0 $copts->{password} = $self->get_line( $prompt, AS_PASSWORD );
34 0 return $copts;
35 1 };
36  
37 1 ▶my $_build_qdb = sub {
Subroutine _build__qualified_db: called CC=3 SCAR=11.4
38 1 my $self = shift;
39 1 my $cmds = $self->ddl_commands->{ lc $self->driver };
40 1 ▶ my $code = $cmds ? $cmds->{ '-qualify_db' } : undef;
Branch: 1 branch
BranchTrueFalseTotal
$cmds ? : 1 0 1
41  
42 1 ▶ return $code ? $code->( $self, $self->database ) : $self->database;
Branch: 1 branch
BranchTrueFalseTotal
$code ? : 1 0 1
43 1 };
44  
45 3 ▶my $_connect_info = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
46 3 my $self = shift;
47  
48 3 return $self->get_connect_info( $self, { database => $self->database } );
49 1 };
50  
51 2 ▶my $_extract_from_dsn = sub {
Subroutine __ANON__: called CC=4 SCAR=15.5
52 2 my ($self, $field, $dsn) = @_;
53  
54 2 ▶ $self->options and $self->options->{bootstrap} and return;
Branch: 1 branch
BranchTrueFalseTotal
$self->options and $self->options->{"bootstrap"} and return 0 2 2
Condition33%1/3
$self->options and $self->options->{"bootstrap"}
!ll&&!rl&&r
200
MC/DC0%0/2
$self->options and $self->options->{"bootstrap"}
$self->options $self->options->{"bootstrap"}
Truth table33%1/3
A and B
A= $self->optionsB= $self->options->{"bootstrap"}
ABresult
0X0
100
111
55  
56 2 return (map { s{ \A $field [=] }{}mx; $_ }
57 2 ▶ grep { m{ \A $field [=] }mx }
Condition33%1/3
$dsn // $self->dsn
l!l&&r!l&&!r
020
MC/DC0%0/2
$dsn // $self->dsn
$dsn $self->dsn
Truth table33%1/3
A // B
A= $dsnB= $self->dsn
ABresult
1X1
011
000
58 split m{ [;] }mx, $dsn // $self->dsn)[ 0 ];
59 1 };
60  
61 1 ▶my $_qualify_database_path = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
62 1 return $_[ 0 ]->config->datadir->catfile( $_[ 1 ].'.db' )->pathname;
63 1 };
64  
65 1 ▶my $_rebuild_dsn = sub {
Subroutine __ANON__: called CC=3 SCAR=11.3
66 1 my $self = shift;
67 1 my $dsn = 'dbi:'.$self->driver.':database='.$self->_qualified_db;
68  
69 1 ▶ $self->host and $dsn .= ';host='.$self->host;
Branch: 1 branch
BranchTrueFalseTotal
$self->host and $dsn .= ';host=' . $self->host 1 0 1
70 1 ▶ $self->port and $dsn .= ';port='.$self->port;
Branch: 1 branch
BranchTrueFalseTotal
$self->port and $dsn .= ';port=' . $self->port 1 0 1
71  
72 1 return $self->_set_dsn( $dsn );
73 1 };
74  
75 1 ▶my $_rebuild_qdb = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
76 1 my $self = shift; $self->_set__qualified_db( $self->$_build_qdb ); return;
77 1 };
78  
79 # Public attributes
80 1 option 'all' => is => 'ro', isa => Bool, default => FALSE,
81 documentation => 'Perform operation for all possible schema',
82 short => 'a';
83  
84 1 option 'database' => is => 'rwp', isa => NonEmptySimpleStr,
85 documentation => 'The database to connect to',
86 format => 's', lazy => TRUE, required => TRUE,
87 trigger => $_rebuild_qdb;
88  
89 option 'db_admin_accounts' => is => 'ro', isa => HashRef,
90 documentation => 'For each RDBMS the name of the system database',
91 1 ▶ default => sub { { mysql => 'mysql',
Subroutine __ANON__: called CC=1 SCAR=0.0
92 pg => 'postgres',
93 sqlite => NUL, } },
94 1 format => 's%';
95  
96 option 'db_admin_ids' => is => 'ro', isa => HashRef,
97 documentation => 'The default admin user ids for each RDBMS',
98 1 ▶ default => sub { { mysql => 'root',
Subroutine __ANON__: called CC=1 SCAR=0.0
99 pg => 'postgres',
100 sqlite => NUL, } },
101 1 format => 's%';
102  
103 option 'db_attr' => is => 'ro', isa => HashRef,
104 documentation => 'Default database connection attributes',
105 1 ▶ default => sub { { add_drop_table => TRUE,
Subroutine __ANON__: called CC=1 SCAR=0.0
106 no_comments => TRUE,
107 quote_identifiers => TRUE, } },
108 1 format => 's%';
109  
110 1 option 'dry_run' => is => 'ro', isa => Bool, default => FALSE,
111 documentation => 'Prints out commands, do not execute them',
112 short => 'd';
113  
114 1 option 'preversion' => is => 'rwp', isa => Str, default => NUL,
115 documentation => 'Previous schema version',
116 format => 's';
117  
118 option 'rdbms' => is => 'lazy', isa => ArrayRef, autosplit => COMMA,
119 documentation => 'List of supported RDBMSs',
120 0 ▶ default => sub { [ qw( MySQL PostgreSQL SQLite ) ] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
121 1 format => 's@';
122  
123 0 ▶option 'schema_classes' => is => 'lazy', isa => HashRef, default => sub { {} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
124 1 documentation => 'The database schema classes',
125 format => 's%';
126  
127 1 option 'schema_version' => is => 'ro', isa => NonEmptySimpleStr,
128 documentation => 'Current schema version',
129 default => '0.1', format => 's';
130  
131 1 option 'unlink' => is => 'rwp', isa => Bool, default => FALSE,
132 documentation => 'If true remove DDL file before creating new ones';
133  
134 1 option 'yes' => is => 'ro', isa => Bool, default => FALSE,
135 documentation => 'When true flips the defaults for yes/no questions',
136 short => 'y';
137  
138 1 has 'connect_options' => is => 'lazy', isa => HashRef,
139 builder => $_build_connect_options;
140  
141 1 ▶has 'ddl_commands' => is => 'lazy', isa => HashRef, builder => sub { {
Subroutine _build_ddl_commands: called CC=1 SCAR=0.0
142 1 'mysql' => {
143 'create_user' => "create user '[_2]'\@'%' identified by '[_3]';",
144 'create_db' => 'create database [_3] default '
145 . 'character set utf8 collate utf8_unicode_ci;',
146 'drop_db' => 'drop database if exists [_3];',
147 'drop_user' => "drop user '[_2]'\@'%';",
148 'exists_db' => 'select 1 from information_schema.SCHEMATA '
149 . "where SCHEMA_NAME = '[_3]';",
150 'exists_user' => 'select 1 from mysql.user '
151 . "where User = '[_2]' and Host = '%';",
152 'grant_all' => "grant all privileges on [_3].* to '[_2]'\@'%' "
153 . 'with grant option;',
154 '-execute_ddl' => 'mysql -A -h [_1] -u [_2] -p"[_3]" [_5]', },
155 'pg' => {
156 'create_user' => "create role [_2] login password '[_3]';",
157 'create_db' => "create database [_3] owner [_2] encoding 'UTF8';",
158 'drop_db' => 'drop database if exists [_3];',
159 'drop_user' => 'drop user if exists [_2];',
160 'exists_db' => "select 1 from pg_database where datname = '[_3]';",
161 'exists_user' => "select 1 from pg_user where usename = '[_2]';",
162 '-execute_ddl' => 'PGPASSWORD=[_3] '
163 . 'psql -h [_1] -q -t -U [_2] -w -c "[_4]"',
164 '-no_pipe' => TRUE, },
165 'sqlite' => {
166 '-execute_ddl' => "sqlite3 [_6] '[_4]'",
167 '-no_pipe' => TRUE,
168 1 '-qualify_db' => $_qualify_database_path, }, } };
169  
170 has 'driver' => is => 'rwp', isa => NonEmptySimpleStr,
171 1 ▶ builder => sub { (split m{ [:] }mx, $_[ 0 ]->dsn)[ 1 ] },
Subroutine __ANON__: called CC=1 SCAR=0.0
172 1 lazy => TRUE, trigger => $_rebuild_dsn;
173  
174 has 'dsn' => is => 'rwp', isa => NonEmptySimpleStr,
175 1 ▶ builder => sub { $_[ 0 ]->$_connect_info->[ 0 ] },
Subroutine __ANON__: called CC=1 SCAR=0.0
176 1 lazy => TRUE;
177  
178 has 'host' => is => 'rwp', isa => Maybe[SimpleStr],
179 1 ▶ builder => sub { $_[ 0 ]->$_extract_from_dsn( 'host' ) },
Subroutine _build_host: called CC=1 SCAR=0.0
180 1 lazy => TRUE, trigger => $_rebuild_dsn;
181  
182 has 'password' => is => 'rwp', isa => SimpleStr,
183 1 ▶ builder => sub { $_[ 0 ]->$_connect_info->[ 2 ] },
Subroutine _build_password: called CC=1 SCAR=0.0
184 1 lazy => TRUE;
185  
186 has 'port' => is => 'rwp', isa => Maybe[PositiveInt],
187 1 ▶ builder => sub { $_[ 0 ]->$_extract_from_dsn( 'port' ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
188 1 lazy => TRUE, trigger => $_rebuild_dsn;
189  
190 has 'user' => is => 'rwp', isa => SimpleStr,
191 1 ▶ builder => sub { $_[ 0 ]->$_connect_info->[ 1 ] },
Subroutine _build_user: called CC=1 SCAR=0.0
192 1 lazy => TRUE;
193  
194 1 has '_qualified_db' => is => 'rwp', isa => NonEmptySimpleStr,
195 builder => $_build_qdb, lazy => TRUE, trigger => $_rebuild_dsn;
196  
197 # Private functions
198 0 ▶my $_inflate = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
199 0 return inflate_placeholders [ 'undef', 'null', TRUE ], @_;
200 1 };
201  
202 0 ▶my $_unquote = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
203 0 local $_ = $_[ 0 ]; s{ \A [\'\"] }{}mx; s{ [\'\"] \z }{}mx; return $_;
204 1 };
205  
206 # Private methods
207 0 ▶my $_connect_attr = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
208 0 return { %{ $_[ 0 ]->$_connect_info->[ 3 ] }, %{ $_[ 0 ]->db_attr } };
209 1 };
210  
211 0 ▶my $_create_ddl = sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
212 0 my ($self, $schema_class, $dir) = @_;
213  
214 0 my $version = $self->schema_version;
215 0 my $schema = $schema_class->connect
216 ( $self->dsn, $self->user, $self->password, $self->$_connect_attr );
217  
218 0 ▶ if ($self->unlink) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->unlink) 0 0 0
219 0 for my $path ($self->ddl_paths( $schema, $version, $dir )) {
220 0 ▶ $path->is_file and $path->unlink;
Branch: 1 branch
BranchTrueFalseTotal
$path->is_file and $path->unlink 0 0 0
221 }
222 }
223  
224 $schema->create_ddl_dir
225 0 ( $self->rdbms, $version, $dir, $self->preversion, $self->$_connect_attr);
226 0 return;
227 1 };
228  
229 0 ▶my $_list_population_classes = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
230 0 my ($self, $schema_class, $dir) = @_; my $res = [];
231  
232 0 my $dist = distname $schema_class;
233 0 my $extn = $self->config->extension;
234 0 my $re = qr{ \A $dist [-] \d+ [-] (.*) \Q$extn\E \z }mx;
235 0 ▶ my $io = io( $dir )->filter( sub { $_->filename =~ $re } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
236  
237 0 for my $path ($io->all_files) {
238 0 my ($class) = $path->filename =~ $re; push @{ $res }, [ $class, $path ];
239 }
240  
241 0 return $res;
242 1 };
243  
244 0 ▶my $_deploy_and_populate = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
245 0 my ($self, $schema_class, $dir) = @_; my $res; my $schema;
246  
247 0 ▶ if ($self->dry_run) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->dry_run) { } 0 0 0
248 0 $self->output( "Would deploy schema ${schema_class} from ${dir}" );
249 0 $self->dumper( map { $_->basename }
250 0 ▶ $dir->filter( sub { m{ \.sql \z }mx } )->all_files );
Subroutine __ANON__: not called CC=1 SCAR=6.9
251 }
252 else {
253 0 $self->info( "Deploying schema ${schema_class} and populating" );
254 0 $schema = $schema_class->connect
255 ( $self->dsn, $self->user, $self->password, $self->$_connect_attr );
256 0 $schema->storage->ensure_connected;
257 0 $schema->deploy( $self->$_connect_attr, $dir );
258 }
259  
260 0 my $split = Data::Record->new( { split => COMMA, unless => QUOTED_RE, } );
261  
262 0 for my $tuple (@{ $self->$_list_population_classes( $schema_class, $dir ) }){
263 $res->{ $tuple->[ 0 ] }
264 0 = $self->populate_class( $schema, $split, @{ $tuple } );
265 }
266  
267 0 return $res;
268 1 };
269  
270 0 ▶my $_test_for_existance = sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
271 0 my ($self, $copts, $test, @args) = @_;
272  
273 0 ▶ $test or return FALSE; $test = $_inflate->( $test, @args );
Branch: 1 branch
BranchTrueFalseTotal
$test or return 0 0 0 0
274  
275 0 my $r = $self->execute_ddl( $test, $copts, { out => 'buffer' } );
276  
277 0 ▶ $self->debug and $self->dumper( $r );
Branch: 1 branch
BranchTrueFalseTotal
$self->debug and $self->dumper($r) 0 0 0
278  
279 0 ▶ return $r && $r->out =~ m{ 1 }mx ? TRUE : FALSE;
Branch: 1 branch
BranchTrueFalseTotal
$r && $r->out =~ / 1 /mx ? : 0 0 0
Condition0%0/3
$r and $r->out =~ / 1 /mx
!ll&&!rl&&r
000
MC/DC0%0/2
$r and $r->out =~ / 1 /mx
$r $r->out =~ / 1 /mx
Truth table0%0/3
A and B
A= $rB= $r->out =~ / 1 /mx
ABresult
0X0
100
111
280 1 };
281  
282 # Public methods
283 0 ▶sub create_database : method {
Subroutine create_database: not called CC=11 SCAR=48.8
Pod documented
284 0 my $self = shift;
285 0 my $driver = $self->driver;
286 0 my $cmds = $self->ddl_commands->{ lc $driver };
287 0 ▶ my @dbs = $self->all ? keys %{ $self->schema_classes } : $self->database;
Branch: 1 branch
BranchTrueFalseTotal
$self->all ? : 0 0 0
288 0 my $copts = $self->connect_options;
289  
290 0 for my $db (@dbs) {
291 0 ▶ my $ddl = $cmds->{create_db} or return FAILED;
Branch: 1 branch
BranchTrueFalseTotal
my $ddl = $cmds->{"create_db"} or return 1 0 0 0
292 0 my @args = ($self->host, $self->user, $db);
293  
294 0 ▶ my $r; not $self->$_test_for_existance( $copts, $cmds->{exists_db}, @args)
Branch: 1 branch
BranchTrueFalseTotal
not $self->$_test_for_existance($copts, $cmds->{"exists_db"}, @args) and $self->info("Creating $driver database $db") and $r = $self->execute_ddl(&$_inflate($ddl, @args), $copts) 0 0 0
Condition0%0/3
not $self->$_test_for_existance($copts, $cmds->{"exists_db"}, @args) and $self->info("Creating $driver database $db")
!ll&&!rl&&r
000
MC/DC0%0/2
not $self->$_test_for_existance($copts, $cmds->{"exists_db"}, @args) and $self->info("Creating $driver database $db")
not $self->$_test_for_existance($copts, $cmds->{"exists_db"}, @args) $self->info("Creating $driver database $db")
Truth table0%0/3
not(A) and B
A= not $self->$_test_for_existance($copts, $cmds->{"exists_db"}, @args)B= $self->info("Creating $driver database $db")
ABresult
0X0
100
111
295 and $self->info( "Creating ${driver} database ${db}" )
296 and $r = $self->execute_ddl( $_inflate->( $ddl, @args ), $copts );
297  
298 0 ▶ $self->debug and $r and $self->dumper( $r ); $r = FALSE;
Branch: 1 branch
BranchTrueFalseTotal
$self->debug and $r and $self->dumper($r) 0 0 0
Condition0%0/3
$self->debug and $r
!ll&&!rl&&r
000
MC/DC0%0/2
$self->debug and $r
$self->debug $r
Truth table0%0/3
A and B
A= $self->debugB= $r
ABresult
0X0
100
111
299  
300 $ddl = $cmds->{grant_all}
301 0 ▶ and $r = $self->execute_ddl( $_inflate->( $ddl, @args ), $copts );
Branch: 1 branch
BranchTrueFalseTotal
$ddl = $cmds->{"grant_all"} and $r = $self->execute_ddl(&$_inflate($ddl, @args), $copts) 0 0 0
302  
303 0 ▶ $self->debug and $r and $self->dumper( $r );
Branch: 1 branch
BranchTrueFalseTotal
$self->debug and $r and $self->dumper($r) 0 0 0
Condition0%0/3
$self->debug and $r
!ll&&!rl&&r
000
MC/DC0%0/2
$self->debug and $r
$self->debug $r
Truth table0%0/3
A and B
A= $self->debugB= $r
ABresult
0X0
100
111
304 }
305  
306 0 return OK;
307 }
308  
309 0 ▶sub create_ddl : method {
Subroutine create_ddl: not called CC=4 SCAR=30.0
Pod documented
310 0 my $self = shift; $self->info( 'Creating DDL for '.$self->dsn );
311  
312 0 for my $schema_class (values %{ $self->schema_classes }) {
313 0 ensure_class_loaded $schema_class;
314 0 ▶ $self->dry_run and $self->output( "Would create ${schema_class}" )
Branch: 1 branch
BranchTrueFalseTotal
$self->dry_run and $self->output("Would create $schema_class") and next 0 0 0
Condition0%0/3
$self->dry_run and $self->output("Would create $schema_class")
!ll&&!rl&&r
000
MC/DC0%0/2
$self->dry_run and $self->output("Would create $schema_class")
$self->dry_run $self->output("Would create $schema_class")
Truth table0%0/3
A and B
A= $self->dry_runB= $self->output("Would create $schema_class")
ABresult
0X0
100
111
315 and next;
316 0 $self->$_create_ddl( $schema_class, $self->config->sharedir );
317 }
318  
319 0 return OK;
320 }
321  
322 0 ▶sub create_schema : method { # Create databases and edit credentials
Subroutine create_schema: not called CC=2 SCAR=17.9
Pod documented
323 0 my $self = shift;
324 0 my $default = $self->yes;
325 0 my $text = 'Schema creation requires a database, id and password. '
326 . 'For Postgres the driver is Pg and the port 5432. For '
327 . 'MySQL the driver is mysql and the port 3306';
328  
329 0 $self->output( $text, AS_PARA );
330 0 ▶ $self->yorn( '+Create database schema', $default, TRUE, 0 ) or return OK;
Branch: 1 branch
BranchTrueFalseTotal
$self->yorn("+Create database schema", $default, 1, 0) or return 0 0 0 0
331 0 $self->edit_credentials;
332 0 $self->connect_options;
333 0 $self->drop_database;
334 0 $self->drop_user;
335 0 $self->create_user;
336 0 $self->create_database;
337 0 $self->deploy_and_populate;
338 0 return OK;
339 }
340  
341 0 ▶sub create_user : method {
Subroutine create_user: not called CC=6 SCAR=37.4
Pod documented
342 0 my $self = shift;
343 0 my $user = $self->user;
344 0 my $driver = $self->driver;
345 0 my $cmds = $self->ddl_commands->{ lc $driver };
346 0 ▶ my $ddl = $cmds->{create_user} or return FAILED;
Branch: 1 branch
BranchTrueFalseTotal
my $ddl = $cmds->{"create_user"} or return 1 0 0 0
347 0 my @args = ($self->host, $user, $self->password);
348 0 my $copts = $self->connect_options;
349  
350 0 ▶ my $r; not $self->$_test_for_existance( $copts, $cmds->{exists_user}, @args )
Branch: 1 branch
BranchTrueFalseTotal
not $self->$_test_for_existance($copts, $cmds->{"exists_user"}, @args) and $self->info("Creating $driver user $user") and $r = $self->execute_ddl(&$_inflate($ddl, @args), $copts) 0 0 0
Condition0%0/3
not $self->$_test_for_existance($copts, $cmds->{"exists_user"}, @args) and $self->info("Creating $driver user $user")
!ll&&!rl&&r
000
MC/DC0%0/2
not $self->$_test_for_existance($copts, $cmds->{"exists_user"}, @args) and $self->info("Creating $driver user $user")
not $self->$_test_for_existance($copts, $cmds->{"exists_user"}, @args) $self->info("Creating $driver user $user")
Truth table0%0/3
not(A) and B
A= not $self->$_test_for_existance($copts, $cmds->{"exists_user"}, @args)B= $self->info("Creating $driver user $user")
ABresult
0X0
100
111
351 and $self->info( "Creating ${driver} user ${user}" )
352 and $r = $self->execute_ddl( $_inflate->( $ddl, @args ), $copts );
353  
354 0 ▶ $self->debug and $r and $self->dumper( $r );
Branch: 1 branch
BranchTrueFalseTotal
$self->debug and $r and $self->dumper($r) 0 0 0
Condition0%0/3
$self->debug and $r
!ll&&!rl&&r
000
MC/DC0%0/2
$self->debug and $r
$self->debug $r
Truth table0%0/3
A and B
A= $self->debugB= $r
ABresult
0X0
100
111
355 0 return OK;
356 }
357  
358 0 ▶sub ddl_paths {
Subroutine ddl_paths: not called CC=2 SCAR=17.9
Pod documented
359 0 my ($self, $schema, $version, $dir) = @_; my @paths = ();
360  
361 0 for my $rdb (@{ $self->rdbms }) {
362 0 push @paths, io( $schema->ddl_filename( $rdb, $version, $dir ) );
363 }
364  
365 0 return @paths;
366 }
367  
368 0 ▶sub deploy_and_populate : method {
Subroutine deploy_and_populate: not called CC=5 SCAR=34.0
Pod documented
369 0 my $self = shift;
370  
371 0 my @classes = $self->all ? values %{ $self->schema_classes }
372 0 ▶ : $self->schema_classes->{ $self->database };
Branch: 1 branch
BranchTrueFalseTotal
$self->all ? : 0 0 0
373  
374 0 for my $schema_class (@classes) {
375 0 $self->info( "Deploy and populate ${schema_class}" );
376 0 ▶ $self->yorn( '+Continue', $self->yes, TRUE, 0 ) or next;
Branch: 1 branch
BranchTrueFalseTotal
$self->yorn("+Continue", $self->yes, 1, 0) or next 0 0 0
377 0 ensure_class_loaded $schema_class;
378 0 ▶ $schema_class->can( 'config' ) and $schema_class->config( $self->config );
Branch: 1 branch
BranchTrueFalseTotal
$schema_class->can("config") and $schema_class->config($self->config) 0 0 0
379 0 $self->$_deploy_and_populate( $schema_class, $self->config->sharedir );
380 }
381  
382 0 return OK;
383 }
384  
385 0 ▶sub deploy_file { # Deprecated
Subroutine deploy_file: not called CC=1 SCAR=6.9
Pod documented
386 0 my $self = shift; return $self->populate_class( @_ );
387 };
388  
389 0 ▶sub drop_database : method {
Subroutine drop_database: not called CC=6 SCAR=37.4
Pod documented
390 0 my $self = shift;
391 0 my $driver = $self->driver;
392 0 my $cmds = $self->ddl_commands->{ lc $driver };
393 0 ▶ my @dbs = $self->all ? keys %{ $self->schema_classes } : $self->database;
Branch: 1 branch
BranchTrueFalseTotal
$self->all ? : 0 0 0
394 0 my $copts = $self->connect_options;
395  
396 0 ▶ $self->yorn( '+Really drop the database', $self->yes, TRUE, 0 ) or return OK;
Branch: 1 branch
BranchTrueFalseTotal
$self->yorn("+Really drop the database", $self->yes, 1, 0) or return 0 0 0 0
397  
398 0 for my $db (@dbs) {
399 0 ▶ my $ddl = $cmds->{ 'drop_db' } or return FAILED;
Branch: 1 branch
BranchTrueFalseTotal
my $ddl = $cmds->{"drop_db"} or return 1 0 0 0
400 0 my @args = ($self->host, $self->user, $db);
401  
402 0 $self->info( "Droping ${driver} database ${db}" );
403  
404 0 my $r = $self->execute_ddl( $_inflate->( $ddl, @args ), $copts );
405  
406 0 ▶ $self->debug and $self->dumper( $r );
Branch: 1 branch
BranchTrueFalseTotal
$self->debug and $self->dumper($r) 0 0 0
407 }
408  
409 0 return OK;
410 }
411  
412 0 ▶sub drop_user : method {
Subroutine drop_user: not called CC=5 SCAR=34.0
Pod documented
413 0 my $self = shift;
414 0 my $user = $self->user;
415 0 my $driver = $self->driver;
416 0 my $cmds = $self->ddl_commands->{ lc $driver };
417 0 ▶ my $ddl = $cmds->{ 'drop_user' } or return FAILED;
Branch: 1 branch
BranchTrueFalseTotal
my $ddl = $cmds->{"drop_user"} or return 1 0 0 0
418 0 my @args = ($self->host, $user, $self->database);
419 0 my $cmd_opts = { expected_rv => 1, out => 'buffer' };
420 0 my $copts = $self->connect_options;
421  
422 0 ▶ $self->yorn( '+Really drop the user', $self->yes, TRUE, 0 ) or return OK;
Branch: 1 branch
BranchTrueFalseTotal
$self->yorn("+Really drop the user", $self->yes, 1, 0) or return 0 0 0 0
423 0 ▶ $self->$_test_for_existance( $copts, $cmds->{exists_user}, @args )
Branch: 1 branch
BranchTrueFalseTotal
$self->$_test_for_existance($copts, $cmds->{"exists_user"}, @args) or return 0 0 0 0
424 or return OK;
425 0 $self->info( "Droping ${driver} user ${user}" );
426  
427 0 my $r = $self->execute_ddl( $_inflate->( $ddl, @args ), $copts, $cmd_opts );
428  
429 0 ▶ $self->debug and $self->dumper( $r );
Branch: 1 branch
BranchTrueFalseTotal
$self->debug and $self->dumper($r) 0 0 0
430 0 return OK;
431 }
432  
433 0 ▶sub edit_credentials : method {
Subroutine edit_credentials: not called CC=12 SCAR=50.5
Pod documented
434 0 my $self = shift;
435 0 my $self_cfg = $self->config;
436 0 my $db = $self->database;
437 0 my $bootstrap = $self->options->{bootstrap};
438 0 ▶ my $cfg_data = $bootstrap ? {} : $self->load_config_data( $self_cfg, $db );
Branch: 1 branch
BranchTrueFalseTotal
$bootstrap ? : 0 0 0
439 0 ▶ my $copts = $bootstrap ? {}
Branch: 1 branch
BranchTrueFalseTotal
$bootstrap ? : 0 0 0
440 : $self->extract_creds_from( $self_cfg, $db, $cfg_data );
441 0 my $stored_pw = $copts->{password};
442 0 my $prompts = { name => 'Database name',
443 driver => 'Driver type',
444 host => 'Host name',
445 port => 'Port number',
446 user => 'User name',
447 password => 'User password' };
448 0 my $defaults = { name => $db,
449 driver => '_field',
450 host => 'localhost',
451 port => '_field',
452 user => '_field',
453 password => NUL };
454  
455 0 for my $field (qw( name driver host port user password )) {
456 0 my $setter = "_set_${field}";
457 0 my $prompt = '+'.$prompts->{ $field };
458 0 ▶ my $is_pw = $field eq 'password' ? TRUE : FALSE;
Branch: 1 branch
BranchTrueFalseTotal
$field eq 'password' ? : 0 0 0
459 my $value = $defaults->{ $field } ne '_field' ? $defaults->{ $field }
460 0 ▶ : $copts->{ $field };
Branch: 1 branch
BranchTrueFalseTotal
$defaults->{$field} ne '_field' ? : 0 0 0
461  
462 0 $value = $self->get_line( $prompt, $value, TRUE, 0, FALSE, $is_pw );
463 0 ▶ $field ne 'name' and $self->$setter( $value // NUL );
Branch: 1 branch
BranchTrueFalseTotal
$field ne "name" and $self->$setter($value // "") 0 0 0
Condition0%0/2
$value // ""
l!l
00
MC/DC0%0/1
$value // ""
$value
Truth table0%0/2
A
A= $value
Aresult
11
00
464 0 ▶ $is_pw and $value = encrypt_for_config $self_cfg, $value, $stored_pw;
Branch: 1 branch
BranchTrueFalseTotal
$is_pw and $value = &encrypt_for_config($self_cfg, $value, $stored_pw) 0 0 0
465 0 ▶ $copts->{ $field } = $value // NUL;
Condition0%0/2
$value // ""
l!l
00
MC/DC0%0/1
$value // ""
$value
Truth table0%0/2
A
A= $value
Aresult
11
00
466 }
467  
468 0 $cfg_data->{credentials}->{ $copts->{name} } = $copts;
469 0 ▶ $self->dry_run and $self->dumper( $cfg_data ) and return OK;
Branch: 1 branch
BranchTrueFalseTotal
$self->dry_run and $self->dumper($cfg_data) and return 0 0 0 0
Condition0%0/3
$self->dry_run and $self->dumper($cfg_data)
!ll&&!rl&&r
000
MC/DC0%0/2
$self->dry_run and $self->dumper($cfg_data)
$self->dry_run $self->dumper($cfg_data)
Truth table0%0/3
A and B
A= $self->dry_runB= $self->dumper($cfg_data)
ABresult
0X0
100
111
470 0 $self->dump_config_data( $self_cfg, $copts->{name}, $cfg_data );
471 0 return OK;
472 }
473  
474 0 ▶sub execute_ddl {
Subroutine execute_ddl: not called CC=13 SCAR=52.0
Pod documented
475 0 my ($self, $ddl, $connect_opts, $cmd_opts) = @_;
476  
477 0 ▶ my $drvr = $connect_opts->{driver } // lc $self->driver;
Condition0%0/2
$connect_opts->{"driver"} // lc $self->driver
l!l
00
MC/DC0%0/1
$connect_opts->{"driver"} // lc $self->driver
$connect_opts->{"driver"}
Truth table0%0/2
A
A= $connect_opts->{"driver"}
Aresult
11
00
478 0 ▶ my $db = $connect_opts->{database} // $self->db_admin_accounts->{ $drvr };
Condition0%0/3
$connect_opts->{"database"} // $self->db_admin_accounts->{$drvr}
l!l&&r!l&&!r
000
MC/DC0%0/2
$connect_opts->{"database"} // $self->db_admin_accounts->{$drvr}
$connect_opts->{"database"} $self->db_admin_accounts->{$drvr}
Truth table0%0/3
A // B
A= $connect_opts->{"database"}B= $self->db_admin_accounts->{$drvr}
ABresult
1X1
011
000
479 0 ▶ my $host = $connect_opts->{host } // $self->host || 'localhost';
Condition0%0/5
($connect_opts->{'host'} // $self->host) || 'localhost'
l!l
00
$connect_opts->{"host"} // $self->host
l!l&&r!l&&!r
000
MC/DC0%0/2
($connect_opts->{'host'} // $self->host) || 'localhost'
$connect_opts->{"host"} $self->host
Truth table0%0/3
A // B
A= $connect_opts->{"host"}B= $self->host
ABresult
1X1
011
000
480 0 ▶ my $user = $connect_opts->{user } // $self->db_admin_ids->{ $drvr };
Condition0%0/3
$connect_opts->{"user"} // $self->db_admin_ids->{$drvr}
l!l&&r!l&&!r
000
MC/DC0%0/2
$connect_opts->{"user"} // $self->db_admin_ids->{$drvr}
$connect_opts->{"user"} $self->db_admin_ids->{$drvr}
Truth table0%0/3
A // B
A= $connect_opts->{"user"}B= $self->db_admin_ids->{$drvr}
ABresult
1X1
011
000
481 0 my $pass = $connect_opts->{password};
482 0 ▶ my $cmds = $self->ddl_commands->{ $drvr }
Branch: 1 branch
BranchTrueFalseTotal
my $cmds = $self->ddl_commands->{$drvr} or $self->fatal("Driver [_1] unknown", {"args", [$drvr]}) 0 0 0
483 or $self->fatal( 'Driver [_1] unknown', { args => [ $drvr ] } );
484 0 my $code = $cmds->{ '-qualify_db' };
485 0 ▶ my $qdb = $code ? $code->( $self, $db ) : $db;
Branch: 1 branch
BranchTrueFalseTotal
$code ? : 0 0 0
486 0 my $cmd = $cmds->{ '-execute_ddl' };
487  
488 0 $cmd = $_inflate->( $cmd, $host, $user, $pass, $ddl, $db, $qdb );
489 0 ▶ $cmds->{ '-no_pipe' } or $cmd = "echo \"${ddl}\" | ${cmd}";
Branch: 1 branch
BranchTrueFalseTotal
$cmds->{"-no_pipe"} or $cmd = qq[echo "$ddl" | $cmd] 0 0 0
490 0 ▶ $self->dry_run and $self->output( $cmd ) and return;
Branch: 1 branch
BranchTrueFalseTotal
$self->dry_run and $self->output($cmd) and return 0 0 0
Condition0%0/3
$self->dry_run and $self->output($cmd)
!ll&&!rl&&r
000
MC/DC0%0/2
$self->dry_run and $self->output($cmd)
$self->dry_run $self->output($cmd)
Truth table0%0/3
A and B
A= $self->dry_runB= $self->output($cmd)
ABresult
0X0
100
111
491 0 ▶ $self->verbose and $self->output( $cmd );
Branch: 1 branch
BranchTrueFalseTotal
$self->verbose and $self->output($cmd) 0 0 0
492  
493 0 ▶ return $self->run_cmd( $cmd, { out => 'stdout', %{ $cmd_opts // {} } } );
Condition0%0/2
$cmd_opts // {}
l!l
00
MC/DC0%0/1
$cmd_opts // {}
$cmd_opts
Truth table0%0/2
A
A= $cmd_opts
Aresult
11
00
494 }
495  
496 0 ▶sub populate_class {
Subroutine populate_class: not called CC=2 SCAR=17.9
Pod documented
497 0 my ($self, $schema, $split, $class, $path) = @_; my $res;
498  
499 0 ▶ if ($class) { $self->output( "Populating ${class}" ) }
Branch: 1 branch
BranchTrueFalseTotal
if ($class) { } 0 0 0
500 0 else { $self->fatal ( 'No class in [_1]', $path->filename ) }
501  
502 0 my $data = $self->file->dataclass_schema->load( $path );
503 0 my $flds = [ split SPC, $data->{fields} ];
504 0 my @rows = map { [ map { $_unquote->( trim $_ ) } $split->records( $_ ) ] }
505 0 @{ $data->{rows} };
506  
507 0 ▶ try {
Subroutine __ANON__: not called CC=2 SCAR=17.9
508 0 ▶ if ($self->dry_run) { $self->dumper( $flds, \@rows ) }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->dry_run) { } 0 0 0
509 0 else { $res = $schema->populate( $class, [ $flds, @rows ] ) }
510 }
511 0 ▶ catch {
Subroutine __ANON__: not called CC=4 SCAR=30.0
512 0 ▶ if ($_->can( 'class' ) and $_->class eq 'ValidationErrors') {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->can("class") and $_->class eq "ValidationErrors") 0 0 0
Condition0%0/3
$_->can("class") and $_->class eq "ValidationErrors"
!ll&&!rl&&r
000
MC/DC0%0/2
$_->can("class") and $_->class eq "ValidationErrors"
$_->can("class") $_->class eq "ValidationErrors"
Truth table0%0/3
A and B
A= $_->can("class")B= $_->class eq "ValidationErrors"
ABresult
0X0
100
111
513 0 $self->warning( "${_}" ) for (@{ $_->args });
514 }
515  
516 0 throw $_;
517 0 };
518  
519 0 return $res;
520 }
521  
522 0 ▶sub repopulate_class : method {
Subroutine repopulate_class: not called CC=4 SCAR=30.0
Pod documented
523 0 my $self = shift;
524 0 my $dir = $self->config->sharedir;
525 0 ▶ my $class = $self->next_argv or throw Unspecified, [ 'class name' ];
Branch: 1 branch
BranchTrueFalseTotal
my $class = $self->next_argv or &throw(Unspecified(), ["class name"]) 0 0 0
526 0 my $schema_class = $self->schema_classes->{ $self->database };
527 0 my $tuples = $self->$_list_population_classes( $schema_class, $dir );
528 0 my $split = Data::Record->new( { split => COMMA, unless => QUOTED_RE, } );
529  
530 0 ensure_class_loaded $schema_class;
531  
532 0 my $schema = $schema_class->connect
533 ( $self->dsn, $self->user, $self->password, $self->$_connect_attr );
534  
535 0 for my $tuple (grep { $_->[ 0 ] =~ m{ \A $class \z }imx } @{ $tuples }) {
536 0 my $data = $self->file->dataclass_schema->load( $tuple->[ 1 ] );
537 0 my $flds = [ split SPC, $data->{fields} ];
538 0 my @rows = map { [ map { $_unquote->( trim $_ ) }
539 0 $split->records( $_ ) ] } @{ $data->{rows} };
540 0 my $rs = $schema->resultset( $tuple->[ 0 ] );
541  
542 0 for my $row (@rows) {
543 0 my $name = $row->[ 0 ]; my $type = $row->[ 1 ];
544  
545 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
546 0 $rs->create( { name => $name, type_class => $type } );
547 0 $self->info( "Create a ${type} type called ${name}" );
548 }
549 0 ▶ catch {};
Subroutine __ANON__: not called CC=1 SCAR=6.9
550 }
551 }
552  
553 0 return OK;
554 }
555  
556 1 1;
557  
558 __END__
« Usul/TraitFor/Prompting.pm ↑ Summary Usul/TraitFor/RunningMethods.pm »