1#!/usr/bin/perl 2 3use strict; 4use warnings; 5use Test::More; 6use Test::Warn; 7use Test::Exception; 8 9use Path::Class; 10use File::Copy; 11 12#warn "$dsn $user $pass"; 13my ($dsn, $user, $pass); 14 15BEGIN { 16 ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MYSQL_${_}" } qw/DSN USER PASS/}; 17 18 plan skip_all => 'Set $ENV{DBICTEST_MYSQL_DSN}, _USER and _PASS to run this test' 19 unless ($dsn); 20 21 eval { require Time::HiRes } 22 || plan skip_all => 'Test needs Time::HiRes'; 23 Time::HiRes->import(qw/time sleep/); 24 25 require DBIx::Class; 26 plan skip_all => 27 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('deploy') 28 unless DBIx::Class::Optional::Dependencies->req_ok_for ('deploy') 29} 30 31use lib qw(t/lib); 32use DBICTest; # do not remove even though it is not used 33 34use_ok('DBICVersion_v1'); 35 36my $version_table_name = 'dbix_class_schema_versions'; 37my $old_table_name = 'SchemaVersions'; 38 39my $ddl_dir = dir ('t', 'var'); 40mkdir ($ddl_dir) unless -d $ddl_dir; 41 42my $fn = { 43 v1 => $ddl_dir->file ('DBICVersion-Schema-1.0-MySQL.sql'), 44 v2 => $ddl_dir->file ('DBICVersion-Schema-2.0-MySQL.sql'), 45 v3 => $ddl_dir->file ('DBICVersion-Schema-3.0-MySQL.sql'), 46 trans_v12 => $ddl_dir->file ('DBICVersion-Schema-1.0-2.0-MySQL.sql'), 47 trans_v23 => $ddl_dir->file ('DBICVersion-Schema-2.0-3.0-MySQL.sql'), 48}; 49 50my $schema_v1 = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 1 }); 51eval { $schema_v1->storage->dbh->do('drop table ' . $version_table_name) }; 52eval { $schema_v1->storage->dbh->do('drop table ' . $old_table_name) }; 53 54is($schema_v1->ddl_filename('MySQL', '1.0', $ddl_dir), $fn->{v1}, 'Filename creation working'); 55unlink( $fn->{v1} ) if ( -e $fn->{v1} ); 56$schema_v1->create_ddl_dir('MySQL', undef, $ddl_dir); 57 58ok(-f $fn->{v1}, 'Created DDL file'); 59$schema_v1->deploy({ add_drop_table => 1 }); 60 61my $tvrs = $schema_v1->{vschema}->resultset('Table'); 62is($schema_v1->_source_exists($tvrs), 1, 'Created schema from DDL file'); 63 64# loading a new module defining a new version of the same table 65DBICVersion::Schema->_unregister_source ('Table'); 66use_ok('DBICVersion_v2'); 67 68my $schema_v2 = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 1 }); 69{ 70 unlink($fn->{v2}); 71 unlink($fn->{trans_v12}); 72 73 is($schema_v2->get_db_version(), '1.0', 'get_db_version ok'); 74 is($schema_v2->schema_version, '2.0', 'schema version ok'); 75 $schema_v2->create_ddl_dir('MySQL', '2.0', $ddl_dir, '1.0'); 76 ok(-f $fn->{trans_v12}, 'Created DDL file'); 77 78 warnings_like ( 79 sub { $schema_v2->upgrade() }, 80 qr/DB version .+? is lower than the schema version/, 81 'Warn before upgrade', 82 ); 83 84 is($schema_v2->get_db_version(), '2.0', 'db version number upgraded'); 85 86 lives_ok ( sub { 87 $schema_v2->storage->dbh->do('select NewVersionName from TestVersion'); 88 }, 'new column created' ); 89 90 warnings_exist ( 91 sub { $schema_v2->create_ddl_dir('MySQL', '2.0', $ddl_dir, '1.0') }, 92 [ 93 qr/Overwriting existing DDL file - $fn->{v2}/, 94 qr/Overwriting existing diff file - $fn->{trans_v12}/, 95 ], 96 'An overwrite warning generated for both the DDL and the diff', 97 ); 98} 99 100{ 101 my $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass); 102 lives_ok (sub { 103 $schema_version->storage->dbh->do('select * from ' . $version_table_name); 104 }, 'version table exists'); 105 106 lives_ok (sub { 107 $schema_version->storage->dbh->do("DROP TABLE IF EXISTS $old_table_name"); 108 $schema_version->storage->dbh->do("RENAME TABLE $version_table_name TO $old_table_name"); 109 }, 'versions table renamed to old style table'); 110 111 $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass); 112 is($schema_version->get_db_version, '2.0', 'transition from old table name to new okay'); 113 114 dies_ok (sub { 115 $schema_version->storage->dbh->do('select * from ' . $old_table_name); 116 }, 'old version table gone'); 117 118} 119 120# repeat the v1->v2 process for v2->v3 before testing v1->v3 121DBICVersion::Schema->_unregister_source ('Table'); 122use_ok('DBICVersion_v3'); 123 124my $schema_v3 = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 1 }); 125{ 126 unlink($fn->{v3}); 127 unlink($fn->{trans_v23}); 128 129 is($schema_v3->get_db_version(), '2.0', 'get_db_version 2.0 ok'); 130 is($schema_v3->schema_version, '3.0', 'schema version 3.0 ok'); 131 $schema_v3->create_ddl_dir('MySQL', '3.0', $ddl_dir, '2.0'); 132 ok(-f $fn->{trans_v23}, 'Created DDL 2.0 -> 3.0 file'); 133 134 warnings_exist ( 135 sub { $schema_v3->upgrade() }, 136 qr/DB version .+? is lower than the schema version/, 137 'Warn before upgrade', 138 ); 139 140 is($schema_v3->get_db_version(), '3.0', 'db version number upgraded'); 141 142 lives_ok ( sub { 143 $schema_v3->storage->dbh->do('select ExtraColumn from TestVersion'); 144 }, 'new column created'); 145} 146 147# now put the v1 schema back again 148{ 149 # drop all the tables... 150 eval { $schema_v1->storage->dbh->do('drop table ' . $version_table_name) }; 151 eval { $schema_v1->storage->dbh->do('drop table ' . $old_table_name) }; 152 eval { $schema_v1->storage->dbh->do('drop table TestVersion') }; 153 154 { 155 local $DBICVersion::Schema::VERSION = '1.0'; 156 $schema_v1->deploy; 157 } 158 is($schema_v1->get_db_version(), '1.0', 'get_db_version 1.0 ok'); 159} 160 161# attempt v1 -> v3 upgrade 162{ 163 local $SIG{__WARN__} = sub { warn if $_[0] !~ /Attempting upgrade\.$/ }; 164 $schema_v3->upgrade(); 165 is($schema_v3->get_db_version(), '3.0', 'db version number upgraded'); 166} 167 168# check behaviour of DBIC_NO_VERSION_CHECK env var and ignore_version connect attr 169{ 170 my $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass); 171 eval { 172 $schema_version->storage->dbh->do("DELETE from $version_table_name"); 173 }; 174 175 176 warnings_like ( sub { 177 $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass); 178 }, qr/Your DB is currently unversioned/, 'warning detected without env var or attr' ); 179 180 warnings_like ( sub { 181 $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 1 }); 182 }, [], 'warning not detected with attr set'); 183 184 185 local $ENV{DBIC_NO_VERSION_CHECK} = 1; 186 warnings_like ( sub { 187 $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass); 188 }, [], 'warning not detected with env var set'); 189 190 warnings_like ( sub { 191 $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 0 }); 192 }, qr/Your DB is currently unversioned/, 'warning detected without env var or attr'); 193} 194 195# attempt a deploy/upgrade cycle within one second 196{ 197 eval { $schema_v2->storage->dbh->do('drop table ' . $version_table_name) }; 198 eval { $schema_v2->storage->dbh->do('drop table ' . $old_table_name) }; 199 eval { $schema_v2->storage->dbh->do('drop table TestVersion') }; 200 201 # this attempts to sleep until the turn of the second 202 my $t = time(); 203 sleep (int ($t) + 1 - $t); 204 note ('Fast deploy/upgrade start: ', time() ); 205 206 { 207 local $DBICVersion::Schema::VERSION = '2.0'; 208 $schema_v2->deploy; 209 } 210 211 local $SIG{__WARN__} = sub { warn if $_[0] !~ /Attempting upgrade\.$/ }; 212 $schema_v2->upgrade(); 213 214 is($schema_v2->get_db_version(), '3.0', 'Fast deploy/upgrade'); 215}; 216 217unless ($ENV{DBICTEST_KEEP_VERSIONING_DDL}) { 218 unlink $_ for (values %$fn); 219} 220 221done_testing; 222