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