DBIx/Class/Migration/PostgresqlSandbox.pm untested

filter: Statementstmt 0.0% 0 / 123 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 187 CC 6 SCAR 16.9
CC
6
Cov
0%
_determine_auto_start
12.0
_build_test_postgresql
12.0
make_sandbox
6.0
CRAP 5.4
1 package DBIx::Class::Migration::PostgresqlSandbox;
2  
3 use Moo;
4 use Test::Postgresql58;
5 use File::Spec::Functions 'catdir', 'catfile';
6 use File::Path 'mkpath';
7 use POSIX qw(SIGINT);
8 use DBIx::Class::Migration::Types -all;
9  
10 with 'DBIx::Class::Migration::Sandbox';
11  
12 has test_postgresql => (is=>'lazy', isa=>Object);
13  
14 sub _generate_sandbox_dir {
15 my $schema_class = (my $self = shift)->schema_class;
16 $schema_class =~ s/::/-/g;
17 catdir($self->target_dir, lc($schema_class));
18 }
19  
20 sub _determine_auto_start {
21 my $base_dir = shift;
22 if(-d $base_dir) {
23 if( -e catdir($base_dir, 'data','postmaster.pid')) {
24 return 0;
25 } else {
26 return 1;
27 }
28 } else {
29 return 2;
30 }
31 }
32  
33 sub _build_test_postgresql {
34 my $base_dir = (my $self = shift)->_generate_sandbox_dir;
35 my $auto_start = _determine_auto_start($base_dir);
36 my %config = (
37 auto_start => $auto_start,
38 base_dir => $base_dir,
39 initdb_args => $Test::Postgresql58::Defaults{initdb_args},
40 postmaster_args => $Test::Postgresql58::Defaults{postmaster_args});
41  
42 unless($auto_start) {
43 open ( my $pid_fh, '<', catdir($base_dir, 'data','postmaster.pid')) ||
44 $self->log_die( "Can't open PID file" );
45 my @lines = <$pid_fh>;
46 close ($pid_fh);
47 $config{port} = $lines[3];
48 }
49  
50 if(my $testdb = Test::Postgresql58->new(%config)) {
51 return $testdb;
52 } else {
53 $self->log_die( $Test::Postgresql58::errstr );
54 }
55 }
56  
57 sub _write_start {
58 my $base_dir = (my $self = shift)->test_postgresql->base_dir;
59 mkpath(my $bin = catdir($base_dir, 'bin'));
60 open( my $fh, '>', catfile($bin, 'start'))
61 || $self->log_die( "Cannot open $bin/start: $!" );
62  
63 my $test_postgresql = $self->test_postgresql;
64 my $postmaster = $test_postgresql->{postmaster};
65 my $data = catdir($base_dir, 'data');
66 my $port = $test_postgresql->{port};
67  
68 print $fh <<START;
69 #!/usr/bin/env sh
70  
71 $postmaster -p $port -D $data &
72 START
73  
74 close($fh);
75  
76 chmod oct("0755"), catfile($bin, 'start');
77 }
78  
79 sub _write_stop {
80 my $base_dir = (my $self = shift)->test_postgresql->base_dir;
81 mkpath(my $bin = catdir($base_dir, 'bin'));
82 open( my $fh, '>', catfile($bin, 'stop'))
83 || $self->log_die( "Cannot open $bin/stop: $!" );
84  
85 my $test_postgresql = $self->test_postgresql;
86 my $postmaster = $test_postgresql->{postmaster};
87 my $pid = catdir($base_dir, 'data','postmaster.pid');
88  
89 print $fh <<STOP;
90 #!/usr/bin/env sh
91  
92 kill -INT `head -1 $pid`
93 STOP
94  
95 close($fh);
96  
97 chmod oct("0755"), catfile($bin, 'stop');
98 }
99  
100 sub _write_use {
101 my $base_dir = (my $self = shift)->test_postgresql->base_dir;
102 mkpath(my $bin = catdir($base_dir, 'bin'));
103 open( my $fh, '>', catfile($bin, 'use'))
104 || $self->log_die( "Cannot open $bin/use: $!" );
105  
106 my $test_postgresql = $self->test_postgresql;
107 my $postmaster = $test_postgresql->{postmaster};
108 my $psql = $postmaster;
109 $psql =~s/postmaster$/psql/; # ugg
110 my $port = $test_postgresql->{port};
111  
112 print $fh <<USE;
113 #!/usr/bin/env sh
114  
115 $psql -h localhost --user postgres --port $port -d template1
116 USE
117  
118 close($fh);
119  
120 chmod oct("0755"), catfile($bin, 'use');
121 }
122  
123 sub _write_dump {
124 my $base_dir = (my $self = shift)->test_postgresql->base_dir;
125 mkpath(my $bin = catdir($base_dir, 'bin'));
126 open( my $fh, '>', catfile($bin, 'dump'))
127 || $self->log_die( "Cannot open $bin/dump: $!" );
128  
129 my $test_postgresql = $self->test_postgresql;
130 my $postmaster = $test_postgresql->{postmaster};
131 my $psql = $postmaster;
132 $psql =~s/postmaster$/pg_dump/; # ugg
133 my $port = $test_postgresql->{port};
134  
135 print $fh <<USE;
136 #!/usr/bin/env sh
137  
138 $psql -h localhost --user postgres --port $port \$@
139 USE
140  
141 close($fh);
142  
143 chmod oct("0755"), catfile($bin, 'dump');
144 }
145  
146 sub _write_config {
147 my $base_dir = (my $self = shift)->test_postgresql->base_dir;
148 mkpath(my $bin = catdir($base_dir, 'bin'));
149 open( my $fh, '>', catfile($bin, 'config'))
150 || $self->log_die( "Cannot open $bin/config $!" );
151  
152 my $test_postgresql = $self->test_postgresql;
153 my $postmaster = $test_postgresql->{postmaster};
154 my $psql = $postmaster;
155 $psql =~s/postmaster$/pg_dump/; # ugg
156 my $port = $test_postgresql->{port};
157  
158 print $fh <<USE;
159 #!/usr/bin/env perl
160  
161 my \$connect_info => { dsn => 'DBI:Pg:dbname=template1;host=localhost;port=$port', user => 'postgres', password => '' }
162  
163 USE
164  
165 close($fh);
166  
167 chmod oct("0755"), catfile($bin, 'config');
168 }
169  
170  
171 sub make_sandbox {
172 my $self = shift;
173 my $base_dir = $self->_generate_sandbox_dir;
174  
175 if($self->test_postgresql) {
176 $self->_write_start;
177 $self->_write_stop;
178 $self->_write_use;
179 $self->_write_dump;
180 $self->_write_config;
181  
182 my $port = $self->test_postgresql->port;
183 return "DBI:Pg:dbname=template1;host=127.0.0.1;port=$port",'postgres','';
184 } else {
185 $self->log_die( "can't start a postgresql sandbox" );
186 }
187 }
188  
189 ## I have to stop the database manually, not sure why, something borks
190 ## postgresql when SQLT->translate in DBIC-DH is called.
191  
192 #sub DEMOLISH { shift->test_postgresql->stop(SIGINT) }
193  
194 # ^ 03/04/2015 commenting this out since I 'think' its not an issue anymore
195  
196 __PACKAGE__->meta->make_immutable;
197  
198 =head1 NAME
199  
200 DBIx::Class::Migration::PostgresqlSandbox - Autocreate a postgresql sandbox
201  
202 =head1 SYNOPSIS
203  
204 use DBIx::Class::Migration;
205  
206 my $migration = DBIx::Class::Migration->new(
207 schema_class=>'Local::Schema',
208 db_sandbox_class=>'DBIx::Class::Migration::PostgresqlSandbox'),
209  
210 $migration->prepare;
211 $migration->install;
212  
213 =head1 DESCRIPTION
214  
215 This automatically creates a postgresql sandbox in your C<target_dir> that you can
216 use for initial prototyping, development and demonstration. If you want to
217 use this, you will need to add L<Test::Postgresql58> to your C<Makefile.PL> or your
218 C<dist.ini> file, and get that installed properly. It also requires that you
219 have Postgresql installed locally (although Postgresql does not need to be running, as
220 long as we can find in $PATH the binary installation). If your copy of Postgresql
221 is not installed in a normal location, you might need to locally alter $PATH
222 so that we can find it. For example, on my Mac, the path to Postgresql binaries
223 are at C</Library/PostgreSQL/bin> so you can alter the PATH for a single command
224 like so:
225  
226 PATH=Library/PostgreSQL/bin:$PATH [command]
227  
228 Or, if you are using Postgresql a lot, you can edit your C<.bashrc> to make the
229 above permanent.
230  
231 NOTE: You might find installing L<DBD::Pg> to be easier if you edit the
232 C<$PATH> before trying to install it.
233  
234 In addition to the Postgresql sandbox, we create several helper scripts C<start>,
235 C<stop> and C<use> which can be used to start, stop and open shell level access
236 to you mysql sandbox. C<dump> lets you easily access pg_dump and C<config> is
237 a Perl library that returns a hashref of the connection info, which is suitable
238 to use in L<DBIx::Class> Schema connect.
239  
240 These helper scripts will be located in a child directory of your C<target_dir>
241 (which defaults to C<share> under your project root directory). For example:
242  
243 [target_dir]/[schema_class]/bin/[start|stop|use]
244  
245 If your schema class is C<MyApp::Schema> you should see helper scripts like
246  
247 /MyApp-Web
248 /lib
249 /MyApp
250 Schema.pm
251 /Schema
252 ...
253 /share
254 /migrations
255 /fixtures
256 /myapp-schema
257 /bin
258 config
259 dump
260 start
261 stop
262 use
263  
264 This give you a system for installing a sandbox locally for development,
265 starting and stopping it for use (for example in a web application like one you
266 might create with L<Catalyst>) and for using it by opening a native C<psql>
267 shell (such as if you wish to review the database manually, and run native SQL
268 queries).
269  
270 =head1 SEE ALSO
271  
272 L<DBIx::Class::Migration>, L<DBD::Pg>, L<Test::Postgresql58>.
273  
274 =head1 AUTHOR
275  
276 See L<DBIx::Class::Migration> for author information
277  
278 =head1 COPYRIGHT & LICENSE
279  
280 See L<DBIx::Class::Migration> for copyright and license information
281  
282 =cut
283  
284