This code belogs in Storage::DBI
[dbsrgits/DBIx-Class-Historic.git] / t / 94versioning.t
CommitLineData
c9d2e0a2 1#!/usr/bin/perl
7f6f5b69 2
c9d2e0a2 3use strict;
4use warnings;
5use Test::More;
86ef9f0d 6use File::Spec;
f925f7cb 7use File::Copy;
1475105d 8use Time::HiRes qw/time sleep/;
c9d2e0a2 9
1d48fcff 10#warn "$dsn $user $pass";
11my ($dsn, $user, $pass);
12
c9d2e0a2 13BEGIN {
1d48fcff 14 ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MYSQL_${_}" } qw/DSN USER PASS/};
15
16 plan skip_all => 'Set $ENV{DBICTEST_MYSQL_DSN}, _USER and _PASS to run this test'
8424c090 17 unless ($dsn);
1d48fcff 18
b2b2e7fd 19 require DBIx::Class::Storage::DBI;
7f6f5b69 20 plan skip_all =>
b2b2e7fd 21 'Test needs SQL::Translator ' . DBIx::Class::Storage::DBI->_sqlt_minimum_version
22 if not DBIx::Class::Storage::DBI->_sqlt_version_ok;
c9d2e0a2 23}
24
b4b1e91c 25my $version_table_name = 'dbix_class_schema_versions';
26my $old_table_name = 'SchemaVersions';
27
1475105d 28my $ddl_dir = File::Spec->catdir ('t', 'var');
29my $fn = {
86456031 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'),
1475105d 33};
34
c9d2e0a2 35use lib qw(t/lib);
d8190011 36use DBICTest; # do not remove even though it is not used
37
86456031 38use_ok('DBICVersionOrig');
c9d2e0a2 39
99a74c4a 40my $schema_orig = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 1 });
b4b1e91c 41eval { $schema_orig->storage->dbh->do('drop table ' . $version_table_name) };
42eval { $schema_orig->storage->dbh->do('drop table ' . $old_table_name) };
c9d2e0a2 43
86456031 44is($schema_orig->ddl_filename('MySQL', '1.0', $ddl_dir), $fn->{v1}, 'Filename creation working');
1475105d 45unlink( $fn->{v1} ) if ( -e $fn->{v1} );
86456031 46$schema_orig->create_ddl_dir('MySQL', undef, $ddl_dir);
c9d2e0a2 47
1475105d 48ok(-f $fn->{v1}, 'Created DDL file');
1d48fcff 49$schema_orig->deploy({ add_drop_table => 1 });
c9d2e0a2 50
1d48fcff 51my $tvrs = $schema_orig->{vschema}->resultset('Table');
a2800991 52is($schema_orig->_source_exists($tvrs), 1, 'Created schema from DDL file');
c9d2e0a2 53
1475105d 54# loading a new module defining a new version of the same table
55DBICVersion::Schema->_unregister_source ('Table');
c9d2e0a2 56eval "use DBICVersionNew";
1475105d 57
58my $schema_upgrade = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 1 });
f925f7cb 59{
1475105d 60 unlink($fn->{v2});
61 unlink($fn->{trans});
f925f7cb 62
f925f7cb 63 is($schema_upgrade->get_db_version(), '1.0', 'get_db_version ok');
1d48fcff 64 is($schema_upgrade->schema_version, '2.0', 'schema version ok');
86456031 65 $schema_upgrade->create_ddl_dir('MySQL', '2.0', $ddl_dir, '1.0');
1475105d 66 ok(-f $fn->{trans}, 'Created DDL file');
67
68 {
69 my $w;
70 local $SIG{__WARN__} = sub { $w = shift };
e9fbbbf4 71
72 sleep 1; # remove this when TODO below is completed
73
1475105d 74 $schema_upgrade->upgrade();
86456031 75 like ($w, qr/Attempting upgrade\.$/, 'Warn before upgrade');
1475105d 76 }
77
1d48fcff 78 is($schema_upgrade->get_db_version(), '2.0', 'db version number upgraded');
f925f7cb 79
80 eval {
f925f7cb 81 $schema_upgrade->storage->dbh->do('select NewVersionName from TestVersion');
82 };
83 is($@, '', 'new column created');
99a74c4a 84
1475105d 85 # should overwrite files and warn about it
86 my @w;
7a063741 87 local $SIG{__WARN__} = sub {
341d5ede 88 if ($_[0] =~ /Overwriting existing/) {
7a063741 89 push @w, $_[0];
90 }
91 else {
92 warn @_;
93 }
94 };
86456031 95 $schema_upgrade->create_ddl_dir('MySQL', '2.0', $ddl_dir, '1.0');
1475105d 96
97 is (2, @w, 'A warning generated for both the DDL and the diff');
341d5ede 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');
f925f7cb 100}
b4b1e91c 101
102{
103 my $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass);
104 eval {
105 $schema_version->storage->dbh->do('select * from ' . $version_table_name);
106 };
107 is($@, '', 'version table exists');
108
109 eval {
110 $schema_version->storage->dbh->do("DROP TABLE IF EXISTS $old_table_name");
86456031 111 $schema_version->storage->dbh->do("RENAME TABLE $version_table_name TO $old_table_name");
b4b1e91c 112 };
113 is($@, '', 'versions table renamed to old style table');
114
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');
117
118 eval {
119 $schema_version->storage->dbh->do('select * from ' . $old_table_name);
120 };
121 ok($@, 'old version table gone');
122
123}
f81b9157 124
125# check behaviour of DBIC_NO_VERSION_CHECK env var and ignore_version connect attr
126{
127 my $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass);
128 eval {
d0dfedc2 129 $schema_version->storage->dbh->do("DELETE from $version_table_name");
f81b9157 130 };
131
d0dfedc2 132
133 my $warn = '';
fc38383e 134 local $SIG{__WARN__} = sub { $warn = shift };
f81b9157 135 $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass);
d0dfedc2 136 like($warn, qr/Your DB is currently unversioned/, 'warning detected without env var or attr');
f81b9157 137
d0dfedc2 138
139 # should warn
140 $warn = '';
f81b9157 141 $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 1 });
d0dfedc2 142 is($warn, '', 'warning not detected with attr set');
f81b9157 143 # should not warn
144
1475105d 145 local $ENV{DBIC_NO_VERSION_CHECK} = 1;
d0dfedc2 146 $warn = '';
f81b9157 147 $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass);
d0dfedc2 148 is($warn, '', 'warning not detected with env var set');
f81b9157 149 # should not warn
150
d0dfedc2 151 $warn = '';
f81b9157 152 $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 0 });
d0dfedc2 153 like($warn, qr/Your DB is currently unversioned/, 'warning detected without env var or attr');
f81b9157 154 # should warn
155}
1475105d 156
157# attempt a deploy/upgrade cycle within one second
e9fbbbf4 158TODO: {
159
160 local $TODO = 'To fix this properly the table must be extended with an autoinc column, mst will not accept anything less';
161
1475105d 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') };
165
166 # this attempts to sleep until the turn of the second
167 my $t = time();
168 sleep (int ($t) + 1 - $t);
169 diag ('Fast deploy/upgrade start: ', time() );
170
171 {
172 local $DBICVersion::Schema::VERSION = '1.0';
173 $schema_orig->deploy;
174 }
1475105d 175
e9fbbbf4 176 local $SIG{__WARN__} = sub { warn if $_[0] !~ /Attempting upgrade\.$/ };
1475105d 177 $schema_upgrade->upgrade();
1475105d 178
179 is($schema_upgrade->get_db_version(), '2.0', 'Fast deploy/upgrade');
e9fbbbf4 180};
1475105d 181
182unless ($ENV{DBICTEST_KEEP_VERSIONING_DDL}) {
183 unlink $_ for (values %$fn);
184}
7f6f5b69 185
186done_testing;