1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2 use DBIx::Class::Optional::Dependencies -skip_all_without => qw(deploy test_rdbms_mysql);
11 use Time::HiRes qw/time sleep/;
14 use DBIx::Class::_Util qw( sigwarn_silencer mkdir_p );
15 use DBICTest::Util 'rm_rf';
17 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MYSQL_${_}" } qw/DSN USER PASS/};
19 # this is just to grab a lock
21 my $s = DBICTest::Schema->connect($dsn, $user, $pass);
24 # in case it came from the env
25 $ENV{DBIC_NO_VERSION_CHECK} = 0;
27 use_ok('DBICVersion_v1');
29 my $version_table_name = 'dbix_class_schema_versions';
30 my $old_table_name = 'SchemaVersions';
32 my $ddl_dir = "t/var/versioning_ddl-$$";
33 mkdir_p $ddl_dir unless -d $ddl_dir;
36 v1 => "$ddl_dir/DBICVersion-Schema-1.0-MySQL.sql",
37 v2 => "$ddl_dir/DBICVersion-Schema-2.0-MySQL.sql",
38 v3 => "$ddl_dir/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",
43 my $schema_v1 = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 1 });
44 eval { $schema_v1->storage->dbh->do('drop table ' . $version_table_name) };
45 eval { $schema_v1->storage->dbh->do('drop table ' . $old_table_name) };
47 is($schema_v1->ddl_filename('MySQL', '1.0', $ddl_dir), $fn->{v1}, 'Filename creation working');
48 unlink( $fn->{v1} ) if ( -e $fn->{v1} );
49 $schema_v1->create_ddl_dir('MySQL', undef, $ddl_dir);
51 ok(-f $fn->{v1}, 'Created DDL file');
52 $schema_v1->deploy({ add_drop_table => 1 });
54 my $tvrs = $schema_v1->{vschema}->resultset('Table');
55 is($schema_v1->_source_exists($tvrs), 1, 'Created schema from DDL file');
57 # loading a new module defining a new version of the same table
58 DBICVersion::Schema->_unregister_source ('Table');
59 use_ok('DBICVersion_v2');
61 my $schema_v2 = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 1 });
64 unlink($fn->{trans_v12});
66 is($schema_v2->get_db_version(), '1.0', 'get_db_version ok');
67 is($schema_v2->schema_version, '2.0', 'schema version ok');
68 $schema_v2->create_ddl_dir('MySQL', '2.0', $ddl_dir, '1.0');
69 ok(-f $fn->{trans_v12}, 'Created DDL file');
72 sub { $schema_v2->upgrade() },
73 qr/DB version .+? is lower than the schema version/,
74 'Warn before upgrade',
77 is($schema_v2->get_db_version(), '2.0', 'db version number upgraded');
80 $schema_v2->storage->dbh->do('select NewVersionName from TestVersion');
81 }, 'new column created' );
84 sub { $schema_v2->create_ddl_dir('MySQL', '2.0', $ddl_dir, '1.0') },
86 qr/Overwriting existing DDL file - \Q$fn->{v2}\E/,
87 qr/Overwriting existing diff file - \Q$fn->{trans_v12}\E/,
89 'An overwrite warning generated for both the DDL and the diff',
94 my $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass);
96 $schema_version->storage->dbh->do('select * from ' . $version_table_name);
97 }, 'version table exists');
100 $schema_version->storage->dbh->do("DROP TABLE IF EXISTS $old_table_name");
101 $schema_version->storage->dbh->do("RENAME TABLE $version_table_name TO $old_table_name");
102 }, 'versions table renamed to old style table');
104 $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass);
105 is($schema_version->get_db_version, '2.0', 'transition from old table name to new okay');
108 $schema_version->storage->dbh->do('select * from ' . $old_table_name);
109 }, 'old version table gone');
113 # repeat the v1->v2 process for v2->v3 before testing v1->v3
114 DBICVersion::Schema->_unregister_source ('Table');
115 use_ok('DBICVersion_v3');
117 my $schema_v3 = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 1 });
120 unlink($fn->{trans_v23});
122 is($schema_v3->get_db_version(), '2.0', 'get_db_version 2.0 ok');
123 is($schema_v3->schema_version, '3.0', 'schema version 3.0 ok');
124 $schema_v3->create_ddl_dir('MySQL', '3.0', $ddl_dir, '2.0');
125 ok(-f $fn->{trans_v23}, 'Created DDL 2.0 -> 3.0 file');
128 sub { $schema_v3->upgrade() },
129 qr/DB version .+? is lower than the schema version/,
130 'Warn before upgrade',
133 is($schema_v3->get_db_version(), '3.0', 'db version number upgraded');
136 $schema_v3->storage->dbh->do('select ExtraColumn from TestVersion');
137 }, 'new column created');
140 # now put the v1 schema back again
142 # drop all the tables...
143 eval { $schema_v1->storage->dbh->do('drop table ' . $version_table_name) };
144 eval { $schema_v1->storage->dbh->do('drop table ' . $old_table_name) };
145 eval { $schema_v1->storage->dbh->do('drop table TestVersion') };
148 local $DBICVersion::Schema::VERSION = '1.0';
151 is($schema_v1->get_db_version(), '1.0', 'get_db_version 1.0 ok');
154 # attempt v1 -> v3 upgrade
156 local $SIG{__WARN__} = sigwarn_silencer( qr/Attempting upgrade\.$/ );
157 $schema_v3->upgrade();
158 is($schema_v3->get_db_version(), '3.0', 'db version number upgraded');
161 # Now, try a v1 -> v3 upgrade with a file that has comments strategically placed in it.
162 # First put the v1 schema back again...
164 # drop all the tables...
165 eval { $schema_v1->storage->dbh->do('drop table ' . $version_table_name) };
166 eval { $schema_v1->storage->dbh->do('drop table ' . $old_table_name) };
167 eval { $schema_v1->storage->dbh->do('drop table TestVersion') };
170 local $DBICVersion::Schema::VERSION = '1.0';
173 is($schema_v1->get_db_version(), '1.0', 'get_db_version 1.0 ok');
176 # add a "harmless" comment before one of the statements.
178 my ($perl) = $^X =~ /(.+)/;
180 system( qq($perl -pi.bak -e "s/ALTER/-- this is a comment\nALTER/" $fn->{trans_v23}) );
183 # Then attempt v1 -> v3 upgrade
185 local $SIG{__WARN__} = sigwarn_silencer( qr/Attempting upgrade\.$/ );
186 $schema_v3->upgrade();
187 is($schema_v3->get_db_version(), '3.0', 'db version number upgraded to 3.0');
189 # make sure that the column added after the comment is actually added.
191 $schema_v3->storage->dbh->do('select ExtraColumn from TestVersion');
192 }, 'new column created');
196 # check behaviour of DBIC_NO_VERSION_CHECK env var and ignore_version connect attr
198 my $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass);
200 $schema_version->storage->dbh->do("DELETE from $version_table_name");
204 warnings_like ( sub {
205 $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass);
206 }, qr/Your DB is currently unversioned/, 'warning detected without env var or attr' );
208 warnings_like ( sub {
209 $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 1 });
210 }, [], 'warning not detected with attr set');
213 local $ENV{DBIC_NO_VERSION_CHECK} = 1;
214 warnings_like ( sub {
215 $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass);
216 }, [], 'warning not detected with env var set');
218 warnings_like ( sub {
219 $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 0 });
220 }, qr/Your DB is currently unversioned/, 'warning detected without env var or attr');
223 # attempt a deploy/upgrade cycle within one second
225 eval { $schema_v2->storage->dbh->do('drop table ' . $version_table_name) };
226 eval { $schema_v2->storage->dbh->do('drop table ' . $old_table_name) };
227 eval { $schema_v2->storage->dbh->do('drop table TestVersion') };
229 # this attempts to sleep until the turn of the second
231 sleep (int ($t) + 1 - $t);
232 note ('Fast deploy/upgrade start: ', time() );
235 local $DBICVersion::Schema::VERSION = '2.0';
239 local $SIG{__WARN__} = sigwarn_silencer( qr/Attempting upgrade\.$/ );
241 $schema_v2->upgrade();
243 is($schema_v2->get_db_version(), '3.0', 'Fast deploy/upgrade');
246 # Check that it Schema::Versioned deals with new/all forms of connect arguments.
248 my $get_db_version_run = 0;
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;
256 # Make sure the env var isn't whats triggering it
257 local $ENV{DBIC_NO_VERSION_CHECK} = 0;
259 DBICVersion::Schema->connect({
266 ok($get_db_version_run == 0, "attributes pulled from hashref connect_info");
267 $get_db_version_run = 0;
269 DBICVersion::Schema->connect( $dsn, $user, $pass, { ignore_version => 1 } );
270 ok($get_db_version_run == 0, "attributes pulled from list connect_info");
273 # at this point we have v1, v2 and v3 still connected
274 # make sure they are the only connections and everything else is gone
277 { defined $_ and $_->{Active} }
279 { @{$_->{ChildHandles}} }
280 values %{ { DBI->installed_drivers } }
281 ), 3, "Expected number of connections at end of script"
285 rm_rf $ddl_dir unless $ENV{DBICTEST_KEEP_VERSIONING_DDL};