Warn-free test suite with later DBIC
[dbsrgits/DBIx-Class-DeploymentHandler.git] / t / deploy_methods / sql_translator.t
CommitLineData
02d58ac0 1#!perl
2
cbbd1b5f 3use strict;
4use warnings;
5
02d58ac0 6use Test::More;
7use Test::Exception;
8
9use lib 't/lib';
10use DBICDHTest;
459a67e3 11use aliased 'DBIx::Class::DeploymentHandler::DeployMethod::SQL::Translator';
3b98a3a1 12use File::Spec::Functions;
d50f2521 13use File::Path qw(rmtree mkpath);
459a67e3 14
f3b5161e 15my $dbh = DBICDHTest::dbh();
624e3018 16my @connection = (sub { $dbh }, { ignore_version => 1 });
459a67e3 17my $sql_dir = 't/sql';
18
19DBICDHTest::ready;
25c3bec3 20unlink 'stuffthatran';
21
22for (qw(initialize upgrade downgrade deploy)) {
23 mkpath(catfile(qw( t sql _common), $_, '_any' ));
24 open my $fh, '>',
25 catfile(qw( t sql _common), $_, qw(_any 000-win.pl ));
26 print {$fh} 'sub {open my $fh, ">>", "stuffthatran"; use Data::Dumper::Concise; print {$fh} join(",", @{$_[1]||[]}) . "\n"; }';
27 close $fh;
28}
29
30for (qw(initialize upgrade downgrade deploy)) {
31 mkpath(catfile(qw( t sql SQLite), $_, '_any' ));
32 open my $fh, '>',
33 catfile(qw( t sql SQLite), $_, qw(_any 000-win2.pl ));
34 print {$fh} 'sub {open my $fh, ">>", "stuffthatran"; use Data::Dumper::Concise; print {$fh} join(",", @{$_[1]||[]}) . "\n"; }';
35 close $fh;
36}
459a67e3 37
38VERSION1: {
39 use_ok 'DBICVersion_v1';
40 my $s = DBICVersion::Schema->connect(@connection);
41 my $dm = Translator->new({
3b98a3a1 42 schema => $s,
91adde75 43 script_directory => $sql_dir,
3b98a3a1 44 databases => ['SQLite'],
02a7b8ac 45 sql_translator_args => { add_drop_table => 0 },
459a67e3 46 });
47
48 ok( $dm, 'DBIC::DH::DM::SQL::Translator gets instantiated correctly' );
3b98a3a1 49
91557c90 50 $dm->prepare_deploy;
fc4b7602 51
ff40cb1f 52 mkpath(catfile(qw( t sql SQLite initialize 1.0 )));
fc4b7602 53 open my $prerun, '>',
ff40cb1f 54 catfile(qw( t sql SQLite initialize 1.0 003-semiautomatic.pl ));
5b5defbc 55 print {$prerun} "sub {use File::Touch; touch(q(foobar));}";
fc4b7602 56 close $prerun;
ff40cb1f 57 $dm->initialize({ version => '1.0' });
fc4b7602 58
25c3bec3 59 ok -e 'foobar', 'code got run in preinit';
fc4b7602 60
92624ee5 61 dies_ok {$dm->prepare_deploy} 'prepare_deploy dies if you run it twice' ;
3b98a3a1 62
63 ok(
58eb99c3 64 -f catfile(qw( t sql SQLite deploy 1.0 001-auto.sql )),
3b98a3a1 65 '1.0 schema gets generated properly'
66 );
67
68 dies_ok {
69 $s->resultset('Foo')->create({
70 bar => 'frew',
71 })
72 } 'schema not deployed';
73
026eaf0c 74 $dm->deploy;
3b98a3a1 75
76 lives_ok {
77 $s->resultset('Foo')->create({
78 bar => 'frew',
79 })
80 } 'schema is deployed';
81}
82
83VERSION2: {
84 use_ok 'DBICVersion_v2';
85 my $s = DBICVersion::Schema->connect(@connection);
86 my $dm = Translator->new({
87 schema => $s,
91adde75 88 script_directory => $sql_dir,
3b98a3a1 89 databases => ['SQLite'],
02a7b8ac 90 sql_translator_args => { add_drop_table => 0 },
91557c90 91 txn_wrap => 1,
3b98a3a1 92 });
93
94 ok( $dm, 'DBIC::DH::SQL::Translator w/2.0 instantiates correctly');
95
cbbd1b5f 96 my $version = $s->schema_version();
91557c90 97 $dm->prepare_deploy;
3b98a3a1 98 ok(
58eb99c3 99 -f catfile(qw( t sql SQLite deploy 2.0 001-auto.sql )),
3b98a3a1 100 '2.0 schema gets generated properly'
101 );
58eb99c3 102 mkpath(catfile(qw( t sql SQLite upgrade 1.0-2.0 )));
be140a5f 103 $dm->prepare_upgrade({
104 from_version => '1.0',
105 to_version => '2.0',
106 version_set => [qw(1.0 2.0)]
107 });
d50f2521 108
109 {
110 my $warned = 0;
111 local $SIG{__WARN__} = sub{$warned = 1};
be140a5f 112 $dm->prepare_upgrade({
113 from_version => '0.0',
114 to_version => '1.0',
115 version_set => [qw(0.0 1.0)]
116 });
d50f2521 117 ok( $warned, 'prepare_upgrade with a bogus preversion warns' );
118 }
3b98a3a1 119 ok(
58eb99c3 120 -f catfile(qw( t sql SQLite upgrade 1.0-2.0 001-auto.sql )),
d50f2521 121 '1.0-2.0 diff gets generated properly and default start and end versions get set'
3b98a3a1 122 );
58eb99c3 123 mkpath(catfile(qw( t sql SQLite downgrade 2.0-1.0 )));
be140a5f 124 $dm->prepare_downgrade({
125 from_version => $version,
126 to_version => '1.0',
127 version_set => [$version, '1.0']
128 });
3b98a3a1 129 ok(
58eb99c3 130 -f catfile(qw( t sql SQLite downgrade 2.0-1.0 001-auto.sql )),
d50f2521 131 '2.0-1.0 diff gets generated properly'
3b98a3a1 132 );
133 dies_ok {
134 $s->resultset('Foo')->create({
135 bar => 'frew',
136 baz => 'frew',
137 })
138 } 'schema not deployed';
139 dies_ok {
140 $s->resultset('Foo')->create({
141 bar => 'frew',
142 baz => 'frew',
143 })
144 } 'schema not uppgrayyed';
d50f2521 145
58eb99c3 146 mkpath catfile(qw( t sql _common upgrade 1.0-2.0 ));
d50f2521 147 open my $common, '>',
58eb99c3 148 catfile(qw( t sql _common upgrade 1.0-2.0 002-semiautomatic.sql ));
d50f2521 149 print {$common} qq<INSERT INTO Foo (bar, baz) VALUES ("hello", "world");\n\n>;
150 close $common;
151
0841a743 152 open my $common_pl, '>',
58eb99c3 153 catfile(qw( t sql _common upgrade 1.0-2.0 003-semiautomatic.pl ));
0841a743 154 print {$common_pl} q|
5b5defbc 155 sub {
91557c90 156 my $schema = shift;
157 $schema->resultset('Foo')->create({
158 bar => 'goodbye',
159 baz => 'blue skies',
160 })
161 }
162 |;
0841a743 163 close $common_pl;
164
be140a5f 165 $dm->upgrade_single_step({ version_set => [qw( 1.0 2.0 )] });
d50f2521 166 is( $s->resultset('Foo')->search({
167 bar => 'hello',
168 baz => 'world',
169 })->count, 1, '_common migration got run');
0841a743 170 is( $s->resultset('Foo')->search({
171 bar => 'goodbye',
172 #baz => 'blue skies',
173 })->count, 1, '_common perl migration got run');
3b98a3a1 174 lives_ok {
175 $s->resultset('Foo')->create({
176 bar => 'frew',
177 baz => 'frew',
178 })
179 } 'schema is deployed';
be140a5f 180 $dm->downgrade_single_step({ version_set => [qw( 2.0 1.0 )] });
d50f2521 181 dies_ok {
182 $s->resultset('Foo')->create({
183 bar => 'frew',
184 baz => 'frew',
185 })
fc4b7602 186 } 'schema is downgrayyed';
be140a5f 187 $dm->upgrade_single_step({ version_set => [qw( 1.0 2.0 )] });
459a67e3 188}
02d58ac0 189
3b98a3a1 190VERSION3: {
191 use_ok 'DBICVersion_v3';
192 my $s = DBICVersion::Schema->connect(@connection);
193 my $dm = Translator->new({
194 schema => $s,
91adde75 195 script_directory => $sql_dir,
3b98a3a1 196 databases => ['SQLite'],
02a7b8ac 197 sql_translator_args => { add_drop_table => 0 },
d50f2521 198 txn_wrap => 0,
3b98a3a1 199 });
200
201 ok( $dm, 'DBIC::DH::SQL::Translator w/3.0 instantiates correctly');
202
cbbd1b5f 203 my $version = $s->schema_version();
91557c90 204 $dm->prepare_deploy;
3b98a3a1 205 ok(
58eb99c3 206 -f catfile(qw( t sql SQLite deploy 3.0 001-auto.sql )),
3b98a3a1 207 '2.0 schema gets generated properly'
208 );
be140a5f 209 $dm->prepare_downgrade({
210 from_version => $version,
211 to_version => '1.0',
212 version_set => [$version, '1.0']
213 });
3b98a3a1 214 ok(
58eb99c3 215 -f catfile(qw( t sql SQLite downgrade 3.0-1.0 001-auto.sql )),
d50f2521 216 '3.0-1.0 diff gets generated properly'
217 );
be140a5f 218 $dm->prepare_upgrade({
219 from_version => '1.0',
220 to_version => $version,
221 version_set => ['1.0', $version]
222 });
d50f2521 223 ok(
58eb99c3 224 -f catfile(qw( t sql SQLite upgrade 1.0-3.0 001-auto.sql )),
3b98a3a1 225 '1.0-3.0 diff gets generated properly'
226 );
be140a5f 227 $dm->prepare_upgrade({
228 from_version => '2.0',
229 to_version => $version,
230 version_set => ['2.0', $version]
231 });
92624ee5 232 dies_ok {
be140a5f 233 $dm->prepare_upgrade({
234 from_version => '2.0',
235 to_version => $version,
236 version_set => ['2.0', $version]
237 });
92624ee5 238 }
239 'prepare_upgrade dies if you clobber an existing upgrade file' ;
3b98a3a1 240 ok(
58eb99c3 241 -f catfile(qw( t sql SQLite upgrade 1.0-2.0 001-auto.sql )),
3b98a3a1 242 '2.0-3.0 diff gets generated properly'
243 );
244 dies_ok {
245 $s->resultset('Foo')->create({
246 bar => 'frew',
247 baz => 'frew',
248 biff => 'frew',
249 })
250 } 'schema not deployed';
be140a5f 251 $dm->upgrade_single_step({ version_set => [qw( 2.0 3.0 )] });
3b98a3a1 252 lives_ok {
253 $s->resultset('Foo')->create({
254 bar => 'frew',
255 baz => 'frew',
256 biff => 'frew',
257 })
5b766a24 258 } 'schema is deployed';
d50f2521 259 dies_ok {
be140a5f 260 $dm->upgrade_single_step({ version_set => [qw( 2.0 3.0 )] });
d50f2521 261 } 'dies when sql dir does not exist';
3b98a3a1 262}
25c3bec3 263
264my $stuff_that_ran = do { local( @ARGV, $/ ) = 'stuffthatran'; <> };
265is $stuff_that_ran,
266'
267
2681.0
2691.0
2701.0,2.0
2711.0,2.0
2722.0,1.0
2732.0,1.0
2741.0,2.0
2751.0,2.0
2762.0,3.0
2772.0,3.0
2782.0,3.0
2792.0,3.0
280', '_any got ran the right amount of times with the right args';
281
02d58ac0 282done_testing;
d50f2521 283#vim: ts=2 sw=2 expandtab