DBIx/QuickDB/Driver.pm

filter: Statementstmt 15.2% 35 / 230 Branchbran 0.0% 0 / 108 Conditioncond 0.0% 0 / 50 MC/DCmcdc 0.0% 0 / 29 Subroutinesub 23.9% 11 / 46 Podpod 83.8% 26 / 31 totaltotal 14.5% 72 / 494 CC 76 SCAR 41.2
CC
76
Cov
9%
run_command
240.0
DESTROY
132.0
clone
72.0
CRAP 61.6
« DBIx/QuickDB/Driver/MySQL.pm ↑ Summary DBIx/QuickDB/Driver/MariaDB.pm »
1 package DBIx::QuickDB::Driver;
2 23 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 23 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 23 our $VERSION = '0.000047';
6  
7 23 ▶use Carp qw/croak confess/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 23 ▶use File::Path qw/remove_tree/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 23 ▶use File::Temp qw/tempdir/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 23 ▶use POSIX ":sys_wait_h";
Subroutine BEGIN: called CC=1 SCAR=0.0
11 23 ▶use Scalar::Util qw/blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 23 ▶use Time::HiRes qw/sleep time/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 23 ▶use DBIx::QuickDB::Util qw/clone_dir env_timeout/;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 23 ▶use DBIx::QuickDB::Watcher;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 23 use DBIx::QuickDB::Util::HashBase qw{
19 -root_pid
20 -dir
21 -_cleanup
22 -autostop -autostart
23 verbose
24 -_log_id
25 username
26 password
27 env_vars
28 <watcher
29 <cloned_from
30 23 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 0 ▶sub viable { (0, "viable() is not implemented for the " . $_[0]->name . " driver") }
Subroutine viable: not called CC=1 SCAR=6.9
Pod documented
33  
34 0 ▶sub socket { confess "socket() is not implemented for the " . $_[0]->name . " driver" }
Subroutine socket: not called CC=1 SCAR=6.9
Pod documented
35 0 ▶sub load_sql { confess "load_sql() is not implemented for the " . $_[0]->name . " driver" }
Subroutine load_sql: not called CC=1 SCAR=6.9
Pod documented
36 0 ▶sub bootstrap { confess "bootstrap() is not implemented for the " . $_[0]->name . " driver" }
Subroutine bootstrap: not called CC=1 SCAR=6.9
37 0 ▶sub connect_string { confess "connect_string() is not implemented for the " . $_[0]->name . " driver" }
Subroutine connect_string: not called CC=1 SCAR=6.9
Pod documented
38 0 ▶sub start_command { confess "start_command() is not implemented for the " . $_[0]->name . " driver" }
Subroutine start_command: not called CC=1 SCAR=6.9
Pod documented
39 0 ▶sub shell_command { confess "shell_command() is not implemented for the " . $_[0]->name . " driver" }
Subroutine shell_command: not called CC=1 SCAR=6.9
Pod documented
40  
41 0 ▶sub error_log { undef }
Subroutine error_log: not called CC=1 SCAR=6.9
Pod undocumented
42  
43 sub read_error_log {
44 0 ▶ my $self = shift;
Subroutine read_error_log: not called CC=4 SCAR=30.0
Pod undocumented
45 0 ▶ my $log = $self->error_log or return "";
Branch: 1 branch
BranchTrueFalseTotal
my $log = $self->error_log or return "" 0 0 0
46 0 ▶ return "" unless -f $log;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $log 0 0 0
47 0 ▶ open(my $fh, '<', $log) or return "Could not open error log '$log': $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $log or return "Could not open error log '${log}': $!" 0 0 0
48 0 return join "" => <$fh>;
49 }
50  
51 0 ▶sub list_env_vars { qw/DBI_USER DBI_PASS DBI_DSN/ }
Subroutine list_env_vars: not called CC=1 SCAR=6.9
Pod documented
52  
53 0 ▶sub version_string { 'unknown' }
Subroutine version_string: not called CC=1 SCAR=6.9
Pod documented
54  
55 0 ▶sub stop_sig { 'TERM' }
Subroutine stop_sig: not called CC=1 SCAR=6.9
Pod documented
56  
57 ▶sub write_config {}
Subroutine write_config: not called CC=1 SCAR=0.0
Pod documented
58  
59 sub do_in_env {
60 0 ▶ my $self = shift;
Subroutine do_in_env: not called CC=2 SCAR=17.9
Pod documented
61 0 my ($code) = @_;
62  
63 0 my $old = $self->mask_env_vars;
64  
65 0 my $ok = eval { $code->(); 1 };
66 0 my $err = $@;
67  
68 0 $self->unmask_env_vars($old);
69  
70 0 ▶ die $err unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 0 0
71  
72 0 return;
73 }
74  
75 sub mask_env_vars {
76 0 ▶ my $self = shift;
Subroutine mask_env_vars: not called CC=6 SCAR=37.4
Pod documented
77  
78 0 my %old;
79  
80 0 for my $var ($self->list_env_vars) {
81 0 ▶ next unless defined $ENV{$var};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ENV{$var} 0 0 0
82 0 $old{$var} = delete $ENV{$var};
83 }
84  
85 0 ▶ my $env_vars = $self->env_vars || {};
Condition0%0/2
$self->env_vars || {}
l!l
00
MC/DC0%0/1
$self->env_vars || {}
$self->env_vars
Truth table0%0/2
A
A= $self->env_vars
Aresult
11
00
86 0 for my $var (keys %$env_vars) {
87 0 ▶ $old{$var} = delete $ENV{$var} unless defined $old{$var};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $old{$var} 0 0 0
88 0 $ENV{$var} = $env_vars->{$var};
89 }
90  
91 0 return \%old;
92 }
93  
94 sub unmask_env_vars {
95 0 ▶ my $self = shift;
Subroutine unmask_env_vars: not called CC=3 SCAR=24.8
Pod documented
96 0 my ($old) = @_;
97  
98 0 for my $var (keys %$old) {
99 0 my $val = $old->{$var};
100  
101 0 ▶ if (defined $val) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) { } 0 0 0
102 0 $ENV{$var} = $val;
103 }
104 else {
105 0 delete $ENV{$var};
106 }
107 }
108  
109 0 return;
110 }
111  
112 sub name {
113 0 ▶ my $in = shift;
Subroutine name: not called CC=2 SCAR=17.9
Pod documented
114 0 ▶ my $type = blessed($in) || $in;
Condition0%0/3
builtin::blessed($in) || $in
l!l&&r!l&&!r
000
MC/DC0%0/2
builtin::blessed($in) || $in
builtin::blessed($in) $in
Truth table0%0/3
A || B
A= builtin::blessed($in)B= $in
ABresult
1X1
011
000
115  
116 0 $type =~ s/^DBIx::QuickDB::Driver:://;
117  
118 0 return $type;
119 }
120  
121 sub init {
122 0 ▶ my $self = shift;
Subroutine init: not called CC=5 SCAR=34.0
Pod documented
123  
124 0 ▶ confess "'dir' is a required attribute" unless $self->{+DIR};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"dir"} 0 0 0
125  
126 0 $self->{+ROOT_PID} = $$;
127 0 $self->{+_CLEANUP} = delete $self->{cleanup};
128  
129 0 ▶ $self->{+USERNAME} = '' unless defined $self->{+USERNAME};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"username"} 0 0 0
130 0 ▶ $self->{+PASSWORD} = '' unless defined $self->{+PASSWORD};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"password"} 0 0 0
131  
132 0 ▶ $self->{+ENV_VARS} ||= {};
Condition0%0/2
$self->{"env_vars"} ||= {}
l!l
00
MC/DC0%0/1
$self->{"env_vars"} ||= {}
$self->{"env_vars"}
Truth table0%0/2
A
A= $self->{"env_vars"}
Aresult
11
00
133  
134 0 return;
135 }
136  
137 sub clone_data {
138 0 ▶ my $self = shift;
Subroutine clone_data: not called CC=1 SCAR=6.9
Pod documented
139  
140 return (
141 USERNAME() => $self->{+USERNAME},
142 PASSWORD() => $self->{+PASSWORD},
143 VERBOSE() => $self->{+VERBOSE},
144 AUTOSTOP() => $self->{+AUTOSTOP},
145 AUTOSTART() => $self->{+AUTOSTART},
146  
147 cleanup => $self->{+_CLEANUP},
148  
149 0 ENV_VARS() => {%{$self->{+ENV_VARS}}},
150 );
151 }
152  
153 sub resync {
154 0 ▶ my $self = shift;
Subroutine resync: not called CC=6 SCAR=37.4
Pod documented
155  
156 0 ▶ my $from = $self->{+CLONED_FROM} or croak "No original source to sync from";
Branch: 1 branch
BranchTrueFalseTotal
my $from = $self->{"cloned_from"} or croak("No original source to sync from") 0 0 0
157  
158 0 my $started = $self->started;
159 0 ▶ $self->stop if $started;
Branch: 1 branch
BranchTrueFalseTotal
if $started 0 0 0
160  
161 0 ▶ clone_dir($from, $self->{+DIR}, verbose => (($self->{+VERBOSE} // 0) > 2) ? 1 : 0, checksum => 1);
Branch: 1 branch
BranchTrueFalseTotal
($self->{'verbose'} // 0) > 2 ? : 0 0 0
Condition0%0/2
$self->{"verbose"} // 0
l!l
00
MC/DC0%0/1
$self->{"verbose"} // 0
$self->{"verbose"}
Truth table0%0/2
A
A= $self->{"verbose"}
Aresult
11
00
162  
163 0 $self->write_config();
164  
165 0 ▶ $self->start if $started;
Branch: 1 branch
BranchTrueFalseTotal
if $started 0 0 0
166 }
167  
168 sub clone {
169 0 ▶ my $self = shift;
Subroutine clone: not called CC=8 SCAR=42.8
Pod documented
170 0 my %params = @_;
171  
172 0 ▶ confess "Cannot clone a started database, please stop it first."
Branch: 1 branch
BranchTrueFalseTotal
if $self->started 0 0 0
173 if $self->started;
174  
175 0 my $orig_dir = $self->{+DIR};
176 0 ▶ my $new_dir = delete $params{dir} // tempdir('DB-QUICK-CLONE-XXXXXX', CLEANUP => 0, $ENV{QDB_TMPDIR} ? (DIR => $ENV{QDB_TMPDIR}) : (TMPDIR => 1));
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'QDB_TMPDIR'} ? : 0 0 0
Condition0%0/3
delete $params{"dir"} // tempdir("DB-QUICK-CLONE-XXXXXX", "CLEANUP", 0, $ENV{"QDB_TMPDIR"} ? ("DIR", $ENV{"QDB_TMPDIR"}) : ("TMPDIR", 1))
l!l&&r!l&&!r
000
MC/DC0%0/2
delete $params{"dir"} // tempdir("DB-QUICK-CLONE-XXXXXX", "CLEANUP", 0, $ENV{"QDB_TMPDIR"} ? ("DIR", $ENV{"QDB_TMPDIR"}) : ("TMPDIR", 1))
delete $params{"dir"} tempdir("DB-QUICK-CLONE-XXXXXX", "CLEANUP", 0, $ENV{"QDB_TMPDIR"} ? ("DIR", $ENV{"QDB_TMPDIR"}) : ("TMPDIR", 1))
Truth table0%0/3
A // B
A= delete $params{"dir"}B= tempdir("DB-QUICK-CLONE-XXXXXX", "CLEANUP", 0, $ENV{"QDB_TMPDIR"} ? ("DIR", $ENV{"QDB_TMPDIR"}) : ("TMPDIR", 1))
ABresult
1X1
011
000
177  
178 0 ▶ clone_dir($orig_dir, $new_dir, verbose => (($self->{+VERBOSE} // 0) > 2) ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
($self->{'verbose'} // 0) > 2 ? : 0 0 0
Condition0%0/2
$self->{"verbose"} // 0
l!l
00
MC/DC0%0/1
$self->{"verbose"} // 0
$self->{"verbose"}
Truth table0%0/2
A
A= $self->{"verbose"}
Aresult
11
00
179  
180 0 my $class = ref($self);
181 my %ok = (
182 cleanup => 1,
183 0 map {$_ => 1} DBIx::QuickDB::Util::HashBase::attr_list($class),
184 );
185 0 my @bad = grep { !$ok{$_} } keys %params;
186  
187 0 ▶ confess "Invalid options to clone(): " . join(', ' => @bad)
Branch: 1 branch
BranchTrueFalseTotal
if @bad 0 0 0
188 if @bad;
189  
190 0 my $clone = $class->new(
191 $self->clone_data,
192  
193 %params,
194  
195 DIR() => $new_dir,
196  
197 WATCHER() => undef,
198  
199 CLONED_FROM() => $orig_dir,
200 );
201  
202 0 $clone->write_config();
203 0 ▶ $clone->start if $clone->{+AUTOSTART};
Branch: 1 branch
BranchTrueFalseTotal
if $clone->{"autostart"} 0 0 0
204  
205 0 return $clone;
206 }
207  
208 sub gen_log {
209 0 ▶ my $self = shift;
Subroutine gen_log: not called CC=2 SCAR=17.9
Pod undocumented
210 0 ▶ return if $self->no_log(@_);
Branch: 1 branch
BranchTrueFalseTotal
if $self->no_log(@_) 0 0 0
211 0 return $self->{+DIR} . "/cmd-log-$$-" . $self->{+_LOG_ID}++;
212 }
213  
214 sub no_log {
215 0 ▶ my $self = shift;
Subroutine no_log: not called CC=3 SCAR=24.8
Pod documented
216 0 my ($params) = @_;
217 0 ▶ return $self->{+VERBOSE} || $params->{no_log} || $ENV{DB_VERBOSE};
Condition0%0/6
$self->{'verbose'} || $params->{'no_log'} || $ENV{'DB_VERBOSE'}
l!l&&r!l&&!r
000
$self->{"verbose"} or $params->{"no_log"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$self->{'verbose'} || $params->{'no_log'} || $ENV{'DB_VERBOSE'}
$self->{"verbose"} $params->{"no_log"} $ENV{'DB_VERBOSE'}
Truth table0%0/4
A or B || C
A= $self->{"verbose"}B= $params->{"no_log"}C= $ENV{'DB_VERBOSE'}
ABCresult
1XX1
01X1
0011
0000
218 }
219  
220 sub run_command {
221 0 ▶ my $self = shift;
Subroutine run_command: not called CC=15 SCAR=54.8
Pod documented
222 0 my ($cmd, $params) = @_;
223  
224 0 my $no_log = $self->no_log($params);
225 0 ▶ my $log_file = $params->{log_file} || ($no_log ? undef : $self->gen_log);
Branch: 1 branch
BranchTrueFalseTotal
$no_log ? : 0 0 0
Condition0%0/3
$params->{'log_file'} || ($no_log ? undef : $self->gen_log)
l!l&&r!l&&!r
000
MC/DC0%0/2
$params->{'log_file'} || ($no_log ? undef : $self->gen_log)
$params->{'log_file'} ($no_log ? undef : $self->gen_log)
Truth table0%0/3
A || B
A= $params->{'log_file'}B= ($no_log ? undef : $self->gen_log)
ABresult
1X1
011
000
226  
227 0 my $pid = fork();
228 0 ▶ croak "Could not fork" unless defined $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 0 0 0
229  
230 0 ▶ if ($pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) 0 0 0
231 0 local $?;
232 0 ▶ return ($pid, $log_file) if $params->{no_wait};
Branch: 1 branch
BranchTrueFalseTotal
if $params->{"no_wait"} 0 0 0
233 0 my $ret = waitpid($pid, 0);
234 0 my $exit = $?;
235 0 ▶ die "waitpid returned $ret" unless $ret == $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless $ret == $pid 0 0 0
236  
237 0 ▶ return unless $exit;
Branch: 1 branch
BranchTrueFalseTotal
unless $exit 0 0 0
238  
239 0 my $log = "";
240 0 ▶ unless ($no_log) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($no_log) 0 0 0
241 0 ▶ open(my $fh, '<', $log_file) or warn "Failed to open log: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $log_file or warn "Failed to open log: $!" 0 0 0
242 0 $log = eval { join "" => <$fh> };
243 }
244 0 my $error_log = $self->read_error_log;
245 0 ▶ $log .= "\n=== error log ===\n$error_log" if length $error_log;
Branch: 1 branch
BranchTrueFalseTotal
if length $error_log 0 0 0
246 0 croak "Failed to run command '" . join(' ' => @$cmd) . "' ($exit)\n$log";
247 }
248  
249 0 $self->mask_env_vars;
250  
251 0 ▶ unless ($no_log) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($no_log) 0 0 0
252 0 ▶ open(my $log, '>', $log_file) or die "Could not open log file ($log_file): $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $log, ">", $log_file or die "Could not open log file ($log_file): $!" 0 0 0
253 0 close(STDOUT);
254 0 open(STDOUT, '>&', $log);
255 0 close(STDERR);
256 0 open(STDERR, '>&', $log);
257 }
258  
259 0 ▶ if (my $file = $params->{stdin}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $file = $params->{"stdin"}) 0 0 0
260 0 close(STDIN);
261 0 ▶ open(STDIN, '<', $file) or die "Could not open new STDIN ($file): $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDIN, "<", $file or die "Could not open new STDIN ($file): $!" 0 0 0
262 }
263  
264 0 exec(@$cmd);
265 }
266  
267 0 ▶sub should_cleanup { shift->{+_CLEANUP} }
Subroutine should_cleanup: not called CC=1 SCAR=6.9
Pod documented
268  
269 # Flush server state durably to disk so a hard kill mid-shutdown cannot leave
270 # the data dir needing crash recovery. Drivers that benefit (e.g. PostgreSQL)
271 # override this; the default is a no-op.
272 ▶sub checkpoint { }
Subroutine checkpoint: not called CC=1 SCAR=0.0
Pod undocumented
273  
274 sub cleanup {
275 0 ▶ my $self = shift;
Subroutine cleanup: not called CC=2 SCAR=17.9
Pod documented
276  
277 # Ignore errors here.
278 0 my $err = [];
279 0 ▶ remove_tree($self->{+DIR}, {safe => 1, error => \$err}) if -d $self->{+DIR};
Branch: 1 branch
BranchTrueFalseTotal
if -d $self->{"dir"} 0 0 0
280 0 return;
281 }
282  
283 sub connect {
284 0 ▶ my $self = shift;
Subroutine connect: not called CC=2 SCAR=17.9
Pod documented
285 0 my ($db_name, %params) = @_;
286  
287 0 ▶ %params = (AutoCommit => 1, RaiseError => 1) unless @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ > 1 0 0 0
288  
289 0 my $dbh;
290 $self->do_in_env(
291 sub {
292 0 ▶ my $cstring = $self->connect_string($db_name);
Subroutine __ANON__: not called CC=1 SCAR=6.9
293 0 require DBI;
294 0 $dbh = DBI->connect($cstring, $self->username, $self->password, \%params);
295 }
296 0 );
297  
298 0 return $dbh;
299 }
300  
301 sub started {
302 0 ▶ my $self = shift;
Subroutine started: not called CC=3 SCAR=24.8
Pod undocumented
303  
304 0 my $socket = $self->socket;
305 0 ▶ return 1 if $self->{+WATCHER} || -S $socket;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"watcher"} or -S $socket 0 0 0
Condition0%0/3
$self->{"watcher"} or -S $socket
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"watcher"} or -S $socket
$self->{"watcher"} -S $socket
Truth table0%0/3
A or B
A= $self->{"watcher"}B= -S $socket
ABresult
1X1
011
000
306 0 return 0;
307 }
308  
309 sub start {
310 0 ▶ my $self = shift;
Subroutine start: not called CC=5 SCAR=34.0
Pod documented
311 0 my @args = @_;
312  
313 0 my $dir = $self->{+DIR};
314 0 my $socket = $self->socket;
315  
316 0 ▶ return if $self->{+WATCHER} || -S $socket;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"watcher"} or -S $socket 0 0 0
Condition0%0/3
$self->{"watcher"} or -S $socket
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"watcher"} or -S $socket
$self->{"watcher"} -S $socket
Truth table0%0/3
A or B
A= $self->{"watcher"}B= -S $socket
ABresult
1X1
011
000
317  
318 0 my $watcher = $self->{+WATCHER} = DBIx::QuickDB::Watcher->new(db => $self, args => \@args);
319  
320 # Defaults to 10s; tunable via QDB_START_TIMEOUT for slow hosts that need
321 # longer to bring a server up (e.g. a clone doing crash recovery).
322 0 my $timeout = env_timeout(QDB_START_TIMEOUT => 10);
323  
324 0 my $start = time;
325 0 ▶ until (-S $socket) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-S $socket) 0 0 0
326 0 my $waited = time - $start;
327  
328 0 ▶ if ($waited > $timeout) {
Branch: 1 branch
BranchTrueFalseTotal
if ($waited > $timeout) 0 0 0
329 0 my $error_log = $self->read_error_log;
330 0 $watcher->eliminate();
331 0 confess "Timed out waiting for server to start\n$error_log";
332 0 last;
333 }
334  
335 0 sleep 0.01;
336 }
337  
338 0 return;
339 }
340  
341 sub stop {
342 0 ▶ my $self = shift;
Subroutine stop: not called CC=8 SCAR=42.8
Pod documented
343 0 my %params = @_;
344  
345 0 ▶ my $watcher = delete $self->{+WATCHER} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $watcher = delete $self->{"watcher"} or return 0 0 0
346  
347 # Flush a durable checkpoint while the server is still running. If shutdown
348 # is slow enough to be SIGKILLed, this ensures the on-disk state is already
349 # consistent so a later clone does not crash-recover and jump SERIAL
350 # sequences forward (PostgreSQL SEQ_LOG_VALS=32), corrupting the clone.
351 # No-op for drivers that do not need it.
352 0 $self->checkpoint;
353  
354 DBI->visit_handles(
355 sub {
356 0 ▶ my ($driver_handle) = @_;
Subroutine __ANON__: not called CC=5 SCAR=34.0
357  
358 $driver_handle->disconnect
359 if $driver_handle->{Type} && $driver_handle->{Type} eq 'db'
360 0 ▶ && $driver_handle->{Name} && index($driver_handle->{Name}, $self->{+DIR}) >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $driver_handle->{"Type"} and $driver_handle->{"Type"} eq "db" and $driver_handle->{"Name"} and index($driver_handle->{"Name"}, $self->{"dir"}) != -1 0 0 0
Condition0%0/9
$driver_handle->{"Type"} and $driver_handle->{"Type"} eq "db" and $driver_handle->{"Name"} and index($driver_handle->{"Name"}, $self->{"dir"}) != -1
!ll&&!rl&&r
000
$driver_handle->{"Type"} and $driver_handle->{"Type"} eq "db" and $driver_handle->{"Name"}
!ll&&!rl&&r
000
$driver_handle->{"Type"} and $driver_handle->{"Type"} eq "db"
!ll&&!rl&&r
000
MC/DC0%0/4
$driver_handle->{"Type"} and $driver_handle->{"Type"} eq "db" and $driver_handle->{"Name"} and index($driver_handle->{"Name"}, $self->{"dir"}) != -1
$driver_handle->{"Type"} $driver_handle->{"Type"} eq "db" $driver_handle->{"Name"} index($driver_handle->{"Name"}, $self->{"dir"}) != -1
Truth table0%0/5
A and B and C and D
A= $driver_handle->{"Type"}B= $driver_handle->{"Type"} eq "db"C= $driver_handle->{"Name"}D= index($driver_handle->{"Name"}, $self->{"dir"}) != -1
ABCDresult
0XXX0
10XX0
110X0
11100
11111
361  
362 0 return 1;
363 }
364 0 );
365  
366 0 my $server_pid = $watcher->server_pid;
367 0 $watcher->stop();
368  
369 0 ▶ unless ($params{no_wait}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($params{"no_wait"}) 0 0 0
370 0 $watcher->wait();
371  
372 # The watcher reaps the server process before it exits, so once wait()
373 # has returned the server is gone. Confirm via the pid rather than the
374 # unix socket file: a server killed with SIGKILL (e.g. a slow shutdown
375 # that blew the watcher's grace period) never gets to remove its socket,
376 # so waiting for the socket to disappear would hang and then time out.
377 # The watcher escalates to SIGKILL after QDB_STOP_GRACE and gives up at
378 # twice that; outlast the watcher's give-up so we never confess before
379 # it has finished reaping the server. Defaults to 10s (grace 4).
380 0 my $stop_timeout = env_timeout(QDB_STOP_GRACE => 4) * 2 + 2;
381 0 my $start = time;
382 0 ▶ while ($server_pid && kill(0, $server_pid)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($server_pid and kill 0, $server_pid) 0 0 0
Condition0%0/3
$server_pid and kill 0, $server_pid
!ll&&!rl&&r
000
MC/DC0%0/2
$server_pid and kill 0, $server_pid
$server_pid kill 0, $server_pid
Truth table0%0/3
A and B
A= $server_pidB= kill 0, $server_pid
ABresult
0X0
100
111
383 0 ▶ confess "Timed out waiting for server to stop" if time - $start > $stop_timeout;
Branch: 1 branch
BranchTrueFalseTotal
if &DBIx::QuickDB::Driver::time() - $start > $stop_timeout 0 0 0
384 0 sleep 0.01;
385 }
386  
387 # Remove a stale unix socket left behind by a hard kill so it does not
388 # confuse callers or a later run that reuses the same directory.
389 0 my $socket = $self->socket;
390 0 ▶ unlink($socket) if $socket && -S $socket;
Branch: 1 branch
BranchTrueFalseTotal
if $socket and -S $socket 0 0 0
Condition0%0/3
$socket and -S $socket
!ll&&!rl&&r
000
MC/DC0%0/2
$socket and -S $socket
$socket -S $socket
Truth table0%0/3
A and B
A= $socketB= -S $socket
ABresult
0X0
100
111
391 }
392  
393 0 return;
394 }
395  
396 sub shell {
397 0 ▶ my $self = shift;
Subroutine shell: not called CC=2 SCAR=17.9
Pod documented
398 0 my ($db_name) = @_;
399 0 ▶ $db_name = 'quickdb' unless defined $db_name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $db_name 0 0 0
400  
401 0 system($self->shell_command($db_name));
402 }
403  
404 sub DESTROY {
405 0 ▶ my $self = shift;
Subroutine DESTROY: not called CC=11 SCAR=48.8
406 0 ▶ return unless $self->{+ROOT_PID} && $self->{+ROOT_PID} == $$;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"root_pid"} and $self->{"root_pid"} == $$ 0 0 0
Condition0%0/3
$self->{"root_pid"} and $self->{"root_pid"} == $$
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"root_pid"} and $self->{"root_pid"} == $$
$self->{"root_pid"} $self->{"root_pid"} == $$
Truth table0%0/3
A and B
A= $self->{"root_pid"}B= $self->{"root_pid"} == $$
ABresult
0X0
100
111
407  
408 0 ▶ if (my $watcher = delete $self->{+WATCHER}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $watcher = delete $self->{'watcher'}) { } 0 0 0
elsif ($self->should_cleanup) { } 0 0 0
409 0 my $server_pid = $watcher->server_pid;
410 0 $watcher->eliminate();
411 # Watcher::DESTROY (triggered by $watcher going out of scope) calls
412 # wait(), which blocks until the watcher process exits. After that,
413 # the server should be stopped and the dir cleaned up. But as a
414 # fallback, kill the server directly if it's still alive.
415 0 undef $watcher;
416  
417 0 ▶ if ($server_pid && kill(0, $server_pid)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($server_pid and kill 0, $server_pid) 0 0 0
Condition0%0/3
$server_pid and kill 0, $server_pid
!ll&&!rl&&r
000
MC/DC0%0/2
$server_pid and kill 0, $server_pid
$server_pid kill 0, $server_pid
Truth table0%0/3
A and B
A= $server_pidB= kill 0, $server_pid
ABresult
0X0
100
111
418 0 kill('TERM', $server_pid);
419 0 my $start = time;
420 0 ▶ while (kill(0, $server_pid)) {
Branch: 1 branch
BranchTrueFalseTotal
if (kill 0, $server_pid) 0 0 0
421 0 ▶ last if time - $start > 5;
Branch: 1 branch
BranchTrueFalseTotal
if &DBIx::QuickDB::Driver::time() - $start > 5 0 0 0
422 0 sleep 0.1;
423 }
424 0 ▶ kill('KILL', $server_pid) if kill(0, $server_pid);
Branch: 1 branch
BranchTrueFalseTotal
if kill 0, $server_pid 0 0 0
425 }
426 0 ▶ $self->cleanup() if $self->should_cleanup;
Branch: 1 branch
BranchTrueFalseTotal
if $self->should_cleanup 0 0 0
427 }
428 elsif ($self->should_cleanup) {
429 0 $self->cleanup();
430 }
431  
432 0 return;
433 }
434  
435 23 1;
436  
437 __END__
« DBIx/QuickDB/Driver/MySQL.pm ↑ Summary DBIx/QuickDB/Driver/MariaDB.pm »