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