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