1use strict; 2use warnings; 3use Test::More; 4use Test::Git; 5use Test::Requires::Git; 6 7use File::Spec; 8use File::Path 'make_path'; 9use DBI; 10use Time::HiRes; 11use Test::Requires 'Test::mysqld'; 12 13test_requires_git; 14 15use_ok 'GitDDL::Migrator'; 16 17my $repo = test_repository;#( temp => [ CLEANUP => 0 ]); 18my $mysqld = Test::mysqld->new(my_cnf => {'skip-networking' => ''}) or plan skip_all => $Test::mysqld::errstr; 19 20my $gd = GitDDL::Migrator->new( 21 work_tree => $repo->work_tree, 22 ddl_file => File::Spec->catfile('sql', 'ddl.sql'), 23 dsn => [$mysqld->dsn], 24); 25 26my $first_sql = <<__SQL__; 27CREATE TABLE first ( 28 id INTEGER NOT NULL, 29 name VARCHAR(191) DEFAULT NULL 30) ENGINE=InnoDB DEFAULT CHARACTER SET utf8; 31__SQL__ 32 33make_path(File::Spec->catfile($repo->work_tree, 'sql')); 34 35open my $fh, '>', File::Spec->catfile($repo->work_tree, 'sql', 'ddl.sql') or die $!; 36print $fh $first_sql; 37close $fh; 38 39$repo->run('add', File::Spec->catfile('sql', 'ddl.sql')); 40$repo->run('commit', '--author', 'Daisuke Murase <typester@cpan.org>', 41 '-m', 'initial commit'); 42 43ok !$gd->database_version; 44 45$gd->deploy; 46 47$gd->_dbh->do('INSERT INTO first (id, name) VALUES (1, "test")') 48 or die $gd->_dbh->errstr; 49 50my $first_version = $gd->database_version; 51ok $first_version, 'first version ok'; 52 53is $gd->ddl_version, $first_version, 'ddl_version == database_version ok'; 54ok $gd->check_version, 'check_version ok'; 55 56my $second_sql = <<__SQL__; 57CREATE TABLE second ( 58 id INTEGER NOT NULL, 59 name VARCHAR(191) NOT NULL 60) ENGINE=InnoDB DEFAULT CHARACTER SET utf8; 61__SQL__ 62 63open $fh, '>>', File::Spec->catfile($repo->work_tree, 'sql', 'ddl.sql') or die $!; 64print $fh $second_sql; 65close $fh; 66 67$repo->run('add', File::Spec->catfile('sql', 'ddl.sql')); 68$repo->run('commit', '--author', 'Daisuke Murase <typester@cpan.org>', 69 '-m', 'initial commit'); 70 71ok !$gd->check_version, 'check_version not ok ok'; 72 73like $gd->diff, qr/CREATE TABLE `second`/, 'diff looks ok'; 74 75eval { 76 $gd->rollback_diff; 77}; 78like $@, qr/No rollback/; 79 80sleep 0.001; 81$gd->upgrade_database; 82like $gd->rollback_diff, qr/DROP TABLE.*second.*/; 83 84$gd->_dbh->do('INSERT INTO second (id, name) VALUES (1, "test")') 85 or die $gd->_dbh->errstr; 86 87ok $gd->check_version, 'check_version ok again'; 88 89$gd->check_ddl_mismatch; 90pass 'no mismatch'; 91 92$gd->_dbh->do('CREATE TABLE third ( 93 id INTEGER NOT NULL, 94 name VARCHAR(191) NOT NULL 95) ENGINE=InnoDB DEFAULT CHARACTER SET utf8;'); 96 97 98$gd = GitDDL::Migrator->new( 99 work_tree => $repo->work_tree, 100 ddl_file => File::Spec->catfile('sql', 'ddl.sql'), 101 dsn => [$mysqld->dsn], 102); 103eval { 104 $gd->check_ddl_mismatch; 105}; 106like $@, qr/^Mismatch between ddl version and real database is found/; 107like $@, qr/CREATE TABLE.*third/; 108like $gd->diff_from_real_database, qr/DROP TABLE.*third/; 109 110done_testing; 111