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