f0b510f23e532849dc4e6d8aded687c4ea34d0e9
[dbsrgits/DBIx-Class-DeploymentHandler.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 my ($dsn, $user, $pass) = ('dbi:SQLite::memory:','','');
13
14 BEGIN {
15   eval { require Time::HiRes }
16     || plan skip_all => 'Test needs Time::HiRes';
17   Time::HiRes->import(qw/time sleep/);
18
19   require DBIx::Class;
20   plan skip_all =>
21       'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('deploy')
22     unless DBIx::Class::Optional::Dependencies->req_ok_for ('deploy')
23 }
24
25 use lib qw(t/lib);
26 use DBICTest; # do not remove even though it is not used
27
28 use_ok('DBICVersion_v1');
29
30 my $version_table_name = 'dbix_class_schema_versions';
31 my $old_table_name = 'SchemaVersions';
32
33 my $ddl_dir = dir ('t', 'var');
34 mkdir ($ddl_dir) unless -d $ddl_dir;
35
36 my $fn = {
37     v1 => $ddl_dir->file ('DBICVersion-Schema-1.0-MySQL.sql'),
38     v2 => $ddl_dir->file ('DBICVersion-Schema-2.0-MySQL.sql'),
39     v3 => $ddl_dir->file ('DBICVersion-Schema-3.0-MySQL.sql'),
40     trans_v12 => $ddl_dir->file ('DBICVersion-Schema-1.0-2.0-MySQL.sql'),
41     trans_v23 => $ddl_dir->file ('DBICVersion-Schema-2.0-3.0-MySQL.sql'),
42 };
43
44 my $schema_v1 = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 1 });
45 eval { $schema_v1->storage->dbh->do('drop table ' . $version_table_name) };
46 eval { $schema_v1->storage->dbh->do('drop table ' . $old_table_name) };
47
48 is($schema_v1->ddl_filename('MySQL', '1.0', $ddl_dir), $fn->{v1}, 'Filename creation working');
49 unlink( $fn->{v1} ) if ( -e $fn->{v1} );
50 $schema_v1->create_ddl_dir('MySQL', undef, $ddl_dir);
51
52 ok(-f $fn->{v1}, 'Created DDL file');
53 $schema_v1->deploy({ add_drop_table => 1 });
54
55 my $tvrs = $schema_v1->{vschema}->resultset('Table');
56 is($schema_v1->_source_exists($tvrs), 1, 'Created schema from DDL file');
57
58 # loading a new module defining a new version of the same table
59 DBICVersion::Schema->_unregister_source ('Table');
60 use_ok('DBICVersion_v2');
61
62 my $schema_v2 = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 1 });
63 {
64   unlink($fn->{v2});
65   unlink($fn->{trans_v12});
66
67   is($schema_v2->get_db_version(), '1.0', 'get_db_version ok');
68   is($schema_v2->schema_version, '2.0', 'schema version ok');
69   $schema_v2->create_ddl_dir('MySQL', '2.0', $ddl_dir, '1.0');
70   ok(-f $fn->{trans_v12}, 'Created DDL file');
71
72   warnings_like (
73     sub { $schema_v2->upgrade() },
74     qr/DB version .+? is lower than the schema version/,
75     'Warn before upgrade',
76   );
77
78   is($schema_v2->get_db_version(), '2.0', 'db version number upgraded');
79
80   lives_ok ( sub {
81     $schema_v2->storage->dbh->do('select NewVersionName from TestVersion');
82   }, 'new column created' );
83
84   warnings_exist (
85     sub { $schema_v2->create_ddl_dir('MySQL', '2.0', $ddl_dir, '1.0') },
86     [
87       qr/Overwriting existing DDL file - $fn->{v2}/,
88       qr/Overwriting existing diff file - $fn->{trans_v12}/,
89     ],
90     'An overwrite warning generated for both the DDL and the diff',
91   );
92 }
93
94 {
95   my $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass);
96   lives_ok (sub {
97     $schema_version->storage->dbh->do('select * from ' . $version_table_name);
98   }, 'version table exists');
99
100   lives_ok (sub {
101     $schema_version->storage->dbh->do("DROP TABLE IF EXISTS $old_table_name");
102     $schema_version->storage->dbh->do("RENAME TABLE $version_table_name TO $old_table_name");
103   }, 'versions table renamed to old style table');
104
105   $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass);
106   is($schema_version->get_db_version, '2.0', 'transition from old table name to new okay');
107
108   dies_ok (sub {
109     $schema_version->storage->dbh->do('select * from ' . $old_table_name);
110   }, 'old version table gone');
111
112 }
113
114 # repeat the v1->v2 process for v2->v3 before testing v1->v3
115 DBICVersion::Schema->_unregister_source ('Table');
116 use_ok('DBICVersion_v3');
117
118 my $schema_v3 = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 1 });
119 {
120   unlink($fn->{v3});
121   unlink($fn->{trans_v23});
122
123   is($schema_v3->get_db_version(), '2.0', 'get_db_version 2.0 ok');
124   is($schema_v3->schema_version, '3.0', 'schema version 3.0 ok');
125   $schema_v3->create_ddl_dir('MySQL', '3.0', $ddl_dir, '2.0');
126   ok(-f $fn->{trans_v23}, 'Created DDL 2.0 -> 3.0 file');
127
128   warnings_exist (
129     sub { $schema_v3->upgrade() },
130     qr/DB version .+? is lower than the schema version/,
131     'Warn before upgrade',
132   );
133
134   is($schema_v3->get_db_version(), '3.0', 'db version number upgraded');
135
136   lives_ok ( sub {
137     $schema_v3->storage->dbh->do('select ExtraColumn from TestVersion');
138   }, 'new column created');
139 }
140
141 # now put the v1 schema back again
142 {
143   # drop all the tables...
144   eval { $schema_v1->storage->dbh->do('drop table ' . $version_table_name) };
145   eval { $schema_v1->storage->dbh->do('drop table ' . $old_table_name) };
146   eval { $schema_v1->storage->dbh->do('drop table TestVersion') };
147
148   {
149     local $DBICVersion::Schema::VERSION = '1.0';
150     $schema_v1->deploy;
151   }
152   is($schema_v1->get_db_version(), '1.0', 'get_db_version 1.0 ok');
153 }
154
155 # attempt v1 -> v3 upgrade
156 {
157   local $SIG{__WARN__} = sub { warn if $_[0] !~ /Attempting upgrade\.$/ };
158   $schema_v3->upgrade();
159   is($schema_v3->get_db_version(), '3.0', 'db version number upgraded');
160 }
161
162 # check behaviour of DBIC_NO_VERSION_CHECK env var and ignore_version connect attr
163 {
164   my $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass);
165   eval {
166     $schema_version->storage->dbh->do("DELETE from $version_table_name");
167   };
168
169
170   warnings_like ( sub {
171     $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass);
172   }, qr/Your DB is currently unversioned/, 'warning detected without env var or attr' );
173
174   warnings_like ( sub {
175     $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 1 });
176   },  [], 'warning not detected with attr set');
177
178
179   local $ENV{DBIC_NO_VERSION_CHECK} = 1;
180   warnings_like ( sub {
181     $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass);
182   }, [], 'warning not detected with env var set');
183
184   warnings_like ( sub {
185     $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 0 });
186   }, qr/Your DB is currently unversioned/, 'warning detected without env var or attr');
187 }
188
189 # attempt a deploy/upgrade cycle within one second
190 {
191   eval { $schema_v2->storage->dbh->do('drop table ' . $version_table_name) };
192   eval { $schema_v2->storage->dbh->do('drop table ' . $old_table_name) };
193   eval { $schema_v2->storage->dbh->do('drop table TestVersion') };
194
195   # this attempts to sleep until the turn of the second
196   my $t = time();
197   sleep (int ($t) + 1 - $t);
198   note ('Fast deploy/upgrade start: ', time() );
199
200   {
201     local $DBICVersion::Schema::VERSION = '2.0';
202     $schema_v2->deploy;
203   }
204
205   local $SIG{__WARN__} = sub { warn if $_[0] !~ /Attempting upgrade\.$/ };
206   $schema_v2->upgrade();
207
208   is($schema_v2->get_db_version(), '3.0', 'Fast deploy/upgrade');
209 };
210
211 unless ($ENV{DBICTEST_KEEP_VERSIONING_DDL}) {
212     unlink $_ for (values %$fn);
213 }
214
215 done_testing;