Commit | Line | Data |
c9d2e0a2 |
1 | #!/usr/bin/perl |
7f6f5b69 |
2 | |
c9d2e0a2 |
3 | use strict; |
4 | use warnings; |
5 | use Test::More; |
86ef9f0d |
6 | use File::Spec; |
f925f7cb |
7 | use File::Copy; |
c9d2e0a2 |
8 | |
1d48fcff |
9 | #warn "$dsn $user $pass"; |
10 | my ($dsn, $user, $pass); |
11 | |
c9d2e0a2 |
12 | BEGIN { |
1d48fcff |
13 | ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MYSQL_${_}" } qw/DSN USER PASS/}; |
14 | |
15 | plan skip_all => 'Set $ENV{DBICTEST_MYSQL_DSN}, _USER and _PASS to run this test' |
8424c090 |
16 | unless ($dsn); |
1d48fcff |
17 | |
21d70f0b |
18 | eval { require Time::HiRes } |
19 | || plan skip_all => 'Test needs Time::HiRes'; |
20 | Time::HiRes->import(qw/time sleep/); |
21 | |
b2b2e7fd |
22 | require DBIx::Class::Storage::DBI; |
7f6f5b69 |
23 | plan skip_all => |
b2b2e7fd |
24 | 'Test needs SQL::Translator ' . DBIx::Class::Storage::DBI->_sqlt_minimum_version |
25 | if not DBIx::Class::Storage::DBI->_sqlt_version_ok; |
c9d2e0a2 |
26 | } |
27 | |
b4b1e91c |
28 | my $version_table_name = 'dbix_class_schema_versions'; |
29 | my $old_table_name = 'SchemaVersions'; |
30 | |
1475105d |
31 | my $ddl_dir = File::Spec->catdir ('t', 'var'); |
32 | my $fn = { |
86456031 |
33 | v1 => File::Spec->catfile($ddl_dir, 'DBICVersion-Schema-1.0-MySQL.sql'), |
34 | v2 => File::Spec->catfile($ddl_dir, 'DBICVersion-Schema-2.0-MySQL.sql'), |
d2bc7045 |
35 | v3 => File::Spec->catfile($ddl_dir, 'DBICVersion-Schema-3.0-MySQL.sql'), |
36 | trans_v12 => File::Spec->catfile($ddl_dir, 'DBICVersion-Schema-1.0-2.0-MySQL.sql'), |
37 | trans_v23 => File::Spec->catfile($ddl_dir, 'DBICVersion-Schema-2.0-3.0-MySQL.sql'), |
1475105d |
38 | }; |
39 | |
c9d2e0a2 |
40 | use lib qw(t/lib); |
d8190011 |
41 | use DBICTest; # do not remove even though it is not used |
42 | |
d2bc7045 |
43 | use_ok('DBICVersion_v1'); |
c9d2e0a2 |
44 | |
d2bc7045 |
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) }; |
c9d2e0a2 |
48 | |
d2bc7045 |
49 | is($schema_v1->ddl_filename('MySQL', '1.0', $ddl_dir), $fn->{v1}, 'Filename creation working'); |
1475105d |
50 | unlink( $fn->{v1} ) if ( -e $fn->{v1} ); |
d2bc7045 |
51 | $schema_v1->create_ddl_dir('MySQL', undef, $ddl_dir); |
c9d2e0a2 |
52 | |
1475105d |
53 | ok(-f $fn->{v1}, 'Created DDL file'); |
d2bc7045 |
54 | $schema_v1->deploy({ add_drop_table => 1 }); |
c9d2e0a2 |
55 | |
d2bc7045 |
56 | my $tvrs = $schema_v1->{vschema}->resultset('Table'); |
57 | is($schema_v1->_source_exists($tvrs), 1, 'Created schema from DDL file'); |
c9d2e0a2 |
58 | |
1475105d |
59 | # loading a new module defining a new version of the same table |
60 | DBICVersion::Schema->_unregister_source ('Table'); |
d2bc7045 |
61 | eval "use DBICVersion_v2"; |
1475105d |
62 | |
d2bc7045 |
63 | my $schema_v2 = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 1 }); |
f925f7cb |
64 | { |
1475105d |
65 | unlink($fn->{v2}); |
d2bc7045 |
66 | unlink($fn->{trans_v12}); |
f925f7cb |
67 | |
d2bc7045 |
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'); |
1475105d |
72 | |
73 | { |
74 | my $w; |
75 | local $SIG{__WARN__} = sub { $w = shift }; |
e9fbbbf4 |
76 | |
d2bc7045 |
77 | $schema_v2->upgrade(); |
86456031 |
78 | like ($w, qr/Attempting upgrade\.$/, 'Warn before upgrade'); |
1475105d |
79 | } |
80 | |
d2bc7045 |
81 | is($schema_v2->get_db_version(), '2.0', 'db version number upgraded'); |
f925f7cb |
82 | |
83 | eval { |
d2bc7045 |
84 | $schema_v2->storage->dbh->do('select NewVersionName from TestVersion'); |
f925f7cb |
85 | }; |
86 | is($@, '', 'new column created'); |
99a74c4a |
87 | |
1475105d |
88 | # should overwrite files and warn about it |
89 | my @w; |
7a063741 |
90 | local $SIG{__WARN__} = sub { |
341d5ede |
91 | if ($_[0] =~ /Overwriting existing/) { |
7a063741 |
92 | push @w, $_[0]; |
93 | } |
94 | else { |
95 | warn @_; |
96 | } |
97 | }; |
d2bc7045 |
98 | $schema_v2->create_ddl_dir('MySQL', '2.0', $ddl_dir, '1.0'); |
1475105d |
99 | |
100 | is (2, @w, 'A warning generated for both the DDL and the diff'); |
341d5ede |
101 | like ($w[0], qr/Overwriting existing DDL file - $fn->{v2}/, 'New version DDL overwrite warning'); |
d2bc7045 |
102 | like ($w[1], qr/Overwriting existing diff file - $fn->{trans_v12}/, 'Upgrade diff overwrite warning'); |
f925f7cb |
103 | } |
b4b1e91c |
104 | |
105 | { |
106 | my $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass); |
107 | eval { |
108 | $schema_version->storage->dbh->do('select * from ' . $version_table_name); |
109 | }; |
110 | is($@, '', 'version table exists'); |
111 | |
112 | eval { |
113 | $schema_version->storage->dbh->do("DROP TABLE IF EXISTS $old_table_name"); |
86456031 |
114 | $schema_version->storage->dbh->do("RENAME TABLE $version_table_name TO $old_table_name"); |
b4b1e91c |
115 | }; |
116 | is($@, '', '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 | eval { |
122 | $schema_version->storage->dbh->do('select * from ' . $old_table_name); |
123 | }; |
124 | ok($@, 'old version table gone'); |
125 | |
126 | } |
f81b9157 |
127 | |
d2bc7045 |
128 | # repeat the v1->v2 process for v2->v3 before testing v1->v3 |
129 | DBICVersion::Schema->_unregister_source ('Table'); |
130 | eval "use DBICVersion_v3"; |
131 | |
132 | my $schema_v3 = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 1 }); |
133 | { |
134 | unlink($fn->{v3}); |
135 | unlink($fn->{trans_v23}); |
136 | |
137 | is($schema_v3->get_db_version(), '2.0', 'get_db_version 2.0 ok'); |
138 | is($schema_v3->schema_version, '3.0', 'schema version 3.0 ok'); |
139 | $schema_v3->create_ddl_dir('MySQL', '3.0', $ddl_dir, '2.0'); |
140 | ok(-f $fn->{trans_v23}, 'Created DDL 2.0 -> 3.0 file'); |
141 | |
142 | { |
143 | my $w; |
144 | local $SIG{__WARN__} = sub { $w = shift }; |
145 | |
146 | $schema_v3->upgrade(); |
147 | like ($w, qr/Attempting upgrade\.$/, 'Warn before upgrade'); |
148 | } |
149 | |
150 | is($schema_v3->get_db_version(), '3.0', 'db version number upgraded'); |
151 | |
152 | eval { |
153 | $schema_v3->storage->dbh->do('select ExtraColumn from TestVersion'); |
154 | }; |
155 | is($@, '', 'new column created'); |
156 | } |
157 | |
158 | # now put the v1 schema back again |
159 | { |
160 | # drop all the tables... |
161 | eval { $schema_v1->storage->dbh->do('drop table ' . $version_table_name) }; |
162 | eval { $schema_v1->storage->dbh->do('drop table ' . $old_table_name) }; |
163 | eval { $schema_v1->storage->dbh->do('drop table TestVersion') }; |
164 | |
165 | { |
166 | local $DBICVersion::Schema::VERSION = '1.0'; |
167 | $schema_v1->deploy; |
168 | } |
169 | is($schema_v1->get_db_version(), '1.0', 'get_db_version 1.0 ok'); |
170 | } |
171 | |
172 | # attempt v1 -> v3 upgrade.... |
173 | { |
174 | { |
175 | my $w; |
176 | local $SIG{__WARN__} = sub { $w = shift }; |
177 | |
178 | $schema_v3->upgrade(); |
179 | like ($w, qr/Attempting upgrade\.$/, 'Warn before upgrade'); |
180 | } |
181 | |
182 | is($schema_v3->get_db_version(), '3.0', 'db version number upgraded'); |
183 | } |
184 | |
f81b9157 |
185 | # check behaviour of DBIC_NO_VERSION_CHECK env var and ignore_version connect attr |
186 | { |
187 | my $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass); |
188 | eval { |
d0dfedc2 |
189 | $schema_version->storage->dbh->do("DELETE from $version_table_name"); |
f81b9157 |
190 | }; |
191 | |
d0dfedc2 |
192 | |
193 | my $warn = ''; |
fc38383e |
194 | local $SIG{__WARN__} = sub { $warn = shift }; |
f81b9157 |
195 | $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass); |
d0dfedc2 |
196 | like($warn, qr/Your DB is currently unversioned/, 'warning detected without env var or attr'); |
f81b9157 |
197 | |
d0dfedc2 |
198 | |
199 | # should warn |
200 | $warn = ''; |
f81b9157 |
201 | $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 1 }); |
d0dfedc2 |
202 | is($warn, '', 'warning not detected with attr set'); |
f81b9157 |
203 | # should not warn |
204 | |
1475105d |
205 | local $ENV{DBIC_NO_VERSION_CHECK} = 1; |
d0dfedc2 |
206 | $warn = ''; |
f81b9157 |
207 | $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass); |
d0dfedc2 |
208 | is($warn, '', 'warning not detected with env var set'); |
f81b9157 |
209 | # should not warn |
210 | |
d0dfedc2 |
211 | $warn = ''; |
f81b9157 |
212 | $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 0 }); |
d0dfedc2 |
213 | like($warn, qr/Your DB is currently unversioned/, 'warning detected without env var or attr'); |
f81b9157 |
214 | # should warn |
215 | } |
1475105d |
216 | |
217 | # attempt a deploy/upgrade cycle within one second |
e9fbbbf4 |
218 | TODO: { |
219 | |
220 | local $TODO = 'To fix this properly the table must be extended with an autoinc column, mst will not accept anything less'; |
221 | |
d2bc7045 |
222 | eval { $schema_v2->storage->dbh->do('drop table ' . $version_table_name) }; |
223 | eval { $schema_v2->storage->dbh->do('drop table ' . $old_table_name) }; |
224 | eval { $schema_v2->storage->dbh->do('drop table TestVersion') }; |
1475105d |
225 | |
226 | # this attempts to sleep until the turn of the second |
227 | my $t = time(); |
228 | sleep (int ($t) + 1 - $t); |
229 | diag ('Fast deploy/upgrade start: ', time() ); |
230 | |
231 | { |
d2bc7045 |
232 | local $DBICVersion::Schema::VERSION = '2.0'; |
233 | $schema_v2->deploy; |
1475105d |
234 | } |
1475105d |
235 | |
e9fbbbf4 |
236 | local $SIG{__WARN__} = sub { warn if $_[0] !~ /Attempting upgrade\.$/ }; |
d2bc7045 |
237 | $schema_v2->upgrade(); |
1475105d |
238 | |
d2bc7045 |
239 | is($schema_v2->get_db_version(), '3.0', 'Fast deploy/upgrade'); |
e9fbbbf4 |
240 | }; |
1475105d |
241 | |
242 | unless ($ENV{DBICTEST_KEEP_VERSIONING_DDL}) { |
243 | unlink $_ for (values %$fn); |
244 | } |
7f6f5b69 |
245 | |
246 | done_testing; |