blib/lib/DBIO/MySQL/Deploy.pm

filter: Statementstmt 61.2% 76 / 124 Branchbran 53.1% 17 / 32 Conditioncond 61.9% 13 / 21 MC/DCmcdc 60.0% 6 / 10 Subroutinesub 76.0% 19 / 25 Podpod 100% 1 / 1 totaltotal 61.9% 132 / 213 CC 29 SCAR 29.9
CC
29
Cov
60%
_introspect_current
90.0
_refuse_unfillable
13.1
_build_target_model
6.0
CRAP 19.9
« blib/lib/DBIO/MySQL/Storage.pm ↑ Summary blib/lib/DBIO/MySQL/Diff/Table.pm »
1 package DBIO::MySQL::Deploy;
2 # ABSTRACT: Deploy and upgrade MySQL/MariaDB schemas via test-deploy-and-compare
3  
4 39 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 39 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 39 our $VERSION = '0.900007';
8  
9 39 ▶use base 'DBIO::Deploy::Base::TempDatabase';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 39 ▶use DBI ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 39 ▶use DBIO::Exception ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 39 ▶use DBIO::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 39 ▶use DBIO::MySQL::DDL ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 39 ▶use DBIO::MySQL::Identifier ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 39 ▶use DBIO::MySQL::Introspect ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 39 ▶use DBIO::MySQL::Diff ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 # The three class-name hooks DBIO::Deploy::Base needs.
20  
21 4 ▶sub _ddl_class { 'DBIO::MySQL::DDL' }
Subroutine _ddl_class: called CC=1 SCAR=0.0
22 0 ▶sub _introspect_class { 'DBIO::MySQL::Introspect' }
Subroutine _introspect_class: not called CC=1 SCAR=6.9
23 0 ▶sub _diff_class { 'DBIO::MySQL::Diff' }
Subroutine _diff_class: not called CC=1 SCAR=6.9
24  
25  
26  
27 2 ▶sub install {
Subroutine install: called CC=3 SCAR=11.0
Pod documented
28 2 my ($self) = @_;
29 2 $self->_refuse_missing_databases($self->_dbh);
30 2 ▶ return $self->SUPER::install unless $self->{add_drop_table};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"add_drop_table"} 1 1 2
31  
32 1 my $dbh = $self->_dbh;
33 1 $dbh->do('SET FOREIGN_KEY_CHECKS = 0');
34 1 my $ok = eval { $self->_execute_ddl($dbh, $self->_ddl_class->drop_ddl($self->schema)); 1 };
35 1 my $err = $@;
36 1 $dbh->do('SET FOREIGN_KEY_CHECKS = 1');
37 1 ▶ die $err unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 1 1
38  
39 1 return $self->SUPER::install;
40 }
41  
42  
43 4 ▶sub _check_before_apply {
Subroutine _check_before_apply: called CC=1 SCAR=0.0
44 4 my ($self, $diff) = @_;
45 4 $self->_refuse_missing_databases($self->_dbh);
46 4 $self->_refuse_unfillable($diff, $self->_dbh);
47 3 return $self->SUPER::_check_before_apply($diff);
48 }
49  
50  
51 12 ▶sub _refuse_unfillable {
Subroutine _refuse_unfillable: called CC=13 SCAR=25.7
52 12 my ($self, $diff, $dbh) = @_;
53  
54 5 ▶ my $has_rows = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
55 5 my ($table, $where, $database) = @_;
56 5 ▶ return scalar $dbh->selectrow_array(sprintf 'SELECT 1 FROM %s%s LIMIT 1',
Condition100%2/2
$where // ""
l!l
23
MC/DC100%1/1
$where // ""
$where
Truth table100%2/2
A
A= $where
Aresult
11
00
57 DBIO::MySQL::Identifier::quote_table_in($database, $table), $where // '');
58 12 };
59  
60 12 for my $op (@{ $diff->operations }) {
61 12 ▶ next unless $op->isa('DBIO::MySQL::Diff::Column');
Branch: 1 branch
BranchTrueFalseTotal
unless $op->isa("DBIO::MySQL::Diff::Column") 0 12 12
62 12 my ($table, $column, $new) = ($op->table_name, $op->column_name, $op->new_info);
63  
64 12 ▶ if ($op->action eq 'add') {
Branch: 2 branches
BranchTrueFalseTotal
if ($op->action eq 'add') { } 7 5 12
elsif ($op->action eq 'modify') { } 5 0 5
65 next unless $new->{not_null} && !defined $new->{default_value}
66 7 ▶ && !$new->{is_auto_increment};
Branch: 1 branch
BranchTrueFalseTotal
unless $new->{"not_null"} and not defined $new->{"default_value"} and not $new->{"is_auto_increment"} 4 3 7
Condition100%6/6
$new->{"not_null"} and not defined $new->{"default_value"} and not $new->{"is_auto_increment"}
!ll&&!rl&&r
313
$new->{"not_null"} and not defined $new->{"default_value"}
!ll&&!rl&&r
214
MC/DC100%3/3
$new->{"not_null"} and not defined $new->{"default_value"} and not $new->{"is_auto_increment"}
$new->{"not_null"} not defined $new->{"default_value"} not $new->{"is_auto_increment"}
Truth table100%4/4
A and not(B) and not(C)
A= $new->{"not_null"}B= not defined $new->{"default_value"}C= not $new->{"is_auto_increment"}
ABCresult
0XX0
10X0
1100
1111
67 3 ▶ next unless $has_rows->($table, undef, $op->database);
Branch: 1 branch
BranchTrueFalseTotal
unless &$has_rows($table, undef, $op->database) 1 2 3
68 2 DBIO::Exception->throw(sprintf
69 'DBIO::MySQL refuses to add the column %s.%s: it is NOT NULL without a '
70 . 'default, and table %s holds rows that would have no value for it',
71 $table, $column, $table);
72 }
73 elsif ($op->action eq 'modify') {
74 5 ▶ next unless $new->{not_null} && !($op->old_info && $op->old_info->{not_null});
Branch: 1 branch
BranchTrueFalseTotal
unless $new->{"not_null"} and not $op->old_info && $op->old_info->{"not_null"} 3 2 5
Condition83%5/6
$new->{"not_null"} and not $op->old_info && $op->old_info->{"not_null"}
!ll&&!rl&&r
212
$op->old_info && $op->old_info->{'not_null'}
!ll&&!rl&&r
021
MC/DC66%2/3
$new->{"not_null"} and not $op->old_info && $op->old_info->{"not_null"}
$new->{"not_null"} $op->old_info $op->old_info->{'not_null'}
Truth table75%3/4
A and not(B && C)
A= $new->{"not_null"}B= $op->old_infoC= $op->old_info->{'not_null'}
ABCresult
0XX0
1110
10X1
1101
75 2 ▶ next unless $has_rows->($table,
Branch: 1 branch
BranchTrueFalseTotal
unless &$has_rows($table, ' WHERE ' . DBIO::MySQL::Identifier::quote_ident($column) . ' IS NULL', $op->database) 1 1 2
76 ' WHERE ' . DBIO::MySQL::Identifier::quote_ident($column) . ' IS NULL', $op->database);
77 1 DBIO::Exception->throw(sprintf
78 'DBIO::MySQL refuses to make the column %s.%s NOT NULL: table %s holds '
79 . 'rows where it is NULL', $table, $column, $table);
80 }
81 }
82 9 return;
83 }
84  
85  
86 2 ▶sub _refuse_missing_databases {
Subroutine _refuse_missing_databases: called CC=3 SCAR=17.3
87 2 my ($self, $dbh) = @_;
88 2 for my $pair (DBIO::MySQL::DDL->qualified_tables($self->schema)) {
89 0 my ($table, $database) = @$pair;
90 0 my ($found) = $dbh->selectrow_array(
91 'SELECT COUNT(*) FROM information_schema.schemata WHERE schema_name = ?',
92 undef, $database);
93 0 ▶ next if $found;
Branch: 1 branch
BranchTrueFalseTotal
if $found 0 0 0
94 0 DBIO::Exception->throw(sprintf
95 'DBIO::MySQL refuses to create the table %s: the database %s does not '
96 . 'exist, and the driver does not create databases', $table, $database);
97 }
98 2 return;
99 }
100  
101 # What the install writes into the live database is the whole schema. The
102 # temporary database of a diff gets the tables of one database: the current one
103 # (those named without a database), or one of the other databases, under the
104 # names they have there (karr k122).
105 2 ▶sub _install_ddl {
Subroutine _install_ddl: called CC=3 SCAR=12.0
106 2 my ($self) = @_;
107 return $self->_ddl_class->install_ddl($self->schema,
108 defined $self->{_only_database} ? { database => $self->{_only_database} }
109 2 ▶ : $self->{_local_only} ? { local_only => 1 }
Branch: 2 branches
BranchTrueFalseTotal
defined $self->{'_only_database'} ? : 0 2 2
$self->{'_local_only'} ? : 0 2 2
110 : ());
111 }
112  
113 0 ▶sub _deploy_and_introspect_temp {
Subroutine _deploy_and_introspect_temp: not called CC=1 SCAR=6.9
114 0 my $self = shift;
115 0 local $self->{_local_only} = 1;
116 0 return $self->SUPER::_deploy_and_introspect_temp(@_);
117 }
118  
119  
120 0 ▶sub _other_databases {
Subroutine _other_databases: not called CC=1 SCAR=6.9
121 0 my ($self) = @_;
122 0 my %seen;
123 0 return sort grep { !$seen{$_}++ } map { $_->[1] } DBIO::MySQL::DDL->qualified_tables($self->schema);
124 }
125  
126  
127 0 ▶sub _introspect_current {
Subroutine _introspect_current: not called CC=9 SCAR=45.0
128 0 my ($self) = @_;
129 0 my @databases = $self->_other_databases;
130 0 ▶ $self->_refuse_missing_databases($self->_dbh) if @databases;
Branch: 1 branch
BranchTrueFalseTotal
if @databases 0 0 0
131  
132 0 my $model = $self->SUPER::_introspect_current;
133 0 for my $database (@databases) {
134 0 ▶ my %mine = map { $_->[0] =~ /\A\Q$database\E\.(.*)\z/s ? ($1 => 1) : () }
Branch: 1 branch
BranchTrueFalseTotal
$_->[0] =~ /\A\Q$database\E\.(.*)\z/s ? : 0 0 0
135 DBIO::MySQL::DDL->qualified_tables($self->schema);
136 0 my ($dsn, $user, $pass) = $self->_temp_connect_info($database);
137 0 ▶ my $dbh = eval { DBI->connect($dsn, $user, $pass, { RaiseError => 1, PrintError => 0, AutoCommit => 1 }) }
Branch: 1 branch
BranchTrueFalseTotal
my $dbh = eval { do { "DBI"->connect($dsn, $user, $pass, {"RaiseError", 1, "PrintError", 0, "AutoCommit", 1}) } } or die DBIO::Util::mask_connect_secrets($@ || "Cannot connect to the database ${database}: $DBI::errstr", [$dsn, $user, $pass]) 0 0 0
Condition0%0/2
$@ || "Cannot connect to the database ${database}: $DBI::errstr"
l!l
00
MC/DC0%0/1
$@ || "Cannot connect to the database ${database}: $DBI::errstr"
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
138 or die DBIO::Util::mask_connect_secrets(
139 $@ || "Cannot connect to the database $database: $DBI::errstr", [ $dsn, $user, $pass ]);
140 0 my $other = eval { $self->_new_introspect($dbh)->model };
141 0 my $err = $@;
142 0 eval { $dbh->disconnect; 1 };
143 0 ▶ die $err if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 0 0
144 0 ▶ my %keep = map { $_ => 1 } grep { $mine{$_} && ($other->{tables}{$_}{kind} // '') eq 'table' } keys %{ $other->{tables} };
Condition0%0/5
$mine{$_} && ($other->{'tables'}{$_}{'kind'} // '') eq 'table'
!ll&&!rl&&r
000
$other->{"tables"}{$_}{"kind"} // ""
l!l
00
MC/DC0%0/2
$mine{$_} && ($other->{'tables'}{$_}{'kind'} // '') eq 'table'
$mine{$_} $other->{"tables"}{$_}{"kind"}
Truth table0%0/3
A && B
A= $mine{$_}B= $other->{"tables"}{$_}{"kind"}
ABresult
0X0
100
111
145 $model->{other_databases}{$database} = { map {
146 0 my $section = $_;
147 0 ($section => { map { $_ => $other->{$section}{$_} } grep { $keep{$_} } keys %{ $other->{$section} } })
148 } qw(tables columns indexes foreign_keys) };
149 }
150 0 return $model;
151 }
152  
153  
154 0 ▶sub _build_target_model {
Subroutine _build_target_model: not called CC=2 SCAR=17.9
155 0 my $self = shift;
156 0 my $model = $self->SUPER::_build_target_model(@_);
157 0 for my $database ($self->_other_databases) {
158 0 local $self->{_only_database} = $database;
159 0 $model->{other_databases}{$database} = $self->SUPER::_build_target_model(@_);
160 }
161 0 return $model;
162 }
163  
164  
165 # --- MySQL-specific temp-database dialect ---
166  
167 1 ▶sub _create_temp_db {
Subroutine _create_temp_db: called CC=1 SCAR=0.0
168 1 my ($self, $dbh) = @_;
169 1 my $name = $self->temp_db_prefix . $$ . '_' . time();
170 1 $dbh->do('CREATE DATABASE ' . DBIO::MySQL::Identifier::quote_ident($name));
171 1 return $name;
172 }
173  
174 1 ▶sub _drop_temp_db {
Subroutine _drop_temp_db: called CC=1 SCAR=0.0
175 1 my ($self, $dbh, $name) = @_;
176 1 $dbh->do('DROP DATABASE IF EXISTS ' . DBIO::MySQL::Identifier::quote_ident($name));
177 }
178  
179 39 1;
180  
181 __END__
« blib/lib/DBIO/MySQL/Storage.pm ↑ Summary blib/lib/DBIO/MySQL/Diff/Table.pm »