Module/Build/Database/PostgreSQL.pm

filter: Statementstmt 15.0% 49 / 325 Branchbran 0.9% 1 / 106 Conditioncond 0.0% 0 / 52 MC/DCmcdc 0.0% 0 / 31 Subroutinesub 27.2% 12 / 44 Podpod 0.0% 0 / 6 totaltotal 10.9% 62 / 564 CC 72 SCAR 39.8
CC
72
Cov
10%
_generate_docs
156.0
_create_database
132.0
_start_new_db
110.0
CRAP 53.7
« Module/Build/Database.pm ↑ Summary Module/Build/Database/Helpers.pm »
1 =head1 NAME
2  
3 Module::Build::Database::PostgreSQL - PostgreSQL implementation for MBD
4  
5 =head1 SYNOPSIS
6  
7 In Build.PL :
8  
9 my $builder = Module::Build::Database->new(
10 database_type => "PostgreSQL",
11 database_options => {
12 name => "my_database_name",
13 schema => "my_schema_name",
14 # Extra items for scratch databases :
15 append_to_conf => "text to add to postgresql.conf",
16 after_create => q[create schema audit;],
17 },
18 database_extensions => {
19 postgis => { schema => "public", },
20 # directory with postgis.sql and spatial_ref_sys.sql
21 postgis_base => '/usr/local/share/postgresql/contrib'
22 },
23 );
24  
25 =head1 DESCRIPTION
26  
27 Postgres driver for L<Module::Build::Database>.
28  
29 =head1 OPTIONS
30  
31 All of the options above may be changed via the Module::Build option
32 handling, e.g.
33  
34 perl Build.PL --database_options name=my_name
35 perl Build.PL --postgis_base=/usr/local/share/postgresql/contrib
36  
37 The options are as follows ;
38  
39 =over 4
40  
41 =item database_options
42  
43 =over 4
44  
45 =item name
46  
47 the name of the database (i.e. 'create database $name')
48  
49 =item schema
50  
51 the name of the schema to be managed by MBD
52  
53 =item append_to_conf
54  
55 extra options to append to C<postgresql.conf> before starting test instances of postgres
56  
57 =item after_create
58  
59 extra SQL to run after running a 'create database' statement. Note that this will be run in several
60 different situations :
61  
62 =over 4
63  
64 =item 1.
65  
66 during a L<dbtest|Module::Build::Database#dbtest> (creating a test db)
67  
68 =item 2.
69  
70 during a L<dbfakeinstall|Module::Build::Database#dbfakeinstall> (also creating a test db)
71  
72 =item 3.
73  
74 during an initial L<dbinstall|Module::Build::Database#dbinstall>; when the target database does not yet exist.
75  
76 =back
77  
78 An example of using the after_create statement would be to create a second schema which
79 will not be managed by MBD, but on which the MBD-managed schema depends.
80  
81 =back
82  
83 =item database_extension
84  
85 To specify a server side procedural language you can use the C<database_extension> -E<gt> C<languages>
86 option, like so:
87  
88 my $builder = Module::Build::Database->new(
89 database_extension => {
90 languages => [ 'plperl', 'pltcl' ],
91 },
92 );
93  
94 Trying to create languages to a patch will not work because they not stored in the main schema and will
95 not be included in C<base.sql> when you run C<Build dbdist>.
96  
97 This is also similar to
98  
99 after_create => 'create extension ...',
100  
101 except it is executed on B<every> L<dbinstall|Module::Build::Database#dbinstall> meaning you can use this to add extensions to
102 existing database deployments.
103  
104 =item postgis_base
105  
106 Specify the directory containing postgis.sql and spatial_ref_sys.sql. If specified these SQL files will be loaded so that
107 you can use PostGIS in your database.
108  
109 =item leave_running
110  
111 If set to true, and if you are not using a persistent scratch database (see next option), then the scratch database will
112 not be stopped and torn down after running C<Build dbtest> or C<Build dbfakeinstall>.
113  
114 =item scratch_database
115  
116 You can use this option to specify the connection settings for a persistent scratch or temporary database instance, used by
117 the C<Build dbtest> and C<Build dbfakeinstall> to test schema. B<IMPORTANT>: the C<Build dbtest> and C<Build dbfakeinstall>
118 will drop and re-create databases on the scratch instance with the same name as the database on your production instance so
119 it is I<very> important that if you use a persistent scratch database that it be dedicated to that task.
120  
121 my $builder = Module::Build::Database->new(
122 scratch_database => {
123 PGHOST => 'databasehost',
124 PGPORT => '5555',
125 PGUSER => 'dbuser',
126 },
127 );
128  
129 If you specify any one of these keys for this option (C<PGHOST>, C<PGPORT>, C<PGUSER>) then MBD will use a persistent
130 scratch database. Any missing values will use the default.
131  
132 You can also specify these settings using environment variables:
133  
134 % export MBD_SCRATCH_PGHOST=databasehost
135 % export MBD_SCRATCH_PGPORT=5555
136 % export MBD_SCRATCH_PGUSER=dbuser
137  
138 By default this module will create its own scratch PostgreSQL instance that uses unix domain sockets for communication
139 each time it needs one when you use the C<Build dbtest> or C<Build dbfakeinstall> commands. Situations where you might
140 need to use a persistent scratch database:
141  
142 =over 4
143  
144 =item 1.
145  
146 The server and server binaries are hosted on a system different to the one that you are doing development
147  
148 =item 2.
149  
150 You are using MBD on Windows where unix domain sockets are not available
151  
152 =back
153  
154 =back
155  
156 =head1 NOTES
157  
158 The environment variables understood by C<psql>:
159 C<PGUSER>, C<PGHOST> and C<PGPORT> will be used when
160 connecting to a live database (for L<dbinstall|Module::Build::Database#dbinstall> and
161 L<fakeinstall||Module::Build::Database#dbfakeinstall>). C<PGDATABASE> will be ignored;
162 the name of the database should be specified in
163 Build.PL instead.
164  
165 =cut
166  
167 package Module::Build::Database::PostgreSQL;
168 4 ▶use base 'Module::Build::Database';
Subroutine BEGIN: called CC=1 SCAR=0.0
169 4 ▶use File::Temp qw/tempdir/;
Subroutine BEGIN: called CC=1 SCAR=0.0
170 4 ▶use File::Path qw/rmtree/;
Subroutine BEGIN: called CC=1 SCAR=0.0
171 4 ▶use File::Basename qw/dirname/;
Subroutine BEGIN: called CC=1 SCAR=0.0
172 4 ▶use File::Copy::Recursive qw/fcopy dirmove/;
Subroutine BEGIN: called CC=1 SCAR=0.0
173 4 ▶use Path::Class qw/file/;
Subroutine BEGIN: called CC=1 SCAR=0.0
174 4 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
175 4 ▶use File::Which qw( which );
Subroutine BEGIN: called CC=1 SCAR=0.0
176  
177 4 ▶use Module::Build::Database::PostgreSQL::Templates;
Subroutine BEGIN: called CC=1 SCAR=0.0
178 4 ▶use Module::Build::Database::Helpers qw/do_system verify_bin info debug/;
Subroutine BEGIN: called CC=1 SCAR=0.0
179 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
180 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
181 4 our $VERSION = '0.58';
182  
183 4 __PACKAGE__->add_property(database_options => default => { name => "foo", schema => "bar" });
184 4 __PACKAGE__->add_property(database_extensions => default => { postgis => 0 } );
185 4 __PACKAGE__->add_property(postgis_base => default => "/usr/local/share/postgis" );
186 4 __PACKAGE__->add_property(_tmp_db_dir => default => "" );
187 4 __PACKAGE__->add_property(leave_running => default => 0 ); # leave running after dbtest?
188 0 __PACKAGE__->add_property(scratch_database => default => { map {; "PG$_" => $ENV{"MBD_SCRATCH_PG$_"} }
189 4 grep { defined $ENV{"MBD_SCRATCH_PG$_"} }
190 qw( HOST PORT USER ) } );
191  
192 # Binaries used by this module. They should be in $ENV{PATH}.
193 4 our %Bin = (
194 Psql => 'psql',
195 Pgctl => 'pg_ctl',
196 Postgres => 'postgres',
197 Initdb => 'initdb',
198 Createdb => 'createdb',
199 Dropdb => 'dropdb',
200 Pgdump => 'pg_dump',
201 Pgdoc => [ qw/pg_autodoc postgresql_autodoc/ ],
202 );
203 4 my $server_bin_dir;
204 4 ▶if(my $pg_config = which 'pg_config')
Branch: 1 branch
BranchTrueFalseTotal
if (my $pg_config = which("pg_config")) 0 4 4
205 {
206 0 ▶ $pg_config = Win32::GetShortPathName($pg_config) if $^O eq 'MSWin32' && $pg_config =~ /\s/;
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq "MSWin32" and $pg_config =~ /\s/ 0 0 0
Condition0%0/3
$^O eq "MSWin32" and $pg_config =~ /\s/
!ll&&!rl&&r
000
MC/DC0%0/2
$^O eq "MSWin32" and $pg_config =~ /\s/
$^O eq "MSWin32" $pg_config =~ /\s/
Truth table0%0/3
A and B
A= $^O eq "MSWin32"B= $pg_config =~ /\s/
ABresult
0X0
100
111
207 0 $server_bin_dir = `$pg_config --bindir`;
208 0 chomp $server_bin_dir;
209 0 ▶ $server_bin_dir = Win32::GetShortPathName($server_bin_dir) if $^O eq 'MSWin32' && $server_bin_dir =~ /\s/;
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq "MSWin32" and $server_bin_dir =~ /\s/ 0 0 0
Condition0%0/3
$^O eq "MSWin32" and $server_bin_dir =~ /\s/
!ll&&!rl&&r
000
MC/DC0%0/2
$^O eq "MSWin32" and $server_bin_dir =~ /\s/
$^O eq "MSWin32" $server_bin_dir =~ /\s/
Truth table0%0/3
A and B
A= $^O eq "MSWin32"B= $server_bin_dir =~ /\s/
ABresult
0X0
100
111
210 0 ▶ undef $server_bin_dir unless -d $server_bin_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $server_bin_dir 0 0 0
211 }
212 4 verify_bin(\%Bin, $server_bin_dir);
213  
214 0 ▶sub _do_psql {
Subroutine _do_psql: not called CC=1 SCAR=6.9
215 0 my $self = shift;
216 0 my $sql = shift;
217 0 my $database_name = $self->database_options('name');
218 0 my $tmp = File::Temp->new(TEMPLATE => "tmp_db_XXXX", SUFFIX => '.sql');
219 0 print $tmp $sql;
220 0 $tmp->close;
221 # -q: quiet, ON_ERROR_STOP: throw exceptions
222 0 local $ENV{PERL5LIB};
223 0 my $ret = do_system( $Bin{Psql}, "-q", "-vON_ERROR_STOP=1", "-f", "$tmp", $database_name );
224 0 $tmp->unlink_on_destroy($ret);
225 0 $ret;
226 }
227 0 ▶sub _do_psql_out {
Subroutine _do_psql_out: not called CC=1 SCAR=6.9
228 0 my $self = shift;
229 0 my $sql = shift;
230 0 my $database_name = $self->database_options('name');
231 # -F field separator, -x extended output, -A: unaligned
232 0 local $ENV{PERL5LIB};
233 0 do_system( $Bin{Psql}, "-q", "-vON_ERROR_STOP=1", "-A", "-F ' : '", "-x", "-c", qq["$sql"], $database_name );
234 }
235 0 ▶sub _do_psql_file {
Subroutine _do_psql_file: not called CC=3 SCAR=24.8
236 0 my $self = shift;
237 0 my $filename = shift;
238 0 ▶ unless (-e $filename) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $filename) 0 0 0
239 0 warn "could not open file $filename";
240 0 return 0;
241 }
242 0 ▶ unless (-s $filename) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-s $filename) 0 0 0
243 0 warn "file $filename is empty";
244 0 return 0;
245 }
246 0 my $database_name = $self->database_options('name');
247 # -q: quiet, ON_ERROR_STOP: throw exceptions
248 0 local $ENV{PERL5LIB};
249 0 do_system($Bin{Psql},"-q","-vON_ERROR_STOP=1","-f",$filename, $database_name);
250 }
251 0 ▶sub _do_psql_into_file {
Subroutine _do_psql_into_file: not called CC=2 SCAR=17.9
252 0 my $self = shift;
253 0 my $filename = shift;
254 0 my $sql = shift;
255 0 my $database_name = $self->database_options('name');
256 # -A: unaligned, -F: field separator, -t: tuples only, ON_ERROR_STOP: throw exceptions
257 0 local $ENV{PERL5LIB};
258 0 ▶ my $q = $^O eq 'MSWin32' ? '"' : "'";
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 0 0 0
259 0 do_system( $Bin{Psql}, "-q", "-vON_ERROR_STOP=1", "-A", "-F $q\t$q", "-t", "-c", qq["$sql"], $database_name, ">", "$filename" );
260 }
261 0 ▶sub _do_psql_capture {
Subroutine _do_psql_capture: not called CC=1 SCAR=6.9
262 0 my $self = shift;
263 0 my $sql = shift;
264 0 my $database_name = $self->database_options('name');
265 0 local $ENV{PERL5LIB};
266 0 return qx[$Bin{Psql} -c "$sql" $database_name];
267 }
268  
269 0 ▶sub _cleanup_old_dbs {
Subroutine _cleanup_old_dbs: not called CC=4 SCAR=30.0
270 0 my $self = shift;
271 0 my %args = @_; # pass all => 1 to clean up the current one too
272  
273 0 my $glob;
274 {
275 0 my $tmpdir = tempdir("mbdtest_XXXXXX", TMPDIR => 1);
276 0 $glob = "$tmpdir";
277 0 rmtree($tmpdir);
278 }
279 0 $glob =~ s/mbdtest_.*$/mbdtest_*/;
280 0 for my $thisdir (glob $glob) {
281 0 ▶ next unless -d $thisdir && -w $thisdir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $thisdir and -w $thisdir 0 0 0
Condition0%0/3
-d $thisdir and -w $thisdir
!ll&&!rl&&r
000
MC/DC0%0/2
-d $thisdir and -w $thisdir
-d $thisdir -w $thisdir
Truth table0%0/3
A and B
A= -d $thisdirB= -w $thisdir
ABresult
0X0
100
111
282 0 debug "cleaning up old tmp instance : $thisdir";
283 0 $self->_stop_db("$thisdir/db");
284 0 rmtree($thisdir);
285 }
286 }
287  
288 0 ▶sub _start_new_db {
Subroutine _start_new_db: not called CC=10 SCAR=47.0
289 0 my $self = shift;
290 # Start a new database and return the host on which it was started.
291  
292 0 my $database_name = $self->database_options('name');
293 0 $ENV{PGDATABASE} = $database_name;
294  
295 0 ▶ if(%{ $self->scratch_database }) {
Branch: 1 branch
BranchTrueFalseTotal
if (%{$self->scratch_database;}) { } 0 0 0
296 0 delete @ENV{qw( PGHOST PGUSER PGPORT )};
297 0 %ENV = (%ENV, %{ $self->scratch_database });
298 0 do_system("_silent", $Bin{Dropdb}, $database_name);
299  
300 } else {
301  
302 0 $self->_cleanup_old_dbs();
303  
304 0 my $tmpdir = tempdir("mbdtest_XXXXXX", TMPDIR => 1);
305 0 my $dbdir = $tmpdir."/db";
306 0 my $initlog = "$tmpdir/postgres.log";
307 0 $self->_tmp_db_dir($dbdir);
308  
309 0 $ENV{PGHOST} = "$dbdir"; # makes psql use a socket, not a tcp port
310 0 delete $ENV{PGUSER};
311 0 delete $ENV{PGPORT};
312  
313 0 debug "initializing database (log: $initlog)";
314  
315 0 ▶ do_system($Bin{Initdb}, "-D", "$dbdir", ">>", "$initlog", "2>&1") or do {
Branch: 1 branch
BranchTrueFalseTotal
do_system($Module::Build::Database::PostgreSQL::Bin{"Initdb"}, "-D", "$dbdir", ">>", "$initlog", "2>&1") or do { my $log = ""; $log = file($initlog)->slurp if -e $initlog; die qq[could not initdb ($Module::Build::Database::PostgreSQL::Bin{"Initdb"})\n$log\n] } 0 0 0
316 0 my $log = '';
317 0 ▶ $log = file($initlog)->slurp if -e $initlog;
Branch: 1 branch
BranchTrueFalseTotal
if -e $initlog 0 0 0
318 0 die "could not initdb ($Bin{Initdb})\n$log\n";
319 };
320  
321 0 ▶ if (my $conf_append = $self->database_options('append_to_conf')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $conf_append = $self->database_options("append_to_conf")) 0 0 0
322 0 ▶ die "cannot find postgresql.conf" unless -e "$dbdir/postgresql.conf";
Branch: 1 branch
BranchTrueFalseTotal
unless -e "$dbdir/postgresql.conf" 0 0 0
323 0 ▶ open my $fp, ">> $dbdir/postgresql.conf" or die "could not open postgresql.conf : $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fp, ">> $dbdir/postgresql.conf" or die "could not open postgresql.conf : $!" 0 0 0
324 0 print $fp $conf_append;
325 0 close $fp;
326 }
327  
328 0 my $pmopts = qq[-k $dbdir -h '' -p 5432];
329  
330 0 debug "# starting postgres in $dbdir";
331 0 ▶ do_system($Bin{Pgctl}, qq[-o "$pmopts"], "-w", "-t", 120, "-D", "$dbdir", "-l", "postmaster.log", "start") or do {
Branch: 1 branch
BranchTrueFalseTotal
do_system($Module::Build::Database::PostgreSQL::Bin{"Pgctl"}, qq[-o "$pmopts"], "-w", "-t", 120, "-D", "$dbdir", "-l", "postmaster.log", "start") or do { my $log; if (-e "$dbdir/postmaster.log") { $log = file("$dbdir/postmaster.log")->slurp; } else { $log = "no log file : $dbdir/postmaster.log"; } ; die "could not start postgres\n$log\n " } 0 0 0
332 0 my $log;
333 0 ▶ if (-e "$dbdir/postmaster.log") {
Branch: 1 branch
BranchTrueFalseTotal
if (-e "$dbdir/postmaster.log") { } 0 0 0
334 0 $log = file("$dbdir/postmaster.log")->slurp;
335 } else {
336 0 $log = "no log file : $dbdir/postmaster.log";
337 }
338 0 die "could not start postgres\n$log\n ";
339 };
340  
341 0 my $domain = $dbdir.'/.s.PGSQL.5432';
342 0 ▶ -e $domain or die "could not find $domain";
Branch: 1 branch
BranchTrueFalseTotal
-e $domain or die "could not find $domain" 0 0 0
343 }
344  
345 0 $self->_create_database();
346  
347 0 return $self->_dbhost;
348 }
349  
350 0 ▶sub _remove_db {
Subroutine _remove_db: not called CC=4 SCAR=30.0
351 0 my $self = shift;
352 0 ▶ return if $ENV{MBD_DONT_STOP_TEST_DB} || %{ $self->scratch_database };
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"MBD_DONT_STOP_TEST_DB"} or %{$self->scratch_database;} 0 0 0
Condition0%0/3
$ENV{"MBD_DONT_STOP_TEST_DB"} or %{$self->scratch_database;}
l!l&&r!l&&!r
000
MC/DC0%0/2
$ENV{"MBD_DONT_STOP_TEST_DB"} or %{$self->scratch_database;}
$ENV{"MBD_DONT_STOP_TEST_DB"} %{$self->scratch_database;}
Truth table0%0/3
A or B
A= $ENV{"MBD_DONT_STOP_TEST_DB"}B= %{$self->scratch_database;}
ABresult
1X1
011
000
353 0 ▶ my $dbdir = shift || $self->_tmp_db_dir();
Condition0%0/3
shift() || $self->_tmp_db_dir
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $self->_tmp_db_dir
shift() $self->_tmp_db_dir
Truth table0%0/3
A || B
A= shift()B= $self->_tmp_db_dir
ABresult
1X1
011
000
354 0 $dbdir =~ s/\/db$//;
355 0 rmtree $dbdir;
356 }
357  
358 0 ▶sub _stop_db {
Subroutine _stop_db: not called CC=7 SCAR=40.3
359 0 my $self = shift;
360 0 ▶ return if $ENV{MBD_DONT_STOP_TEST_DB} || %{ $self->scratch_database };
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"MBD_DONT_STOP_TEST_DB"} or %{$self->scratch_database;} 0 0 0
Condition0%0/3
$ENV{"MBD_DONT_STOP_TEST_DB"} or %{$self->scratch_database;}
l!l&&r!l&&!r
000
MC/DC0%0/2
$ENV{"MBD_DONT_STOP_TEST_DB"} or %{$self->scratch_database;}
$ENV{"MBD_DONT_STOP_TEST_DB"} %{$self->scratch_database;}
Truth table0%0/3
A or B
A= $ENV{"MBD_DONT_STOP_TEST_DB"}B= %{$self->scratch_database;}
ABresult
1X1
011
000
361 0 ▶ my $dbdir = shift || $self->_tmp_db_dir();
Condition0%0/3
shift() || $self->_tmp_db_dir
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $self->_tmp_db_dir
shift() $self->_tmp_db_dir
Truth table0%0/3
A || B
A= shift()B= $self->_tmp_db_dir
ABresult
1X1
011
000
362 0 my $pid_file = "$dbdir/postmaster.pid";
363 0 ▶ unless (-e $pid_file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $pid_file) 0 0 0
364 0 debug "no pid file ($pid_file), not stopping db";
365 0 return;
366 }
367 0 my ($pid) = IO::File->new("<$pid_file")->getlines;
368 0 chomp $pid;
369 0 kill "TERM", $pid;
370 0 sleep 1;
371 0 ▶ return unless kill 0, $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless kill 0, $pid 0 0 0
372 0 ▶ kill 9, $pid or info "could not send signal to $pid";
Branch: 1 branch
BranchTrueFalseTotal
kill 9, $pid or &info("could not send signal to $pid") 0 0 0
373 }
374  
375 0 ▶sub _apply_base_sql {
Subroutine _apply_base_sql: not called CC=3 SCAR=24.8
376 0 my $self = shift;
377 0 ▶ my $filename = shift || $self->base_dir."/db/dist/base.sql";
Condition0%0/2
shift() || $self->base_dir . '/db/dist/base.sql'
l!l
00
MC/DC0%0/1
shift() || $self->base_dir . '/db/dist/base.sql'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
378 0 ▶ return unless -e $filename;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $filename 0 0 0
379 0 info "applying base.sql";
380 0 $self->_do_psql_file($filename);
381 }
382  
383 0 ▶sub _apply_base_data {
Subroutine _apply_base_data: not called CC=3 SCAR=24.8
384 0 my $self = shift;
385 0 ▶ my $filename = shift || $self->base_dir."/db/dist/base_data.sql";
Condition0%0/2
shift() || $self->base_dir . '/db/dist/base_data.sql'
l!l
00
MC/DC0%0/1
shift() || $self->base_dir . '/db/dist/base_data.sql'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
386 0 ▶ return 1 unless -e $filename;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $filename 0 0 0
387 0 info "applying base_data.sql";
388 0 $self->_do_psql_file($filename);
389 }
390  
391 0 ▶sub _dump_base_sql {
Subroutine _dump_base_sql: not called CC=8 SCAR=42.8
392 # Optional parameter "outfile" gives the name of the file into which to dump the schema.
393 # If the parameter is omitted, dump and atomically rename to db/dist/base.sql.
394 0 my $self = shift;
395 0 my %args = @_;
396 0 ▶ my $outfile = $args{outfile} || $self->base_dir. "/db/dist/base.sql";
Condition0%0/2
$args{'outfile'} || $self->base_dir . '/db/dist/base.sql'
l!l
00
MC/DC0%0/1
$args{'outfile'} || $self->base_dir . '/db/dist/base.sql'
$args{'outfile'}
Truth table0%0/2
A
A= $args{'outfile'}
Aresult
11
00
397  
398 0 my $tmpfile = file( tempdir( CLEANUP => 1 ), 'dump.sql');
399  
400 # -x : no privileges, -O : no owner, -s : schema only, -n : only this schema
401 0 my $database_schema = $self->database_options('schema');
402 0 my $database_name = $self->database_options('name');
403 0 local $ENV{PERL5LIB};
404 do_system( $Bin{Pgdump}, "-xOs", "-E", "utf8", "-n", $database_schema, $database_name, ">", $tmpfile )
405 0 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
do_system($Module::Build::Database::PostgreSQL::Bin{"Pgdump"}, "-xOs", "-E", "utf8", "-n", $database_schema, $database_name, ">", $tmpfile) or do { &info("Error running pgdump"); die "Error running pgdump : $! ${^CHILD_ERROR_NATIVE}"; return 0 } 0 0 0
406 0 info "Error running pgdump";
407 0 die "Error running pgdump : $! ${^CHILD_ERROR_NATIVE}";
408 0 return 0;
409 };
410  
411 0 my @lines = $tmpfile->slurp();
412 0 ▶ unless (@lines) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@lines) 0 0 0
413 0 die "# Could not run pgdump and write to $tmpfile";
414 }
415 @lines = grep {
416 0 ▶ $_ !~ /^--/
Condition0%0/6
$_ =~ /^--/ || $_ =~ /^CREATE SCHEMA $database_schema;$/ || $_ =~ /^SET (search_path|lock_timeout)/
l!l&&r!l&&!r
000
$_ =~ /^--/ or $_ =~ /^CREATE SCHEMA $database_schema;$/
l!l&&r!l&&!r
000
MC/DC0%0/3
$_ =~ /^--/ || $_ =~ /^CREATE SCHEMA $database_schema;$/ || $_ =~ /^SET (search_path|lock_timeout)/
$_ =~ /^--/ $_ =~ /^CREATE SCHEMA $database_schema;$/ $_ =~ /^SET (search_path|lock_timeout)/
Truth table0%0/4
A or B || C
A= $_ =~ /^--/B= $_ =~ /^CREATE SCHEMA $database_schema;$/C= $_ =~ /^SET (search_path|lock_timeout)/
ABCresult
1XX1
01X1
0011
0000
417 and $_ !~ /^CREATE SCHEMA $database_schema;$/
418 and $_ !~ /^SET (search_path|lock_timeout)/
419 } @lines;
420 0 file($outfile)->spew(join '', @lines);
421 0 ▶ if (@lines > 0 && !-s $outfile) {
Branch: 1 branch
BranchTrueFalseTotal
if (@lines > 0 and not -s $outfile) 0 0 0
Condition0%0/3
@lines > 0 and not -s $outfile
!ll&&!rl&&r
000
MC/DC0%0/2
@lines > 0 and not -s $outfile
@lines > 0 not -s $outfile
Truth table0%0/3
A and not(B)
A= @lines > 0B= not -s $outfile
ABresult
0X0
100
111
422 0 die "# Unable to write to $outfile";
423 }
424 0 return 1;
425 }
426  
427 0 ▶sub _dump_base_data {
Subroutine _dump_base_data: not called CC=4 SCAR=30.0
428 # Optional parameter "outfile, defaults to db/dist/base_data.sql
429 0 my $self = shift;
430 0 my %args = @_;
431 0 ▶ my $outfile = $args{outfile} || $self->base_dir. "/db/dist/base_data.sql";
Condition0%0/2
$args{'outfile'} || $self->base_dir . '/db/dist/base_data.sql'
l!l
00
MC/DC0%0/1
$args{'outfile'} || $self->base_dir . '/db/dist/base_data.sql'
$args{'outfile'}
Truth table0%0/2
A
A= $args{'outfile'}
Aresult
11
00
432  
433 0 my $tmpfile = File::Temp->new(
434 TEMPLATE => (dirname $outfile)."/dump_XXXXXX",
435 UNLINK => 0
436 );
437 0 $tmpfile->close;
438  
439 # -x : no privileges, -O : no owner, -s : schema only, -n : only this schema
440 0 my $database_schema = $self->database_options('schema');
441 0 my $database_name = $self->database_options('name');
442 0 local $ENV{PERL5LIB};
443 0 ▶ do_system( $Bin{Pgdump}, "--data-only", "-xO", "-E", "utf8", "-n", $database_schema, $database_name,
Branch: 1 branch
BranchTrueFalseTotal
do_system($Module::Build::Database::PostgreSQL::Bin{"Pgdump"}, "--data-only", "-xO", "-E", "utf8", "-n", $database_schema, $database_name, "|", "egrep -v '^SET (lock_timeout|search_path)'", ">", "$tmpfile") or return 0 0 0 0
444 "|", "egrep -v '^SET (lock_timeout|search_path)'",
445 ">", "$tmpfile" )
446 or return 0;
447 0 ▶ rename "$tmpfile", $outfile or die "rename failed: $!";
Branch: 1 branch
BranchTrueFalseTotal
rename "$tmpfile", $outfile or die "rename failed: $!" 0 0 0
448 }
449  
450 0 ▶sub _apply_patch {
Subroutine _apply_patch: not called CC=1 SCAR=6.9
451 0 my $self = shift;
452 0 my $patch_file = shift;
453  
454 0 return $self->_do_psql_file($self->base_dir."/db/patches/$patch_file");
455 }
456  
457 0 ▶sub _is_fresh_install {
Subroutine _is_fresh_install: not called CC=2 SCAR=17.9
458 0 my $self = shift;
459  
460 0 my $database_name = $self->database_options('name');
461 0 ▶ unless ($self->_database_exists) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_database_exists) 0 0 0
462 0 info "database $database_name does not exist";
463 0 return 1;
464 }
465  
466 0 my $file = File::Temp->new(); $file->close;
467 0 my $database_schema = $self->database_options('schema');
468 0 $self->_do_psql_into_file("$file","\\dn $database_schema");
469 0 return !do_system("_silent","grep -q $database_schema $file");
470 }
471  
472 0 ▶sub _show_live_db {
Subroutine _show_live_db: not called CC=6 SCAR=37.4
473 # Display the connection information
474 0 my $self = shift;
475  
476 0 ▶ info "PGUSER : " . ( $ENV{PGUSER} || "<undef>" );
Condition0%0/2
$ENV{'PGUSER'} || '<undef>'
l!l
00
MC/DC0%0/1
$ENV{'PGUSER'} || '<undef>'
$ENV{'PGUSER'}
Truth table0%0/2
A
A= $ENV{'PGUSER'}
Aresult
11
00
477 0 ▶ info "PGHOST : " . ( $ENV{PGHOST} || "<undef>" );
Condition0%0/2
$ENV{'PGHOST'} || '<undef>'
l!l
00
MC/DC0%0/1
$ENV{'PGHOST'} || '<undef>'
$ENV{'PGHOST'}
Truth table0%0/2
A
A= $ENV{'PGHOST'}
Aresult
11
00
478 0 ▶ info "PGPORT : " . ( $ENV{PGPORT} || "<undef>" );
Condition0%0/2
$ENV{'PGPORT'} || '<undef>'
l!l
00
MC/DC0%0/1
$ENV{'PGPORT'} || '<undef>'
$ENV{'PGPORT'}
Truth table0%0/2
A
A= $ENV{'PGPORT'}
Aresult
11
00
479  
480 0 ▶ my $database_name = shift || $self->database_options('name');
Condition0%0/3
shift() || $self->database_options('name')
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $self->database_options('name')
shift() $self->database_options('name')
Truth table0%0/3
A || B
A= shift()B= $self->database_options('name')
ABresult
1X1
011
000
481 0 info "database : $database_name";
482  
483 0 ▶ return unless $self->_database_exists;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_database_exists 0 0 0
484 0 $self->_do_psql_out("select current_database(),session_user,version();");
485 }
486  
487 0 ▶sub _patch_table_exists {
Subroutine _patch_table_exists: not called CC=1 SCAR=6.9
488 # returns true or false
489 0 my $self = shift;
490 0 my $file = File::Temp->new(); $file->close;
491 0 my $database_schema = $self->database_options('schema');
492 0 $self->_do_psql_into_file("$file","select tablename from pg_tables where tablename='patches_applied' and schemaname = '$database_schema'");
493 0 return do_system("_silent","grep -q patches_applied $file");
494 }
495  
496 0 ▶sub _dump_patch_table {
Subroutine _dump_patch_table: not called CC=2 SCAR=17.9
497 # Dump the patch table in an existing db into a flat file, that
498 # will be in the same format as patches_applied.txt.
499 0 my $self = shift;
500 0 my %args = @_;
501 0 ▶ my $filename = $args{outfile} or Carp::confess "need a filename";
Branch: 1 branch
BranchTrueFalseTotal
my $filename = $args{"outfile"} or Carp::confess("need a filename") 0 0 0
502 0 my $database_schema = $self->database_options('schema');
503 0 $self->_do_psql_into_file($filename,"select patch_name,patch_md5 from $database_schema.patches_applied order by patch_name");
504 }
505  
506 0 ▶sub _create_patch_table {
Subroutine _create_patch_table: not called CC=1 SCAR=6.9
507 0 my $self = shift;
508 # create a new patch table
509 0 my $database_schema = $self->database_options('schema');
510 0 my $sql = <<EOSQL;
511 CREATE TABLE $database_schema.patches_applied (
512 patch_name varchar(255) primary key,
513 patch_md5 varchar(255),
514 when_applied timestamp );
515 EOSQL
516 0 $self->_do_psql($sql);
517 }
518  
519 0 ▶sub _insert_patch_record {
Subroutine _insert_patch_record: not called CC=1 SCAR=6.9
520 0 my $self = shift;
521 0 my $record = shift;
522 0 my ($name,$md5) = @$record;
523 0 my $database_schema = $self->database_options('schema');
524 0 $self->_do_psql("insert into $database_schema.patches_applied (patch_name, patch_md5, when_applied) ".
525 " values ('$name','$md5',now()) ");
526 }
527  
528 0 ▶sub _database_exists {
Subroutine _database_exists: not called CC=2 SCAR=17.9
529 0 my $self = shift;
530 0 ▶ my $database_name = shift || $self->database_options('name');
Condition0%0/3
shift() || $self->database_options('name')
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $self->database_options('name')
shift() $self->database_options('name')
Truth table0%0/3
A || B
A= shift()B= $self->database_options('name')
ABresult
1X1
011
000
531 0 local $ENV{PERL5LIB};
532 0 scalar grep /^$database_name$/, map { [split /:/]->[0] } `psql -Alt -F:`;
533 }
534  
535 0 ▶sub _create_language_extensions {
Subroutine _create_language_extensions: not called CC=4 SCAR=30.0
536 0 my $self = shift;
537 0 my $list = $self->database_extensions('languages');
538 0 ▶ return unless $list;
Branch: 1 branch
BranchTrueFalseTotal
unless $list 0 0 0
539 0 foreach my $lang (@$list) {
540 0 ▶ $self->_do_psql("create extension if not exists $lang") || die "error creating language: $lang";
Branch: 1 branch
BranchTrueFalseTotal
$self->_do_psql("create extension if not exists $lang") or die "error creating language: $lang" 0 0 0
541 }
542 }
543  
544 0 ▶sub _create_database {
Subroutine _create_database: not called CC=11 SCAR=48.8
545 0 my $self = shift;
546  
547 0 my $database_name = $self->database_options('name');
548 0 my $database_schema = $self->database_options('schema');
549  
550 # create the database if necessary
551 0 ▶ unless ($self->_database_exists($database_name)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_database_exists($database_name)) 0 0 0
552 0 local $ENV{PERL5LIB};
553 0 ▶ do_system($Bin{Createdb}, $database_name) or die "could not createdb";
Branch: 1 branch
BranchTrueFalseTotal
do_system($Module::Build::Database::PostgreSQL::Bin{"Createdb"}, $database_name) or die "could not createdb" 0 0 0
554 }
555  
556 # Create a fresh schema in the database.
557 0 ▶ $self->_do_psql("create schema $database_schema") unless $database_schema eq 'public';
Branch: 1 branch
BranchTrueFalseTotal
unless $database_schema eq "public" 0 0 0
558  
559 0 $self->_do_psql("alter database $database_name set client_min_messages to ERROR");
560  
561 0 $self->_do_psql("alter database $database_name set search_path to $database_schema;");
562  
563 # stolen from http://wiki.postgresql.org/wiki/CREATE_OR_REPLACE_LANGUAGE
564 0 $self->_do_psql(<<'SAFE_MAKE_PLPGSQL');
565 CREATE OR REPLACE FUNCTION make_plpgsql()
566 RETURNS VOID
567 LANGUAGE SQL
568 AS $$
569 CREATE LANGUAGE plpgsql;
570 $$;
571  
572 SELECT
573 CASE
574 WHEN EXISTS(
575 SELECT 1
576 FROM pg_catalog.pg_language
577 WHERE lanname='plpgsql'
578 )
579 THEN NULL
580 ELSE make_plpgsql() END;
581  
582 DROP FUNCTION make_plpgsql();
583 SAFE_MAKE_PLPGSQL
584  
585 0 ▶ if (my $postgis = $self->database_extensions('postgis')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $postgis = $self->database_extensions("postgis")) 0 0 0
586 0 info "applying postgis extension";
587 0 ▶ my $postgis_schema = $postgis->{schema} or die "No schema given for postgis";
Branch: 1 branch
BranchTrueFalseTotal
my $postgis_schema = $postgis->{"schema"} or die "No schema given for postgis" 0 0 0
588 0 ▶ $self->_do_psql("create schema $postgis_schema") unless $postgis_schema eq 'public';
Branch: 1 branch
BranchTrueFalseTotal
unless $postgis_schema eq "public" 0 0 0
589 0 $self->_do_psql("alter database $database_name set search_path to $postgis_schema;");
590 # We need to run "createlang plpgsql" first.
591 0 ▶ $self->_do_psql_file($self->postgis_base. "/postgis.sql") or die "could not do postgis.sql";
Branch: 1 branch
BranchTrueFalseTotal
$self->_do_psql_file($self->postgis_base . '/postgis.sql') or die "could not do postgis.sql" 0 0 0
592 0 ▶ $self->_do_psql_file($self->postgis_base. "/spatial_ref_sys.sql") or die "could not do spatial_ref_sys.sql";
Branch: 1 branch
BranchTrueFalseTotal
$self->_do_psql_file($self->postgis_base . '/spatial_ref_sys.sql') or die "could not do spatial_ref_sys.sql" 0 0 0
593 0 $self->_do_psql("alter database $database_name set search_path to $database_schema, $postgis_schema");
594 }
595  
596 0 ▶ if (my $sql = $self->database_options('post_initdb')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sql = $self->database_options("post_initdb")) 0 0 0
597 0 info "applying post_initdb (nb: this option has been renamed to 'after_create')";
598 0 $self->_do_psql($sql);
599 }
600  
601 0 ▶ if (my $sql = $self->database_options('after_create')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sql = $self->database_options("after_create")) 0 0 0
602 0 info "applying after_create";
603 0 $self->_do_psql($sql);
604 }
605  
606 0 1;
607 }
608  
609 0 ▶sub _remove_patches_applied_table {
Subroutine _remove_patches_applied_table: not called CC=1 SCAR=6.9
610 0 my $self = shift;
611 0 my $database_schema = $self->database_options('schema');
612 0 $self->_do_psql("drop table if exists $database_schema.patches_applied;");
613 }
614  
615 0 ▶sub _generate_docs {
Subroutine _generate_docs: not called CC=12 SCAR=50.5
616 0 my $self = shift;
617 0 my %args = @_;
618 0 ▶ my $dir = $args{dir} or die "missing dir";
Branch: 1 branch
BranchTrueFalseTotal
my $dir = $args{"dir"} or die "missing dir" 0 0 0
619 0 my $tmpdir = tempdir;
620 0 my $tc = "Module::Build::Database::PostgreSQL::Templates";
621 0 my $database_name = $self->database_options('name');
622 0 my $database_schema = $self->database_options('schema');
623  
624 0 $self->_start_new_db();
625 0 $self->_apply_base_sql();
626  
627 0 chdir $tmpdir;
628 0 for my $filename ($tc->filenames) {
629 0 ▶ open my $fp, ">$filename" or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $fp, ">$filename" or die $! 0 0 0
630 0 print ${fp} $tc->file_contents($filename);
631 0 close $fp;
632 }
633  
634 # http://perlmonks.org/?node_id=821413
635 0 do_system( $Bin{Pgdoc}, "-d", $database_name, "-s", $database_schema, "-l .", "-t pod" );
636 0 do_system( $Bin{Pgdoc}, "-d", $database_name, "-s", $database_schema, "-l .", "-t html" );
637 0 do_system( $Bin{Pgdoc}, "-d", $database_name, "-s", $database_schema, "-l .", "-t dot" );
638  
639 0 for my $type (qw(pod html)) {
640 0 ▶ my $fp = IO::File->new("<$database_name.$type") or die $!;
Branch: 1 branch
BranchTrueFalseTotal
my $fp = "IO::File"->new("<$database_name.$type") or die $! 0 0 0
641 0 ▶ mkdir $type or die $!;
Branch: 1 branch
BranchTrueFalseTotal
mkdir $type or die $! 0 0 0
642 0 my $outfp;
643 0 ▶ while (<$fp>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fp)) 0 0 0
644 0 ▶ s/^_CUT: (.*)$// and do { $outfp = IO::File->new(">$type/$1") or die $!; };
Branch: 2 branches
BranchTrueFalseTotal
s/^_CUT: (.*)$// and do { $outfp = "IO::File"->new(">$type/$1") or die $! } 0 0 0
$outfp = "IO::File"->new(">$type/$1") or die $! 0 0 0
645 0 ▶ s/^_DB: (.*)$// and do { $_ = $self->_do_psql_capture($1); s/^/ /gm; };
Branch: 1 branch
BranchTrueFalseTotal
s/^_DB: (.*)$// and do { $_ = $self->_do_psql_capture($1); s/^/ /gm } 0 0 0
646 0 ▶ print ${outfp} $_ if defined($outfp);
Branch: 1 branch
BranchTrueFalseTotal
if defined $outfp 0 0 0
647 }
648 }
649 0 dirmove "$tmpdir/pod", "$dir/pod";
650 0 info "Generated $dir/pod";
651 0 dirmove "$tmpdir/html", "$dir/html";
652 0 info "Generated $dir/html";
653 0 fcopy "$tmpdir/$database_name.dot", "$dir";
654 0 info "Generated $dir/$database_name.dot";
655 }
656  
657 0 ▶sub ACTION_dbtest { shift->SUPER::ACTION_dbtest(@_); }
Subroutine ACTION_dbtest: not called CC=1 SCAR=6.9
Pod undocumented
658 0 ▶sub ACTION_dbclean { shift->SUPER::ACTION_dbclean(@_); }
Subroutine ACTION_dbclean: not called CC=1 SCAR=6.9
Pod undocumented
659 0 ▶sub ACTION_dbdist { shift->SUPER::ACTION_dbdist(@_); }
Subroutine ACTION_dbdist: not called CC=1 SCAR=6.9
Pod undocumented
660 0 ▶sub ACTION_dbdocs { shift->SUPER::ACTION_dbdocs(@_); }
Subroutine ACTION_dbdocs: not called CC=1 SCAR=6.9
Pod undocumented
661 0 ▶sub ACTION_dbinstall { shift->SUPER::ACTION_dbinstall(@_); }
Subroutine ACTION_dbinstall: not called CC=1 SCAR=6.9
Pod undocumented
662 0 ▶sub ACTION_dbfakeinstall { shift->SUPER::ACTION_dbfakeinstall(@_); }
Subroutine ACTION_dbfakeinstall: not called CC=1 SCAR=6.9
Pod undocumented
663  
664 0 ▶sub _dbhost {
Subroutine _dbhost: not called CC=2 SCAR=17.9
665 0 ▶ return $ENV{PGHOST} || 'localhost';
Condition0%0/2
$ENV{'PGHOST'} || 'localhost'
l!l
00
MC/DC0%0/1
$ENV{'PGHOST'} || 'localhost'
$ENV{'PGHOST'}
Truth table0%0/2
A
A= $ENV{'PGHOST'}
Aresult
11
00
666 }
667  
668 4 1;
669  
« Module/Build/Database.pm ↑ Summary Module/Build/Database/Helpers.pm »