| 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_"
MC/DC0%0/1
$_[0]{"temp_db_prefix"} // "_dbio_tmp_"
$_[0]{"temp_db_prefix"}
Truth table0%0/2
A
A= $_[0]{"temp_db_prefix"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%6/6
$storage->can("transaction_depth") and $storage->transaction_depth or not $live->{"AutoCommit"}
$storage->can("transaction_depth") and $storage->transaction_depth
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 3 | my ($dsn, $user, $pass) = @{ $storage->_dbi_connect_info }; | |||||||||||||||||||||||||||||||||||||||||||||
| 73 | 3 | ▶ | my $dbh = eval { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 3 | ▶ | DBI->connect($dsn, $user, $pass, { | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$@ || "Cannot connect to the database: $DBI::errstr"
MC/DC0%0/1
$@ || "Cannot connect to the database: $DBI::errstr"
$@
Truth table0%0/2
A
A= $@
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 6 | ▶ | DBI->connect($dsn, $user, $pass, { | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$@ || "Cannot connect to temp database: $DBI::errstr"
MC/DC0%0/1
$@ || "Cannot connect to temp database: $DBI::errstr"
$@
Truth table50%1/2
A
A= $@
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 4 | my $model; | |||||||||||||||||||||||||||||||||||||||||||||
| 111 | 4 | ▶ | unless ($err) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 3 | eval { $model = $self->_new_introspect($temp_dbh)->model }; | |||||||||||||||||||||||||||||||||||||||||||||
| 113 | 3 | ▶ | $err = $@ unless $model; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
$err ||= $@
MC/DC0%0/2
$err ||= $@
$err
$@
Truth table66%2/3
A ||= B
A= $errB= $@
| |||||||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 4 | ▶ | die $err if $err; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%6/6
not defined $dsn or ref $dsn or $dsn eq ""
not defined $dsn or ref $dsn
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 ""
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||||||