| 1 | package Module::Build::Service::postgresql; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | { | ||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | $Module::Build::Service::postgresql::VERSION = '0.91'; | |||||||||||||||||||||||||||||||||||||||||
| 4 | } | ||||||||||||||||||||||||||||||||||||||||||
| 5 | # ABSTRACT: Service implementation for postgresql | ||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use File::Path qw{make_path remove_tree}; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use File::Slurp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use File::Spec; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Log::Any qw{$log}; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Moo; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 1 | extends 'Module::Build::Service::Base'; | |||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | 1 | has 'bindir' => (is => 'lazy'); | |||||||||||||||||||||||||||||||||||||||||
| 16 | 0 | ▶ | sub _build_bindir { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_bindir: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 18 | 0 | for my $prefix ("/usr/lib/postgresql", "/usr/local/lib/postgresql") { | |||||||||||||||||||||||||||||||||||||||||
| 19 | 0 | my $dir = File::Spec->catdir ($prefix, $self->version, "bin"); | |||||||||||||||||||||||||||||||||||||||||
| 20 | 0 | $log->tracef ("Checking %s for binaries", $dir); | |||||||||||||||||||||||||||||||||||||||||
| 21 | 0 | ▶ | if (-e File::Spec->catfile ($dir, "pg_ctl") and -e File::Spec->catfile ($dir, "createdb") and -e File::Spec->catfile ($dir, "pg_dumpall")) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
-e "File::Spec"->catfile($dir, "pg_ctl") and -e "File::Spec"->catfile($dir, "createdb") and -e "File::Spec"->catfile($dir, "pg_dumpall")
-e "File::Spec"->catfile($dir, "pg_ctl") and -e "File::Spec"->catfile($dir, "createdb")
MC/DC0%0/3
-e "File::Spec"->catfile($dir, "pg_ctl") and -e "File::Spec"->catfile($dir, "createdb") and -e "File::Spec"->catfile($dir, "pg_dumpall")
-e "File::Spec"->catfile($dir, "pg_ctl")
-e "File::Spec"->catfile($dir, "createdb")
-e "File::Spec"->catfile($dir, "pg_dumpall")
Truth table0%0/4
A and B and C
A= -e "File::Spec"->catfile($dir, "pg_ctl")B= -e "File::Spec"->catfile($dir, "createdb")C= -e "File::Spec"->catfile($dir, "pg_dumpall")
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | 0 | $log->tracef ("Returning %s as binary directory", $dir); | |||||||||||||||||||||||||||||||||||||||||
| 23 | 0 | return $dir; | |||||||||||||||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||||||||||||||||
| 26 | 0 | die "Couldn't find postgresql binaries"; | |||||||||||||||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | 1 | has 'data' => (is => 'lazy'); | |||||||||||||||||||||||||||||||||||||||||
| 31 | 0 | ▶ | sub _build_data { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_data: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 33 | 0 | my $dir = File::Spec->catdir ($self->_builder->mbs_data_dir, 'postgresql'); | |||||||||||||||||||||||||||||||||||||||||
| 34 | 0 | ▶ | -d $dir or make_path ($dir) or die "Couldn't create data directory " . $dir; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
-d $dir or make_path($dir)
MC/DC0%0/2
-d $dir or make_path($dir)
-d $dir
make_path($dir)
Truth table0%0/3
A or B
A= -d $dirB= make_path($dir)
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | $dir; | |||||||||||||||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | 1 | has 'dump' => (is => 'lazy'); | |||||||||||||||||||||||||||||||||||||||||
| 40 | 0 | ▶ | sub _build_dump { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_dump: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | File::Spec->catfile ($self->_builder->mbs_log_dir, 'postgresql.sql'); | |||||||||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | has 'service' => (is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | ▶ | isa => sub {!defined $_[0] or ref $_[0] eq "ARRAY"}, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | 1 | predicate => 'has_service'); | |||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | 1 | has 'version' => (is => 'lazy'); | |||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | ▶ | sub _build_version { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_version: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | 0 | ▶ | local $SIG{__WARN__} = sub {}; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | (my $version = qx/psql -V/) =~ s/^psql \(PostgreSQL\) (\d+\.\d+)(?:\.|beta|rc)\d+.+/$1/s; | |||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | ▶ | die "Couldn't not figure out postgresql version" unless $version; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | $version; | |||||||||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | ▶ | sub _bin { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_bin: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | my ($self, $bin) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | File::Spec->catfile ($self->bindir, $bin); | |||||||||||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | 0 | ▶ | sub start_service { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
start_service: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | $log->debug ("postgresql service initializing"); | |||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | $self->run_process ($self->_bin ("pg_ctl"), "init", "-s", "-D", $self->data, "-o", "-A trust"); | |||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | $log->debug ("postgresql service starting"); | |||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | ▶ | my $logfile = $self->log =~ m,^/, ? $self->log : File::Spec->catfile ($self->_builder->mbs_log_dir, $self->log); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | $self->run_process ($self->_bin ("pg_ctl"), "start", "-s", "-w", "-D", $self->data, "-l", $logfile, "-o", "-h '' -F -k " . $self->data); | |||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | ▶ | if ($self->has_service) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | $log->debug ("Writing service file"); | |||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | $ENV{PGSYSCONFDIR} = $self->data; | |||||||||||||||||||||||||||||||||||||||||
| 77 | write_file "$ENV{PGSYSCONFDIR}/pg_service.conf", map { | ||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | ▶ | my ($service, $database) = ref $_ ? @{$_} : ($_, $_); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | $self->run_process ($self->_bin ("createdb"), "-h", $self->data, $database); | |||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | sprintf "[%s]\ndbname=%s\nhost=%s\n", $service, $database, $self->data; | |||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | } @{$self->service}; | |||||||||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | warn "Postgresql instance available using '-h " . $self->data . "'\n"; | |||||||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | ▶ | sub stop_service { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
stop_service: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | ▶ | if ($self->dump) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | $log->trace ("Dumping sql data"); | |||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | $self->run_process ($self->_bin ("pg_dumpall"), "-h", $self->data, "-f", $self->dump); | |||||||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | $log->debug ("postgresql service stopping"); | |||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | $self->run_process ($self->_bin ("pg_ctl"), "stop", "-m", "immediate", "-s", "-D", $self->data); | |||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | $log->debug ("Cleaning up"); | |||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | remove_tree ($self->data); | |||||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | __END__ | ||||||||||||||||||||||||||||||||||||||||||