Merge 'trunk' into 'multiple_version_upgrade'
[dbsrgits/DBIx-Class.git] / t / 94versioning.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5 use Test::More;
6 use Test::Warn;
7 use Test::Exception;
8
9 use Path::Class;
10 use File::Copy;
11
12 #warn "$dsn $user $pass";
13 my ($dsn, $user, $pass);
14
15 BEGIN {
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'
19     unless ($dsn);
20
21   eval { require Time::HiRes }
22     || plan skip_all => 'Test needs Time::HiRes';
23   Time::HiRes->import(qw/time sleep/);
24
25   require DBIx::Class::Storage::DBI;
26   plan skip_all =>
27       'Test needs SQL::Translator ' . DBIx::Class::Storage::DBI->_sqlt_minimum_version
28     if not DBIx::Class::Storage::DBI->_sqlt_version_ok;
29 }
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 my $fn = {
36     v1 => $ddl_dir->file ('DBICVersion-Schema-1.0-MySQL.sql'),
37     v2 => $ddl_dir->file ('DBICVersion-Schema-2.0-MySQL.sql'),
38     v3 => $ddl_dir->file ('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 use lib qw(t/lib);
44 use DBICTest; # do not remove even though it is not used
45
46 use_ok('DBICVersion_v1');
47
48 my $schema_v1 = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 1 });
49 eval { $schema_v1->storage->dbh->do('drop table ' . $version_table_name) };
50 eval { $schema_v1->storage->dbh->do('drop table ' . $old_table_name) };
51
52 is($schema_v1->ddl_filename('MySQL', '1.0', $ddl_dir), $fn->{v1}, 'Filename creation working');
53 unlink( $fn->{v1} ) if ( -e $fn->{v1} );
54 $schema_v1->create_ddl_dir('MySQL', undef, $ddl_dir);
55
56 ok(-f $fn->{v1}, 'Created DDL file');
57 $schema_v1->deploy({ add_drop_table => 1 });
58
59 my $tvrs = $schema_v1->{vschema}->resultset('Table');
60 is($schema_v1->_source_exists($tvrs), 1, 'Created schema from DDL file');
61
62 # loading a new module defining a new version of the same table
63 DBICVersion::Schema->_unregister_source ('Table');
64 eval "use DBICVersion_v2";
65
66 my $schema_v2 = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 1 });
67 {
68   unlink($fn->{v2});
69   unlink($fn->{trans_v12});
70
71   is($schema_v2->get_db_version(), '1.0', 'get_db_version ok');
72   is($schema_v2->schema_version, '2.0', 'schema version ok');
73   $schema_v2->create_ddl_dir('MySQL', '2.0', $ddl_dir, '1.0');
74   ok(-f $fn->{trans_v12}, 'Created DDL file');
75
76   sleep 1;    # remove this when TODO below is completed
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 - $fn->{v2}/,
93       qr/Overwriting existing diff file - $fn->{trans}/,
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 eval "use 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   {
134     my $w;
135     local $SIG{__WARN__} = sub { $w = shift };
136
137     $schema_v3->upgrade();
138     like ($w, qr/Attempting upgrade\.$/, 'Warn before upgrade');
139   }
140
141   is($schema_v3->get_db_version(), '3.0', 'db version number upgraded');
142
143   eval {
144     $schema_v3->storage->dbh->do('select ExtraColumn from TestVersion');
145   };
146   is($@, '', 'new column created');
147 }
148
149 # now put the v1 schema back again
150 {
151   # drop all the tables...
152   eval { $schema_v1->storage->dbh->do('drop table ' . $version_table_name) };
153   eval { $schema_v1->storage->dbh->do('drop table ' . $old_table_name) };
154   eval { $schema_v1->storage->dbh->do('drop table TestVersion') };
155
156   {
157     local $DBICVersion::Schema::VERSION = '1.0';
158     $schema_v1->deploy;
159   }
160   is($schema_v1->get_db_version(), '1.0', 'get_db_version 1.0 ok');
161 }
162
163 # attempt v1 -> v3 upgrade....
164 {
165   {
166     my $w;
167     local $SIG{__WARN__} = sub { $w = shift };
168
169     $schema_v3->upgrade();
170     like ($w, qr/Attempting upgrade\.$/, 'Warn before upgrade');
171   }
172
173   is($schema_v3->get_db_version(), '3.0', 'db version number upgraded');
174 }
175
176 # check behaviour of DBIC_NO_VERSION_CHECK env var and ignore_version connect attr
177 {
178   my $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass);
179   eval {
180     $schema_version->storage->dbh->do("DELETE from $version_table_name");
181   };
182
183
184   warnings_like ( sub {
185     $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass);
186   }, qr/Your DB is currently unversioned/, 'warning detected without env var or attr' );
187
188   warnings_like ( sub {
189     $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 1 });
190   },  [], 'warning not detected with attr set');
191
192
193   local $ENV{DBIC_NO_VERSION_CHECK} = 1;
194   warnings_like ( sub {
195     $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass);
196   }, [], 'warning not detected with env var set');
197
198   warnings_like ( sub {
199     $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 0 });
200   }, qr/Your DB is currently unversioned/, 'warning detected without env var or attr');
201 }
202
203 # attempt a deploy/upgrade cycle within one second
204 TODO: {
205
206   local $TODO = 'To fix this properly the table must be extended with an autoinc column, mst will not accept anything less';
207
208   eval { $schema_v2->storage->dbh->do('drop table ' . $version_table_name) };
209   eval { $schema_v2->storage->dbh->do('drop table ' . $old_table_name) };
210   eval { $schema_v2->storage->dbh->do('drop table TestVersion') };
211
212   # this attempts to sleep until the turn of the second
213   my $t = time();
214   sleep (int ($t) + 1 - $t);
215   diag ('Fast deploy/upgrade start: ', time() );
216
217   {
218     local $DBICVersion::Schema::VERSION = '2.0';
219     $schema_v2->deploy;
220   }
221
222   local $SIG{__WARN__} = sub { warn if $_[0] !~ /Attempting upgrade\.$/ };
223   $schema_v2->upgrade();
224
225   is($schema_v2->get_db_version(), '3.0', 'Fast deploy/upgrade');
226 };
227
228 unless ($ENV{DBICTEST_KEEP_VERSIONING_DDL}) {
229     unlink $_ for (values %$fn);
230 }
231
232 done_testing;