12 #warn "$dsn $user $pass";
13 my ($dsn, $user, $pass);
16 ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MYSQL_${_}" } qw/DSN USER PASS/};
18 plan skip_all => 'Set $ENV{DBICTEST_MYSQL_DSN}, _USER and _PASS to run this test'
21 eval { require Time::HiRes }
22 || plan skip_all => 'Test needs Time::HiRes';
23 Time::HiRes->import(qw/time sleep/);
25 require DBIx::Class::Storage::DBI;
27 'Test needs SQL::Translator ' . DBIx::Class::Storage::DBI->_sqlt_minimum_version
28 if not DBIx::Class::Storage::DBI->_sqlt_version_ok;
31 my $version_table_name = 'dbix_class_schema_versions';
32 my $old_table_name = 'SchemaVersions';
34 my $ddl_dir = dir ('t', 'var');
36 v1 => $ddl_dir->file ('DBICVersion-Schema-1.0-MySQL.sql'),
37 v2 => $ddl_dir->file ('DBICVersion-Schema-2.0-MySQL.sql'),
38 trans => $ddl_dir->file ('DBICVersion-Schema-1.0-2.0-MySQL.sql'),
42 use DBICTest; # do not remove even though it is not used
44 use_ok('DBICVersionOrig');
46 my $schema_orig = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 1 });
47 eval { $schema_orig->storage->dbh->do('drop table ' . $version_table_name) };
48 eval { $schema_orig->storage->dbh->do('drop table ' . $old_table_name) };
50 is($schema_orig->ddl_filename('MySQL', '1.0', $ddl_dir), $fn->{v1}, 'Filename creation working');
51 unlink( $fn->{v1} ) if ( -e $fn->{v1} );
52 $schema_orig->create_ddl_dir('MySQL', undef, $ddl_dir);
54 ok(-f $fn->{v1}, 'Created DDL file');
55 $schema_orig->deploy({ add_drop_table => 1 });
57 my $tvrs = $schema_orig->{vschema}->resultset('Table');
58 is($schema_orig->_source_exists($tvrs), 1, 'Created schema from DDL file');
60 # loading a new module defining a new version of the same table
61 DBICVersion::Schema->_unregister_source ('Table');
62 eval "use DBICVersionNew";
64 my $schema_upgrade = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 1 });
69 is($schema_upgrade->get_db_version(), '1.0', 'get_db_version ok');
70 is($schema_upgrade->schema_version, '2.0', 'schema version ok');
71 $schema_upgrade->create_ddl_dir('MySQL', '2.0', $ddl_dir, '1.0');
72 ok(-f $fn->{trans}, 'Created DDL file');
74 sleep 1; # remove this when TODO below is completed
76 sub { $schema_upgrade->upgrade() },
77 qr/DB version .+? is lower than the schema version/,
78 'Warn before upgrade',
81 is($schema_upgrade->get_db_version(), '2.0', 'db version number upgraded');
84 $schema_upgrade->storage->dbh->do('select NewVersionName from TestVersion');
85 }, 'new column created' );
88 sub { $schema_upgrade->create_ddl_dir('MySQL', '2.0', $ddl_dir, '1.0') },
90 qr/Overwriting existing DDL file - $fn->{v2}/,
91 qr/Overwriting existing diff file - $fn->{trans}/,
93 'An overwrite warning generated for both the DDL and the diff',
98 my $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass);
100 $schema_version->storage->dbh->do('select * from ' . $version_table_name);
101 }, 'version table exists');
104 $schema_version->storage->dbh->do("DROP TABLE IF EXISTS $old_table_name");
105 $schema_version->storage->dbh->do("RENAME TABLE $version_table_name TO $old_table_name");
106 }, 'versions table renamed to old style table');
108 $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass);
109 is($schema_version->get_db_version, '2.0', 'transition from old table name to new okay');
112 $schema_version->storage->dbh->do('select * from ' . $old_table_name);
113 }, 'old version table gone');
117 # check behaviour of DBIC_NO_VERSION_CHECK env var and ignore_version connect attr
119 my $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass);
121 $schema_version->storage->dbh->do("DELETE from $version_table_name");
125 warnings_like ( sub {
126 $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass);
127 }, qr/Your DB is currently unversioned/, 'warning detected without env var or attr' );
129 warnings_like ( sub {
130 $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 1 });
131 }, [], 'warning not detected with attr set');
134 local $ENV{DBIC_NO_VERSION_CHECK} = 1;
135 warnings_like ( sub {
136 $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass);
137 }, [], 'warning not detected with env var set');
139 warnings_like ( sub {
140 $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 0 });
141 }, qr/Your DB is currently unversioned/, 'warning detected without env var or attr');
144 # attempt a deploy/upgrade cycle within one second
147 local $TODO = 'To fix this properly the table must be extended with an autoinc column, mst will not accept anything less';
149 eval { $schema_orig->storage->dbh->do('drop table ' . $version_table_name) };
150 eval { $schema_orig->storage->dbh->do('drop table ' . $old_table_name) };
151 eval { $schema_orig->storage->dbh->do('drop table TestVersion') };
153 # this attempts to sleep until the turn of the second
155 sleep (int ($t) + 1 - $t);
156 diag ('Fast deploy/upgrade start: ', time() );
159 local $DBICVersion::Schema::VERSION = '1.0';
160 $schema_orig->deploy;
163 local $SIG{__WARN__} = sub { warn if $_[0] !~ /Attempting upgrade\.$/ };
164 $schema_upgrade->upgrade();
166 is($schema_upgrade->get_db_version(), '2.0', 'Fast deploy/upgrade');
169 unless ($ENV{DBICTEST_KEEP_VERSIONING_DDL}) {
170 unlink $_ for (values %$fn);