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