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