Commit | Line | Data |
c9d2e0a2 |
1 | #!/usr/bin/perl |
7f6f5b69 |
2 | |
c9d2e0a2 |
3 | use strict; |
4 | use warnings; |
5 | use Test::More; |
5dfa3524 |
6 | use Test::Warn; |
7 | use Test::Exception; |
8 | |
9 | use Path::Class; |
f925f7cb |
10 | use File::Copy; |
c9d2e0a2 |
11 | |
1d48fcff |
12 | #warn "$dsn $user $pass"; |
13 | my ($dsn, $user, $pass); |
14 | |
c9d2e0a2 |
15 | BEGIN { |
1d48fcff |
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' |
8424c090 |
19 | unless ($dsn); |
1d48fcff |
20 | |
21d70f0b |
21 | eval { require Time::HiRes } |
22 | || plan skip_all => 'Test needs Time::HiRes'; |
23 | Time::HiRes->import(qw/time sleep/); |
24 | |
b2b2e7fd |
25 | require DBIx::Class::Storage::DBI; |
7f6f5b69 |
26 | plan skip_all => |
b2b2e7fd |
27 | 'Test needs SQL::Translator ' . DBIx::Class::Storage::DBI->_sqlt_minimum_version |
28 | if not DBIx::Class::Storage::DBI->_sqlt_version_ok; |
c9d2e0a2 |
29 | } |
30 | |
b4b1e91c |
31 | my $version_table_name = 'dbix_class_schema_versions'; |
32 | my $old_table_name = 'SchemaVersions'; |
33 | |
5dfa3524 |
34 | my $ddl_dir = dir ('t', 'var'); |
1475105d |
35 | my $fn = { |
5dfa3524 |
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'), |
1475105d |
39 | }; |
40 | |
c9d2e0a2 |
41 | use lib qw(t/lib); |
d8190011 |
42 | use DBICTest; # do not remove even though it is not used |
43 | |
86456031 |
44 | use_ok('DBICVersionOrig'); |
c9d2e0a2 |
45 | |
99a74c4a |
46 | my $schema_orig = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 1 }); |
b4b1e91c |
47 | eval { $schema_orig->storage->dbh->do('drop table ' . $version_table_name) }; |
48 | eval { $schema_orig->storage->dbh->do('drop table ' . $old_table_name) }; |
c9d2e0a2 |
49 | |
86456031 |
50 | is($schema_orig->ddl_filename('MySQL', '1.0', $ddl_dir), $fn->{v1}, 'Filename creation working'); |
1475105d |
51 | unlink( $fn->{v1} ) if ( -e $fn->{v1} ); |
86456031 |
52 | $schema_orig->create_ddl_dir('MySQL', undef, $ddl_dir); |
c9d2e0a2 |
53 | |
1475105d |
54 | ok(-f $fn->{v1}, 'Created DDL file'); |
1d48fcff |
55 | $schema_orig->deploy({ add_drop_table => 1 }); |
c9d2e0a2 |
56 | |
1d48fcff |
57 | my $tvrs = $schema_orig->{vschema}->resultset('Table'); |
a2800991 |
58 | is($schema_orig->_source_exists($tvrs), 1, 'Created schema from DDL file'); |
c9d2e0a2 |
59 | |
1475105d |
60 | # loading a new module defining a new version of the same table |
61 | DBICVersion::Schema->_unregister_source ('Table'); |
c9d2e0a2 |
62 | eval "use DBICVersionNew"; |
1475105d |
63 | |
64 | my $schema_upgrade = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 1 }); |
f925f7cb |
65 | { |
1475105d |
66 | unlink($fn->{v2}); |
67 | unlink($fn->{trans}); |
f925f7cb |
68 | |
f925f7cb |
69 | is($schema_upgrade->get_db_version(), '1.0', 'get_db_version ok'); |
1d48fcff |
70 | is($schema_upgrade->schema_version, '2.0', 'schema version ok'); |
86456031 |
71 | $schema_upgrade->create_ddl_dir('MySQL', '2.0', $ddl_dir, '1.0'); |
1475105d |
72 | ok(-f $fn->{trans}, 'Created DDL file'); |
73 | |
5dfa3524 |
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 | ); |
1475105d |
80 | |
1d48fcff |
81 | is($schema_upgrade->get_db_version(), '2.0', 'db version number upgraded'); |
f925f7cb |
82 | |
5dfa3524 |
83 | lives_ok ( sub { |
f925f7cb |
84 | $schema_upgrade->storage->dbh->do('select NewVersionName from TestVersion'); |
5dfa3524 |
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 | ); |
f925f7cb |
95 | } |
b4b1e91c |
96 | |
97 | { |
98 | my $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass); |
5dfa3524 |
99 | lives_ok (sub { |
b4b1e91c |
100 | $schema_version->storage->dbh->do('select * from ' . $version_table_name); |
5dfa3524 |
101 | }, 'version table exists'); |
b4b1e91c |
102 | |
5dfa3524 |
103 | lives_ok (sub { |
b4b1e91c |
104 | $schema_version->storage->dbh->do("DROP TABLE IF EXISTS $old_table_name"); |
86456031 |
105 | $schema_version->storage->dbh->do("RENAME TABLE $version_table_name TO $old_table_name"); |
5dfa3524 |
106 | }, 'versions table renamed to old style table'); |
b4b1e91c |
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 | |
5dfa3524 |
111 | dies_ok (sub { |
b4b1e91c |
112 | $schema_version->storage->dbh->do('select * from ' . $old_table_name); |
5dfa3524 |
113 | }, 'old version table gone'); |
b4b1e91c |
114 | |
115 | } |
f81b9157 |
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 { |
d0dfedc2 |
121 | $schema_version->storage->dbh->do("DELETE from $version_table_name"); |
f81b9157 |
122 | }; |
123 | |
d0dfedc2 |
124 | |
5dfa3524 |
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' ); |
f81b9157 |
128 | |
5dfa3524 |
129 | warnings_like ( sub { |
130 | $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 1 }); |
131 | }, [], 'warning not detected with attr set'); |
d0dfedc2 |
132 | |
f81b9157 |
133 | |
1475105d |
134 | local $ENV{DBIC_NO_VERSION_CHECK} = 1; |
5dfa3524 |
135 | warnings_like ( sub { |
136 | $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass); |
137 | }, [], 'warning not detected with env var set'); |
f81b9157 |
138 | |
5dfa3524 |
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'); |
f81b9157 |
142 | } |
1475105d |
143 | |
144 | # attempt a deploy/upgrade cycle within one second |
e9fbbbf4 |
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 | |
1475105d |
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 | } |
1475105d |
162 | |
e9fbbbf4 |
163 | local $SIG{__WARN__} = sub { warn if $_[0] !~ /Attempting upgrade\.$/ }; |
1475105d |
164 | $schema_upgrade->upgrade(); |
1475105d |
165 | |
166 | is($schema_upgrade->get_db_version(), '2.0', 'Fast deploy/upgrade'); |
e9fbbbf4 |
167 | }; |
1475105d |
168 | |
169 | unless ($ENV{DBICTEST_KEEP_VERSIONING_DDL}) { |
170 | unlink $_ for (values %$fn); |
171 | } |
7f6f5b69 |
172 | |
173 | done_testing; |