Add import-time action stub to OptDeps, switch distbuild checks to it
[dbsrgits/DBIx-Class.git] / t / 94versioning.t
CommitLineData
c9d2e0a2 1use strict;
2use warnings;
68de9438 3
c9d2e0a2 4use Test::More;
d7a58a29 5use Test::Warn;
6use Test::Exception;
7
4bea1fe7 8use Path::Class;
9use File::Copy;
10use Time::HiRes qw/time sleep/;
11
12use lib qw(t/lib);
8d6b1478 13use DBICTest;
052a832c 14use DBIx::Class::_Util 'sigwarn_silencer';
4bea1fe7 15
1d48fcff 16my ($dsn, $user, $pass);
17
c9d2e0a2 18BEGIN {
1d48fcff 19 ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MYSQL_${_}" } qw/DSN USER PASS/};
20
21 plan skip_all => 'Set $ENV{DBICTEST_MYSQL_DSN}, _USER and _PASS to run this test'
8424c090 22 unless ($dsn);
1d48fcff 23
2527233b 24 require DBIx::Class;
7f6f5b69 25 plan skip_all =>
2527233b 26 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('deploy')
49b3a264 27 unless DBIx::Class::Optional::Dependencies->req_ok_for ('deploy');
28
29 plan skip_all =>
30 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('test_rdbms_mysql')
31 unless DBIx::Class::Optional::Dependencies->req_ok_for ('test_rdbms_mysql');
c9d2e0a2 32}
33
8d6b1478 34# this is just to grab a lock
35{
36 my $s = DBICTest::Schema->connect($dsn, $user, $pass);
37}
38
eed5492f 39# in case it came from the env
40$ENV{DBIC_NO_VERSION_CHECK} = 0;
41
b9ae34d3 42use_ok('DBICVersion_v1');
b9ae34d3 43
b4b1e91c 44my $version_table_name = 'dbix_class_schema_versions';
45my $old_table_name = 'SchemaVersions';
46
8d6b1478 47my $ddl_dir = dir(qw/t var/, "versioning_ddl-$$");
48$ddl_dir->mkpath unless -d $ddl_dir;
b9ae34d3 49
1475105d 50my $fn = {
d7a58a29 51 v1 => $ddl_dir->file ('DBICVersion-Schema-1.0-MySQL.sql'),
52 v2 => $ddl_dir->file ('DBICVersion-Schema-2.0-MySQL.sql'),
d40a22fc 53 v3 => $ddl_dir->file ('DBICVersion-Schema-3.0-MySQL.sql'),
4f591da2 54 trans_v12 => $ddl_dir->file ('DBICVersion-Schema-1.0-2.0-MySQL.sql'),
55 trans_v23 => $ddl_dir->file ('DBICVersion-Schema-2.0-3.0-MySQL.sql'),
1475105d 56};
57
d2bc7045 58my $schema_v1 = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 1 });
59eval { $schema_v1->storage->dbh->do('drop table ' . $version_table_name) };
60eval { $schema_v1->storage->dbh->do('drop table ' . $old_table_name) };
c9d2e0a2 61
d2bc7045 62is($schema_v1->ddl_filename('MySQL', '1.0', $ddl_dir), $fn->{v1}, 'Filename creation working');
1475105d 63unlink( $fn->{v1} ) if ( -e $fn->{v1} );
d2bc7045 64$schema_v1->create_ddl_dir('MySQL', undef, $ddl_dir);
c9d2e0a2 65
1475105d 66ok(-f $fn->{v1}, 'Created DDL file');
d2bc7045 67$schema_v1->deploy({ add_drop_table => 1 });
c9d2e0a2 68
d2bc7045 69my $tvrs = $schema_v1->{vschema}->resultset('Table');
70is($schema_v1->_source_exists($tvrs), 1, 'Created schema from DDL file');
c9d2e0a2 71
1475105d 72# loading a new module defining a new version of the same table
73DBICVersion::Schema->_unregister_source ('Table');
7eb9a6f1 74use_ok('DBICVersion_v2');
1475105d 75
d2bc7045 76my $schema_v2 = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 1 });
f925f7cb 77{
1475105d 78 unlink($fn->{v2});
d2bc7045 79 unlink($fn->{trans_v12});
f925f7cb 80
d2bc7045 81 is($schema_v2->get_db_version(), '1.0', 'get_db_version ok');
82 is($schema_v2->schema_version, '2.0', 'schema version ok');
83 $schema_v2->create_ddl_dir('MySQL', '2.0', $ddl_dir, '1.0');
84 ok(-f $fn->{trans_v12}, 'Created DDL file');
1475105d 85
d7a58a29 86 warnings_like (
d40a22fc 87 sub { $schema_v2->upgrade() },
d7a58a29 88 qr/DB version .+? is lower than the schema version/,
89 'Warn before upgrade',
90 );
1475105d 91
d2bc7045 92 is($schema_v2->get_db_version(), '2.0', 'db version number upgraded');
f925f7cb 93
d7a58a29 94 lives_ok ( sub {
d2bc7045 95 $schema_v2->storage->dbh->do('select NewVersionName from TestVersion');
d7a58a29 96 }, 'new column created' );
97
98 warnings_exist (
d40a22fc 99 sub { $schema_v2->create_ddl_dir('MySQL', '2.0', $ddl_dir, '1.0') },
d7a58a29 100 [
475713af 101 qr/Overwriting existing DDL file - \Q$fn->{v2}\E/,
102 qr/Overwriting existing diff file - \Q$fn->{trans_v12}\E/,
d7a58a29 103 ],
104 'An overwrite warning generated for both the DDL and the diff',
105 );
f925f7cb 106}
b4b1e91c 107
108{
109 my $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass);
d7a58a29 110 lives_ok (sub {
b4b1e91c 111 $schema_version->storage->dbh->do('select * from ' . $version_table_name);
d7a58a29 112 }, 'version table exists');
b4b1e91c 113
d7a58a29 114 lives_ok (sub {
b4b1e91c 115 $schema_version->storage->dbh->do("DROP TABLE IF EXISTS $old_table_name");
86456031 116 $schema_version->storage->dbh->do("RENAME TABLE $version_table_name TO $old_table_name");
d7a58a29 117 }, 'versions table renamed to old style table');
b4b1e91c 118
119 $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass);
120 is($schema_version->get_db_version, '2.0', 'transition from old table name to new okay');
121
d7a58a29 122 dies_ok (sub {
b4b1e91c 123 $schema_version->storage->dbh->do('select * from ' . $old_table_name);
d7a58a29 124 }, 'old version table gone');
b4b1e91c 125
126}
f81b9157 127
d2bc7045 128# repeat the v1->v2 process for v2->v3 before testing v1->v3
129DBICVersion::Schema->_unregister_source ('Table');
7eb9a6f1 130use_ok('DBICVersion_v3');
d2bc7045 131
132my $schema_v3 = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 1 });
133{
134 unlink($fn->{v3});
135 unlink($fn->{trans_v23});
136
137 is($schema_v3->get_db_version(), '2.0', 'get_db_version 2.0 ok');
138 is($schema_v3->schema_version, '3.0', 'schema version 3.0 ok');
139 $schema_v3->create_ddl_dir('MySQL', '3.0', $ddl_dir, '2.0');
140 ok(-f $fn->{trans_v23}, 'Created DDL 2.0 -> 3.0 file');
141
b9ae34d3 142 warnings_exist (
143 sub { $schema_v3->upgrade() },
144 qr/DB version .+? is lower than the schema version/,
145 'Warn before upgrade',
146 );
d2bc7045 147
148 is($schema_v3->get_db_version(), '3.0', 'db version number upgraded');
149
b9ae34d3 150 lives_ok ( sub {
d2bc7045 151 $schema_v3->storage->dbh->do('select ExtraColumn from TestVersion');
b9ae34d3 152 }, 'new column created');
d2bc7045 153}
154
155# now put the v1 schema back again
156{
157 # drop all the tables...
158 eval { $schema_v1->storage->dbh->do('drop table ' . $version_table_name) };
159 eval { $schema_v1->storage->dbh->do('drop table ' . $old_table_name) };
160 eval { $schema_v1->storage->dbh->do('drop table TestVersion') };
161
162 {
163 local $DBICVersion::Schema::VERSION = '1.0';
164 $schema_v1->deploy;
165 }
166 is($schema_v1->get_db_version(), '1.0', 'get_db_version 1.0 ok');
167}
168
b9ae34d3 169# attempt v1 -> v3 upgrade
d2bc7045 170{
052a832c 171 local $SIG{__WARN__} = sigwarn_silencer( qr/Attempting upgrade\.$/ );
7eb9a6f1 172 $schema_v3->upgrade();
d2bc7045 173 is($schema_v3->get_db_version(), '3.0', 'db version number upgraded');
174}
175
b703fec7 176# Now, try a v1 -> v3 upgrade with a file that has comments strategically placed in it.
177# First put the v1 schema back again...
178{
179 # drop all the tables...
180 eval { $schema_v1->storage->dbh->do('drop table ' . $version_table_name) };
181 eval { $schema_v1->storage->dbh->do('drop table ' . $old_table_name) };
182 eval { $schema_v1->storage->dbh->do('drop table TestVersion') };
183
184 {
185 local $DBICVersion::Schema::VERSION = '1.0';
186 $schema_v1->deploy;
187 }
188 is($schema_v1->get_db_version(), '1.0', 'get_db_version 1.0 ok');
189}
190
191# add a "harmless" comment before one of the statements.
f3ec358e 192{
193 my ($perl) = $^X =~ /(.+)/;
194 local $ENV{PATH};
195 system( qq($perl -pi.bak -e "s/ALTER/-- this is a comment\nALTER/" $fn->{trans_v23}) );
196}
b703fec7 197
198# Then attempt v1 -> v3 upgrade
199{
052a832c 200 local $SIG{__WARN__} = sigwarn_silencer( qr/Attempting upgrade\.$/ );
b703fec7 201 $schema_v3->upgrade();
202 is($schema_v3->get_db_version(), '3.0', 'db version number upgraded to 3.0');
203
204 # make sure that the column added after the comment is actually added.
205 lives_ok ( sub {
206 $schema_v3->storage->dbh->do('select ExtraColumn from TestVersion');
207 }, 'new column created');
208}
209
210
f81b9157 211# check behaviour of DBIC_NO_VERSION_CHECK env var and ignore_version connect attr
212{
213 my $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass);
214 eval {
d0dfedc2 215 $schema_version->storage->dbh->do("DELETE from $version_table_name");
f81b9157 216 };
217
d0dfedc2 218
d7a58a29 219 warnings_like ( sub {
220 $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass);
221 }, qr/Your DB is currently unversioned/, 'warning detected without env var or attr' );
f81b9157 222
d7a58a29 223 warnings_like ( sub {
224 $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 1 });
225 }, [], 'warning not detected with attr set');
d0dfedc2 226
f81b9157 227
1475105d 228 local $ENV{DBIC_NO_VERSION_CHECK} = 1;
d7a58a29 229 warnings_like ( sub {
230 $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass);
231 }, [], 'warning not detected with env var set');
f81b9157 232
d7a58a29 233 warnings_like ( sub {
234 $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 0 });
235 }, qr/Your DB is currently unversioned/, 'warning detected without env var or attr');
f81b9157 236}
1475105d 237
238# attempt a deploy/upgrade cycle within one second
7eb9a6f1 239{
d2bc7045 240 eval { $schema_v2->storage->dbh->do('drop table ' . $version_table_name) };
241 eval { $schema_v2->storage->dbh->do('drop table ' . $old_table_name) };
242 eval { $schema_v2->storage->dbh->do('drop table TestVersion') };
1475105d 243
244 # this attempts to sleep until the turn of the second
245 my $t = time();
246 sleep (int ($t) + 1 - $t);
7eb9a6f1 247 note ('Fast deploy/upgrade start: ', time() );
1475105d 248
249 {
d2bc7045 250 local $DBICVersion::Schema::VERSION = '2.0';
251 $schema_v2->deploy;
1475105d 252 }
1475105d 253
052a832c 254 local $SIG{__WARN__} = sigwarn_silencer( qr/Attempting upgrade\.$/ );
255
d2bc7045 256 $schema_v2->upgrade();
1475105d 257
d2bc7045 258 is($schema_v2->get_db_version(), '3.0', 'Fast deploy/upgrade');
e9fbbbf4 259};
1475105d 260
8012b15c 261# Check that it Schema::Versioned deals with new/all forms of connect arguments.
262{
263 my $get_db_version_run = 0;
264
265 no warnings qw/once redefine/;
266 local *DBIx::Class::Schema::Versioned::get_db_version = sub {
267 $get_db_version_run = 1;
268 return $_[0]->schema_version;
269 };
270
271 # Make sure the env var isn't whats triggering it
272 local $ENV{DBIC_NO_VERSION_CHECK} = 0;
273
274 DBICVersion::Schema->connect({
275 dsn => $dsn,
8273e845 276 user => $user,
8012b15c 277 pass => $pass,
278 ignore_version => 1
279 });
8273e845 280
8012b15c 281 ok($get_db_version_run == 0, "attributes pulled from hashref connect_info");
282 $get_db_version_run = 0;
283
284 DBICVersion::Schema->connect( $dsn, $user, $pass, { ignore_version => 1 } );
285 ok($get_db_version_run == 0, "attributes pulled from list connect_info");
286}
287
81023d83 288# at this point we have v1, v2 and v3 still connected
289# make sure they are the only connections and everything else is gone
290is
291 scalar( grep
292 { defined $_ and $_->{Active} }
293 map
294 { @{$_->{ChildHandles}} }
295 values %{ { DBI->installed_drivers } }
296 ), 3, "Expected number of connections at end of script"
297;
298
8d6b1478 299END {
300 unless ($ENV{DBICTEST_KEEP_VERSIONING_DDL}) {
301 $ddl_dir->rmtree;
302 }
1475105d 303}
7f6f5b69 304
305done_testing;