blib/lib/DBIx/QuickDB/Driver/PostgreSQL.pm

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