blib/lib/DBIO/Deploy/Base/TempDatabase.pm

filter: Statementstmt 98.8% 89 / 90 Branchbran 96.6% 29 / 30 Conditioncond 71.4% 15 / 21 MC/DCmcdc 54.5% 6 / 11 Subroutinesub 92.8% 13 / 14 Podpod 100% 1 / 1 totaltotal 91.6% 153 / 167 CC 24 SCAR 16.1
CC
24
Cov
94%
_deploy_and_introspect_temp
9.0
_admin_dbh
6.1
temp_db_prefix
6.0
CRAP 5.0
« blib/lib/DBIO/Future/Immediate.pm ↑ Summary blib/lib/DBIO/Test/Schema/Artist.pm »
1 package DBIO::Deploy::Base::TempDatabase;
2 # ABSTRACT: Deploy base for drivers that diff against a temporary database
3  
4 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 6 our $VERSION = '0.900007';
8  
9 6 ▶use base 'DBIO::Deploy::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 6 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use DBIO::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14  
15 1 ▶sub _create_temp_db { die ref(shift) . '::_create_temp_db not implemented' }
Subroutine _create_temp_db: called CC=1 SCAR=0.0
16  
17  
18 1 ▶sub _drop_temp_db { die ref(shift) . '::_drop_temp_db not implemented' }
Subroutine _drop_temp_db: called CC=1 SCAR=0.0
19  
20  
21 0 ▶sub temp_db_prefix { $_[0]->{temp_db_prefix} // '_dbio_tmp_' }
Subroutine temp_db_prefix: not called CC=2 SCAR=17.9
Pod documented
Condition0%0/2
$_[0]{"temp_db_prefix"} // "_dbio_tmp_"
l!l
00
MC/DC0%0/1
$_[0]{"temp_db_prefix"} // "_dbio_tmp_"
$_[0]{"temp_db_prefix"}
Truth table0%0/2
A
A= $_[0]{"temp_db_prefix"}
Aresult
11
00
22  
23  
24 24 ▶sub _build_target_model {
Subroutine _build_target_model: called CC=4 SCAR=13.9
25 24 my ($self) = @_;
26  
27 # The connect info is read once before any temp database exists: one the
28 # temp connection cannot be derived from stops the build here.
29 24 $self->_temp_connect_info('probe');
30  
31 19 my $dbh = $self->_admin_dbh;
32  
33 19 my $temp_db = eval { $self->_create_temp_db($dbh) };
34 19 my $create_err = $@;
35 19 ▶ unless (defined $temp_db) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $temp_db) 1 18 19
36 1 $self->_release_admin_dbh;
37 1 die $create_err;
38 }
39  
40 18 my $model = eval { $self->_deploy_and_introspect_temp($temp_db) };
41 18 my $err = $@;
42  
43 # Warned about and not raised (karr k316): the model is there, or $err is
44 # what the caller has to hear. With a plain warn, as DBIO::Deploy::Base
45 # does it -- no DBIO::Carp in a deploy class.
46 18 ▶ eval { $self->_drop_temp_db($dbh, $temp_db); 1 } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_drop_temp_db($dbh, $temp_db); 1 } } or do { my $drop_err = "$@"; my $info = eval { do { [$self->_temp_connect_info($temp_db)] } }; ($drop_err = DBIO::Util::mask_connect_secrets($drop_err, $info)) =~ s/\s+\z//; warn ref($self) . ": temporary database '${temp_db}' could not be dropped and " . "is still on the server -- drop it by hand: $drop_err\n" } 10 8 18
47 8 my $drop_err = "$@";
48 # a DBD may repeat the DSN or the password in its error: never the
49 # password (karr k297). _temp_connect_info dies of a coderef DSN, and the
50 # warning is not to fail of what it is masked with.
51 8 my $info = eval { [ $self->_temp_connect_info($temp_db) ] };
52 8 ($drop_err = DBIO::Util::mask_connect_secrets($drop_err, $info)) =~ s/\s+\z//;
53 8 warn ref($self) . ": temporary database '$temp_db' could not be dropped and "
54 . "is still on the server -- drop it by hand: $drop_err\n";
55 };
56  
57 18 $self->_release_admin_dbh;
58  
59 18 ▶ die $err if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 3 15 18
60 15 return $model;
61 }
62  
63  
64 19 ▶sub _admin_dbh {
Subroutine _admin_dbh: called CC=6 SCAR=18.1
65 19 my ($self) = @_;
66 19 my $storage = $self->schema->storage;
67 19 my $live = $self->_dbh;
68 return $live
69 unless ($storage->can('transaction_depth') && $storage->transaction_depth)
70 19 ▶ || !$live->{AutoCommit};
Branch: 1 branch
BranchTrueFalseTotal
unless $storage->can("transaction_depth") and $storage->transaction_depth or not $live->{"AutoCommit"} 16 3 19
Condition100%6/6
$storage->can("transaction_depth") and $storage->transaction_depth or not $live->{"AutoCommit"}
l!l&&r!l&&!r
2116
$storage->can("transaction_depth") and $storage->transaction_depth
!ll&&!rl&&r
1432
MC/DC100%3/3
$storage->can("transaction_depth") and $storage->transaction_depth or not $live->{"AutoCommit"}
$storage->can("transaction_depth") $storage->transaction_depth not $live->{"AutoCommit"}
Truth table80%4/5
A and B or not(C)
A= $storage->can("transaction_depth")B= $storage->transaction_depthC= not $live->{"AutoCommit"}
ABCresult
11X1
0X11
1011
0X00
1000
71  
72 3 my ($dsn, $user, $pass) = @{ $storage->_dbi_connect_info };
73 3 ▶ my $dbh = eval {
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: $DBI::errstr", [$dsn, $user, $pass]) 3 0 3
74 3 ▶ DBI->connect($dsn, $user, $pass, {
Condition0%0/2
$@ || "Cannot connect to the database: $DBI::errstr"
l!l
00
MC/DC0%0/1
$@ || "Cannot connect to the database: $DBI::errstr"
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
75 RaiseError => 1, PrintError => 0, AutoCommit => 1,
76 })
77 } or die DBIO::Util::mask_connect_secrets(
78 $@ || "Cannot connect to the database: $DBI::errstr", [ $dsn, $user, $pass ],
79 );
80 3 $self->{_admin_dbh} = $dbh;
81 3 return $dbh;
82 }
83  
84 # Closes the connection _admin_dbh opened, if it opened one. A failing
85 # disconnect changes nothing about the outcome of the build.
86 19 ▶sub _release_admin_dbh {
Subroutine _release_admin_dbh: called CC=2 SCAR=6.9
87 19 my ($self) = @_;
88 19 ▶ my $own = delete $self->{_admin_dbh} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $own = delete $self->{"_admin_dbh"} or return 3 16 19
89 3 eval { $own->disconnect; 1 };
90 3 return;
91 }
92  
93  
94 6 ▶sub _deploy_and_introspect_temp {
Subroutine _deploy_and_introspect_temp: called CC=9 SCAR=22.0
95 6 my ($self, $temp_db) = @_;
96  
97 6 my ($dsn, $user, $pass) = $self->_temp_connect_info($temp_db);
98 # DBI dies quoting the DSN it was given: never the password (karr k297)
99 6 ▶ my $temp_dbh = eval {
Branch: 1 branch
BranchTrueFalseTotal
my $temp_dbh = eval { do { "DBI"->connect($dsn, $user, $pass, {"RaiseError", 1, "PrintError", 0, "AutoCommit", 1}) } } or die DBIO::Util::mask_connect_secrets($@ || "Cannot connect to temp database: $DBI::errstr", [$dsn, $user, $pass]) 4 2 6
100 6 ▶ DBI->connect($dsn, $user, $pass, {
Condition50%1/2
$@ || "Cannot connect to temp database: $DBI::errstr"
l!l
20
MC/DC0%0/1
$@ || "Cannot connect to temp database: $DBI::errstr"
$@
Truth table50%1/2
A
A= $@
Aresult
11
00
101 RaiseError => 1, PrintError => 0, AutoCommit => 1,
102 })
103 } or die DBIO::Util::mask_connect_secrets(
104 $@ || "Cannot connect to temp database: $DBI::errstr", [ $dsn, $user, $pass ],
105 );
106  
107 4 my $err;
108 4 ▶ eval { $self->_execute_ddl($temp_dbh, $self->_install_ddl); 1 } or $err = $@;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_execute_ddl($temp_dbh, $self->_install_ddl); 1 } } or $err = $@ 3 1 4
109  
110 4 my $model;
111 4 ▶ unless ($err) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($err) 3 1 4
112 3 eval { $model = $self->_new_introspect($temp_dbh)->model };
113 3 ▶ $err = $@ unless $model;
Branch: 1 branch
BranchTrueFalseTotal
unless $model 1 2 3
114 }
115  
116 # a disconnect that fails is raised only when nothing else was: it does not
117 # replace the error of the deploy (karr k316)
118 4 ▶ eval { $temp_dbh->disconnect; 1 } or $err ||= $@;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $temp_dbh->disconnect; 1 } } or $err ||= $@ 1 3 4
Condition66%2/3
$err ||= $@
l!l&&r!l&&!r
210
MC/DC0%0/2
$err ||= $@
$err $@
Truth table66%2/3
A ||= B
A= $errB= $@
ABresult
1X1
011
000
119  
120 4 ▶ die $err if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 3 1 4
121 1 return $model;
122 }
123  
124  
125 55 ▶sub _temp_connect_info {
Subroutine _temp_connect_info: called CC=5 SCAR=16.1
126 55 my ($self, $temp_db) = @_;
127 55 my ($dsn, $user, $pass) = @{ $self->schema->storage->_dbi_connect_info };
128  
129 55 ▶ if (ref $dsn eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $dsn eq "CODE") 6 49 55
130 6 die ref($self) . ' does not support coderef DSN for temp database connections';
131 }
132 49 ▶ if (!defined $dsn || ref $dsn || $dsn eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $dsn or ref $dsn or $dsn eq "") 9 40 49
Condition100%6/6
not defined $dsn or ref $dsn or $dsn eq ""
l!l&&r!l&&!r
7240
not defined $dsn or ref $dsn
l!l&&r!l&&!r
5242
MC/DC100%3/3
not defined $dsn or ref $dsn or $dsn eq ""
not defined $dsn ref $dsn $dsn eq ""
Truth table100%4/4
not(A) or B or C
A= not defined $dsnB= ref $dsnC= $dsn eq ""
ABCresult
1XX1
01X1
0011
0000
133 9 die ref($self) . ' cannot derive a temp database connection: '
134 . 'the storage has no DSN (a plain, non-empty string) to derive it from';
135 }
136  
137 40 return ($self->_temp_dsn($dsn, $temp_db), $user, $pass);
138 }
139  
140  
141 42 ▶sub _temp_dsn {
Subroutine _temp_dsn: called CC=2 SCAR=6.9
142 42 my ($self, $dsn, $temp_db) = @_;
143  
144 42 ▶ if ($dsn =~ /(?:database|dbname)=/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dsn =~ /(?:database|dbname)=/i) { } 37 5 42
145 37 $dsn =~ s/(database|dbname)=[^;]+/$1=$temp_db/i;
146 }
147 else {
148 5 $dsn .= ";dbname=$temp_db";
149 }
150  
151 42 return $dsn;
152 }
153  
154 6 1;
155  
156 __END__
« blib/lib/DBIO/Future/Immediate.pm ↑ Summary blib/lib/DBIO/Test/Schema/Artist.pm »