8 #warn "$dsn $user $pass";
9 my ($dsn, $user, $pass);
12 ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MYSQL_${_}" } qw/DSN USER PASS/};
14 plan skip_all => 'Set $ENV{DBICTEST_MYSQL_DSN}, _USER and _PASS to run this test'
18 eval "use DBD::mysql; use SQL::Translator 0.09;";
20 ? ( skip_all => 'needs DBD::mysql and SQL::Translator 0.09 for testing' )
24 my $version_table_name = 'dbix_class_schema_versions';
25 my $old_table_name = 'SchemaVersions';
28 use_ok('DBICVersionOrig');
30 my $schema_orig = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 1 });
31 eval { $schema_orig->storage->dbh->do('drop table ' . $version_table_name) };
32 eval { $schema_orig->storage->dbh->do('drop table ' . $old_table_name) };
34 is($schema_orig->ddl_filename('MySQL', '1.0', 't/var'), File::Spec->catfile('t', 'var', 'DBICVersion-Schema-1.0-MySQL.sql'), 'Filename creation working');
35 unlink('t/var/DBICVersion-Schema-1.0-MySQL.sql') if (-e 't/var/DBICVersion-Schema-1.0-MySQL.sql');
36 $schema_orig->create_ddl_dir('MySQL', undef, 't/var');
38 ok(-f 't/var/DBICVersion-Schema-1.0-MySQL.sql', 'Created DDL file');
39 $schema_orig->deploy({ add_drop_table => 1 });
41 my $tvrs = $schema_orig->{vschema}->resultset('Table');
42 is($schema_orig->_source_exists($tvrs), 1, 'Created schema from DDL file');
44 eval "use DBICVersionNew";
46 unlink('t/var/DBICVersion-Schema-2.0-MySQL.sql');
47 unlink('t/var/DBICVersion-Schema-1.0-2.0-MySQL.sql');
49 my $schema_upgrade = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 1 });
50 is($schema_upgrade->get_db_version(), '1.0', 'get_db_version ok');
51 is($schema_upgrade->schema_version, '2.0', 'schema version ok');
52 $schema_upgrade->create_ddl_dir('MySQL', '2.0', 't/var', '1.0');
53 ok(-f 't/var/DBICVersion-Schema-1.0-2.0-MySQL.sql', 'Created DDL file');
54 $schema_upgrade->upgrade();
55 is($schema_upgrade->get_db_version(), '2.0', 'db version number upgraded');
58 $schema_upgrade->storage->dbh->do('select NewVersionName from TestVersion');
60 is($@, '', 'new column created');
62 # should overwrite files
63 $schema_upgrade->create_ddl_dir('MySQL', '2.0', 't/var', '1.0');
67 my $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass);
69 $schema_version->storage->dbh->do('select * from ' . $version_table_name);
71 is($@, '', 'version table exists');
74 $schema_version->storage->dbh->do("DROP TABLE IF EXISTS $old_table_name");
75 $schema_version->storage->dbh->do("RENAME TABLE $version_table_name TO $old_table_name");
77 is($@, '', 'versions table renamed to old style table');
79 $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass);
80 is($schema_version->get_db_version, '2.0', 'transition from old table name to new okay');
83 $schema_version->storage->dbh->do('select * from ' . $old_table_name);
85 ok($@, 'old version table gone');
89 # check behaviour of DBIC_NO_VERSION_CHECK env var and ignore_version connect attr
91 my $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass);
93 $schema_version->storage->dbh->do("DELETE from $version_table_name");
98 $SIG{__WARN__} = sub { $warn = shift };
99 $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass);
100 like($warn, qr/Your DB is currently unversioned/, 'warning detected without env var or attr');
105 $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 1 });
106 is($warn, '', 'warning not detected with attr set');
109 $ENV{DBIC_NO_VERSION_CHECK} = 1;
111 $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass);
112 is($warn, '', 'warning not detected with env var set');
116 $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 0 });
117 like($warn, qr/Your DB is currently unversioned/, 'warning detected without env var or attr');