| 1 | package DBIx::Class::Migration::MySQLSandbox; | ||
| 2 | |||
| 3 | use Moo; | ||
| 4 | use Test::mysqld; | ||
| 5 | use File::Spec::Functions 'catdir', 'catfile', 'splitpath'; | ||
| 6 | use File::Path 'mkpath'; | ||
| 7 | use File::Temp 'tempdir'; | ||
| 8 | use Config::MySQL::Reader; | ||
| 9 | |||
| 10 | with 'DBIx::Class::Migration::Sandbox'; | ||
| 11 | |||
| 12 | has test_mysqld => (is=>'lazy'); | ||
| 13 | |||
| 14 | sub _generate_schema_path_part { | ||
| 15 | my $schema_class = (my $self = shift)->schema_class; | ||
| 16 | $schema_class =~ s/::/-/g; | ||
| 17 | return lc $schema_class; | ||
| 18 | } | ||
| 19 | |||
| 20 | sub _generate_sandbox_dir { | ||
| 21 | my $self = shift; | ||
| 22 | return catdir($self->target_dir, $self->_generate_schema_path_part); | ||
| 23 | } | ||
| 24 | |||
| 25 | sub _generate_unique_socket { | ||
| 26 | my $schema_path_part = (my $self = shift)->_generate_schema_path_part; | ||
| 27 | my $sockdir = catdir( tempdir(CLEANUP => 1), $schema_path_part); | ||
| 28 | |||
| 29 | mkpath($sockdir) unless -d $sockdir; | ||
| 30 | return catfile( $sockdir, 'mysqld.sock'); | ||
| 31 | } | ||
| 32 | |||
| 33 | sub _build_test_mysqld { | ||
| 34 | my $base_dir = (my $self = shift)->_generate_sandbox_dir; | ||
| 35 | my $auto_start = 2; | ||
| 36 | my $my_cnf = { | ||
| 37 | 'skip-networking' => '', | ||
| 38 | socket => $self->_generate_unique_socket }; | ||
| 39 | |||
| 40 | if( -d $base_dir) { | ||
| 41 | $auto_start = 1; | ||
| 42 | my $conf = Config::MySQL::Reader->read_file( catfile($base_dir, 'etc', 'my.cnf') ) || | ||
| 43 | $self->log_die( "Can't read my.cnf file" ); | ||
| 44 | $my_cnf = { socket => $conf->{mysqld}->{socket} }; | ||
| 45 | if( -e $conf->{mysqld}->{socket}) { | ||
| 46 | $auto_start = 0; | ||
| 47 | } else { | ||
| 48 | my ($volume, $directory, $file) = | ||
| 49 | splitpath($conf->{mysqld}->{socket}); | ||
| 50 | mkpath $directory; | ||
| 51 | } | ||
| 52 | } | ||
| 53 | |||
| 54 | return Test::mysqld->new( | ||
| 55 | auto_start => $auto_start, | ||
| 56 | base_dir => $base_dir, | ||
| 57 | my_cnf => $my_cnf ); | ||
| 58 | } | ||
| 59 | |||
| 60 | sub _write_start { | ||
| 61 | my $base_dir = (my $self = shift)->test_mysqld->base_dir; | ||
| 62 | mkpath(my $bin = catdir($base_dir, 'bin')); | ||
| 63 | open( my $fh, '>', catfile($bin, 'start')) | ||
| 64 | || $self->log_die( "Cannot open $bin/start: $!" ); | ||
| 65 | |||
| 66 | my $mysqld = $self->test_mysqld->{mysqld}; | ||
| 67 | my $my_cnf = catfile($base_dir, 'etc', 'my.cnf'); | ||
| 68 | print $fh <<START; | ||
| 69 | #!/usr/bin/env sh | ||
| 70 | |||
| 71 | $mysqld --defaults-file=$my_cnf & | ||
| 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_mysqld->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 $PIDFILE = $self->test_mysqld->{my_cnf}->{'pid-file'}; | ||
| 86 | print $fh <<STOP; | ||
| 87 | #!/usr/bin/env sh | ||
| 88 | |||
| 89 | kill \$(cat $PIDFILE) | ||
| 90 | STOP | ||
| 91 | |||
| 92 | close($fh); | ||
| 93 | |||
| 94 | chmod oct("0755"), catfile($bin, 'stop'); | ||
| 95 | } | ||
| 96 | |||
| 97 | sub _write_use { | ||
| 98 | my $base_dir = (my $self = shift)->test_mysqld->base_dir; | ||
| 99 | mkpath(my $bin = catdir($base_dir, 'bin')); | ||
| 100 | open( my $fh, '>', catfile($bin, 'use')) | ||
| 101 | || $self->log_die( "Cannot open $bin/use: $!" ); | ||
| 102 | |||
| 103 | my $mysqld = $self->test_mysqld->{mysqld}; | ||
| 104 | my $SOCKET = $self->test_mysqld->my_cnf->{socket}; | ||
| 105 | $mysqld =~s/d$//; ## ug. sorry :( | ||
| 106 | |||
| 107 | print $fh <<USE; | ||
| 108 | #!/usr/bin/env sh | ||
| 109 | |||
| 110 | $mysqld --socket=$SOCKET -u root test | ||
| 111 | USE | ||
| 112 | |||
| 113 | close($fh); | ||
| 114 | |||
| 115 | chmod oct("0755"), catfile($bin, 'use'); | ||
| 116 | } | ||
| 117 | |||
| 118 | sub make_sandbox { | ||
| 119 | my $self = shift; | ||
| 120 | my $base_dir = $self->_generate_sandbox_dir; | ||
| 121 | |||
| 122 | if( -e catfile($base_dir, 'tmp', 'mysqld.pid')) { | ||
| 123 | return $self->test_mysqld->dsn; | ||
| 124 | } elsif($self->test_mysqld) { | ||
| 125 | $self->_write_start; | ||
| 126 | $self->_write_stop; | ||
| 127 | $self->_write_use; | ||
| 128 | return $self->test_mysqld->dsn; | ||
| 129 | } else { | ||
| 130 | $self->log_die( "can't start a mysql sandbox" ); | ||
| 131 | } | ||
| 132 | } | ||
| 133 | |||
| 134 | __PACKAGE__->meta->make_immutable; | ||
| 135 | |||
| 136 | =head1 NAME | ||
| 137 | |||
| 138 | DBIx::Class::Migration::MySQLSandbox - Autocreate a mysql sandbox | ||
| 139 | |||
| 140 | =head1 SYNOPSIS | ||
| 141 | |||
| 142 | use DBIx::Class::Migration; | ||
| 143 | |||
| 144 | my $migration = DBIx::Class::Migration->new( | ||
| 145 | schema_class=>'Local::Schema', | ||
| 146 | db_sandbox_class=>'DBIx::Class::Migration::MySQLSandbox'), | ||
| 147 | |||
| 148 | $migration->prepare; | ||
| 149 | $migration->install; | ||
| 150 | |||
| 151 | =head1 DESCRIPTION | ||
| 152 | |||
| 153 | This automatically creates a mysql sandbox in your C<target_dir> that you can | ||
| 154 | use for initial prototyping, development and demonstration. If you want to | ||
| 155 | use this, you will need to add L<Test::mysqld> to your C<Makefile.PL> or your | ||
| 156 | C<dist.ini> file, and get that installed properly. It also requires that you | ||
| 157 | have MySQL installed locally (although MySQL does not need to be running, as | ||
| 158 | long as we can find in $PATH the binary installation). If your copy of MySQL | ||
| 159 | is not installed in a normal location, you might need to locally alter $PATH | ||
| 160 | so that we can find it. | ||
| 161 | |||
| 162 | For example, on my Mac, C<mysqld> is located at C</usr/local/mysql/bin/mysqld> | ||
| 163 | so I could do something like this: | ||
| 164 | |||
| 165 | PATH=/usr/local/mysqlbin:$PATH [command] | ||
| 166 | |||
| 167 | and alter the C<$PATH> for one time running, or I could change the C<$PATH> | ||
| 168 | permanently by editing my C<.bashrc> file (typically located in C<$HOME>). | ||
| 169 | |||
| 170 | NOTE: You might find installing L<DBD::mysql> to be easier if you edit the | ||
| 171 | C<$PATH> before trying to install it. | ||
| 172 | |||
| 173 | In addition to the MySQL sandbox, we create three helper scripts C<start>, | ||
| 174 | C<stop> and C<use> which can be used to start, stop and open shell level access | ||
| 175 | to you mysql sandbox. | ||
| 176 | |||
| 177 | These helper scripts will be located in a child directory of your C<target_dir> | ||
| 178 | (which defaults to C<share> under your project root directory). For example: | ||
| 179 | |||
| 180 | [target_dir]/[schema_class]/bin/[start|stop|use] | ||
| 181 | |||
| 182 | If your schema class is C<MyApp::Schema> you should see helper scripts like | ||
| 183 | |||
| 184 | /MyApp-Web | ||
| 185 | /lib | ||
| 186 | /MyApp | ||
| 187 | Schema.pm | ||
| 188 | /Schema | ||
| 189 | ... | ||
| 190 | /share | ||
| 191 | /migrations | ||
| 192 | /fixtures | ||
| 193 | /myapp-schema | ||
| 194 | /bin | ||
| 195 | start | ||
| 196 | stop | ||
| 197 | use | ||
| 198 | |||
| 199 | This give you a system for installing a sandbox locally for development, | ||
| 200 | starting and stopping it for use (for example in a web application like one you | ||
| 201 | might create with L<Catalyst>) and for using it by opening a native C<mysql> | ||
| 202 | shell (such as if you wish to review the database manually, and run native SQL | ||
| 203 | queries). | ||
| 204 | |||
| 205 | =head1 SEE ALSO | ||
| 206 | |||
| 207 | L<DBIx::Class::Migration>, L<DBD::mysql>, L<Test::mysqld>. | ||
| 208 | |||
| 209 | =head1 AUTHOR | ||
| 210 | |||
| 211 | See L<DBIx::Class::Migration> for author information | ||
| 212 | |||
| 213 | =head1 COPYRIGHT & LICENSE | ||
| 214 | |||
| 215 | See L<DBIx::Class::Migration> for copyright and license information | ||
| 216 | |||
| 217 | =cut | ||
| 218 |