Use serialized sql instead of vanilla sql
[dbsrgits/DBIx-Class-DeploymentHandler.git] / t / deploy_methods / sql_translator.t
CommitLineData
02d58ac0 1#!perl
2
3use Test::More;
4use Test::Exception;
5
6use lib 't/lib';
7use DBICDHTest;
459a67e3 8use aliased 'DBIx::Class::DeploymentHandler::DeployMethod::SQL::Translator';
3b98a3a1 9use File::Spec::Functions;
d50f2521 10use File::Path qw(rmtree mkpath);
459a67e3 11
12my $db = 'dbi:SQLite:db.db';
13my @connection = ($db, '', '', { ignore_version => 1 });
14my $sql_dir = 't/sql';
15
16DBICDHTest::ready;
17
18VERSION1: {
19 use_ok 'DBICVersion_v1';
20 my $s = DBICVersion::Schema->connect(@connection);
21 my $dm = Translator->new({
3b98a3a1 22 schema => $s,
91adde75 23 script_directory => $sql_dir,
3b98a3a1 24 databases => ['SQLite'],
02a7b8ac 25 sql_translator_args => { add_drop_table => 0 },
459a67e3 26 });
27
28 ok( $dm, 'DBIC::DH::DM::SQL::Translator gets instantiated correctly' );
3b98a3a1 29
91557c90 30 $dm->prepare_deploy;
fc4b7602 31
32 mkpath(catfile(qw( t sql SQLite preinstall 1.0 )));
33 open my $prerun, '>',
34 catfile(qw( t sql SQLite preinstall 1.0 003-semiautomatic.pl ));
5b5defbc 35 print {$prerun} "sub {use File::Touch; touch(q(foobar));}";
fc4b7602 36 close $prerun;
be140a5f 37 $dm->preinstall({ version => '1.0' });
fc4b7602 38
1f0d0633 39 ok -e 'foobar', 'perl migration runs';
fc4b7602 40
d50f2521 41 {
42 my $warned = 0;
43 local $SIG{__WARN__} = sub{$warned = 1};
91557c90 44 $dm->prepare_deploy;
45 ok( $warned, 'prepare_deploy warns if you run it twice' );
d50f2521 46 }
47 mkpath(catfile(qw( t sql _common schema 1.0 )));
48 open my $common, '>',
49 catfile(qw( t sql _common schema 1.0 002-error.sql ));
50 print {$common} qq<syntax fail\n\n>;
51 close $common;
3b98a3a1 52
53 ok(
1f0d0633 54 -f catfile(qw( t sql SQLite schema 1.0 001-auto.sql-json )),
3b98a3a1 55 '1.0 schema gets generated properly'
56 );
57
58 dies_ok {
59 $s->resultset('Foo')->create({
60 bar => 'frew',
61 })
62 } 'schema not deployed';
63
d50f2521 64 mkpath catfile(qw( t sql _common schema 1.0 ));
9c4cee90 65 open $common, '>',
d50f2521 66 catfile(qw( t sql _common schema 1.0 001-auto.sql ));
67 print {$common} qq<This will never get run>;
68 close $common;
69 {
70 my $warned = 0;
71 local $SIG{__WARN__} = sub{$warned = 1};
7d2a6974 72 $dm->deploy;
d50f2521 73 ok( $warned, 'deploy warns on sql errors' );
74 }
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
96 $version = $s->schema_version();
91557c90 97 $dm->prepare_deploy;
3b98a3a1 98 ok(
1f0d0633 99 -f catfile(qw( t sql SQLite schema 2.0 001-auto.sql-json )),
3b98a3a1 100 '2.0 schema gets generated properly'
101 );
d50f2521 102 mkpath(catfile(qw( t sql SQLite up 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(
1f0d0633 120 -f catfile(qw( t sql SQLite up 1.0-2.0 001-auto.sql-json )),
d50f2521 121 '1.0-2.0 diff gets generated properly and default start and end versions get set'
3b98a3a1 122 );
d50f2521 123 mkpath(catfile(qw( t sql SQLite down 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(
1f0d0633 130 -f catfile(qw( t sql SQLite down 2.0-1.0 001-auto.sql-json )),
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
146 mkpath catfile(qw( t sql _common up 1.0-2.0 ));
147 open my $common, '>',
148 catfile(qw( t sql _common up 1.0-2.0 002-semiautomatic.sql ));
149 print {$common} qq<INSERT INTO Foo (bar, baz) VALUES ("hello", "world");\n\n>;
150 close $common;
151
0841a743 152 open my $common_pl, '>',
153 catfile(qw( t sql _common up 1.0-2.0 003-semiautomatic.pl ));
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
203 $version = $s->schema_version();
91557c90 204 $dm->prepare_deploy;
3b98a3a1 205 ok(
1f0d0633 206 -f catfile(qw( t sql SQLite schema 3.0 001-auto.sql-json )),
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(
1f0d0633 215 -f catfile(qw( t sql SQLite down 3.0-1.0 001-auto.sql-json )),
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(
1f0d0633 224 -f catfile(qw( t sql SQLite up 1.0-3.0 001-auto.sql-json )),
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 });
d50f2521 232 {
233 my $warned = 0;
234 local $SIG{__WARN__} = sub{$warned = 1};
be140a5f 235 $dm->prepare_upgrade({
236 from_version => '2.0',
237 to_version => $version,
238 version_set => ['2.0', $version]
239 });
d50f2521 240 ok( $warned, 'prepare_upgrade warns if you clobber an existing upgrade file' );
241 }
3b98a3a1 242 ok(
1f0d0633 243 -f catfile(qw( t sql SQLite up 1.0-2.0 001-auto.sql-json )),
3b98a3a1 244 '2.0-3.0 diff gets generated properly'
245 );
d50f2521 246 mkpath catfile(qw( t sql _generic up 2.0-3.0 ));
1f0d0633 247 rename catfile(qw( t sql SQLite up 2.0-3.0 001-auto.sql-json )), catfile(qw( t sql _generic up 2.0-3.0 001-auto.sql-json ));
d50f2521 248 rmtree(catfile(qw( t sql SQLite )));
249 warn 'how can this be' if -d catfile(qw( t sql SQLite ));
3b98a3a1 250 dies_ok {
251 $s->resultset('Foo')->create({
252 bar => 'frew',
253 baz => 'frew',
254 biff => 'frew',
255 })
256 } 'schema not deployed';
be140a5f 257 $dm->upgrade_single_step({ version_set => [qw( 2.0 3.0 )] });
3b98a3a1 258 lives_ok {
259 $s->resultset('Foo')->create({
260 bar => 'frew',
261 baz => 'frew',
262 biff => 'frew',
263 })
d50f2521 264 } 'schema is deployed using _generic';
265 rmtree(catfile(qw( t sql SQLite )));
266 rmtree(catfile(qw( t sql _generic )));
267 dies_ok {
be140a5f 268 $dm->upgrade_single_step({ version_set => [qw( 2.0 3.0 )] });
d50f2521 269 } 'dies when sql dir does not exist';
3b98a3a1 270}
02d58ac0 271done_testing;
d50f2521 272#vim: ts=2 sw=2 expandtab