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