blib/lib/DBIO/Deploy/Base.pm

filter: Statementstmt 99.5% 209 / 210 Branchbran 95.7% 90 / 94 Conditioncond 67.1% 45 / 67 MC/DCmcdc 43.9% 18 / 41 Subroutinesub 100% 49 / 49 Podpod 100% 11 / 11 totaltotal 89.4% 422 / 472 CC 84 SCAR 17.7
CC
84
Cov
93%
_execute_ddl
15.1
_storage_owns_dbh
10.4
_assert_diff_allowed
9.2
CRAP 5.9
« blib/lib/DBIO/Replicated/Balancer/Random.pm ↑ Summary blib/lib/DBIO/Relationship/CascadeActions.pm »
1 package DBIO::Deploy::Base;
2 # ABSTRACT: Base class for DBIO driver deploy orchestrators
3  
4 382 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 382 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 382 our $VERSION = '0.900007';
8  
9 382 our $CONTRACT_VERSION = '1.3';
10  
11 # Loaded without importing: importing _split_statements would leak it into
12 # this package's method-resolution namespace (t/55namespaces_cleaned.t).
13 # We deliberately do NOT use DBIO::Carp either -- its `import` installs
14 # carp/carp_once/carp_unique into our namespace and triggers the same
15 # namespace leak. The F02 informational warning is fired via a local
16 # one-time closure below.
17 382 ▶use DBIO::SQL::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18 382 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19 382 ▶use DBIO::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21  
22  
23 2 ▶sub contract_version { $CONTRACT_VERSION }
Subroutine contract_version: called CC=1 SCAR=0.0
Pod documented
24  
25 129 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
26 129 my ($class, %args) = @_;
27 129 return bless { %args }, $class;
28 }
29  
30  
31 563 ▶sub schema { $_[0]->{schema} }
Subroutine schema: called CC=1 SCAR=0.0
Pod documented
32  
33  
34 15 ▶sub add_drop_table { $_[0]->{add_drop_table} }
Subroutine add_drop_table: called CC=1 SCAR=0.0
Pod documented
35  
36  
37 55 ▶sub allow_destructive {
Subroutine allow_destructive: called CC=3 SCAR=11.0
Pod documented
38 55 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 5 50 55
39 5 ▶ $_[0]->{allow_destructive} = $_[1] ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$_[1] ? : 5 0 5
40 5 return $_[0];
41 }
42 50 return $_[0]->{allow_destructive};
43 }
44  
45  
46 20 ▶sub deploy_args {
Subroutine deploy_args: called CC=8 SCAR=20.8
Pod documented
47 20 my ($class, $sqltargs) = @_;
48 20 ▶ return () unless defined $sqltargs;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sqltargs 5 15 20
49 15 ▶ DBIO::Exception->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless ref $sqltargs eq "HASH" 2 13 15
Condition33%1/3
ref $sqltargs || $sqltargs
l!l&&r!l&&!r
020
MC/DC0%0/2
ref $sqltargs || $sqltargs
ref $sqltargs $sqltargs
Truth table33%1/3
A || B
A= ref $sqltargsB= $sqltargs
ABresult
1X1
011
000
50 "The options of deploy must be a hash reference, got '" . (ref($sqltargs) || $sqltargs) . "'"
51 ) unless ref $sqltargs eq 'HASH';
52  
53 13 my %args;
54 13 for my $key (sort keys %$sqltargs) {
55 13 ▶ if ($key eq 'add_drop_table') {
Branch: 2 branches
BranchTrueFalseTotal
if ($key eq 'add_drop_table') { } 9 4 13
elsif ($key eq 'version') { } 2 2 4
56 9 ▶ $args{add_drop_table} = $sqltargs->{$key} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$sqltargs->{$key} ? : 7 2 9
57 }
58 elsif ($key eq 'version') {
59 2 DBIO::Exception->throw(
60 "deploy option 'version' is not supported: the native install always "
61 . "installs the current state of the schema, a version cannot be selected"
62 );
63 }
64 else {
65 2 DBIO::Exception->throw(
66 "Unknown deploy option '$key' (known: add_drop_table)"
67 );
68 }
69 }
70 9 return %args;
71 }
72  
73  
74 295 ▶sub _ddl_quote_ident {
Subroutine _ddl_quote_ident: called CC=3 SCAR=11.0
75 295 my ($self, $name) = @_;
76 295 my $storage = $self->_driver_storage;
77 295 ▶ return $storage->_ddl_quote_ident($name)
Branch: 1 branch
BranchTrueFalseTotal
if $storage and $storage->can("_ddl_quote_ident") 43 252 295
Condition100%3/3
$storage and $storage->can("_ddl_quote_ident")
!ll&&!rl&&r
4221043
MC/DC100%2/2
$storage and $storage->can("_ddl_quote_ident")
$storage $storage->can("_ddl_quote_ident")
Truth table100%3/3
A and B
A= $storageB= $storage->can("_ddl_quote_ident")
ABresult
0X0
100
111
78 if $storage && $storage->can('_ddl_quote_ident');
79 252 return DBIO::SQL::Util::_quote_ident($name);
80 }
81  
82 33 ▶sub _dbh { $_[0]->schema->storage->dbh }
Subroutine _dbh: called CC=1 SCAR=0.0
83  
84  
85 372 ▶sub _driver_storage {
Subroutine _driver_storage: called CC=2 SCAR=6.9
86 372 my ($self) = @_;
87 372 ▶ my $storage = eval { $self->schema->storage } || return undef;
Condition100%2/2
eval { do { $self->schema->storage } } || (return undef)
l!l
32844
MC/DC100%1/1
eval { do { $self->schema->storage } } || (return undef)
eval { do { $self->schema->storage } }
Truth table100%2/2
A
A= eval { do { $self->schema->storage } }
Aresult
11
00
88 328 return DBIO::Deploy::Base::driver_storage($storage);
89 }
90  
91  
92 329 ▶sub driver_storage {
Subroutine driver_storage: called CC=3 SCAR=11.0
Pod documented
93 329 my ($storage) = @_;
94 329 ▶ return $storage
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($storage) and $storage->isa("DBIO::Replicated::Storage") 326 3 329
Condition66%2/3
builtin::blessed($storage) and $storage->isa("DBIO::Replicated::Storage")
!ll&&!rl&&r
03263
MC/DC50%1/2
builtin::blessed($storage) and $storage->isa("DBIO::Replicated::Storage")
builtin::blessed($storage) $storage->isa("DBIO::Replicated::Storage")
Truth table66%2/3
A and B
A= builtin::blessed($storage)B= $storage->isa("DBIO::Replicated::Storage")
ABresult
0X0
100
111
95 unless Scalar::Util::blessed($storage) and $storage->isa('DBIO::Replicated::Storage');
96 3 return $storage->master->storage;
97 }
98  
99 # --- abstract hooks: the driver's helper classes -----------------------------
100  
101  
102 1 ▶sub _ddl_class { die ref(shift) . '::_ddl_class not implemented' }
Subroutine _ddl_class: called CC=1 SCAR=0.0
103  
104  
105 1 ▶sub _introspect_class { die ref(shift) . '::_introspect_class not implemented' }
Subroutine _introspect_class: called CC=1 SCAR=0.0
106  
107  
108 1 ▶sub _diff_class { die ref(shift) . '::_diff_class not implemented' }
Subroutine _diff_class: called CC=1 SCAR=0.0
109  
110 # --- overridable factories / glue --------------------------------------------
111  
112  
113 8 ▶sub _new_introspect {
Subroutine _new_introspect: called CC=1 SCAR=0.0
114 8 my ($self, $dbh) = @_;
115 8 return $self->_introspect_class->new(dbh => $dbh);
116 }
117  
118  
119 5 ▶sub _introspect_current {
Subroutine _introspect_current: called CC=1 SCAR=0.0
120 5 my ($self) = @_;
121 5 return $self->_new_introspect($self->_dbh)->model;
122 }
123  
124  
125 5 ▶sub _install_ddl {
Subroutine _install_ddl: called CC=1 SCAR=0.0
126 5 my ($self) = @_;
127 5 return $self->_ddl_class->install_ddl($self->schema);
128 }
129  
130  
131 68 ▶sub _execute_ddl {
Subroutine _execute_ddl: called CC=15 SCAR=27.2
132 68 my ($self, $dbh, $sql) = @_;
133 68 my $storage = $self->_driver_storage;
134  
135 68 my @stmts = _split_ddl($storage, $sql);
136 68 ▶ return 1 unless @stmts;
Branch: 1 branch
BranchTrueFalseTotal
unless @stmts 7 61 68
137  
138 61 ▶ my $use_txn = $storage && $storage->can('_use_transactional_ddl')
Branch: 1 branch
BranchTrueFalseTotal
$storage && $storage->can('_use_transactional_ddl') ? : 33 28 61
Condition100%3/3
$storage and $storage->can("_use_transactional_ddl")
!ll&&!rl&&r
12733
MC/DC100%2/2
$storage and $storage->can("_use_transactional_ddl")
$storage $storage->can("_use_transactional_ddl")
Truth table100%3/3
A and B
A= $storageB= $storage->can("_use_transactional_ddl")
ABresult
0X0
100
111
139 ? $storage->_use_transactional_ddl
140 : 0;
141  
142 61 ▶ if ($use_txn and _storage_owns_dbh($storage, $dbh)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($use_txn and _storage_owns_dbh($storage, $dbh)) { } 12 49 61
elsif ($use_txn) { } 5 44 49
Condition100%3/3
$use_txn and _storage_owns_dbh($storage, $dbh)
!ll&&!rl&&r
44512
MC/DC100%2/2
$use_txn and _storage_owns_dbh($storage, $dbh)
$use_txn _storage_owns_dbh($storage, $dbh)
Truth table100%3/3
A and B
A= $use_txnB= _storage_owns_dbh($storage, $dbh)
ABresult
0X0
100
111
143 12 my @runs = _group_ddl_statements($storage, \@stmts);
144  
145 12 ▶ if (@runs > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@runs > 1) 3 9 12
146 3 ▶ my $class = ref($self) || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
300
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
147 3 my $loose = scalar grep { !$_->[0] } @runs;
148 3 DBIO::Deploy::Base::_warn_split_ddl_once(
149 $class,
150 "$class -- $loose statement(s) cannot run inside a transaction, so the "
151 . "batch was split into " . scalar(@runs) . " runs and is not atomic as a "
152 . "whole; recovery depends on the driver's version-row gate"
153 );
154 }
155  
156 12 for my $run (@runs) {
157 16 my ($transactional, @group) = @$run;
158  
159 16 ▶ if ($transactional) {
Branch: 1 branch
BranchTrueFalseTotal
if ($transactional) { } 12 4 16
160 12 ▶ $storage->txn_do(sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
161 12 $dbh->do($_) for @group;
162 12 });
163 }
164 else {
165 4 $dbh->do($_) for @group;
166 }
167 }
168 }
169 elsif ($use_txn) {
170 # Transactional engine, but a handle the storage does not own: run the
171 # loop bare and say nothing. See _storage_owns_dbh for why bracketing here
172 # would be worse than not bracketing, and why this case gets no warning.
173 5 $dbh->do($_) for @stmts;
174 }
175 else {
176 44 ▶ my $class = ref($self) || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
4400
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
177 44 DBIO::Deploy::Base::_warn_non_txn_ddl_once("$class -- DDL loop is not atomic; recovery depends on the driver's version-row gate");
178 44 $dbh->do($_) for @stmts;
179 }
180 60 return 1;
181 }
182  
183 # The capability is one boolean for the connection, but atomicity is decided
184 # per statement: some engines have DDL that may not be inside a transaction
185 # even where everything else may. PostgreSQL's ALTER TYPE ... ADD VALUE is the
186 # known case -- rejected outright in a transaction block before server 12, and
187 # from 12 on accepted but the new value stays unusable until commit, so an
188 # upgrade that adds an enum value and then references it breaks either way.
189 #
190 # Rather than give that up for the whole batch, consecutive statements are
191 # grouped into runs, in their original order: each run of transaction-safe
192 # statements gets one txn_do, and a statement that cannot be in one runs on its
193 # own, bare. The common case -- every statement safe -- yields exactly one run
194 # and is therefore byte-identical to a single bracket around the loop.
195 #
196 # The trade is real and deliberate: what used to be one transaction becomes
197 # several, so a failure no longer takes the entire upgrade back. That is what
198 # the __VERSION gate in DBIO::DeploymentHandler is for (a re-run recomputes the
199 # diff against live state), and it is why the split is announced once.
200 #
201 # Returns a list of [ $transactional, @stmts ] arrayrefs.
202 75 ▶sub _group_ddl_statements {
Subroutine _group_ddl_statements: called CC=4 SCAR=13.9
203 75 my ($storage, $stmts) = @_;
204  
205 75 my @runs;
206 75 for my $stmt (@$stmts) {
207 1535 my $txn = _stmt_is_txn_safe($storage, $stmt);
208  
209 1535 ▶ if (@runs and $runs[-1][0] == $txn) {
Branch: 1 branch
BranchTrueFalseTotal
if (@runs and $runs[-1][0] == $txn) { } 837 698 1535
Condition100%3/3
@runs and $runs[-1][0] == $txn
!ll&&!rl&&r
72626837
MC/DC100%2/2
@runs and $runs[-1][0] == $txn
@runs $runs[-1][0] == $txn
Truth table100%3/3
A and B
A= @runsB= $runs[-1][0] == $txn
ABresult
0X0
100
111
210 837 push @{ $runs[-1] }, $stmt;
211 }
212 else {
213 698 push @runs, [ $txn, $stmt ];
214 }
215 }
216  
217 75 return @runs;
218 }
219  
220 # Split DDL text under the SQL dialect the storage declares. The storage may be
221 # undef (a deploy with no reachable storage) or one that has never heard of
222 # _ddl_sql_dialect; both read as "not declared", which is the splitter's
223 # behaviour without the option, byte for byte. can() rather than eval, like
224 # _stmt_is_txn_safe: a hook that throws should be heard. The async deploy asks
225 # through the same function with its owner storage.
226 174 ▶sub _split_ddl {
Subroutine _split_ddl: called CC=5 SCAR=16.1
227 174 my ($storage, $sql) = @_;
228 174 ▶ my $dialect = $storage && Scalar::Util::blessed($storage)
Branch: 1 branch
BranchTrueFalseTotal
$storage && builtin::blessed($storage) && $storage->can('_ddl_sql_dialect') ? : 25 149 174
Condition83%5/6
$storage and builtin::blessed($storage) and $storage->can("_ddl_sql_dialect")
!ll&&!rl&&r
114825
$storage and builtin::blessed($storage)
!ll&&!rl&&r
10173
MC/DC66%2/3
$storage and builtin::blessed($storage) and $storage->can("_ddl_sql_dialect")
$storage builtin::blessed($storage) $storage->can("_ddl_sql_dialect")
Truth table75%3/4
A and B and C
A= $storageB= builtin::blessed($storage)C= $storage->can("_ddl_sql_dialect")
ABCresult
0XX0
10X0
1100
1111
229 && $storage->can('_ddl_sql_dialect')
230 ? $storage->_ddl_sql_dialect
231 : undef;
232 174 ▶ return DBIO::SQL::Util::_split_statements($sql,
Branch: 1 branch
BranchTrueFalseTotal
defined $dialect ? : 4 170 174
233 defined $dialect ? { dialect => $dialect } : undef);
234 }
235  
236 # The per-statement half of the transactional_ddl question. Default yes: a
237 # driver that has never heard of the hook keeps the behaviour it had, which is
238 # also the right answer for every engine whose DDL is uniformly transactional.
239 #
240 # can() rather than eval here, unlike _storage_owns_dbh: this hook is new, so
241 # no facade can be delegating it without knowing it exists, and a driver whose
242 # hook throws should be heard rather than silently read as "safe".
243 1535 ▶sub _stmt_is_txn_safe {
Subroutine _stmt_is_txn_safe: called CC=3 SCAR=11.0
244 1535 my ($storage, $stmt) = @_;
245 1535 ▶ return 1 unless $storage->can('_ddl_statement_is_transactional');
Branch: 1 branch
BranchTrueFalseTotal
unless $storage->can("_ddl_statement_is_transactional") 43 1492 1535
246 1492 ▶ return $storage->_ddl_statement_is_transactional($stmt) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$storage->_ddl_statement_is_transactional($stmt) ? : 1160 332 1492
247 }
248  
249 # One-time notice that a batch was split. Same reasoning as the
250 # non-transactional notice below -- an operator needs to know once that this
251 # upgrade was not atomic as a whole, and does not need telling on every deploy.
252 #
253 # Keyed on the CLASS, not on the message, which is where this one differs: the
254 # text carries how many statements forced the split and how many runs came out,
255 # and those vary from batch to batch. Keying on the message would let the same
256 # engine announce itself again for every new shape of upgrade, which is exactly
257 # the nagging the one-shot exists to prevent.
258 382 my %_WARNED_SPLIT_DDL;
259 3 ▶sub _warn_split_ddl_once {
Subroutine _warn_split_ddl_once: called CC=2 SCAR=6.9
260 3 my ($key, $msg) = @_;
261 3 ▶ return if $_WARNED_SPLIT_DDL{$key}++;
Branch: 1 branch
BranchTrueFalseTotal
if $_WARNED_SPLIT_DDL{$key}++ 2 1 3
262 1 warn "split DDL batch on $msg\n";
263 }
264  
265 # The bracket has to own the handle it brackets. _execute_ddl takes $dbh as an
266 # argument but can only bracket through $storage->txn_do, and those are not
267 # always the same connection: DBIO::Deploy::Base::TempDatabase opens its OWN
268 # raw DBI handle to the throwaway database (AutoCommit => 1) and hands that in.
269 # Bracketing there would open a transaction on the LIVE connection around DDL
270 # executing on a different one -- buying no atomicity, since the temp handle
271 # auto-commits every statement anyway, while a failure would roll back whatever
272 # the caller had open on the live connection. Not bracketing is not a
273 # compromise here: the throwaway database is dropped whole on any failure
274 # (TempDatabase::_build_target_model), so the DROP is the unit of recovery and
275 # a half-deployed temp schema is unreachable by definition.
276 #
277 # Hence no warning in that branch either. The F02 notice exists to tell an
278 # operator that a LIVE schema can be left half migrated; it would be noise on a
279 # database that is about to be dropped, and its text -- pointing at the
280 # version-row gate -- would be wrong there as well.
281 #
282 # _dbh is asked first: it is the raw attribute and never connects, and on a
283 # DBIO::Storage::DBI it holds the very handle dbh() hands out. It is called
284 # through eval rather than can(), so a delegating facade that answers by
285 # AUTOLOAD is not written off as handle-less. dbh() is the fallback for a
286 # storage exposing only the public accessor; any deploy reaching this point has
287 # already fetched a handle through it, so the extra call costs a ping at most.
288 #
289 # A storage that answers neither is treated as not owning the handle, leaving
290 # the loop unwrapped. That is the conservative direction: an unwrapped loop
291 # deploys correctly and merely without atomicity, whereas a wrongly wrapped one
292 # can roll back a connection it was never entitled to touch.
293 17 ▶sub _storage_owns_dbh {
Subroutine _storage_owns_dbh: called CC=7 SCAR=23.4
294 17 my ($storage, $dbh) = @_;
295 17 ▶ return 0 unless defined $dbh and ref $dbh;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dbh and ref $dbh 0 17 17
Condition33%1/3
defined $dbh and ref $dbh
!ll&&!rl&&r
0017
MC/DC0%0/2
defined $dbh and ref $dbh
defined $dbh ref $dbh
Truth table33%1/3
A and B
A= defined $dbhB= ref $dbh
ABresult
0X0
100
111
296  
297 0 ▶ my $own = eval { $storage->_dbh } || eval { $storage->dbh };
Condition33%1/3
eval { do { $storage->_dbh } } || eval { do { $storage->dbh } }
l!l&&r!l&&!r
1700
MC/DC0%0/2
eval { do { $storage->_dbh } } || eval { do { $storage->dbh } }
eval { do { $storage->_dbh } } eval { do { $storage->dbh } }
Truth table33%1/3
A || B
A= eval { do { $storage->_dbh } }B= eval { do { $storage->dbh } }
ABresult
1X1
011
000
298 17 ▶ return 0 unless defined $own and ref $own;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $own and ref $own 0 17 17
Condition33%1/3
defined $own and ref $own
!ll&&!rl&&r
0017
MC/DC0%0/2
defined $own and ref $own
defined $own ref $own
Truth table33%1/3
A and B
A= defined $ownB= ref $own
ABresult
0X0
100
111
299  
300 17 ▶ return Scalar::Util::refaddr($own) == Scalar::Util::refaddr($dbh) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
builtin::refaddr($own) == builtin::refaddr($dbh) ? : 12 5 17
301 }
302  
303 # F02: one-time informational warning for non-transactional DDL runs.
304 # Implemented as a local one-time closure rather than `use DBIO::Carp`
305 # to avoid the namespace leak (t/55namespaces_cleaned.t). Keyed on the
306 # full message so a different engine class gets its own one-shot.
307 #
308 # The wording stays as it is, deliberately. It reads as if the engine were at
309 # fault, when the driver may simply never have declared the capability -- but
310 # that is precisely what makes it useful, and it has already done the job:
311 # this notice appearing in a PostgreSQL deploy log is what surfaced that no
312 # driver but ClickHouse declared transactional_ddl at all. Softening it into
313 # "capability not declared" would turn an operator-facing statement about
314 # recovery into an internal remark most operators cannot act on. What the
315 # notice must NOT do is fire where it does not apply, which is why the
316 # foreign-handle branch above is silent.
317 my %_WARNED_NON_TXN_DDL;
318 44 ▶sub _warn_non_txn_ddl_once {
Subroutine _warn_non_txn_ddl_once: called CC=2 SCAR=6.9
319 44 my ($msg) = @_;
320 44 ▶ return if $_WARNED_NON_TXN_DDL{$msg}++;
Branch: 1 branch
BranchTrueFalseTotal
if $_WARNED_NON_TXN_DDL{$msg}++ 38 6 44
321 6 warn "non-transactional DDL on $msg\n";
322 }
323  
324  
325 1 ▶sub _build_target_model {
Subroutine _build_target_model: called CC=1 SCAR=0.0
326 1 die ref(shift) . '::_build_target_model not implemented';
327 }
328  
329 # --- database extensions (ADR 0066) -----------------------------------------
330  
331  
332 6 ▶sub _extension_list { [] }
Subroutine _extension_list: called CC=1 SCAR=0.0
333  
334  
335 3 ▶sub _extension_driver_helpers { {} }
Subroutine _extension_driver_helpers: called CC=1 SCAR=0.0
336  
337  
338 64 ▶sub _extension_connection_schema { undef }
Subroutine _extension_connection_schema: called CC=1 SCAR=0.0
339  
340  
341 64 ▶sub _extension_restricted { 0 }
Subroutine _extension_restricted: called CC=1 SCAR=0.0
342  
343  
344 3 ▶sub _apply_extension_claims {
Subroutine _apply_extension_claims: called CC=1 SCAR=0.0
345 3 my ($self, $model, $addresses) = @_;
346 3 DBIO::Exception->throw(
347 ref($self) . ' cannot apply the extension claims of ' . scalar(@$addresses)
348 . ' address(es): _apply_extension_claims is not implemented'
349 );
350 }
351  
352  
353 1 ▶sub _apply_extension_annotations {
Subroutine _apply_extension_annotations: called CC=1 SCAR=0.0
354 1 my ($self, $model, $triples) = @_;
355 1 DBIO::Exception->throw(
356 ref($self) . ' cannot apply the extension annotations of ' . scalar(@$triples)
357 . ' triple(s): _apply_extension_annotations is not implemented'
358 );
359 }
360  
361  
362 # Read-only all the way down: hashes (keys and values) and the arrays in them.
363 # A file-scoped closure, not a sub: a named helper would be a method of every
364 # deploy class (t/55namespaces_cleaned.t).
365 382 my $lock_deep;
366 354 ▶$lock_deep = sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
367 354 my ($x) = @_;
368 354 ▶ if (ref $x eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $x eq 'HASH') { } 180 174 354
elsif (ref $x eq 'ARRAY') { } 62 112 174
369 180 $lock_deep->($_) for values %$x;
370 180 Hash::Util::lock_hashref($x);
371 }
372 elsif (ref $x eq 'ARRAY') {
373 62 $lock_deep->($_) for @$x;
374 62 Internals::SvREADONLY($_, 1) for @$x;
375 62 Internals::SvREADONLY(@$x, 1);
376 }
377 354 return;
378 382 };
379  
380 62 ▶sub _apply_extension_states {
Subroutine _apply_extension_states: called CC=1 SCAR=0.0
381 62 my ($self, $model, $states) = @_;
382 62 $model->{extensions} = $states;
383 62 require Hash::Util;
384 62 $lock_deep->($model->{extensions});
385 62 return;
386 }
387  
388  
389 2 ▶sub _extension_verdict {
Subroutine _extension_verdict: called CC=2 SCAR=6.9
390 2 my ($self, $change) = @_;
391 2 ▶ my $protocol = $self->{_extension_protocol} or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $protocol = $self->{"_extension_protocol"} or return undef 1 1 2
392 1 return $protocol->verdict($change);
393 }
394  
395 # --- public orchestration ----------------------------------------------------
396  
397  
398 15 ▶sub install {
Subroutine install: called CC=8 SCAR=21.1
Pod documented
399 15 my ($self) = @_;
400 15 my $ddl = $self->_install_ddl;
401 # A class that overrides install owns the dropping; its SUPER::install only
402 # installs.
403 15 ▶ if ($self->add_drop_table
Branch: 1 branch
BranchTrueFalseTotal
if ($self->add_drop_table and (ref($self)->can("install") || 0) == \&DBIO::Deploy::Base::install) 7 8 15
Condition80%4/5
$self->add_drop_table and (ref($self)->can("install") || 0) == \&DBIO::Deploy::Base::install
!ll&&!rl&&r
717
ref($self)->can('install') || 0
l!l
80
MC/DC50%1/2
$self->add_drop_table and (ref($self)->can("install") || 0) == \&DBIO::Deploy::Base::install
$self->add_drop_table ref($self)->can('install')
Truth table66%2/3
A and B
A= $self->add_drop_tableB= ref($self)->can('install')
ABresult
0X0
100
111
404 and (ref($self)->can('install') || 0) == \&DBIO::Deploy::Base::install) {
405 7 my $storage = $self->_driver_storage;
406 7 ▶ my $dialect = $storage && $storage->can('_ddl_sql_dialect')
Branch: 1 branch
BranchTrueFalseTotal
$storage && $storage->can('_ddl_sql_dialect') ? : 6 1 7
Condition66%2/3
$storage and $storage->can("_ddl_sql_dialect")
!ll&&!rl&&r
106
MC/DC50%1/2
$storage and $storage->can("_ddl_sql_dialect")
$storage $storage->can("_ddl_sql_dialect")
Truth table66%2/3
A and B
A= $storageB= $storage->can("_ddl_sql_dialect")
ABresult
0X0
100
111
407 ? $storage->_ddl_sql_dialect : undef;
408 my $drop = DBIO::SQL::Util::_drop_statements(
409 $self->schema,
410 294 ▶ sub { $self->_ddl_quote_ident($_[0]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
411 7 ▶ { cascade => !(defined $dialect && $dialect eq 'sqlite') },
Condition66%2/3
defined $dialect && $dialect eq 'sqlite'
!ll&&!rl&&r
601
MC/DC50%1/2
defined $dialect && $dialect eq 'sqlite'
defined $dialect $dialect eq 'sqlite'
Truth table66%2/3
A && B
A= defined $dialectB= $dialect eq 'sqlite'
ABresult
0X0
100
111
412 );
413 7 ▶ $ddl = "$drop\n\n$ddl" if length $drop;
Branch: 1 branch
BranchTrueFalseTotal
if length $drop 7 0 7
414 }
415 15 $self->_execute_ddl($self->_dbh, $ddl);
416 15 return 1;
417 }
418  
419  
420 26 ▶sub diff {
Subroutine diff: called CC=2 SCAR=6.9
Pod documented
421 26 my ($self, $opts) = @_;
422 26 my %opts = $self->_diff_options($opts);
423 21 my $source_model = $self->_introspect_current;
424 21 my $target_model = $self->_build_target_model;
425  
426 21 my $entries = $self->_extension_list;
427 21 delete $self->{_extension_protocol};
428 21 ▶ return $self->_diff_class->new(
Branch: 1 branch
BranchTrueFalseTotal
unless @$entries 13 8 21
429 source => $source_model,
430 target => $target_model,
431 %opts,
432 ) unless @$entries;
433  
434 # Loaded only when extensions are declared: a plain session pays nothing.
435 8 require DBIO::Extension::Protocol;
436 8 my $protocol = $self->{_extension_protocol} = DBIO::Extension::Protocol->new(
437 entries => $entries,
438 driver => $self->_extension_driver_helpers,
439 connection_schema => $self->_extension_connection_schema,
440 restricted => $self->_extension_restricted,
441 );
442 8 $protocol->inspect($self->_dbh);
443 8 ($source_model, $target_model)
444 = $protocol->extend_models($self, $source_model, $target_model);
445  
446 8 return $self->_diff_class->new(
447 source => $source_model,
448 target => $target_model,
449 extensions => $protocol,
450 %opts,
451 );
452 }
453  
454 # The options of diff, checked against what the diff class declares.
455 26 ▶sub _diff_options {
Subroutine _diff_options: called CC=7 SCAR=19.5
456 26 my ($self, $opts) = @_;
457 26 ▶ return () unless defined $opts;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts 16 10 26
458 10 ▶ DBIO::Exception->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless ref $opts eq "HASH" 2 8 10
Condition66%2/3
ref $opts || $opts
l!l&&r!l&&!r
110
MC/DC0%0/2
ref $opts || $opts
ref $opts $opts
Truth table66%2/3
A || B
A= ref $optsB= $opts
ABresult
1X1
011
000
459 "The options of diff must be a hash reference, got '" . (ref($opts) || $opts) . "'"
460 ) unless ref $opts eq 'HASH';
461  
462 8 my $class = $self->_diff_class;
463 8 my %known = map { $_ => 1 } $class->diff_options;
464 8 for my $key (sort keys %$opts) {
465 DBIO::Exception->throw(
466 "Unknown diff option '$key' for $class (known: "
467 . (join(', ', sort keys %known) || 'none') . ")"
468 8 ▶ ) unless $known{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless $known{$key} 3 5 8
Condition100%2/2
join(', ', sort(keys %known)) || 'none'
l!l
21
MC/DC100%1/1
join(', ', sort(keys %known)) || 'none'
join(', ', sort(keys %known))
Truth table100%2/2
A
A= join(', ', sort(keys %known))
Aresult
11
00
469 }
470 5 return %$opts;
471 }
472  
473  
474 58 ▶sub apply {
Subroutine apply: called CC=3 SCAR=11.0
Pod documented
475 58 my ($self, $diff, %opts) = @_;
476 58 my ($allow, $dry_run) = $self->_apply_options(%opts);
477 57 ▶ return unless $diff->has_changes;
Branch: 1 branch
BranchTrueFalseTotal
unless $diff->has_changes 4 53 57
478  
479 53 ▶ if ($dry_run) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dry_run) 6 47 53
480 6 $diff->as_sql; # a diff that cannot render fails here, as in a real run
481 6 return $diff;
482 }
483  
484 47 $self->_assert_diff_allowed($diff, $allow);
485 29 $self->_check_before_apply($diff);
486 28 return $self->_execute_diff($diff);
487 }
488  
489  
490 29 ▶sub _check_before_apply { return }
Subroutine _check_before_apply: called CC=1 SCAR=0.0
491  
492  
493 15 ▶sub _execute_diff {
Subroutine _execute_diff: called CC=2 SCAR=6.9
494 15 my ($self, $diff) = @_;
495 15 my @sql = $diff->as_sql;
496 15 ▶ $self->{_extension_protocol}->assert_no_refusals if $self->{_extension_protocol};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_extension_protocol"} 2 13 15
497 14 $self->_execute_ddl($self->_dbh, @sql);
498 14 return 1;
499 }
500  
501 # The arguments of apply and upgrade: (allow_destructive, dry_run).
502 76 ▶sub _apply_options {
Subroutine _apply_options: called CC=6 SCAR=17.9
503 76 my ($self, %opts) = @_;
504 76 my %known = map { $_ => 1 } qw(allow_destructive dry_run);
505 76 for my $key (sort keys %opts) {
506 DBIO::Exception->throw(
507 "Unknown option '$key' for apply/upgrade of " . ref($self)
508 . " (known: " . join(', ', sort keys %known) . ")"
509 34 ▶ ) unless $known{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless $known{$key} 2 32 34
510 }
511 my $allow = exists $opts{allow_destructive}
512 74 ▶ ? $opts{allow_destructive} : $self->allow_destructive;
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'allow_destructive'} ? : 24 50 74
513 74 ▶ return ($allow ? 1 : 0, $opts{dry_run} ? 1 : 0);
Branch: 2 branches
BranchTrueFalseTotal
$allow ? : 22 52 74
$opts{'dry_run'} ? : 8 66 74
514 }
515  
516 # Refusals stop every run; destructive and lossy operations stop a run that is
517 # not released with allow_destructive.
518 46 ▶sub _assert_diff_allowed {
Subroutine _assert_diff_allowed: called CC=9 SCAR=22.1
519 46 my ($self, $diff, $allow) = @_;
520 46 my $class = ref $diff;
521  
522 46 ▶ DBIO::Exception->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless $diff->can("classify") and $diff->can("destructive_operations") 1 45 46
Condition66%2/3
$diff->can("classify") and $diff->can("destructive_operations")
!ll&&!rl&&r
1045
MC/DC50%1/2
$diff->can("classify") and $diff->can("destructive_operations")
$diff->can("classify") $diff->can("destructive_operations")
Truth table66%2/3
A and B
A= $diff->can("classify")B= $diff->can("destructive_operations")
ABresult
0X0
100
111
523 "Cannot apply the diff: $class cannot classify itself (a diff class derives from DBIO::Diff::Base)"
524 ) unless $diff->can('classify') && $diff->can('destructive_operations');
525  
526 45 my $c = $diff->classify;
527 45 ▶ if (!defined $c->{class} || @{ $c->{refused} || [] }) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $c->{"class"} or @{$c->{"refused"} or [];}) 5 40 45
Condition60%3/5
not defined $c->{"class"} or @{$c->{"refused"} or [];}
l!l&&r!l&&!r
5040
$c->{'refused'} || []
l!l
400
MC/DC50%1/2
not defined $c->{"class"} or @{$c->{"refused"} or [];}
not defined $c->{"class"} @{$c->{"refused"} or [];}
MC/DC0%0/1
$c->{'refused'} || []
$c->{'refused'}
Truth table66%2/3
not(A) or B
A= not defined $c->{"class"}B= @{$c->{"refused"} or [];}
ABresult
1X1
011
000
Truth table50%1/2
A
A= $c->{'refused'}
Aresult
11
00
528 DBIO::Exception->throw(
529 "Refusing to apply the diff, it contains operations the deploy refuses:\n"
530 5 ▶ . join("\n", map { " $_" } @{ $c->{refused} || [] })
Condition50%1/2
$c->{'refused'} || []
l!l
50
MC/DC0%0/1
$c->{'refused'} || []
$c->{'refused'}
Truth table50%1/2
A
A= $c->{'refused'}
Aresult
11
00
531 );
532 }
533  
534 40 ▶ return if $allow;
Branch: 1 branch
BranchTrueFalseTotal
if $allow 13 27 40
535 27 my @risky = $diff->destructive_operations;
536 27 ▶ return unless @risky;
Branch: 1 branch
BranchTrueFalseTotal
unless @risky 15 12 27
537 DBIO::Exception->throw(
538 "Refusing to apply a destructive diff without allow_destructive => 1; no statement has run. "
539 . "These operations drop or may alter stored data:\n"
540 12 . join("\n", map { ' ' . $_->summary } @risky)
541 );
542 }
543  
544  
545 18 ▶sub upgrade {
Subroutine upgrade: called CC=2 SCAR=6.9
Pod documented
546 18 my ($self, %opts) = @_;
547 18 $self->_apply_options(%opts);
548 17 my $diff = $self->diff;
549 17 ▶ return unless $diff->has_changes;
Branch: 1 branch
BranchTrueFalseTotal
unless $diff->has_changes 2 15 17
550 15 $self->apply($diff, %opts);
551 12 return $diff;
552 }
553  
554 382 1;
555  
556 __END__
« blib/lib/DBIO/Replicated/Balancer/Random.pm ↑ Summary blib/lib/DBIO/Relationship/CascadeActions.pm »