10 use Time::HiRes qw/time sleep/;
14 use DBIx::Class::_Util 'sigwarn_silencer';
16 my ($dsn, $user, $pass);
19 ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MYSQL_${_}" } qw/DSN USER PASS/};
21 plan skip_all => 'Set $ENV{DBICTEST_MYSQL_DSN}, _USER and _PASS to run this test'
26 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('deploy')
27 unless DBIx::Class::Optional::Dependencies->req_ok_for ('deploy');
30 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('test_rdbms_mysql')
31 unless DBIx::Class::Optional::Dependencies->req_ok_for ('test_rdbms_mysql');
34 # this is just to grab a lock
36 my $s = DBICTest::Schema->connect($dsn, $user, $pass);
39 # in case it came from the env
40 $ENV{DBIC_NO_VERSION_CHECK} = 0;
42 use_ok('DBICVersion_v1');
44 my $version_table_name = 'dbix_class_schema_versions';
45 my $old_table_name = 'SchemaVersions';
47 my $ddl_dir = dir(qw/t var/, "versioning_ddl-$$");
48 $ddl_dir->mkpath unless -d $ddl_dir;
51 v1 => $ddl_dir->file ('DBICVersion-Schema-1.0-MySQL.sql'),
52 v2 => $ddl_dir->file ('DBICVersion-Schema-2.0-MySQL.sql'),
53 v3 => $ddl_dir->file ('DBICVersion-Schema-3.0-MySQL.sql'),
54 trans_v12 => $ddl_dir->file ('DBICVersion-Schema-1.0-2.0-MySQL.sql'),
55 trans_v23 => $ddl_dir->file ('DBICVersion-Schema-2.0-3.0-MySQL.sql'),
58 my $schema_v1 = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 1 });
59 eval { $schema_v1->storage->dbh->do('drop table ' . $version_table_name) };
60 eval { $schema_v1->storage->dbh->do('drop table ' . $old_table_name) };
62 is($schema_v1->ddl_filename('MySQL', '1.0', $ddl_dir), $fn->{v1}, 'Filename creation working');
63 unlink( $fn->{v1} ) if ( -e $fn->{v1} );
64 $schema_v1->create_ddl_dir('MySQL', undef, $ddl_dir);
66 ok(-f $fn->{v1}, 'Created DDL file');
67 $schema_v1->deploy({ add_drop_table => 1 });
69 my $tvrs = $schema_v1->{vschema}->resultset('Table');
70 is($schema_v1->_source_exists($tvrs), 1, 'Created schema from DDL file');
72 # loading a new module defining a new version of the same table
73 DBICVersion::Schema->_unregister_source ('Table');
74 use_ok('DBICVersion_v2');
76 my $schema_v2 = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 1 });
79 unlink($fn->{trans_v12});
81 is($schema_v2->get_db_version(), '1.0', 'get_db_version ok');
82 is($schema_v2->schema_version, '2.0', 'schema version ok');
83 $schema_v2->create_ddl_dir('MySQL', '2.0', $ddl_dir, '1.0');
84 ok(-f $fn->{trans_v12}, 'Created DDL file');
87 sub { $schema_v2->upgrade() },
88 qr/DB version .+? is lower than the schema version/,
89 'Warn before upgrade',
92 is($schema_v2->get_db_version(), '2.0', 'db version number upgraded');
95 $schema_v2->storage->dbh->do('select NewVersionName from TestVersion');
96 }, 'new column created' );
99 sub { $schema_v2->create_ddl_dir('MySQL', '2.0', $ddl_dir, '1.0') },
101 qr/Overwriting existing DDL file - \Q$fn->{v2}\E/,
102 qr/Overwriting existing diff file - \Q$fn->{trans_v12}\E/,
104 'An overwrite warning generated for both the DDL and the diff',
109 my $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass);
111 $schema_version->storage->dbh->do('select * from ' . $version_table_name);
112 }, 'version table exists');
115 $schema_version->storage->dbh->do("DROP TABLE IF EXISTS $old_table_name");
116 $schema_version->storage->dbh->do("RENAME TABLE $version_table_name TO $old_table_name");
117 }, 'versions table renamed to old style table');
119 $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass);
120 is($schema_version->get_db_version, '2.0', 'transition from old table name to new okay');
123 $schema_version->storage->dbh->do('select * from ' . $old_table_name);
124 }, 'old version table gone');
128 # repeat the v1->v2 process for v2->v3 before testing v1->v3
129 DBICVersion::Schema->_unregister_source ('Table');
130 use_ok('DBICVersion_v3');
132 my $schema_v3 = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 1 });
135 unlink($fn->{trans_v23});
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');
143 sub { $schema_v3->upgrade() },
144 qr/DB version .+? is lower than the schema version/,
145 'Warn before upgrade',
148 is($schema_v3->get_db_version(), '3.0', 'db version number upgraded');
151 $schema_v3->storage->dbh->do('select ExtraColumn from TestVersion');
152 }, 'new column created');
155 # now put the v1 schema back again
157 # drop all the tables...
158 eval { $schema_v1->storage->dbh->do('drop table ' . $version_table_name) };
159 eval { $schema_v1->storage->dbh->do('drop table ' . $old_table_name) };
160 eval { $schema_v1->storage->dbh->do('drop table TestVersion') };
163 local $DBICVersion::Schema::VERSION = '1.0';
166 is($schema_v1->get_db_version(), '1.0', 'get_db_version 1.0 ok');
169 # attempt v1 -> v3 upgrade
171 local $SIG{__WARN__} = sigwarn_silencer( qr/Attempting upgrade\.$/ );
172 $schema_v3->upgrade();
173 is($schema_v3->get_db_version(), '3.0', 'db version number upgraded');
176 # Now, try a v1 -> v3 upgrade with a file that has comments strategically placed in it.
177 # First put the v1 schema back again...
179 # drop all the tables...
180 eval { $schema_v1->storage->dbh->do('drop table ' . $version_table_name) };
181 eval { $schema_v1->storage->dbh->do('drop table ' . $old_table_name) };
182 eval { $schema_v1->storage->dbh->do('drop table TestVersion') };
185 local $DBICVersion::Schema::VERSION = '1.0';
188 is($schema_v1->get_db_version(), '1.0', 'get_db_version 1.0 ok');
191 # add a "harmless" comment before one of the statements.
193 my ($perl) = $^X =~ /(.+)/;
195 system( qq($perl -pi.bak -e "s/ALTER/-- this is a comment\nALTER/" $fn->{trans_v23}) );
198 # Then attempt v1 -> v3 upgrade
200 local $SIG{__WARN__} = sigwarn_silencer( qr/Attempting upgrade\.$/ );
201 $schema_v3->upgrade();
202 is($schema_v3->get_db_version(), '3.0', 'db version number upgraded to 3.0');
204 # make sure that the column added after the comment is actually added.
206 $schema_v3->storage->dbh->do('select ExtraColumn from TestVersion');
207 }, 'new column created');
211 # check behaviour of DBIC_NO_VERSION_CHECK env var and ignore_version connect attr
213 my $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass);
215 $schema_version->storage->dbh->do("DELETE from $version_table_name");
219 warnings_like ( sub {
220 $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass);
221 }, qr/Your DB is currently unversioned/, 'warning detected without env var or attr' );
223 warnings_like ( sub {
224 $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 1 });
225 }, [], 'warning not detected with attr set');
228 local $ENV{DBIC_NO_VERSION_CHECK} = 1;
229 warnings_like ( sub {
230 $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass);
231 }, [], 'warning not detected with env var set');
233 warnings_like ( sub {
234 $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 0 });
235 }, qr/Your DB is currently unversioned/, 'warning detected without env var or attr');
238 # attempt a deploy/upgrade cycle within one second
240 eval { $schema_v2->storage->dbh->do('drop table ' . $version_table_name) };
241 eval { $schema_v2->storage->dbh->do('drop table ' . $old_table_name) };
242 eval { $schema_v2->storage->dbh->do('drop table TestVersion') };
244 # this attempts to sleep until the turn of the second
246 sleep (int ($t) + 1 - $t);
247 note ('Fast deploy/upgrade start: ', time() );
250 local $DBICVersion::Schema::VERSION = '2.0';
254 local $SIG{__WARN__} = sigwarn_silencer( qr/Attempting upgrade\.$/ );
256 $schema_v2->upgrade();
258 is($schema_v2->get_db_version(), '3.0', 'Fast deploy/upgrade');
261 # Check that it Schema::Versioned deals with new/all forms of connect arguments.
263 my $get_db_version_run = 0;
265 no warnings qw/once redefine/;
266 local *DBIx::Class::Schema::Versioned::get_db_version = sub {
267 $get_db_version_run = 1;
268 return $_[0]->schema_version;
271 # Make sure the env var isn't whats triggering it
272 local $ENV{DBIC_NO_VERSION_CHECK} = 0;
274 DBICVersion::Schema->connect({
281 ok($get_db_version_run == 0, "attributes pulled from hashref connect_info");
282 $get_db_version_run = 0;
284 DBICVersion::Schema->connect( $dsn, $user, $pass, { ignore_version => 1 } );
285 ok($get_db_version_run == 0, "attributes pulled from list connect_info");
289 unless ($ENV{DBICTEST_KEEP_VERSIONING_DDL}) {