10 use Time::HiRes qw/time sleep/;
13 use DBICTest; # do not remove even though it is not used
15 my ($dsn, $user, $pass);
18 ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MYSQL_${_}" } qw/DSN USER PASS/};
20 plan skip_all => 'Set $ENV{DBICTEST_MYSQL_DSN}, _USER and _PASS to run this test'
25 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('deploy')
26 unless DBIx::Class::Optional::Dependencies->req_ok_for ('deploy')
29 use_ok('DBICVersion_v1');
31 my $version_table_name = 'dbix_class_schema_versions';
32 my $old_table_name = 'SchemaVersions';
34 my $ddl_dir = dir ('t', 'var');
35 mkdir ($ddl_dir) unless -d $ddl_dir;
38 v1 => $ddl_dir->file ('DBICVersion-Schema-1.0-MySQL.sql'),
39 v2 => $ddl_dir->file ('DBICVersion-Schema-2.0-MySQL.sql'),
40 v3 => $ddl_dir->file ('DBICVersion-Schema-3.0-MySQL.sql'),
41 trans_v12 => $ddl_dir->file ('DBICVersion-Schema-1.0-2.0-MySQL.sql'),
42 trans_v23 => $ddl_dir->file ('DBICVersion-Schema-2.0-3.0-MySQL.sql'),
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) };
49 is($schema_v1->ddl_filename('MySQL', '1.0', $ddl_dir), $fn->{v1}, 'Filename creation working');
50 unlink( $fn->{v1} ) if ( -e $fn->{v1} );
51 $schema_v1->create_ddl_dir('MySQL', undef, $ddl_dir);
53 ok(-f $fn->{v1}, 'Created DDL file');
54 $schema_v1->deploy({ add_drop_table => 1 });
56 my $tvrs = $schema_v1->{vschema}->resultset('Table');
57 is($schema_v1->_source_exists($tvrs), 1, 'Created schema from DDL file');
59 # loading a new module defining a new version of the same table
60 DBICVersion::Schema->_unregister_source ('Table');
61 use_ok('DBICVersion_v2');
63 my $schema_v2 = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 1 });
66 unlink($fn->{trans_v12});
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');
74 sub { $schema_v2->upgrade() },
75 qr/DB version .+? is lower than the schema version/,
76 'Warn before upgrade',
79 is($schema_v2->get_db_version(), '2.0', 'db version number upgraded');
82 $schema_v2->storage->dbh->do('select NewVersionName from TestVersion');
83 }, 'new column created' );
86 sub { $schema_v2->create_ddl_dir('MySQL', '2.0', $ddl_dir, '1.0') },
88 qr/Overwriting existing DDL file - \Q$fn->{v2}\E/,
89 qr/Overwriting existing diff file - \Q$fn->{trans_v12}\E/,
91 'An overwrite warning generated for both the DDL and the diff',
96 my $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass);
98 $schema_version->storage->dbh->do('select * from ' . $version_table_name);
99 }, 'version table exists');
102 $schema_version->storage->dbh->do("DROP TABLE IF EXISTS $old_table_name");
103 $schema_version->storage->dbh->do("RENAME TABLE $version_table_name TO $old_table_name");
104 }, 'versions table renamed to old style table');
106 $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass);
107 is($schema_version->get_db_version, '2.0', 'transition from old table name to new okay');
110 $schema_version->storage->dbh->do('select * from ' . $old_table_name);
111 }, 'old version table gone');
115 # repeat the v1->v2 process for v2->v3 before testing v1->v3
116 DBICVersion::Schema->_unregister_source ('Table');
117 use_ok('DBICVersion_v3');
119 my $schema_v3 = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 1 });
122 unlink($fn->{trans_v23});
124 is($schema_v3->get_db_version(), '2.0', 'get_db_version 2.0 ok');
125 is($schema_v3->schema_version, '3.0', 'schema version 3.0 ok');
126 $schema_v3->create_ddl_dir('MySQL', '3.0', $ddl_dir, '2.0');
127 ok(-f $fn->{trans_v23}, 'Created DDL 2.0 -> 3.0 file');
130 sub { $schema_v3->upgrade() },
131 qr/DB version .+? is lower than the schema version/,
132 'Warn before upgrade',
135 is($schema_v3->get_db_version(), '3.0', 'db version number upgraded');
138 $schema_v3->storage->dbh->do('select ExtraColumn from TestVersion');
139 }, 'new column created');
142 # now put the v1 schema back again
144 # drop all the tables...
145 eval { $schema_v1->storage->dbh->do('drop table ' . $version_table_name) };
146 eval { $schema_v1->storage->dbh->do('drop table ' . $old_table_name) };
147 eval { $schema_v1->storage->dbh->do('drop table TestVersion') };
150 local $DBICVersion::Schema::VERSION = '1.0';
153 is($schema_v1->get_db_version(), '1.0', 'get_db_version 1.0 ok');
156 # attempt v1 -> v3 upgrade
158 local $SIG{__WARN__} = sub { warn $_[0] if $_[0] !~ /Attempting upgrade\.$/ };
159 $schema_v3->upgrade();
160 is($schema_v3->get_db_version(), '3.0', 'db version number upgraded');
163 # Now, try a v1 -> v3 upgrade with a file that has comments strategically placed in it.
164 # First put the v1 schema back again...
166 # drop all the tables...
167 eval { $schema_v1->storage->dbh->do('drop table ' . $version_table_name) };
168 eval { $schema_v1->storage->dbh->do('drop table ' . $old_table_name) };
169 eval { $schema_v1->storage->dbh->do('drop table TestVersion') };
172 local $DBICVersion::Schema::VERSION = '1.0';
175 is($schema_v1->get_db_version(), '1.0', 'get_db_version 1.0 ok');
178 # add a "harmless" comment before one of the statements.
179 system( qq($^X -pi.bak -e "s/ALTER/-- this is a comment\nALTER/" $fn->{trans_v23}) );
181 # Then attempt v1 -> v3 upgrade
183 local $SIG{__WARN__} = sub { warn $_[0] if $_[0] !~ /Attempting upgrade\.$/ };
184 $schema_v3->upgrade();
185 is($schema_v3->get_db_version(), '3.0', 'db version number upgraded to 3.0');
187 # make sure that the column added after the comment is actually added.
189 $schema_v3->storage->dbh->do('select ExtraColumn from TestVersion');
190 }, 'new column created');
194 # check behaviour of DBIC_NO_VERSION_CHECK env var and ignore_version connect attr
196 my $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass);
198 $schema_version->storage->dbh->do("DELETE from $version_table_name");
202 warnings_like ( sub {
203 $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass);
204 }, qr/Your DB is currently unversioned/, 'warning detected without env var or attr' );
206 warnings_like ( sub {
207 $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 1 });
208 }, [], 'warning not detected with attr set');
211 local $ENV{DBIC_NO_VERSION_CHECK} = 1;
212 warnings_like ( sub {
213 $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass);
214 }, [], 'warning not detected with env var set');
216 warnings_like ( sub {
217 $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 0 });
218 }, qr/Your DB is currently unversioned/, 'warning detected without env var or attr');
221 # attempt a deploy/upgrade cycle within one second
223 eval { $schema_v2->storage->dbh->do('drop table ' . $version_table_name) };
224 eval { $schema_v2->storage->dbh->do('drop table ' . $old_table_name) };
225 eval { $schema_v2->storage->dbh->do('drop table TestVersion') };
227 # this attempts to sleep until the turn of the second
229 sleep (int ($t) + 1 - $t);
230 note ('Fast deploy/upgrade start: ', time() );
233 local $DBICVersion::Schema::VERSION = '2.0';
237 local $SIG{__WARN__} = sub { warn $_[0] if $_[0] !~ /Attempting upgrade\.$/ };
238 $schema_v2->upgrade();
240 is($schema_v2->get_db_version(), '3.0', 'Fast deploy/upgrade');
243 # Check that it Schema::Versioned deals with new/all forms of connect arguments.
245 my $get_db_version_run = 0;
247 no warnings qw/once redefine/;
248 local *DBIx::Class::Schema::Versioned::get_db_version = sub {
249 $get_db_version_run = 1;
250 return $_[0]->schema_version;
253 # Make sure the env var isn't whats triggering it
254 local $ENV{DBIC_NO_VERSION_CHECK} = 0;
256 DBICVersion::Schema->connect({
263 ok($get_db_version_run == 0, "attributes pulled from hashref connect_info");
264 $get_db_version_run = 0;
266 DBICVersion::Schema->connect( $dsn, $user, $pass, { ignore_version => 1 } );
267 ok($get_db_version_run == 0, "attributes pulled from list connect_info");
270 unless ($ENV{DBICTEST_KEEP_VERSIONING_DDL}) {
271 unlink $_ for (values %$fn);