1use strict; 2use warnings; 3BEGIN { 4 use Test::Most; 5 plan skip_all => 'DBICM_TEST_MYSQL not set' 6 unless $ENV{DBICM_TEST_MYSQL} || $ENV{AUTHOR_MODE}; 7} 8use lib 't/lib'; 9use DBIx::Class::Migration; 10use File::Spec::Functions 'catfile', 'splitpath'; 11use File::Path 'rmtree'; 12use Test::Requires qw(Test::mysqld); 13use File::Temp 'tempdir'; 14 15my $dir = tempdir(CLEANUP => 1); # can't put under "t" as mysqld refuses 16chmod 0777, $dir; # default is very secure but mysqld cannot get at it 17 18ok( 19 my $migration = DBIx::Class::Migration->new( 20 schema_class=>'Local::Schema', 21 target_dir => $dir, 22 db_sandbox_class=>'DBIx::Class::Migration::MySQLSandbox', 23 ), 24 'created migration with schema_class'); 25 26isa_ok( 27 my $schema = $migration->schema, 'Local::Schema', 28 'got a reasonable looking schema'); 29 30is( 31 DBIx::Class::Migration::_infer_database_from_schema($schema), 32 'MySQL', 33 'can correctly infer a database DBD'); 34 35$migration->prepare; 36 37ok( 38 (my $target_dir = $migration->target_dir), 39 'got a good target directory'); 40 41ok -d catfile($target_dir, 'fixtures'), 'got fixtures'; 42ok -e catfile($target_dir, 'fixtures','1','conf','all_tables.json'), 'got the all_tables.json'; 43ok -d catfile($target_dir, 'migrations'), 'got migrations'; 44ok -e catfile($target_dir, 'migrations','MySQL','deploy','1','001-auto.sql'), 'found DDL'; 45 46open( 47 my $perl_run, 48 ">", 49 catfile($target_dir, 'migrations', 'MySQL', 'deploy', '1', '002-artists.pl') 50) || die "Cannot open: $!"; 51 52print $perl_run <<'END'; 53 54use Test::Most; 55use DBIx::Class::Migration::RunScript; 56 57migrate { 58 my $self = shift; 59 if($self->set_has_fixtures('all_tables')) { 60 lives_ok { $self->populate('all_tables') } "->populate('all_tables')"; 61 } else { 62 $self->schema 63 ->resultset('Country') 64 ->populate([ 65 ['code'], 66 ['bel'], 67 ['deu'], 68 ['fra'], 69 ]); 70 71 lives_ok { $self->dump('all_tables') } "->dump('all_tables')"; 72 ok $self->set_has_fixtures('all_tables'), 73 'Fixture Set exists!'; 74 } 75 76}; 77 78END 79close($perl_run); 80 81$migration->install; 82 83ok $schema->resultset('Country')->find({code=>'fra'}), 84 'got some previously inserted data'; 85 86ok -e catfile($target_dir, 'fixtures','1','all_tables','country','1.fix'), 87 'found a fixture from runscript dump'; 88 89rmtree catfile($target_dir, 'fixtures','1','all_tables'); 90 91$migration->dump_all_sets; 92 93ok -e catfile($target_dir, 'fixtures','1','all_tables','country','1.fix'), 94 'found a fixture'; 95 96rmtree catfile($target_dir, 'fixtures','1','all_tables'); 97 98$migration->dump_named_sets('all_tables'); 99 100ok -e catfile($target_dir, 'fixtures','1','all_tables','country','1.fix'), 101 'found a fixture'; 102 103$migration->delete_table_rows; 104$migration->populate('all_tables'); 105 106ok $schema->resultset('Country')->find({code=>'fra'}), 107 'got some previously inserted data'; 108 109$migration->drop_tables; 110 111$migration = undef; 112 113NEW_SCOPE_FOR_SCHEMA: { 114 115 ok( my $migration = DBIx::Class::Migration->new( 116 schema_class=>'Local::Schema', 117 target_dir => $dir, 118 db_sandbox_class=>'DBIx::Class::Migration::MySQLSandbox'), 119 'created migration with schema_class'); 120 121 $migration->install; 122 123 ok $schema->resultset('Country')->find({code=>'fra'}), 124 'got some previously inserted data'; 125 126 $migration->delete_table_rows; 127 $migration->populate('all_tables'); 128 129 ok $schema->resultset('Country')->find({code=>'bel'}), 130 'got some previously inserted data'; 131 132 SCOPE_FOR_ALREADY_RUNNING: { 133 134 ## The database is still running, lets make sure we can connect 135 ## and use it without generating an error 136 137 SKIP: { 138 skip "Test::mysqld not patched yet", 3 139 unless (eval qq{use Test::mysqld 0.15; 1} || 0); 140 141 ok( my $migration = DBIx::Class::Migration->new( 142 schema_class=>'Local::Schema', 143 target_dir => $dir, 144 db_sandbox_class=>'DBIx::Class::Migration::MySQLSandbox'), 145 'created migration with schema_class 3'); 146 147 isa_ok( 148 my $schema = $migration->schema, 'Local::Schema', 149 'got a reasonable looking schema'); 150 151 ok $schema->resultset('Country')->find({code=>'fra'}), 152 'got some previously inserted data'; 153 } 154 } 155 156 my ($volume,$directories,$file) = 157 splitpath($migration->db_sandbox->test_mysqld->my_cnf->{socket}); 158 159 rmtree $directories 160 if ($directories && $file); ## check to make sure we don't rmtree root 161 162 $migration = undef; 163 164 RESTORE_EXISTING_NOT_RUNNING: { 165 166 ## The sandbox exists, but the db is NOT running and the 167 ## socket path is gone; 168 169 SKIP: { 170 skip "Test::mysqld not patched yet", 3 171 unless (eval qq{use Test::mysqld 0.15; 1} || 0); 172 173 ok( my $migration = DBIx::Class::Migration->new( 174 schema_class=>'Local::Schema', 175 target_dir => $dir, 176 db_sandbox_class=>'DBIx::Class::Migration::MySQLSandbox'), 177 'created migration with schema_class 4'); 178 179 isa_ok( 180 my $schema = $migration->schema, 'Local::Schema', 181 'got a reasonable looking schema'); 182 183 ok $schema->resultset('Country')->find({code=>'fra'}), 184 'got some previously inserted data'; 185 } 186 } 187 188} 189 190SCOPE_FOR_PARALLEL_TEMP: { 191 192 ok( my $migration1 = DBIx::Class::Migration->new( 193 schema_class=>'Local::Schema', 194 target_dir => $dir, 195 db_sandbox_builder_class => 'DBIx::Class::Migration::TempDirSandboxBuilder', 196 db_sandbox_class=>'DBIx::Class::Migration::MySQLSandbox'), 197 'created migration with schema_class in temp 1'); 198 199 $migration1->install; 200 201 ok( my $migration2 = DBIx::Class::Migration->new( 202 schema_class=>'Local::Schema', 203 target_dir => $dir, 204 db_sandbox_builder_class => 'DBIx::Class::Migration::TempDirSandboxBuilder', 205 db_sandbox_class=>'DBIx::Class::Migration::MySQLSandbox'), 206 'created migration with schema_class in temp 2'); 207 208 $migration2->install; 209 210 ok( my $migration3 = DBIx::Class::Migration->new( 211 schema_class=>'Local::Schema', 212 target_dir => $dir, 213 db_sandbox_builder_class => 'DBIx::Class::Migration::TempDirSandboxBuilder', 214 db_sandbox_class=>'DBIx::Class::Migration::MySQLSandbox'), 215 'created migration with schema_class in temp 3'); 216 217 $migration3->install; 218} 219 220done_testing; 221