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