blib/lib/DBIO/DeploymentHandler.pm

filter: Statementstmt 76.1% 112 / 147 Branchbran 63.1% 24 / 38 Conditioncond 46.6% 14 / 30 MC/DCmcdc 23.5% 4 / 17 Subroutinesub 65.1% 28 / 43 Podpod 100% 25 / 25 totaltotal 69.0% 207 / 300 CC 33 SCAR 14.2
CC
33
Cov
70%
install_version_storage
12.0
upgrade
10.5
_run_hooks
6.1
CRAP 4.1
« blib/lib/DBIO/Replicated/DebugProxy.pm ↑ Summary blib/lib/DBIO/Replicated/Balancer.pm »
1 package DBIO::DeploymentHandler;
2 # ABSTRACT: Extensible DBIO deployment
3  
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 our $VERSION = '0.900007';
8  
9 3 ▶use base 'DBIO::DeploymentHandler::Dad';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 ▶use DBIO::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use DBIO::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 ▶use DBIO::DeploymentHandler::DeployMethod::Native;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use DBIO::DeploymentHandler::VersionHandler::Monotonic;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use DBIO::DeploymentHandler::VersionStorage::Standard;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 3 ▶use DBIO::DeploymentHandler::HandlesDeploy;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶use DBIO::DeploymentHandler::HandlesVersioning;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 3 ▶use DBIO::DeploymentHandler::HandlesVersionStorage;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 3 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24  
25  
26 10 ▶sub new {
Subroutine new: called CC=5 SCAR=16.3
Pod documented
27 10 my ($class, @args) = @_;
28 10 ▶ my $args = @args == 1 ? $args[0] : { @args };
Branch: 1 branch
BranchTrueFalseTotal
@args == 1 ? : 10 0 10
29 10 my $self = $class->SUPER::new($args);
30 10 ▶ $self->{_dh_attrs} ||= {};
Condition50%1/2
$self->{"_dh_attrs"} ||= {}
l!l
010
MC/DC0%0/1
$self->{"_dh_attrs"} ||= {}
$self->{"_dh_attrs"}
Truth table50%1/2
A
A= $self->{"_dh_attrs"}
Aresult
11
00
31  
32 # Support database_version as alternative to initial_version
33 10 ▶ unless (exists $self->{_dh_attrs}{initial_version}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"_dh_attrs"}{"initial_version"}) 10 0 10
34 10 ▶ if (exists $args->{database_version}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{"database_version"}) 2 8 10
35 2 $self->{_dh_attrs}{initial_version} = $args->{database_version};
36 }
37 }
38  
39 # Initialize sub-objects
40 10 $self->_init_deploy_method;
41 8 $self->_init_version_storage;
42 8 $self->_init_version_handler;
43  
44 # Force version_storage to be eagerly loaded
45 8 $self->version_storage;
46  
47 8 return $self;
48 }
49  
50 10 ▶sub _init_deploy_method {
Subroutine _init_deploy_method: called CC=2 SCAR=7.0
51 10 my ($self) = @_;
52 $self->{deploy_method} = DBIO::DeploymentHandler::DeployMethod::Native->new({
53 schema => $self->schema,
54 schema_version => $self->schema_version,
55 version_source => $self->version_source,
56 10 ▶ txn_wrap => $self->{txn_wrap} // 1,
Condition50%1/2
$self->{"txn_wrap"} // 1
l!l
010
MC/DC0%0/1
$self->{"txn_wrap"} // 1
$self->{"txn_wrap"}
Truth table50%1/2
A
A= $self->{"txn_wrap"}
Aresult
11
00
57 });
58 10 DBIO::DeploymentHandler::HandlesDeploy->assert_implemented($self->{deploy_method});
59 8 return $self->{deploy_method};
60 }
61  
62 8 ▶sub _init_version_handler {
Subroutine _init_version_handler: called CC=2 SCAR=6.9
63 8 my ($self) = @_;
64 # Only an explicitly given start (database_version alias or initial_version)
65 # is forwarded -- the lazy default, the live database_version, would need the
66 # version table at construction time (karr k214).
67 $self->{version_handler} = DBIO::DeploymentHandler::VersionHandler::Monotonic->new({
68 schema_version => $self->schema_version,
69 initial_version => $self->{_dh_attrs}{initial_version} // $self->{initial_version},
70 8 ▶ to_version => $self->to_version,
Condition100%3/3
$self->{"_dh_attrs"}{"initial_version"} // $self->{"initial_version"}
l!l&&r!l&&!r
215
MC/DC100%2/2
$self->{"_dh_attrs"}{"initial_version"} // $self->{"initial_version"}
$self->{"_dh_attrs"}{"initial_version"} $self->{"initial_version"}
Truth table100%3/3
A // B
A= $self->{"_dh_attrs"}{"initial_version"}B= $self->{"initial_version"}
ABresult
1X1
011
000
71 });
72 8 DBIO::DeploymentHandler::HandlesVersioning->assert_implemented($self->{version_handler});
73 8 return $self->{version_handler};
74 }
75  
76 8 ▶sub _init_version_storage {
Subroutine _init_version_storage: called CC=1 SCAR=0.0
77 8 my ($self) = @_;
78 $self->{version_storage} = DBIO::DeploymentHandler::VersionStorage::Standard->new({
79 schema => $self->schema,
80 version_source => $self->version_source,
81 version_class => $self->{version_class},
82 8 });
83 8 DBIO::DeploymentHandler::HandlesVersionStorage->assert_implemented($self->{version_storage});
84 8 return $self->{version_storage};
85 }
86  
87 # Attributes
88  
89  
90 2 ▶sub initial_version {
Subroutine initial_version: called CC=3 SCAR=12.0
Pod documented
91 2 my $self = shift;
92 2 ▶ return $self->{_dh_attrs}{initial_version} if exists $self->{_dh_attrs}{initial_version};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"_dh_attrs"}{"initial_version"} 1 1 2
93 1 ▶ return $self->{initial_version} if exists $self->{initial_version};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"initial_version"} 1 0 1
94 0 $self->{_dh_attrs}{initial_version} = $self->database_version;
95 0 return $self->{_dh_attrs}{initial_version};
96 }
97  
98  
99 18 ▶sub version_source {
Subroutine version_source: called CC=2 SCAR=7.1
Pod documented
100 18 my $self = shift;
101 18 ▶ return $self->{version_source} if exists $self->{version_source};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"version_source"} 0 18 18
102 18 return '__VERSION';
103 }
104  
105  
106 0 ▶sub version_class {
Subroutine version_class: not called CC=2 SCAR=17.9
Pod documented
107 0 my $self = shift;
108 0 ▶ return $self->{version_class} if exists $self->{version_class};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"version_class"} 0 0 0
109 0 return 'DBIO::DeploymentHandler::VersionStorage::Standard::VersionResult';
110 }
111  
112  
113 3 ▶sub upgrade_hooks {
Subroutine upgrade_hooks: called CC=2 SCAR=6.9
Pod documented
114 3 my $self = shift;
115 3 ▶ return $self->{upgrade_hooks} ||= {};
Condition100%2/2
$self->{"upgrade_hooks"} ||= {}
l!l
21
MC/DC100%1/1
$self->{"upgrade_hooks"} ||= {}
$self->{"upgrade_hooks"}
Truth table100%2/2
A
A= $self->{"upgrade_hooks"}
Aresult
11
00
116 }
117  
118 # Sub-object accessors
119  
120  
121 3 ▶sub deploy_method { $_[0]->{deploy_method} }
Subroutine deploy_method: called CC=1 SCAR=0.0
Pod documented
122 5 ▶sub version_handler { $_[0]->{version_handler} }
Subroutine version_handler: called CC=1 SCAR=0.0
Pod documented
123 9 ▶sub version_storage { $_[0]->{version_storage} }
Subroutine version_storage: called CC=1 SCAR=0.0
Pod documented
124  
125 # Passthrough methods to deploy_method
126  
127  
128 0 ▶sub prepare_deploy {
Subroutine prepare_deploy: not called CC=1 SCAR=6.9
Pod documented
129 0 my $self = shift;
130 0 $self->deploy_method->prepare_deploy(@_);
131 }
132  
133 0 ▶sub deploy {
Subroutine deploy: not called CC=1 SCAR=6.9
Pod documented
134 0 my $self = shift;
135 0 $self->deploy_method->deploy(@_);
136 }
137  
138 0 ▶sub prepare_resultsource_install {
Subroutine prepare_resultsource_install: not called CC=1 SCAR=6.9
Pod documented
139 0 my $self = shift;
140 0 $self->deploy_method->prepare_resultsource_install(@_);
141 }
142  
143 0 ▶sub install_resultsource {
Subroutine install_resultsource: not called CC=1 SCAR=6.9
Pod documented
144 0 my $self = shift;
145 0 $self->deploy_method->install_resultsource(@_);
146 }
147  
148 0 ▶sub prepare_upgrade {
Subroutine prepare_upgrade: not called CC=1 SCAR=6.9
Pod documented
149 0 my $self = shift;
150 0 $self->deploy_method->prepare_upgrade(@_);
151 }
152  
153 0 ▶sub txn_do {
Subroutine txn_do: not called CC=1 SCAR=6.9
Pod documented
154 0 my $self = shift;
155 0 $self->deploy_method->txn_do(@_);
156 }
157  
158 # Native deploy reconciles the whole schema in one shot. Override Dad's
159 # version-step loop with a single deploy_method->upgrade call, framed by
160 # per-version pre/post hooks for any data migrations the DDL diff cannot
161 # express.
162  
163  
164 3 ▶sub upgrade {
Subroutine upgrade: called CC=9 SCAR=23.5
Pod documented
165 3 my $self = shift;
166 3 ▶ my $args = shift || {};
Condition50%1/2
shift() || {}
l!l
03
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
167  
168 3 my $from = $self->database_version;
169 3 ▶ my $to = $args->{to_version} // $self->schema_version;
Condition33%1/3
$args->{"to_version"} // $self->schema_version
l!l&&r!l&&!r
030
MC/DC0%0/2
$args->{"to_version"} // $self->schema_version
$args->{"to_version"} $self->schema_version
Truth table33%1/3
A // B
A= $args->{"to_version"}B= $self->schema_version
ABresult
1X1
011
000
170 3 ▶ $from = ref($from) ? $from->numify : $from;
Branch: 1 branch
BranchTrueFalseTotal
ref $from ? : 0 3 3
171 3 ▶ $to = ref($to) ? $to->numify : $to;
Branch: 1 branch
BranchTrueFalseTotal
ref $to ? : 0 3 3
172  
173 3 ▶ if ($from == $to) {
Branch: 1 branch
BranchTrueFalseTotal
if ($from == $to) 1 2 3
174 1 carp('no need to run upgrade');
175 1 return;
176 }
177 2 ▶ DBIO::Exception->throw("downgrade from $from to $to not supported by native deploy")
Branch: 1 branch
BranchTrueFalseTotal
if $from > $to 1 1 2
178 if $from > $to;
179  
180 1 carp("upgrading from $from to $to");
181  
182 1 my $diff;
183 1 ▶ my $body = sub {
Subroutine __ANON__: called CC=4 SCAR=15.2
184 1 $self->_run_hooks('pre', $from, $to);
185  
186 $diff = $self->deploy_method->upgrade(
187 exists $args->{allow_destructive}
188 ? { allow_destructive => $args->{allow_destructive} } : ()
189 1 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
exists $args->{'allow_destructive'} ? : 0 1 1
190  
191 1 $self->_run_hooks('post', $from, $to);
192  
193 1 ▶ my $upgrade_sql = ($diff && $diff->can('as_sql')) ? $diff->as_sql : undef;
Branch: 1 branch
BranchTrueFalseTotal
$diff && $diff->can('as_sql') ? : 0 1 1
Condition33%1/3
$diff and $diff->can("as_sql")
!ll&&!rl&&r
100
MC/DC0%0/2
$diff and $diff->can("as_sql")
$diff $diff->can("as_sql")
Truth table33%1/3
A and B
A= $diffB= $diff->can("as_sql")
ABresult
0X0
100
111
194  
195 1 $self->add_database_version({
196 version => $to,
197 ddl => undef,
198 upgrade_sql => $upgrade_sql,
199 });
200 1 };
201  
202 # F10: only wrap in txn_do when the engine can honour it. On engines
203 # whose DDL forces an implicit COMMIT (MySQL and MariaDB on every
204 # version, Oracle, Sybase, Informix), whose rebuild path depends on
205 # AutoCommit=on (SQLite) or whose driver declines the capability
206 # (DB2), the wrap is a no-op or a regression -- the body runs
207 # statement-at-a-time and the __VERSION row gate is the forward-progress
208 # recovery story.
209 1 ▶ if ($self->_storage_uses_transactional_ddl) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_storage_uses_transactional_ddl) { } 0 1 1
210 0 $self->txn_do($body);
211 }
212 else {
213 1 ▶ carp_once("non-transactional upgrade on @{[ ref($self->schema->storage) || $self->schema->storage ]} -- upgrade is not atomic; recovery depends on the __VERSION row gate");
Condition33%1/3
ref $self->schema->storage || $self->schema->storage
l!l&&r!l&&!r
100
MC/DC0%0/2
ref $self->schema->storage || $self->schema->storage
ref $self->schema->storage $self->schema->storage
Truth table33%1/3
A || B
A= ref $self->schema->storageB= $self->schema->storage
ABresult
1X1
011
000
214 1 $body->();
215 }
216  
217 1 return $diff;
218 }
219  
220 # F10: helper that probes the storage's transactional_ddl capability. False
221 # when the storage is missing, the capability is unset, or the storage
222 # reports it as 0. Drivers register via _use_transactional_ddl(1|0) in
223 # L<DBIO::Storage::DBI::Capabilities>.
224 1 ▶sub _storage_uses_transactional_ddl {
Subroutine _storage_uses_transactional_ddl: called CC=4 SCAR=14.6
225 1 my ($self) = @_;
226 1 ▶ my $storage = eval { $self->schema->storage } || return 0;
Condition50%1/2
eval { do { $self->schema->storage } } || (return 0)
l!l
10
MC/DC0%0/1
eval { do { $self->schema->storage } } || (return 0)
eval { do { $self->schema->storage } }
Truth table50%1/2
A
A= eval { do { $self->schema->storage } }
Aresult
11
00
227 1 ▶ return 0 unless $storage->can('_use_transactional_ddl');
Branch: 1 branch
BranchTrueFalseTotal
unless $storage->can("_use_transactional_ddl") 0 1 1
228 1 ▶ return $storage->_use_transactional_ddl ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$storage->_use_transactional_ddl ? : 0 1 1
229 }
230  
231  
232 1 ▶sub downgrade {
Subroutine downgrade: called CC=1 SCAR=0.0
Pod documented
233 1 DBIO::Exception->throw(
234 'downgrade is not supported by the native DBIO deploy method '
235 . '(driver diffs current code vs. live DB, not historical versions)'
236 );
237 }
238  
239 2 ▶sub _run_hooks {
Subroutine _run_hooks: called CC=6 SCAR=18.0
240 2 my ($self, $phase, $from, $to) = @_;
241 2 my $hooks = $self->upgrade_hooks;
242 2 ▶ return unless %$hooks;
Branch: 1 branch
BranchTrueFalseTotal
unless %$hooks 0 2 2
243  
244 2 for my $version (sort { $a <=> $b } keys %$hooks) {
245 6 ▶ next unless $version > $from && $version <= $to;
Branch: 1 branch
BranchTrueFalseTotal
unless $version > $from and $version <= $to 2 4 6
Condition66%2/3
$version > $from and $version <= $to
!ll&&!rl&&r
024
MC/DC50%1/2
$version > $from and $version <= $to
$version > $from $version <= $to
Truth table66%2/3
A and B
A= $version > $fromB= $version <= $to
ABresult
0X0
100
111
246 4 ▶ my $hook = $hooks->{$version}{$phase} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $hook = $hooks->{$version}{$phase} or next 3 1 4
247 3 $hook->($self, {
248 version => $version,
249 from => $from,
250 to => $to,
251 phase => $phase,
252 });
253 }
254 }
255  
256 # Passthrough methods to version_storage
257  
258  
259 0 ▶sub add_database_version {
Subroutine add_database_version: not called CC=1 SCAR=6.9
Pod documented
260 0 my $self = shift;
261 0 $self->version_storage->add_database_version(@_);
262 }
263  
264 0 ▶sub database_version {
Subroutine database_version: not called CC=1 SCAR=6.9
Pod documented
265 0 my $self = shift;
266 0 $self->version_storage->database_version(@_);
267 }
268  
269 0 ▶sub delete_database_version {
Subroutine delete_database_version: not called CC=1 SCAR=6.9
Pod documented
270 0 my $self = shift;
271 0 $self->version_storage->delete_database_version(@_);
272 }
273  
274 0 ▶sub version_storage_is_installed {
Subroutine version_storage_is_installed: not called CC=1 SCAR=6.9
Pod documented
275 0 my $self = shift;
276 0 $self->version_storage->version_storage_is_installed(@_);
277 }
278  
279 # Passthrough methods to version_handler
280  
281  
282 2 ▶sub next_version_set {
Subroutine next_version_set: called CC=1 SCAR=0.0
Pod documented
283 2 my $self = shift;
284 2 $self->version_handler->next_version_set(@_);
285 }
286  
287 0 ▶sub previous_version_set {
Subroutine previous_version_set: not called CC=1 SCAR=6.9
Pod documented
288 0 my $self = shift;
289 0 $self->version_handler->previous_version_set(@_);
290 }
291  
292 # Orthogonal methods
293  
294  
295 0 ▶sub prepare_version_storage_install {
Subroutine prepare_version_storage_install: not called CC=1 SCAR=6.9
Pod documented
296 0 my $self = shift;
297 0 $self->prepare_resultsource_install({
298 result_source => $self->version_storage->version_rs->result_source
299 });
300 }
301  
302  
303 0 ▶sub install_version_storage {
Subroutine install_version_storage: not called CC=3 SCAR=24.8
Pod documented
304 0 my $self = shift;
305 0 ▶ my $version = (shift || {})->{version} || $self->schema_version;
Condition0%0/5
+(shift() || {})->{'version'} || $self->schema_version
l!l&&r!l&&!r
000
shift() || {}
l!l
00
MC/DC0%0/2
+(shift() || {})->{'version'} || $self->schema_version
shift() $self->schema_version
Truth table0%0/3
A || B
A= shift()B= $self->schema_version
ABresult
1X1
011
000
306 0 $self->install_resultsource({
307 result_source => $self->version_storage->version_rs->result_source,
308 version => $version,
309 });
310 }
311  
312  
313 0 ▶sub prepare_install {
Subroutine prepare_install: not called CC=1 SCAR=6.9
Pod documented
314 0 $_[0]->prepare_deploy;
315 0 $_[0]->prepare_version_storage_install;
316 }
317  
318 3 1;
319  
320 # vim: ts=2 sw=2 expandtab
321  
322 __END__
« blib/lib/DBIO/Replicated/DebugProxy.pm ↑ Summary blib/lib/DBIO/Replicated/Balancer.pm »