8 use Time::HiRes qw/time sleep/;
10 #warn "$dsn $user $pass";
11 my ($dsn, $user, $pass);
14 ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MYSQL_${_}" } qw/DSN USER PASS/};
16 plan skip_all => 'Set $ENV{DBICTEST_MYSQL_DSN}, _USER and _PASS to run this test'
19 require DBIx::Class::Storage::DBI;
21 'Test needs SQL::Translator ' . DBIx::Class::Storage::DBI->_sqlt_minimum_version
22 if not DBIx::Class::Storage::DBI->_sqlt_version_ok;
25 my $version_table_name = 'dbix_class_schema_versions';
26 my $old_table_name = 'SchemaVersions';
28 my $ddl_dir = File::Spec->catdir ('t', 'var');
30 v1 => File::Spec->catfile($ddl_dir, 'DBICVersion-Schema-1.0-MySQL.sql'),
31 v2 => File::Spec->catfile($ddl_dir, 'DBICVersion-Schema-2.0-MySQL.sql'),
32 trans => File::Spec->catfile($ddl_dir, 'DBICVersion-Schema-1.0-2.0-MySQL.sql'),
36 use DBICTest; # do not remove even though it is not used
38 use_ok('DBICVersionOrig');
40 my $schema_orig = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 1 });
41 eval { $schema_orig->storage->dbh->do('drop table ' . $version_table_name) };
42 eval { $schema_orig->storage->dbh->do('drop table ' . $old_table_name) };
44 is($schema_orig->ddl_filename('MySQL', '1.0', $ddl_dir), $fn->{v1}, 'Filename creation working');
45 unlink( $fn->{v1} ) if ( -e $fn->{v1} );
46 $schema_orig->create_ddl_dir('MySQL', undef, $ddl_dir);
48 ok(-f $fn->{v1}, 'Created DDL file');
49 $schema_orig->deploy({ add_drop_table => 1 });
51 my $tvrs = $schema_orig->{vschema}->resultset('Table');
52 is($schema_orig->_source_exists($tvrs), 1, 'Created schema from DDL file');
54 # loading a new module defining a new version of the same table
55 DBICVersion::Schema->_unregister_source ('Table');
56 eval "use DBICVersionNew";
58 my $schema_upgrade = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 1 });
63 is($schema_upgrade->get_db_version(), '1.0', 'get_db_version ok');
64 is($schema_upgrade->schema_version, '2.0', 'schema version ok');
65 $schema_upgrade->create_ddl_dir('MySQL', '2.0', $ddl_dir, '1.0');
66 ok(-f $fn->{trans}, 'Created DDL file');
70 local $SIG{__WARN__} = sub { $w = shift };
72 sleep 1; # remove this when TODO below is completed
74 $schema_upgrade->upgrade();
75 like ($w, qr/Attempting upgrade\.$/, 'Warn before upgrade');
78 is($schema_upgrade->get_db_version(), '2.0', 'db version number upgraded');
81 $schema_upgrade->storage->dbh->do('select NewVersionName from TestVersion');
83 is($@, '', 'new column created');
85 # should overwrite files and warn about it
87 local $SIG{__WARN__} = sub {
88 if ($_[0] =~ /Overwriting existing/) {
95 $schema_upgrade->create_ddl_dir('MySQL', '2.0', $ddl_dir, '1.0');
97 is (2, @w, 'A warning generated for both the DDL and the diff');
98 like ($w[0], qr/Overwriting existing DDL file - $fn->{v2}/, 'New version DDL overwrite warning');
99 like ($w[1], qr/Overwriting existing diff file - $fn->{trans}/, 'Upgrade diff overwrite warning');
103 my $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass);
105 $schema_version->storage->dbh->do('select * from ' . $version_table_name);
107 is($@, '', 'version table exists');
110 $schema_version->storage->dbh->do("DROP TABLE IF EXISTS $old_table_name");
111 $schema_version->storage->dbh->do("RENAME TABLE $version_table_name TO $old_table_name");
113 is($@, '', 'versions table renamed to old style table');
115 $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass);
116 is($schema_version->get_db_version, '2.0', 'transition from old table name to new okay');
119 $schema_version->storage->dbh->do('select * from ' . $old_table_name);
121 ok($@, 'old version table gone');
125 # check behaviour of DBIC_NO_VERSION_CHECK env var and ignore_version connect attr
127 my $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass);
129 $schema_version->storage->dbh->do("DELETE from $version_table_name");
134 local $SIG{__WARN__} = sub { $warn = shift };
135 $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass);
136 like($warn, qr/Your DB is currently unversioned/, 'warning detected without env var or attr');
141 $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 1 });
142 is($warn, '', 'warning not detected with attr set');
145 local $ENV{DBIC_NO_VERSION_CHECK} = 1;
147 $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass);
148 is($warn, '', 'warning not detected with env var set');
152 $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 0 });
153 like($warn, qr/Your DB is currently unversioned/, 'warning detected without env var or attr');
157 # attempt a deploy/upgrade cycle within one second
160 local $TODO = 'To fix this properly the table must be extended with an autoinc column, mst will not accept anything less';
162 eval { $schema_orig->storage->dbh->do('drop table ' . $version_table_name) };
163 eval { $schema_orig->storage->dbh->do('drop table ' . $old_table_name) };
164 eval { $schema_orig->storage->dbh->do('drop table TestVersion') };
166 # this attempts to sleep until the turn of the second
168 sleep (int ($t) + 1 - $t);
169 diag ('Fast deploy/upgrade start: ', time() );
172 local $DBICVersion::Schema::VERSION = '1.0';
173 $schema_orig->deploy;
176 local $SIG{__WARN__} = sub { warn if $_[0] !~ /Attempting upgrade\.$/ };
177 $schema_upgrade->upgrade();
179 is($schema_upgrade->get_db_version(), '2.0', 'Fast deploy/upgrade');
182 unless ($ENV{DBICTEST_KEEP_VERSIONING_DDL}) {
183 unlink $_ for (values %$fn);