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