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