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