Test/Database/Migrator.pm untested

filter: Statementstmt 0.0% 0 / 131 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 26 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 193 CC 3 SCAR 11.2
CC
3
Cov
0%
DEMOLISH
12.0
run_tests
2.0
_write_ddl_file
2.0
CRAP 3.1
1 package Test::Database::Migrator;
2  
3 use strict;
4 use warnings;
5 use namespace::autoclean;
6 use autodie;
7  
8 our $VERSION = '0.14';
9  
10 use File::Temp qw( tempdir );
11 use Database::Migrator::Types qw( ClassName Dir File Str );
12 use Log::Dispatch;
13 use Log::Dispatch::TestDiag;
14 use Path::Class qw( dir );
15 use Test::Fatal;
16 use Test::More 0.88;
17  
18 use Moose;
19  
20 has class => (
21 is => 'ro',
22 isa => ClassName,
23 required => 1,
24 );
25  
26 has database => (
27 is => 'ro',
28 isa => Str,
29 default => 'DatabaseMigrator_test_' . $$,
30 );
31  
32 has _tempdir => (
33 is => 'ro',
34 isa => Dir,
35 init_arg => undef,
36 lazy => 1,
37 default => sub { dir( tempdir( CLEANUP => 1 ) ) },
38 );
39  
40 has _schema_file => (
41 is => 'ro',
42 isa => File,
43 init_arg => undef,
44 lazy => 1,
45 default => sub { $_[0]->_tempdir()->file('schema.sql') },
46 );
47  
48 has _migrations_dir => (
49 is => 'ro',
50 isa => Dir,
51 init_arg => undef,
52 lazy => 1,
53 builder => '_build_migrations_dir',
54 );
55  
56 has _dbh => (
57 is => 'ro',
58 isa => 'DBI::db',
59 init_arg => undef,
60 lazy => 1,
61 builder => '_build_dbh',
62 predicate => '_has_dbh',
63 );
64  
65 sub run_tests {
66 my $self = shift;
67  
68 $self->_write_ddl_file( $self->_schema_file(), $self->_schema_ddl() );
69  
70 ok(
71 !$self->_new_migrator()->_database_exists(),
72 $self->database() . ' does not exist yet'
73 );
74  
75 $self->_check_initial_database();
76 $self->_test_migrations();
77 }
78  
79 sub _write_ddl_file {
80 my $self = shift;
81 my $file = shift;
82 my $ddl = shift;
83  
84 open my $fh, '>', $file;
85 print {$fh} $ddl
86 or die $!;
87 close $fh;
88 return;
89 }
90  
91 sub _check_initial_database {
92 my $self = shift;
93  
94 $self->_new_migrator()->create_or_update_database();
95  
96 my $migrator = $self->_new_migrator();
97  
98 ok(
99 $migrator->_database_exists(),
100 $self->database() . ' exists'
101 );
102  
103 return;
104 }
105  
106 sub _schema_ddl {
107 return <<'EOF';
108 CREATE TABLE applied_migration (
109 migration VARCHAR(250) PRIMARY KEY
110 );
111  
112 CREATE TABLE foo (
113 foo_id INTEGER PRIMARY KEY,
114 foo_name VARCHAR(50) NOT NULL
115 );
116 EOF
117 }
118  
119 sub _test_migrations {
120 my $self = shift;
121  
122 $self->_write_first_migration();
123 $self->_write_second_migration();
124  
125 $self->_new_migrator()->create_or_update_database();
126  
127 my $migrations = $self->_dbh()
128 ->selectcol_arrayref('SELECT migration FROM applied_migration') || [];
129  
130 is_deeply(
131 $migrations,
132 [
133 '01-first',
134 '02-second',
135 ],
136 'migrations were recorded in the applied_migration table'
137 );
138  
139 is(
140 exception { $self->_new_migrator()->create_or_update_database() },
141 undef,
142 'no error running migrator again after migrations have been applied'
143 );
144  
145 $self->_write_perl_sub_migration;
146  
147 $self->_new_migrator->create_or_update_database;
148  
149 $migrations
150 = $self->_dbh->selectcol_arrayref(
151 'SELECT migration FROM applied_migration')
152 || [];
153  
154 is_deeply(
155 $migrations,
156 [
157 '01-first',
158 '02-second',
159 '03-third',
160 ],
161 'migrations were recorded in the applied_migration table'
162 );
163  
164 $self->_write_perl_program_migration;
165  
166 like(
167 exception { $self->_new_migrator->create_or_update_database },
168 qr/Use of uninitialized/,
169 'perl program as migration threw exception due to warning',
170 );
171  
172 $migrations
173 = $self->_dbh->selectcol_arrayref(
174 'SELECT migration FROM applied_migration')
175 || [];
176  
177 is_deeply(
178 $migrations,
179 [
180 '01-first',
181 '02-second',
182 '03-third',
183 ],
184 'migrations were recorded in the applied_migration table'
185 );
186  
187 return;
188 }
189  
190 # This migration writes out two separate files to test that the files in a
191 # given migration are run in sorted order. If they're not then the migration
192 # will fail entirely.
193 sub _write_first_migration {
194 my $self = shift;
195  
196 my $dir = $self->_migrations_dir()->subdir('01-first');
197 ## no critic (Lax::ProhibitLeadingZeros::ExceptChmod, ValuesAndExpressions::ProhibitLeadingZeros)
198 $dir->mkpath( 0, 0755 );
199 ## use critic
200  
201 my $create_tables = <<'EOF';
202 CREATE TABLE bar (
203 bar_id INTEGER PRIMARY KEY,
204 bar_name VARCHAR(50) NOT NULL
205 );
206  
207 CREATE TABLE baz (
208 baz_id INTEGER PRIMARY KEY,
209 baz_name VARCHAR(50) NOT NULL
210 );
211 EOF
212  
213 $self->_write_ddl_file(
214 $dir->file('01-create-tables.sql'),
215 $create_tables,
216 );
217  
218 $self->_write_ddl_file(
219 $dir->file('02-create-bar-table-index.sql'),
220 'CREATE INDEX bar_bar_name ON bar (bar_name)',
221 );
222  
223 return;
224 }
225  
226 sub _write_second_migration {
227 my $self = shift;
228  
229 my $dir = $self->_migrations_dir()->subdir('02-second');
230 ## no critic (Lax::ProhibitLeadingZeros::ExceptChmod, ValuesAndExpressions::ProhibitLeadingZeros)
231 $dir->mkpath( 0, 0755 );
232 ## use critic
233  
234 open my $fh, '>', $dir->file('01-create-baz-table-index.sql');
235 print {$fh} <<'EOF' or die $!;
236 CREATE INDEX baz_baz_name ON baz (baz_name);
237 EOF
238 close $fh;
239  
240 return;
241 }
242  
243 sub _write_perl_sub_migration {
244 my $self = shift;
245  
246 my $dir = $self->_migrations_dir->subdir('03-third');
247 $dir->mkpath;
248  
249 my $file = $dir->file('migrate.pl'),
250  
251 my $migration = <<'EOF';
252 use strict;
253 use warnings;
254  
255 sub {
256 my ($migrator) = @_;
257 my $sql = 'CREATE TABLE myperlsub (id INTEGER)';
258 $migrator->dbh->do($sql);
259 return;
260 }
261 EOF
262  
263 $self->_write_ddl_file(
264 $file,
265 $migration,
266 );
267  
268 return;
269 }
270  
271 # This program generates a warning and so causes a failure.
272 sub _write_perl_program_migration {
273 my $self = shift;
274  
275 my $dir = $self->_migrations_dir->subdir('04-fourth');
276 $dir->mkpath;
277  
278 my $file = $dir->file('migrate.pl'),
279  
280 my $migration = <<'EOF';
281 #!/usr/bin/env perl
282  
283 use strict;
284 use warnings;
285  
286 sub main {
287 my $x;
288 print "hello $x\n";
289 return 1;
290 }
291  
292 exit(main() ? 0 : 1);
293 EOF
294  
295 $self->_write_ddl_file(
296 $file,
297 $migration,
298 );
299  
300 chmod 0755, $file->stringify;
301  
302 return;
303 }
304  
305 sub _new_migrator {
306 my $self = shift;
307  
308 return $self->class()->new(
309 database => $self->database(),
310 migration_table => 'applied_migration',
311 schema_file => $self->_schema_file(),
312 migrations_dir => $self->_migrations_dir(),
313 logger => _logger(),
314 );
315 }
316  
317 sub _logger {
318 return Log::Dispatch->new(
319 outputs => [ [ 'TestDiag', min_level => 'info' ] ],
320 );
321 }
322  
323 sub _build_migrations_dir {
324 my $self = shift;
325  
326 my $dir = $self->_tempdir()->subdir('migrations');
327 ## no critic (Lax::ProhibitLeadingZeros::ExceptChmod, ValuesAndExpressions::ProhibitLeadingZeros)
328 $dir->mkpath( 0, 0755 );
329 ## use critic
330  
331 return $dir;
332 }
333  
334 sub _build_dbh {
335 my $self = shift;
336  
337 return $self->_new_migrator()->dbh();
338 }
339  
340 sub DEMOLISH {
341 my $self = shift;
342  
343 if ( $ENV{DATABASE_MIGRATOR_TEST_WAIT} ) {
344 print "\n Waiting to clean up the test database\n\n"
345 or die $!;
346 ## no critic (InputOutput::ProhibitExplicitStdin)
347 my $x = <STDIN>;
348 }
349  
350 $self->_dbh()->disconnect() if $self->_has_dbh();
351  
352 $self->_new_migrator()->_drop_database();
353 }
354  
355 =for Pod::Coverage .*
356  
357 =cut
358  
359 __PACKAGE__->meta()->make_immutable();
360  
361 1;