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