DBIx/QuickDB/Driver/PostgreSQL.pm

filter: Statementstmt 42.9% 85 / 198 Branchbran 30.2% 23 / 76 Conditioncond 18.6% 8 / 43 MC/DCmcdc 0.0% 0 / 25 Subroutinesub 43.7% 14 / 32 Podpod 82.3% 14 / 17 totaltotal 36.8% 144 / 391 CC 60 SCAR 35.0
CC
60
Cov
37%
init
110.0
viable
49.9
version_string
42.0
CRAP 33.0
« DBIx/QuickDB/Driver/MySQL.pm ↑ Summary DBIx/QuickDB/Driver.pm »
1 package DBIx::QuickDB::Driver::PostgreSQL;
2 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 12 our $VERSION = '0.000065';
6  
7 12 ▶use IPC::Cmd qw/can_run/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 12 ▶use DBIx::QuickDB::Util qw/strip_hash_defaults env_timeout/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 12 ▶use Time::HiRes qw/sleep time/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 12 ▶use Scalar::Util qw/reftype/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 12 ▶use parent 'DBIx::QuickDB::Driver';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 12 ▶use DBIx::QuickDB::Util::HashBase qw{
Subroutine BEGIN: called CC=1 SCAR=0.0
15 -data_dir
16  
17 -initdb -createdb -postgres -psql
18  
19 -config
20 -socket
21 -port
22 12 };
23  
24 12 my ($INITDB, $CREATEDB, $POSTGRES, $PSQL, $DBDPG);
25  
26 12 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
27 12 local $@;
28  
29 12 $INITDB = can_run('initdb');
30 12 $CREATEDB = can_run('createdb');
31 12 $POSTGRES = can_run('postgres');
32 12 $PSQL = can_run('psql');
33 12 $DBDPG = eval { require DBD::Pg; 'DBD::Pg'};
34 }
35  
36 0 ▶sub version_string {
Subroutine version_string: not called CC=6 SCAR=37.4
Pod documented
37 0 my $binary;
38  
39 # Go in reverse order assuming the last param hash provided is most important
40 0 for my $arg (reverse @_) {
41 0 ▶ my $type = reftype($arg) or next; # skip if not a ref
Branch: 1 branch
BranchTrueFalseTotal
my $type = builtin::reftype($arg) or next 0 0 0
42 0 ▶ next unless $type eq 'HASH'; # We have a hashref, possibly blessed
Branch: 1 branch
BranchTrueFalseTotal
unless $type eq "HASH" 0 0 0
43  
44 # If we find a launcher we are done looping, we want to use this binary.
45 0 ▶ $binary = $arg->{+POSTGRES} and last;
Branch: 1 branch
BranchTrueFalseTotal
$binary = $arg->{"postgres"} and last 0 0 0
46 }
47  
48 # If no args provided one to use we fallback to the default from $PATH
49 0 ▶ $binary ||= $POSTGRES;
Condition0%0/3
$binary ||= $POSTGRES
l!l&&r!l&&!r
000
MC/DC0%0/2
$binary ||= $POSTGRES
$binary $POSTGRES
Truth table0%0/3
A ||= B
A= $binaryB= $POSTGRES
ABresult
1X1
011
000
50  
51 # Call the binary with '-V', capturing and returning the output using backticks.
52 0 return `$binary -V`;
53 }
54  
55 0 ▶sub list_env_vars {
Subroutine list_env_vars: not called CC=1 SCAR=6.9
Pod documented
56 0 my $self = shift;
57 return (
58 0 $self->SUPER::list_env_vars(),
59 qw{
60 PGAPPNAME PGCLIENTENCODING PGCONNECT_TIMEOUT PGDATABASE PGDATESTYLE
61 PGGEQO PGGSSLIB PGHOST PGHOSTADDR PGKRBSRVNAME PGLOCALEDIR
62 PGOPTIONS PGPASSFILE PGPASSWORD PGPORT PGREQUIREPEER PGREQUIRESSL
63 PGSERVICE PGSERVICEFILE PGSSLCERT PGSSLCOMPRESSION PGSSLCRL
64 PGSSLKEY PGSSLMODE PGSSLROOTCERT PGSYSCONFDIR PGTARGETSESSIONATTRS
65 PGTZ PGUSER
66 }
67 );
68 }
69  
70 1 ▶sub error_log { "$_[0]->{+DIR}/error.log" }
Subroutine error_log: called CC=1 SCAR=0.0
Pod undocumented
71  
72 # Stop the postmaster with SIGTERM ("Smart Shutdown"): it lets in-progress
73 # transactions finish and shuts down cleanly. Smart Shutdown waits for every
74 # client to disconnect first, which previously risked hanging until the watcher
75 # escalated to SIGKILL. That risk is now handled elsewhere: stop() disconnects
76 # our own lingering handles (via DBI->visit_handles) before signalling the
77 # server, forces a CHECKPOINT so a hard kill cannot corrupt a later clone, and
78 # the watcher's grace period before SIGKILL is generous and tunable. A proper
79 # shutdown is preferred whenever it is possible.
80 0 ▶sub stop_sig { 'TERM' }
Subroutine stop_sig: not called CC=1 SCAR=6.9
Pod documented
81  
82 # Fast/disposable teardown uses SIGQUIT ("Immediate Shutdown") rather than the
83 # base-class SIGKILL. Immediate Shutdown aborts all backends without a
84 # checkpoint -- so it is nearly as fast as SIGKILL -- but the postmaster still
85 # runs its exit cleanup and RELEASES its SysV semaphores (and shared memory). A
86 # SIGKILLed postmaster cannot clean up, and because a disposable clone's data
87 # dir is then deleted no future postmaster will ever reuse that IPC key, so the
88 # semaphores leak permanently. On hosts where PostgreSQL uses SysV semaphores
89 # (FreeBSD, OpenBSD, macOS) a suite that hard-kills many disposable clones can
90 # exhaust the kernel SEMMNI/SEMMNS limits; SIGQUIT avoids that.
91 1 ▶sub fast_stop_sig { 'QUIT' }
Subroutine fast_stop_sig: called CC=1 SCAR=0.0
Pod documented
92  
93 12 ▶sub _default_paths {
Subroutine _default_paths: called CC=1 SCAR=0.0
94 return (
95 12 initdb => $INITDB,
96 createdb => $CREATEDB,
97 postgres => $POSTGRES,
98 psql => $PSQL,
99 );
100 }
101  
102 0 ▶sub _default_config {
Subroutine _default_config: not called CC=1 SCAR=6.9
103 0 my $self = shift;
104  
105 return (
106 # QuickDB databases are short-lived test instances: autovacuum buys
107 # nothing, and on PostgreSQL <= 14 its launcher can stall shutdown for
108 # PGSTAT_MAX_WAIT_TIME (10s) waiting on the stats collector ("using
109 # stale statistics instead of current ones because stats collector is
110 # not responding"), which made stop() blow the whole stop grace and
111 # get escalated. Consumers who need it can override.
112 autovacuum => "off",
113  
114 datestyle => "'iso, mdy'",
115 default_text_search_config => "'pg_catalog.english'",
116 lc_messages => "'en_US.UTF-8'",
117 lc_monetary => "'en_US.UTF-8'",
118 lc_numeric => "'en_US.UTF-8'",
119 lc_time => "'en_US.UTF-8'",
120 listen_addresses => "''",
121 log_destination => "'stderr'",
122 logging_collector => "'on'",
123 log_directory => "'$self->{+DIR}'",
124 log_filename => "'error.log'",
125 max_connections => "100",
126 shared_buffers => "128MB",
127 unix_socket_directories => "'$self->{+DIR}'",
128 0 port => $self->{+PORT},
129  
130 #dynamic_shared_memory_type => "posix",
131 #log_timezone => "'US/Pacific'",
132 #timezone => "'US/Pacific'",
133 );
134 }
135  
136 12 ▶sub viable {
Subroutine viable: called CC=19 SCAR=39.1
Pod documented
137 12 my $this = shift;
138 12 my ($spec) = @_;
139  
140 12 ▶ my %check = (ref($this) ? %$this : (), $this->_default_paths, %$spec);
Branch: 1 branch
BranchTrueFalseTotal
ref $this ? : 0 12 12
141  
142 12 my @bad;
143  
144 12 ▶ push @bad => "'DBD::Pg' module could not be loaded, needed for everything" unless $DBDPG;
Branch: 1 branch
BranchTrueFalseTotal
unless $DBDPG 12 0 12
145  
146 12 ▶ if ($spec->{bootstrap}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->{"bootstrap"}) 12 0 12
147 12 ▶ push @bad => "'initdb' command is missing, needed for bootstrap" unless $check{initdb} && -x $check{initdb};
Branch: 1 branch
BranchTrueFalseTotal
unless $check{"initdb"} and -x $check{"initdb"} 12 0 12
Condition33%1/3
$check{"initdb"} and -x $check{"initdb"}
!ll&&!rl&&r
1200
MC/DC0%0/2
$check{"initdb"} and -x $check{"initdb"}
$check{"initdb"} -x $check{"initdb"}
Truth table33%1/3
A and B
A= $check{"initdb"}B= -x $check{"initdb"}
ABresult
0X0
100
111
148 12 ▶ push @bad => "'createdb' command is missing, needed for bootstrap" unless $check{createdb} && -x $check{createdb};
Branch: 1 branch
BranchTrueFalseTotal
unless $check{"createdb"} and -x $check{"createdb"} 12 0 12
Condition33%1/3
$check{"createdb"} and -x $check{"createdb"}
!ll&&!rl&&r
1200
MC/DC0%0/2
$check{"createdb"} and -x $check{"createdb"}
$check{"createdb"} -x $check{"createdb"}
Truth table33%1/3
A and B
A= $check{"createdb"}B= -x $check{"createdb"}
ABresult
0X0
100
111
149 }
150  
151 12 ▶ if ($spec->{autostart}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->{"autostart"}) 12 0 12
152 12 ▶ push @bad => "'postgres' command is missing, needed for autostart" unless $check{postgres} && -x $check{postgres};
Branch: 1 branch
BranchTrueFalseTotal
unless $check{"postgres"} and -x $check{"postgres"} 12 0 12
Condition33%1/3
$check{"postgres"} and -x $check{"postgres"}
!ll&&!rl&&r
1200
MC/DC0%0/2
$check{"postgres"} and -x $check{"postgres"}
$check{"postgres"} -x $check{"postgres"}
Truth table33%1/3
A and B
A= $check{"postgres"}B= -x $check{"postgres"}
ABresult
0X0
100
111
153 }
154  
155 # PostgreSQL's initdb and postgres flatly refuse to run as root (geteuid()
156 # == 0), and there is no override flag; running as root manifests as a start
157 # timeout otherwise. This check is Unix-only, mirroring initdb's own
158 # #ifndef WIN32 guard. Only bootstrap/autostart spawn those binaries.
159 12 ▶ if (($spec->{bootstrap} || $spec->{autostart}) && $^O ne 'MSWin32' && $> == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->{"bootstrap"} || $spec->{"autostart"} and $^O ne "MSWin32" and $> == 0) 0 12 12
Condition33%3/9
$spec->{"bootstrap"} || $spec->{"autostart"} and $^O ne "MSWin32" and $> == 0
!ll&&!rl&&r
0120
$spec->{"bootstrap"} || $spec->{"autostart"} and $^O ne "MSWin32"
!ll&&!rl&&r
0012
$spec->{"bootstrap"} or $spec->{"autostart"}
l!l&&r!l&&!r
1200
MC/DC0%0/4
$spec->{"bootstrap"} || $spec->{"autostart"} and $^O ne "MSWin32" and $> == 0
$spec->{"bootstrap"} $spec->{"autostart"} $^O ne "MSWin32" $> == 0
Truth table14%1/7
A or B and C and D
A= $spec->{"bootstrap"}B= $spec->{"autostart"}C= $^O ne "MSWin32"D= $> == 0
ABCDresult
00XX0
1X0X0
010X0
1X100
01100
1X111
01111
160 0 push @bad => "PostgreSQL's initdb and postgres refuse to run as root (EUID 0); run as an unprivileged user";
161 }
162  
163 12 ▶ if ($spec->{load_sql}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->{"load_sql"}) 12 0 12
164 12 ▶ push @bad => "'psql' command is missing, needed for load_sql" unless $check{psql} && -x $check{psql};
Branch: 1 branch
BranchTrueFalseTotal
unless $check{"psql"} and -x $check{"psql"} 12 0 12
Condition33%1/3
$check{"psql"} and -x $check{"psql"}
!ll&&!rl&&r
1200
MC/DC0%0/2
$check{"psql"} and -x $check{"psql"}
$check{"psql"} -x $check{"psql"}
Truth table33%1/3
A and B
A= $check{"psql"}B= -x $check{"psql"}
ABresult
0X0
100
111
165 }
166  
167 12 ▶ return (1, undef) unless @bad;
Branch: 1 branch
BranchTrueFalseTotal
unless @bad 0 12 12
168 12 return (0, join "\n" => @bad);
169 }
170  
171 0 ▶sub init {
Subroutine init: not called CC=10 SCAR=47.0
Pod documented
172 0 my $self = shift;
173 0 $self->SUPER::init();
174  
175 0 ▶ my $port = $self->{+PORT} ||= '5432';
Condition0%0/2
$self->{"port"} ||= 5432
l!l
00
MC/DC0%0/1
$self->{"port"} ||= 5432
$self->{"port"}
Truth table0%0/2
A
A= $self->{"port"}
Aresult
11
00
176  
177 0 my $dir = $self->{+DIR};
178 0 $self->{+DATA_DIR} = "$dir/data";
179 0 ▶ $self->{+SOCKET} ||= "$dir/.s.PGSQL.$port";
Condition0%0/2
$self->{"socket"} ||= "$dir/.s.PGSQL.$port"
l!l
00
MC/DC0%0/1
$self->{"socket"} ||= "$dir/.s.PGSQL.$port"
$self->{"socket"}
Truth table0%0/2
A
A= $self->{"socket"}
Aresult
11
00
180  
181 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
182 0 ▶ $self->{+ENV_VARS}->{PGPORT} = $port unless defined $self->{+ENV_VARS}->{PGPORT};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"env_vars"}{"PGPORT"} 0 0 0
183  
184 0 my %defaults = $self->_default_paths;
185 0 ▶ $self->{$_} ||= $defaults{$_} for keys %defaults;
Condition0%0/3
$self->{$_} ||= $defaults{$_}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{$_} ||= $defaults{$_}
$self->{$_} $defaults{$_}
Truth table0%0/3
A ||= B
A= $self->{$_}B= $defaults{$_}
ABresult
1X1
011
000
186  
187 0 my %cfg_defs = $self->_default_config;
188 0 ▶ my $cfg = $self->{+CONFIG} ||= {};
Condition0%0/2
$self->{"config"} ||= {}
l!l
00
MC/DC0%0/1
$self->{"config"} ||= {}
$self->{"config"}
Truth table0%0/2
A
A= $self->{"config"}
Aresult
11
00
189  
190 0 for my $key (keys %cfg_defs) {
191 0 ▶ next if defined $cfg->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if defined $cfg->{$key} 0 0 0
192 0 $cfg->{$key} = $cfg_defs{$key};
193 }
194 }
195  
196 0 ▶sub clone_data {
Subroutine clone_data: not called CC=4 SCAR=30.0
Pod documented
197 0 my $self = shift;
198  
199 0 ▶ my $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
200 0 ▶ delete $vars->{PGPORT} if $vars->{PGPORT} && $vars->{PGPORT} eq $self->port;
Branch: 1 branch
BranchTrueFalseTotal
if $vars->{"PGPORT"} and $vars->{"PGPORT"} eq $self->port 0 0 0
Condition0%0/3
$vars->{"PGPORT"} and $vars->{"PGPORT"} eq $self->port
!ll&&!rl&&r
000
MC/DC0%0/2
$vars->{"PGPORT"} and $vars->{"PGPORT"} eq $self->port
$vars->{"PGPORT"} $vars->{"PGPORT"} eq $self->port
Truth table0%0/3
A and B
A= $vars->{"PGPORT"}B= $vars->{"PGPORT"} eq $self->port
ABresult
0X0
100
111
201  
202 my $config = strip_hash_defaults(
203 0 $self->{+CONFIG},
204 { $self->_default_config },
205 );
206  
207 return (
208 0 $self->SUPER::clone_data(),
209 ENV_VARS() => $vars,
210 CONFIG() => $config,
211 );
212 }
213  
214 0 ▶sub write_config {
Subroutine write_config: not called CC=4 SCAR=30.0
Pod documented
215 0 my $self = shift;
216  
217 0 my $db_dir = $self->{+DATA_DIR};
218 0 ▶ open(my $cf, '>', "$db_dir/postgresql.conf") or die "Could not open config file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $cf, ">", "$db_dir/postgresql.conf" or die "Could not open config file: $!" 0 0 0
219 0 for my $key (sort keys %{$self->{+CONFIG}}) {
220 0 my $val = $self->{+CONFIG}->{$key};
221 0 ▶ next unless length($val);
Branch: 1 branch
BranchTrueFalseTotal
unless length $val 0 0 0
222  
223 0 print $cf "$key = $val\n";
224 }
225 0 close($cf);
226 }
227  
228 # PostgreSQL 9.3 creates its Unix-domain listening socket before it allocates
229 # shared memory and semaphores. (Modern releases use a different order.)
230 # Waiting for the socket alone therefore has a small false-ready window on the
231 # affected old releases: the postmaster can create the socket, fail during the
232 # remaining startup work (notably semget() on SysV-IPC platforms), and remove
233 # it again after the base start() has returned.
234 #
235 # Confirm readiness with a real connection to the always-present postgres
236 # database. A healthy postmaster can briefly refuse even that connection, so
237 # retry while its socket still exists, up to the same startup timeout. If the
238 # socket vanishes or the timeout expires, preserve the last DBI exception and
239 # append both server logs. Failures before the logging collector starts are
240 # written only to the watcher's launch log; including it also lets test helpers
241 # recognize an exact resource-exhaustion error without treating an ordinary
242 # connection failure as an environmental skip.
243 2 ▶sub start {
Subroutine start: called CC=12 SCAR=25.9
Pod documented
244 2 my $self = shift;
245  
246 2 $self->SUPER::start(@_);
247  
248 2 my $timeout = env_timeout(QDB_START_TIMEOUT => 10);
249 2 my $start = time;
250 2 my ($dbh, $err);
251 2 while (1) {
252 2 my $ok;
253 {
254 2 local $@;
255 2 $ok = eval {
256 # Call the base connection method directly: this loop replaces
257 # catch_startup() here so it can also tolerate a brief generic
258 # connection-refused error before the postmaster is ready.
259 2 $dbh = $self->SUPER::connect(
260 'postgres',
261 AutoCommit => 1,
262 RaiseError => 1,
263 PrintError => 0,
264 );
265 1 1;
266 };
267 2 ▶ $err = $ok ? undef : $@;
Branch: 1 branch
BranchTrueFalseTotal
$ok ? : 1 1 2
268 }
269  
270 2 ▶ last if $ok;
Branch: 1 branch
BranchTrueFalseTotal
if $ok 1 1 2
271 1 ▶ last unless -S $self->socket;
Branch: 1 branch
BranchTrueFalseTotal
unless -S $self->socket 1 0 1
272 0 ▶ last if time - $start > $timeout;
Branch: 1 branch
BranchTrueFalseTotal
if &DBIx::QuickDB::Driver::PostgreSQL::time() - $start > $timeout 0 0 0
273 0 sleep 0.01;
274 }
275  
276 2 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 1 1 2
277 1 my $watcher = $self->watcher;
278 1 ▶ my $launch_log = $watcher ? $self->_read_file($watcher->log_file) : '';
Branch: 1 branch
BranchTrueFalseTotal
$watcher ? : 1 0 1
279 1 my $error_log = $self->read_error_log;
280  
281 1 my $msg = $err;
282 1 ▶ $msg .= "\n=== server launch log ===\n$launch_log" if length $launch_log;
Branch: 1 branch
BranchTrueFalseTotal
if length $launch_log 1 0 1
283 1 ▶ $msg .= "\n=== error log ===\n$error_log" if length $error_log;
Branch: 1 branch
BranchTrueFalseTotal
if length $error_log 1 0 1
284  
285 # A failed start must not leave the object permanently reporting
286 # started() merely because it still holds a watcher. Stop and reap
287 # the owned postmaster without Driver::stop's checkpoint attempt (the
288 # server never became ready), retaining the original startup error.
289 1 ▶ if ($watcher) {
Branch: 1 branch
BranchTrueFalseTotal
if ($watcher) 1 0 1
290 1 delete $self->{+DBIx::QuickDB::Driver::WATCHER()};
291 1 eval {
292 1 $watcher->stop;
293 1 $watcher->wait;
294 1 my $socket = $self->socket;
295 1 ▶ unlink($socket) if $socket && -S $socket;
Branch: 1 branch
BranchTrueFalseTotal
if $socket and -S $socket 0 1 1
Condition33%1/3
$socket and -S $socket
!ll&&!rl&&r
010
MC/DC0%0/2
$socket and -S $socket
$socket -S $socket
Truth table33%1/3
A and B
A= $socketB= -S $socket
ABresult
0X0
100
111
296 1 1;
297 };
298 }
299  
300 1 die $msg;
301 }
302  
303 1 $dbh->disconnect;
304 1 return;
305 }
306  
307 0 ▶sub bootstrap {
Subroutine bootstrap: not called CC=6 SCAR=37.4
308 0 my $self = shift;
309  
310 0 my $dir = $self->{+DIR};
311 0 my $db_dir = $self->{+DATA_DIR};
312 0 ▶ mkdir($db_dir) or die "Could not create data dir: $!";
Branch: 1 branch
BranchTrueFalseTotal
mkdir $db_dir or die "Could not create data dir: $!" 0 0 0
313 0 $self->run_command([$self->{+INITDB}, '-E', 'UTF8', '--no-locale', '-A', 'trust', '-D', $db_dir]);
314  
315 0 $self->write_config;
316 0 $self->start;
317  
318 0 for my $try (1 .. 10) {
319 0 my ($ok, $err);
320 {
321 0 local $@;
322 0 $ok = eval {
323 0 ▶ $self->catch_startup(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
324 0 $self->run_command([$self->{+CREATEDB}, '-T', 'template0', '-E', 'UTF8', '-h', $dir, 'quickdb']);
325 0 });
326  
327 0 1;
328 };
329 0 $err = $@;
330 }
331  
332 0 ▶ last if $ok;
Branch: 1 branch
BranchTrueFalseTotal
if $ok 0 0 0
333  
334 0 ▶ die $err if $try == 5;
Branch: 1 branch
BranchTrueFalseTotal
if $try == 5 0 0 0
335  
336 0 sleep 0.5;
337 }
338  
339 0 ▶ $self->stop unless $self->{+AUTOSTART};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"autostart"} 0 0 0
340  
341 0 return;
342 }
343  
344 0 ▶sub connect {
Subroutine connect: not called CC=1 SCAR=6.9
Pod documented
345 0 my $self = shift;
346 0 my ($db_name, %params) = @_;
347  
348 0 my $dbh;
349 0 ▶ $self->catch_startup(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
350 0 $dbh = $self->SUPER::connect($db_name, %params);
351 0 });
352  
353 0 return $dbh;
354 }
355  
356 # Force a CHECKPOINT so the on-disk state is durable before shutdown. Without
357 # this, a shutdown that gets SIGKILLed (slow host blowing the watcher's grace
358 # period) leaves the cluster needing crash recovery; cloning that data dir and
359 # starting it replays WAL, which advances SERIAL sequences by SEQ_LOG_VALS (32)
360 # -- e.g. the next inserted row gets id 34 instead of 2. Best effort: any
361 # failure here must not prevent the server from stopping.
362 0 ▶sub checkpoint {
Subroutine checkpoint: not called CC=2 SCAR=17.9
Pod undocumented
363 0 my $self = shift;
364  
365 0 ▶ return unless $self->started;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->started 0 0 0
366  
367 0 eval {
368 0 my $dbh = $self->connect('postgres', AutoCommit => 1, RaiseError => 1, PrintError => 0);
369 0 $dbh->do('CHECKPOINT');
370 0 $dbh->disconnect;
371 0 1;
372 };
373  
374 0 return;
375 }
376  
377 0 ▶sub connect_string {
Subroutine connect_string: not called CC=2 SCAR=17.9
Pod documented
378 0 my $self = shift;
379 0 my ($db_name) = @_;
380 0 ▶ $db_name = 'quickdb' unless defined $db_name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $db_name 0 0 0
381  
382 0 my $dir = $self->{+DIR};
383  
384 0 require DBD::Pg;
385 0 return "dbi:Pg:dbname=$db_name;host=$dir"
386 }
387  
388 0 ▶sub load_sql {
Subroutine load_sql: not called CC=1 SCAR=6.9
Pod documented
389 0 my $self = shift;
390 0 my ($dbname, $file) = @_;
391  
392 0 my $dir = $self->{+DIR};
393  
394 0 ▶ $self->catch_startup(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
395 $self->run_command([
396 0 $self->{+PSQL},
397 '-h' => $dir,
398 '-v' => 'ON_ERROR_STOP=1',
399 '-f' => $file,
400 $dbname,
401 ]);
402 0 });
403 }
404  
405 0 ▶sub shell_command {
Subroutine shell_command: not called CC=1 SCAR=6.9
Pod documented
406 0 my $self = shift;
407 0 my ($db_name) = @_;
408  
409 0 return ($self->{+PSQL}, '-h' => $self->{+DIR}, $db_name);
410 }
411  
412 0 ▶sub start_command {
Subroutine start_command: not called CC=1 SCAR=6.9
Pod documented
413 0 my $self = shift;
414 0 return ($self->{+POSTGRES}, '-D' => $self->{+DATA_DIR}, '-p' => $self->{+PORT});
415 }
416  
417 0 ▶sub catch_startup {
Subroutine catch_startup: not called CC=4 SCAR=30.0
Pod undocumented
418 0 my $self = shift;
419 0 my ($code) = @_;
420  
421 0 my $timeout = env_timeout(QDB_START_TIMEOUT => 10);
422  
423 0 my $start = time;
424 0 while (1) {
425 0 my $waited = time - $start;
426 0 ▶ die "Timeout waiting for server" if $waited > $timeout;
Branch: 1 branch
BranchTrueFalseTotal
if $waited > $timeout 0 0 0
427  
428 0 my ($ok, $err, $out);
429 {
430 0 local $@;
431 0 $ok = eval {
432 0 $out = $code->($self);
433 0 1;
434 };
435  
436 0 $err = $@;
437 }
438  
439 0 ▶ return $out if $ok;
Branch: 1 branch
BranchTrueFalseTotal
if $ok 0 0 0
440  
441 0 ▶ die $err unless $err =~ m/the database system is starting up/;
Branch: 1 branch
BranchTrueFalseTotal
unless $err =~ /the database system is starting up/ 0 0 0
442  
443 0 sleep 0.01;
444 }
445 }
446  
447 12 1;
448  
449 __END__
« DBIx/QuickDB/Driver/MySQL.pm ↑ Summary DBIx/QuickDB/Driver.pm »