9 #warn "$dsn $user $pass";
10 my ($dsn, $user, $pass);
13 ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MYSQL_${_}" } qw/DSN USER PASS/};
15 plan skip_all => 'Set $ENV{DBICTEST_MYSQL_DSN}, _USER and _PASS to run this test'
18 eval { require Time::HiRes }
19 || plan skip_all => 'Test needs Time::HiRes';
20 Time::HiRes->import(qw/time sleep/);
22 require DBIx::Class::Storage::DBI;
24 'Test needs SQL::Translator ' . DBIx::Class::Storage::DBI->_sqlt_minimum_version
25 if not DBIx::Class::Storage::DBI->_sqlt_version_ok;
28 my $version_table_name = 'dbix_class_schema_versions';
29 my $old_table_name = 'SchemaVersions';
31 my $ddl_dir = File::Spec->catdir ('t', 'var');
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'),
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'),
41 use DBICTest; # do not remove even though it is not used
43 use_ok('DBICVersion_v1');
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 eval "use 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');
75 local $SIG{__WARN__} = sub { $w = shift };
77 $schema_v2->upgrade();
78 like ($w, qr/Attempting upgrade\.$/, 'Warn before upgrade');
81 is($schema_v2->get_db_version(), '2.0', 'db version number upgraded');
84 $schema_v2->storage->dbh->do('select NewVersionName from TestVersion');
86 is($@, '', 'new column created');
88 # should overwrite files and warn about it
90 local $SIG{__WARN__} = sub {
91 if ($_[0] =~ /Overwriting existing/) {
98 $schema_v2->create_ddl_dir('MySQL', '2.0', $ddl_dir, '1.0');
100 is (2, @w, 'A warning generated for both the DDL and the diff');
101 like ($w[0], qr/Overwriting existing DDL file - $fn->{v2}/, 'New version DDL overwrite warning');
102 like ($w[1], qr/Overwriting existing diff file - $fn->{trans_v12}/, 'Upgrade diff overwrite warning');
106 my $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass);
108 $schema_version->storage->dbh->do('select * from ' . $version_table_name);
110 is($@, '', 'version table exists');
113 $schema_version->storage->dbh->do("DROP TABLE IF EXISTS $old_table_name");
114 $schema_version->storage->dbh->do("RENAME TABLE $version_table_name TO $old_table_name");
116 is($@, '', 'versions table renamed to old style table');
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');
122 $schema_version->storage->dbh->do('select * from ' . $old_table_name);
124 ok($@, 'old version table gone');
128 # repeat the v1->v2 process for v2->v3 before testing v1->v3
129 DBICVersion::Schema->_unregister_source ('Table');
130 eval "use 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');
144 local $SIG{__WARN__} = sub { $w = shift };
146 $schema_v3->upgrade();
147 like ($w, qr/Attempting upgrade\.$/, 'Warn before upgrade');
150 is($schema_v3->get_db_version(), '3.0', 'db version number upgraded');
153 $schema_v3->storage->dbh->do('select ExtraColumn from TestVersion');
155 is($@, '', 'new column created');
158 # now put the v1 schema back again
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') };
166 local $DBICVersion::Schema::VERSION = '1.0';
169 is($schema_v1->get_db_version(), '1.0', 'get_db_version 1.0 ok');
172 # attempt v1 -> v3 upgrade....
176 local $SIG{__WARN__} = sub { $w = shift };
178 $schema_v3->upgrade();
179 like ($w, qr/Attempting upgrade\.$/, 'Warn before upgrade');
182 is($schema_v3->get_db_version(), '3.0', 'db version number upgraded');
185 # check behaviour of DBIC_NO_VERSION_CHECK env var and ignore_version connect attr
187 my $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass);
189 $schema_version->storage->dbh->do("DELETE from $version_table_name");
194 local $SIG{__WARN__} = sub { $warn = shift };
195 $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass);
196 like($warn, qr/Your DB is currently unversioned/, 'warning detected without env var or attr');
201 $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 1 });
202 is($warn, '', 'warning not detected with attr set');
205 local $ENV{DBIC_NO_VERSION_CHECK} = 1;
207 $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass);
208 is($warn, '', 'warning not detected with env var set');
212 $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 0 });
213 like($warn, qr/Your DB is currently unversioned/, 'warning detected without env var or attr');
217 # attempt a deploy/upgrade cycle within one second
220 local $TODO = 'To fix this properly the table must be extended with an autoinc column, mst will not accept anything less';
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') };
226 # this attempts to sleep until the turn of the second
228 sleep (int ($t) + 1 - $t);
229 diag ('Fast deploy/upgrade start: ', time() );
232 local $DBICVersion::Schema::VERSION = '2.0';
236 local $SIG{__WARN__} = sub { warn if $_[0] !~ /Attempting upgrade\.$/ };
237 $schema_v2->upgrade();
239 is($schema_v2->get_db_version(), '3.0', 'Fast deploy/upgrade');
242 unless ($ENV{DBICTEST_KEEP_VERSIONING_DDL}) {
243 unlink $_ for (values %$fn);