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