Commit | Line | Data |
02d58ac0 |
1 | #!perl |
2 | |
3 | use Test::More; |
4 | use Test::Exception; |
5 | |
6 | use lib 't/lib'; |
7 | use DBICDHTest; |
459a67e3 |
8 | use aliased 'DBIx::Class::DeploymentHandler::DeployMethod::SQL::Translator'; |
3b98a3a1 |
9 | use File::Spec::Functions; |
d50f2521 |
10 | use File::Path qw(rmtree mkpath); |
459a67e3 |
11 | |
12 | my $db = 'dbi:SQLite:db.db'; |
13 | my @connection = ($db, '', '', { ignore_version => 1 }); |
14 | my $sql_dir = 't/sql'; |
15 | |
16 | DBICDHTest::ready; |
17 | |
18 | VERSION1: { |
19 | use_ok 'DBICVersion_v1'; |
20 | my $s = DBICVersion::Schema->connect(@connection); |
21 | my $dm = Translator->new({ |
3b98a3a1 |
22 | schema => $s, |
459a67e3 |
23 | upgrade_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; |
80ff6f6d |
37 | $dm->preinstall('1.0'); |
fc4b7602 |
38 | |
39 | ok -e 'foobar'; |
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( |
54 | -f catfile(qw( t sql SQLite schema 1.0 001-auto.sql )), |
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 | |
83 | VERSION2: { |
84 | use_ok 'DBICVersion_v2'; |
85 | my $s = DBICVersion::Schema->connect(@connection); |
86 | my $dm = Translator->new({ |
87 | schema => $s, |
88 | upgrade_directory => $sql_dir, |
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( |
99 | -f catfile(qw( t sql SQLite schema 2.0 001-auto.sql )), |
100 | '2.0 schema gets generated properly' |
101 | ); |
d50f2521 |
102 | mkpath(catfile(qw( t sql SQLite up 1.0-2.0 ))); |
41219a5d |
103 | $dm->prepare_upgrade(qw(1.0 2.0), [qw(1.0 2.0)]); |
d50f2521 |
104 | |
105 | { |
106 | my $warned = 0; |
107 | local $SIG{__WARN__} = sub{$warned = 1}; |
41219a5d |
108 | $dm->prepare_upgrade(qw(0.0 1.0), [qw(0.0 1.0)]); |
d50f2521 |
109 | ok( $warned, 'prepare_upgrade with a bogus preversion warns' ); |
110 | } |
3b98a3a1 |
111 | ok( |
112 | -f catfile(qw( t sql SQLite up 1.0-2.0 001-auto.sql )), |
d50f2521 |
113 | '1.0-2.0 diff gets generated properly and default start and end versions get set' |
3b98a3a1 |
114 | ); |
d50f2521 |
115 | mkpath(catfile(qw( t sql SQLite down 2.0-1.0 ))); |
41219a5d |
116 | $dm->prepare_downgrade($version, '1.0', [$version, '1.0']); |
3b98a3a1 |
117 | ok( |
118 | -f catfile(qw( t sql SQLite down 2.0-1.0 001-auto.sql )), |
d50f2521 |
119 | '2.0-1.0 diff gets generated properly' |
3b98a3a1 |
120 | ); |
121 | dies_ok { |
122 | $s->resultset('Foo')->create({ |
123 | bar => 'frew', |
124 | baz => 'frew', |
125 | }) |
126 | } 'schema not deployed'; |
127 | dies_ok { |
128 | $s->resultset('Foo')->create({ |
129 | bar => 'frew', |
130 | baz => 'frew', |
131 | }) |
132 | } 'schema not uppgrayyed'; |
d50f2521 |
133 | |
134 | mkpath catfile(qw( t sql _common up 1.0-2.0 )); |
135 | open my $common, '>', |
136 | catfile(qw( t sql _common up 1.0-2.0 002-semiautomatic.sql )); |
137 | print {$common} qq<INSERT INTO Foo (bar, baz) VALUES ("hello", "world");\n\n>; |
138 | close $common; |
139 | |
0841a743 |
140 | open my $common_pl, '>', |
141 | catfile(qw( t sql _common up 1.0-2.0 003-semiautomatic.pl )); |
142 | print {$common_pl} q| |
5b5defbc |
143 | sub { |
91557c90 |
144 | my $schema = shift; |
145 | $schema->resultset('Foo')->create({ |
146 | bar => 'goodbye', |
147 | baz => 'blue skies', |
148 | }) |
149 | } |
150 | |; |
0841a743 |
151 | close $common_pl; |
152 | |
7d2a6974 |
153 | $dm->upgrade_single_step([qw( 1.0 2.0 )]); |
d50f2521 |
154 | is( $s->resultset('Foo')->search({ |
155 | bar => 'hello', |
156 | baz => 'world', |
157 | })->count, 1, '_common migration got run'); |
0841a743 |
158 | is( $s->resultset('Foo')->search({ |
159 | bar => 'goodbye', |
160 | #baz => 'blue skies', |
161 | })->count, 1, '_common perl migration got run'); |
3b98a3a1 |
162 | lives_ok { |
163 | $s->resultset('Foo')->create({ |
164 | bar => 'frew', |
165 | baz => 'frew', |
166 | }) |
167 | } 'schema is deployed'; |
7d2a6974 |
168 | $dm->downgrade_single_step([qw( 2.0 1.0 )]); |
d50f2521 |
169 | dies_ok { |
170 | $s->resultset('Foo')->create({ |
171 | bar => 'frew', |
172 | baz => 'frew', |
173 | }) |
fc4b7602 |
174 | } 'schema is downgrayyed'; |
7d2a6974 |
175 | $dm->upgrade_single_step([qw( 1.0 2.0 )]); |
459a67e3 |
176 | } |
02d58ac0 |
177 | |
3b98a3a1 |
178 | VERSION3: { |
179 | use_ok 'DBICVersion_v3'; |
180 | my $s = DBICVersion::Schema->connect(@connection); |
181 | my $dm = Translator->new({ |
182 | schema => $s, |
183 | upgrade_directory => $sql_dir, |
184 | databases => ['SQLite'], |
02a7b8ac |
185 | sql_translator_args => { add_drop_table => 0 }, |
d50f2521 |
186 | txn_wrap => 0, |
3b98a3a1 |
187 | }); |
188 | |
189 | ok( $dm, 'DBIC::DH::SQL::Translator w/3.0 instantiates correctly'); |
190 | |
191 | $version = $s->schema_version(); |
91557c90 |
192 | $dm->prepare_deploy; |
3b98a3a1 |
193 | ok( |
194 | -f catfile(qw( t sql SQLite schema 3.0 001-auto.sql )), |
195 | '2.0 schema gets generated properly' |
196 | ); |
41219a5d |
197 | $dm->prepare_downgrade($version, '1.0', [$version, '1.0']); |
3b98a3a1 |
198 | ok( |
d50f2521 |
199 | -f catfile(qw( t sql SQLite down 3.0-1.0 001-auto.sql )), |
200 | '3.0-1.0 diff gets generated properly' |
201 | ); |
202 | $dm->prepare_upgrade( '1.0', $version, ['1.0', $version] ); |
203 | ok( |
204 | -f catfile(qw( t sql SQLite up 1.0-3.0 001-auto.sql )), |
3b98a3a1 |
205 | '1.0-3.0 diff gets generated properly' |
206 | ); |
41219a5d |
207 | $dm->prepare_upgrade( '2.0', $version, ['2.0', $version]); |
d50f2521 |
208 | { |
209 | my $warned = 0; |
210 | local $SIG{__WARN__} = sub{$warned = 1}; |
41219a5d |
211 | $dm->prepare_upgrade( '2.0', $version, ['2.0', $version] ); |
d50f2521 |
212 | ok( $warned, 'prepare_upgrade warns if you clobber an existing upgrade file' ); |
213 | } |
3b98a3a1 |
214 | ok( |
215 | -f catfile(qw( t sql SQLite up 1.0-2.0 001-auto.sql )), |
216 | '2.0-3.0 diff gets generated properly' |
217 | ); |
d50f2521 |
218 | mkpath catfile(qw( t sql _generic up 2.0-3.0 )); |
219 | rename catfile(qw( t sql SQLite up 2.0-3.0 001-auto.sql )), catfile(qw( t sql _generic up 2.0-3.0 001-auto.sql )); |
220 | rmtree(catfile(qw( t sql SQLite ))); |
221 | warn 'how can this be' if -d catfile(qw( t sql SQLite )); |
3b98a3a1 |
222 | dies_ok { |
223 | $s->resultset('Foo')->create({ |
224 | bar => 'frew', |
225 | baz => 'frew', |
226 | biff => 'frew', |
227 | }) |
228 | } 'schema not deployed'; |
7d2a6974 |
229 | $dm->upgrade_single_step([qw( 2.0 3.0 )]); |
3b98a3a1 |
230 | lives_ok { |
231 | $s->resultset('Foo')->create({ |
232 | bar => 'frew', |
233 | baz => 'frew', |
234 | biff => 'frew', |
235 | }) |
d50f2521 |
236 | } 'schema is deployed using _generic'; |
237 | rmtree(catfile(qw( t sql SQLite ))); |
238 | rmtree(catfile(qw( t sql _generic ))); |
239 | dies_ok { |
7d2a6974 |
240 | $dm->upgrade_single_step([qw( 2.0 3.0 )]); |
d50f2521 |
241 | } 'dies when sql dir does not exist'; |
3b98a3a1 |
242 | } |
02d58ac0 |
243 | done_testing; |
d50f2521 |
244 | #vim: ts=2 sw=2 expandtab |