has
test_postgresql
=> (
is
=>
'ro'
,
isa
=>
'Object'
,
lazy_build
=>1);
sub
_generate_sandbox_dir {
my
$schema_class
= (
my
$self
=
shift
)->schema_class;
$schema_class
=~ s/::/-/g;
catdir(
$self
->target_dir,
lc
(
$schema_class
));
}
sub
_determine_auto_start {
my
$base_dir
=
shift
;
if
(-d
$base_dir
) {
if
( -e catdir(
$base_dir
,
'data'
,
'postmaster.pid'
)) {
return
0;
}
else
{
return
1;
}
}
else
{
return
2;
}
}
sub
_build_test_postgresql {
my
$base_dir
= (
my
$self
=
shift
)->_generate_sandbox_dir;
my
$auto_start
= _determine_auto_start(
$base_dir
);
my
%config
= (
auto_start
=>
$auto_start
,
base_dir
=>
$base_dir
,
initdb_args
=>
$Test::postgresql::Defaults
{initdb_args},
postmaster_args
=>
$Test::postgresql::Defaults
{postmaster_args});
unless
(
$auto_start
) {
open
(
my
$pid_fh
,
'<'
, catdir(
$base_dir
,
'data'
,
'postmaster.pid'
)) ||
die
"Can't open PID file"
;
my
@lines
= <
$pid_fh
>;
close
(
$pid_fh
);
$config
{port} =
$lines
[3];
}
if
(
my
$testdb
= Test::postgresql->new(
%config
)) {
return
$testdb
;
}
else
{
die
$Test::postgresql::errstr
;
}
}
sub
_write_start {
my
$base_dir
= (
my
$self
=
shift
)->test_postgresql->base_dir;
mkpath(
my
$bin
= catdir(
$base_dir
,
'bin'
));
open
(
my
$fh
,
'>'
, catfile(
$bin
,
'start'
))
||
die
"Cannot open $bin/start: $!"
;
my
$test_postgresql
=
$self
->test_postgresql;
my
$postmaster
=
$test_postgresql
->{postmaster};
my
$data
= catdir(
$base_dir
,
'data'
);
my
$port
=
$test_postgresql
->{port};
print
$fh
<<START;
#!/usr/bin/env sh
$postmaster -p $port -D $data &
START
close
(
$fh
);
chmod
oct
(
"0755"
), catfile(
$bin
,
'start'
);
}
sub
_write_stop {
my
$base_dir
= (
my
$self
=
shift
)->test_postgresql->base_dir;
mkpath(
my
$bin
= catdir(
$base_dir
,
'bin'
));
open
(
my
$fh
,
'>'
, catfile(
$bin
,
'stop'
))
||
die
"Cannot open $bin/stop: $!"
;
my
$test_postgresql
=
$self
->test_postgresql;
my
$postmaster
=
$test_postgresql
->{postmaster};
my
$pid
= catdir(
$base_dir
,
'data'
,
'postmaster.pid'
);
print
$fh
<<STOP;
#!/usr/bin/env sh
kill -INT `head -1 $pid`
STOP
close
(
$fh
);
chmod
oct
(
"0755"
), catfile(
$bin
,
'stop'
);
}
sub
_write_use {
my
$base_dir
= (
my
$self
=
shift
)->test_postgresql->base_dir;
mkpath(
my
$bin
= catdir(
$base_dir
,
'bin'
));
open
(
my
$fh
,
'>'
, catfile(
$bin
,
'use'
))
||
die
"Cannot open $bin/use: $!"
;
my
$test_postgresql
=
$self
->test_postgresql;
my
$postmaster
=
$test_postgresql
->{postmaster};
my
$psql
=
$postmaster
;
$psql
=~s/postmaster$/psql/;
my
$port
=
$test_postgresql
->{port};
print
$fh
<<USE;
#!/usr/bin/env sh
$psql -h localhost --user postgres --port $port -d template1
USE
close
(
$fh
);
chmod
oct
(
"0755"
), catfile(
$bin
,
'use'
);
}
sub
make_sandbox {
my
$self
=
shift
;
my
$base_dir
=
$self
->_generate_sandbox_dir;
if
(
$self
->test_postgresql) {
$self
->_write_start;
$self
->_write_stop;
$self
->_write_use;
my
$port
=
$self
->test_postgresql->port;
return
"DBI:Pg:dbname=template1;host=127.0.0.1;port=$port"
,
'postgres'
,
''
;
}
else
{
die
"can't start a postgresql sandbox"
;
}
}
sub
DEMOLISH {
shift
->test_postgresql->stop(SIGINT) }
__PACKAGE__->meta->make_immutable;