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