Cleanup shebang lines of all maint/example scripts, remove from tests entirely
[dbsrgits/DBIx-Class.git] / t / 94versioning.t
1 use strict;
2 use warnings;
3 use Test::More;
4 use Test::Warn;
5 use Test::Exception;
6
7 use Path::Class;
8 use File::Copy;
9
10 #warn "$dsn $user $pass";
11 my ($dsn, $user, $pass);
12
13 BEGIN {
14   ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MYSQL_${_}" } qw/DSN USER PASS/};
15
16   plan skip_all => 'Set $ENV{DBICTEST_MYSQL_DSN}, _USER and _PASS to run this test'
17     unless ($dsn);
18
19   eval { require Time::HiRes }
20     || plan skip_all => 'Test needs Time::HiRes';
21   Time::HiRes->import(qw/time sleep/);
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
29 use lib qw(t/lib);
30 use DBICTest; # do not remove even though it is not used
31
32 use_ok('DBICVersion_v1');
33
34 my $version_table_name = 'dbix_class_schema_versions';
35 my $old_table_name = 'SchemaVersions';
36
37 my $ddl_dir = dir ('t', 'var');
38 mkdir ($ddl_dir) unless -d $ddl_dir;
39
40 my $fn = {
41     v1 => $ddl_dir->file ('DBICVersion-Schema-1.0-MySQL.sql'),
42     v2 => $ddl_dir->file ('DBICVersion-Schema-2.0-MySQL.sql'),
43     v3 => $ddl_dir->file ('DBICVersion-Schema-3.0-MySQL.sql'),
44     trans_v12 => $ddl_dir->file ('DBICVersion-Schema-1.0-2.0-MySQL.sql'),
45     trans_v23 => $ddl_dir->file ('DBICVersion-Schema-2.0-3.0-MySQL.sql'),
46 };
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 use_ok('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   warnings_like (
77     sub { $schema_v2->upgrade() },
78     qr/DB version .+? is lower than the schema version/,
79     'Warn before upgrade',
80   );
81
82   is($schema_v2->get_db_version(), '2.0', 'db version number upgraded');
83
84   lives_ok ( sub {
85     $schema_v2->storage->dbh->do('select NewVersionName from TestVersion');
86   }, 'new column created' );
87
88   warnings_exist (
89     sub { $schema_v2->create_ddl_dir('MySQL', '2.0', $ddl_dir, '1.0') },
90     [
91       qr/Overwriting existing DDL file - $fn->{v2}/,
92       qr/Overwriting existing diff file - $fn->{trans_v12}/,
93     ],
94     'An overwrite warning generated for both the DDL and the diff',
95   );
96 }
97
98 {
99   my $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass);
100   lives_ok (sub {
101     $schema_version->storage->dbh->do('select * from ' . $version_table_name);
102   }, 'version table exists');
103
104   lives_ok (sub {
105     $schema_version->storage->dbh->do("DROP TABLE IF EXISTS $old_table_name");
106     $schema_version->storage->dbh->do("RENAME TABLE $version_table_name TO $old_table_name");
107   }, 'versions table renamed to old style table');
108
109   $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass);
110   is($schema_version->get_db_version, '2.0', 'transition from old table name to new okay');
111
112   dies_ok (sub {
113     $schema_version->storage->dbh->do('select * from ' . $old_table_name);
114   }, 'old version table gone');
115
116 }
117
118 # repeat the v1->v2 process for v2->v3 before testing v1->v3
119 DBICVersion::Schema->_unregister_source ('Table');
120 use_ok('DBICVersion_v3');
121
122 my $schema_v3 = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 1 });
123 {
124   unlink($fn->{v3});
125   unlink($fn->{trans_v23});
126
127   is($schema_v3->get_db_version(), '2.0', 'get_db_version 2.0 ok');
128   is($schema_v3->schema_version, '3.0', 'schema version 3.0 ok');
129   $schema_v3->create_ddl_dir('MySQL', '3.0', $ddl_dir, '2.0');
130   ok(-f $fn->{trans_v23}, 'Created DDL 2.0 -> 3.0 file');
131
132   warnings_exist (
133     sub { $schema_v3->upgrade() },
134     qr/DB version .+? is lower than the schema version/,
135     'Warn before upgrade',
136   );
137
138   is($schema_v3->get_db_version(), '3.0', 'db version number upgraded');
139
140   lives_ok ( sub {
141     $schema_v3->storage->dbh->do('select ExtraColumn from TestVersion');
142   }, 'new column created');
143 }
144
145 # now put the v1 schema back again
146 {
147   # drop all the tables...
148   eval { $schema_v1->storage->dbh->do('drop table ' . $version_table_name) };
149   eval { $schema_v1->storage->dbh->do('drop table ' . $old_table_name) };
150   eval { $schema_v1->storage->dbh->do('drop table TestVersion') };
151
152   {
153     local $DBICVersion::Schema::VERSION = '1.0';
154     $schema_v1->deploy;
155   }
156   is($schema_v1->get_db_version(), '1.0', 'get_db_version 1.0 ok');
157 }
158
159 # attempt v1 -> v3 upgrade
160 {
161   local $SIG{__WARN__} = sub { warn if $_[0] !~ /Attempting upgrade\.$/ };
162   $schema_v3->upgrade();
163   is($schema_v3->get_db_version(), '3.0', 'db version number upgraded');
164 }
165
166 # Now, try a v1 -> v3 upgrade with a file that has comments strategically placed in it.
167 # First put the v1 schema back again...
168 {
169   # drop all the tables...
170   eval { $schema_v1->storage->dbh->do('drop table ' . $version_table_name) };
171   eval { $schema_v1->storage->dbh->do('drop table ' . $old_table_name) };
172   eval { $schema_v1->storage->dbh->do('drop table TestVersion') };
173
174   {
175     local $DBICVersion::Schema::VERSION = '1.0';
176     $schema_v1->deploy;
177   }
178   is($schema_v1->get_db_version(), '1.0', 'get_db_version 1.0 ok');
179 }
180
181 # add a "harmless" comment before one of the statements.
182 system( qq($^X -pi -e "s/ALTER/-- this is a comment\nALTER/" $fn->{trans_v23};) );
183
184 # Then attempt v1 -> v3 upgrade
185 {
186   local $SIG{__WARN__} = sub { warn if $_[0] !~ /Attempting upgrade\.$/ };
187   $schema_v3->upgrade();
188   is($schema_v3->get_db_version(), '3.0', 'db version number upgraded to 3.0');
189
190   # make sure that the column added after the comment is actually added.
191   lives_ok ( sub {
192     $schema_v3->storage->dbh->do('select ExtraColumn from TestVersion');
193   }, 'new column created');
194 }
195
196
197 # check behaviour of DBIC_NO_VERSION_CHECK env var and ignore_version connect attr
198 {
199   my $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass);
200   eval {
201     $schema_version->storage->dbh->do("DELETE from $version_table_name");
202   };
203
204
205   warnings_like ( sub {
206     $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass);
207   }, qr/Your DB is currently unversioned/, 'warning detected without env var or attr' );
208
209   warnings_like ( sub {
210     $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 1 });
211   },  [], 'warning not detected with attr set');
212
213
214   local $ENV{DBIC_NO_VERSION_CHECK} = 1;
215   warnings_like ( sub {
216     $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass);
217   }, [], 'warning not detected with env var set');
218
219   warnings_like ( sub {
220     $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 0 });
221   }, qr/Your DB is currently unversioned/, 'warning detected without env var or attr');
222 }
223
224 # attempt a deploy/upgrade cycle within one second
225 {
226   eval { $schema_v2->storage->dbh->do('drop table ' . $version_table_name) };
227   eval { $schema_v2->storage->dbh->do('drop table ' . $old_table_name) };
228   eval { $schema_v2->storage->dbh->do('drop table TestVersion') };
229
230   # this attempts to sleep until the turn of the second
231   my $t = time();
232   sleep (int ($t) + 1 - $t);
233   note ('Fast deploy/upgrade start: ', time() );
234
235   {
236     local $DBICVersion::Schema::VERSION = '2.0';
237     $schema_v2->deploy;
238   }
239
240   local $SIG{__WARN__} = sub { warn if $_[0] !~ /Attempting upgrade\.$/ };
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 unless ($ENV{DBICTEST_KEEP_VERSIONING_DDL}) {
274     unlink $_ for (values %$fn);
275 }
276
277 done_testing;