Commit | Line | Data |
45d0d9d5 |
1 | package DBIx::Class::DeploymentHandler::DeployMethod::SQL::Translator; |
334bced5 |
2 | use Moose; |
9af9d0b2 |
3 | |
9a3a62f1 |
4 | # ABSTRACT: Manage your SQL and Perl migrations in nicely laid out directories |
5 | |
9af9d0b2 |
6 | use autodie; |
7 | use Carp qw( carp croak ); |
8465e767 |
8 | use DBIx::Class::DeploymentHandler::Logger; |
9 | use Log::Contextual qw(:log :dlog), -default_logger => |
10 | DBIx::Class::DeploymentHandler::Logger->new({ |
11 | env_prefix => 'DBICDH' |
12 | }); |
9af9d0b2 |
13 | |
2e68a8e1 |
14 | use Method::Signatures::Simple; |
7f50d101 |
15 | use Try::Tiny; |
9af9d0b2 |
16 | |
d23c7c77 |
17 | use SQL::Translator; |
18 | require SQL::Translator::Diff; |
9af9d0b2 |
19 | |
d23c7c77 |
20 | require DBIx::Class::Storage; # loaded for type constraint |
41863428 |
21 | use DBIx::Class::DeploymentHandler::Types; |
22 | |
9af9d0b2 |
23 | use File::Path 'mkpath'; |
24 | use File::Spec::Functions; |
2e68a8e1 |
25 | |
7521a845 |
26 | with 'DBIx::Class::DeploymentHandler::HandlesDeploy'; |
3c1b5ee8 |
27 | |
d54b8d69 |
28 | has schema => ( |
29 | isa => 'DBIx::Class::Schema', |
30 | is => 'ro', |
31 | required => 1, |
d54b8d69 |
32 | ); |
33 | |
334bced5 |
34 | has storage => ( |
35 | isa => 'DBIx::Class::Storage', |
36 | is => 'ro', |
37 | lazy_build => 1, |
38 | ); |
39 | |
2eaf903b |
40 | method _build_storage { |
41 | my $s = $self->schema->storage; |
42 | $s->_determine_driver; |
43 | $s |
44 | } |
45 | |
02a7b8ac |
46 | has sql_translator_args => ( |
334bced5 |
47 | isa => 'HashRef', |
48 | is => 'ro', |
49 | default => sub { {} }, |
50 | ); |
91adde75 |
51 | has script_directory => ( |
334bced5 |
52 | isa => 'Str', |
53 | is => 'ro', |
54 | required => 1, |
55 | default => 'sql', |
56 | ); |
57 | |
334bced5 |
58 | has databases => ( |
59 | coerce => 1, |
60 | isa => 'DBIx::Class::DeploymentHandler::Databases', |
61 | is => 'ro', |
62 | default => sub { [qw( MySQL SQLite PostgreSQL )] }, |
63 | ); |
64 | |
a7d53deb |
65 | has txn_wrap => ( |
66 | is => 'ro', |
67 | isa => 'Bool', |
68 | default => 1, |
69 | ); |
70 | |
73caa630 |
71 | has schema_version => ( |
72 | is => 'ro', |
e86c0c07 |
73 | isa => 'Str', |
73caa630 |
74 | lazy_build => 1, |
75 | ); |
76 | |
6df6dcb9 |
77 | # this will probably never get called as the DBICDH |
78 | # will be passing down a schema_version normally, which |
79 | # is built the same way, but we leave this in place |
73caa630 |
80 | method _build_schema_version { $self->schema->schema_version } |
81 | |
76d311e7 |
82 | method __ddl_consume_with_prefix($type, $versions, $prefix) { |
91adde75 |
83 | my $base_dir = $self->script_directory; |
262166c1 |
84 | |
76d08d08 |
85 | my $main = catfile( $base_dir, $type ); |
86 | my $generic = catfile( $base_dir, '_generic' ); |
87 | my $common = |
88 | catfile( $base_dir, '_common', $prefix, join q(-), @{$versions} ); |
262166c1 |
89 | |
90 | my $dir; |
91 | if (-d $main) { |
76d08d08 |
92 | $dir = catfile($main, $prefix, join q(-), @{$versions}) |
262166c1 |
93 | } elsif (-d $generic) { |
9af9d0b2 |
94 | $dir = catfile($generic, $prefix, join q(-), @{$versions}); |
262166c1 |
95 | } else { |
9af9d0b2 |
96 | croak "neither $main or $generic exist; please write/generate some SQL"; |
262166c1 |
97 | } |
98 | |
99 | opendir my($dh), $dir; |
f36afe83 |
100 | my %files = map { $_ => "$dir/$_" } grep { /\.(?:sql|pl|sql-\w+)$/ && -f "$dir/$_" } readdir $dh; |
262166c1 |
101 | closedir $dh; |
102 | |
103 | if (-d $common) { |
104 | opendir my($dh), $common; |
41219a5d |
105 | for my $filename (grep { /\.(?:sql|pl)$/ && -f catfile($common,$_) } readdir $dh) { |
262166c1 |
106 | unless ($files{$filename}) { |
9af9d0b2 |
107 | $files{$filename} = catfile($common,$filename); |
262166c1 |
108 | } |
109 | } |
110 | closedir $dh; |
111 | } |
112 | |
113 | return [@files{sort keys %files}] |
114 | } |
3c1b5ee8 |
115 | |
fc4b7602 |
116 | method _ddl_preinstall_consume_filenames($type, $version) { |
117 | $self->__ddl_consume_with_prefix($type, [ $version ], 'preinstall') |
118 | } |
119 | |
76d311e7 |
120 | method _ddl_schema_consume_filenames($type, $version) { |
121 | $self->__ddl_consume_with_prefix($type, [ $version ], 'schema') |
3c1b5ee8 |
122 | } |
123 | |
7e08eddd |
124 | method _ddl_protoschema_produce_filename($version) { |
125 | my $dirname = catfile( $self->script_directory, '_protoschema', $version ); |
126 | mkpath($dirname) unless -d $dirname; |
127 | |
128 | return catfile( $dirname, '001-auto.yml' ); |
129 | } |
130 | |
76d311e7 |
131 | method _ddl_schema_produce_filename($type, $version) { |
91adde75 |
132 | my $dirname = catfile( $self->script_directory, $type, 'schema', $version ); |
76d08d08 |
133 | mkpath($dirname) unless -d $dirname; |
d54b8d69 |
134 | |
09bc35e3 |
135 | return catfile( $dirname, '001-auto.sql' ); |
d54b8d69 |
136 | } |
137 | |
76d311e7 |
138 | method _ddl_schema_up_consume_filenames($type, $versions) { |
139 | $self->__ddl_consume_with_prefix($type, $versions, 'up') |
3c1b5ee8 |
140 | } |
141 | |
76d311e7 |
142 | method _ddl_schema_down_consume_filenames($type, $versions) { |
143 | $self->__ddl_consume_with_prefix($type, $versions, 'down') |
a41a04e5 |
144 | } |
145 | |
76d311e7 |
146 | method _ddl_schema_up_produce_filename($type, $versions) { |
91adde75 |
147 | my $dir = $self->script_directory; |
76d311e7 |
148 | |
76d08d08 |
149 | my $dirname = catfile( $dir, $type, 'up', join q(-), @{$versions}); |
150 | mkpath($dirname) unless -d $dirname; |
a41a04e5 |
151 | |
e62add58 |
152 | return catfile( $dirname, '001-auto.sql' ); |
a41a04e5 |
153 | } |
154 | |
76d311e7 |
155 | method _ddl_schema_down_produce_filename($type, $versions, $dir) { |
76d08d08 |
156 | my $dirname = catfile( $dir, $type, 'down', join q(-), @{$versions} ); |
157 | mkpath($dirname) unless -d $dirname; |
24f4524b |
158 | |
09bc35e3 |
159 | return catfile( $dirname, '001-auto.sql'); |
24f4524b |
160 | } |
161 | |
f36afe83 |
162 | method _run_sql_array($sql) { |
41219a5d |
163 | my $storage = $self->storage; |
5d7b27cf |
164 | |
1f0d0633 |
165 | $sql = [grep { |
166 | $_ && # remove blank lines |
167 | !/^(BEGIN|BEGIN TRANSACTION|COMMIT)/ # strip txn's |
168 | } map { |
169 | s/^\s+//; s/\s+$//; # trim whitespace |
170 | join '', grep { !/^--/ } split /\n/ # remove comments |
171 | } @$sql]; |
172 | |
f4075791 |
173 | Dlog_trace { "Running SQL $_" } $sql; |
f36afe83 |
174 | foreach my $line (@{$sql}) { |
5d7b27cf |
175 | $storage->_query_start($line); |
10a62c3d |
176 | # the whole reason we do this is so that we can see the line that was run |
5d7b27cf |
177 | try { |
5d7b27cf |
178 | $storage->dbh_do (sub { $_[1]->do($line) }); |
179 | } |
180 | catch { |
10a62c3d |
181 | die "$_ (running line '$line')" |
5d7b27cf |
182 | } |
183 | $storage->_query_end($line); |
184 | } |
4d09f712 |
185 | return join "\n", @$sql |
f36afe83 |
186 | } |
187 | |
188 | method _run_sql($filename) { |
f4075791 |
189 | log_debug { "Running SQL from $filename" }; |
f36afe83 |
190 | return $self->_run_sql_array($self->_read_sql_file($filename)); |
5d7b27cf |
191 | } |
2e68a8e1 |
192 | |
5d7b27cf |
193 | method _run_perl($filename) { |
f4075791 |
194 | log_debug { "Running Perl from $filename" }; |
5d7b27cf |
195 | my $filedata = do { local( @ARGV, $/ ) = $filename; <> }; |
c8a2f7bd |
196 | |
5d7b27cf |
197 | no warnings 'redefine'; |
198 | my $fn = eval "$filedata"; |
199 | use warnings; |
f4075791 |
200 | Dlog_trace { "Running Perl $_" } $fn; |
5d7b27cf |
201 | |
202 | if ($@) { |
203 | carp "$filename failed to compile: $@"; |
204 | } elsif (ref $fn eq 'CODE') { |
205 | $fn->($self->schema) |
206 | } else { |
207 | carp "$filename should define an anonymouse sub that takes a schema but it didn't!"; |
208 | } |
209 | } |
5d7b27cf |
210 | |
211 | method _run_sql_and_perl($filenames) { |
5d7b27cf |
212 | my @files = @{$filenames}; |
213 | my $guard = $self->schema->txn_scope_guard if $self->txn_wrap; |
214 | |
215 | my $sql = ''; |
41219a5d |
216 | for my $filename (@files) { |
217 | if ($filename =~ /\.sql$/) { |
5d7b27cf |
218 | $sql .= $self->_run_sql($filename) |
398b1385 |
219 | } elsif ( $filename =~ /\.pl$/ ) { |
5d7b27cf |
220 | $self->_run_perl($filename) |
41219a5d |
221 | } else { |
fc4b7602 |
222 | croak "A file ($filename) got to deploy that wasn't sql or perl!"; |
2e68a8e1 |
223 | } |
2e68a8e1 |
224 | } |
a7d53deb |
225 | |
226 | $guard->commit if $self->txn_wrap; |
41219a5d |
227 | |
228 | return $sql; |
229 | } |
230 | |
231 | sub deploy { |
232 | my $self = shift; |
be140a5f |
233 | my $version = (shift @_ || {})->{version} || $self->schema_version; |
f4075791 |
234 | log_info { "deploying version $version" }; |
41219a5d |
235 | |
236 | return $self->_run_sql_and_perl($self->_ddl_schema_consume_filenames( |
237 | $self->storage->sqlt_type, |
92c34cab |
238 | $version, |
41219a5d |
239 | )); |
2e68a8e1 |
240 | } |
241 | |
80ff6f6d |
242 | sub preinstall { |
9faec51a |
243 | my $self = shift; |
244 | my $args = shift; |
245 | my $version = $args->{version} || $self->schema_version; |
f4075791 |
246 | log_info { "preinstalling version $version" }; |
9faec51a |
247 | my $storage_type = $args->{storage_type} || $self->storage->sqlt_type; |
fc4b7602 |
248 | |
249 | my @files = @{$self->_ddl_preinstall_consume_filenames( |
9faec51a |
250 | $storage_type, |
fc4b7602 |
251 | $version, |
252 | )}; |
253 | |
254 | for my $filename (@files) { |
255 | # We ignore sql for now (till I figure out what to do with it) |
256 | if ( $filename =~ /^(.+)\.pl$/ ) { |
fc4b7602 |
257 | my $filedata = do { local( @ARGV, $/ ) = $filename; <> }; |
fc4b7602 |
258 | |
9faec51a |
259 | no warnings 'redefine'; |
5b5defbc |
260 | my $fn = eval "$filedata"; |
fc4b7602 |
261 | use warnings; |
5b5defbc |
262 | |
9faec51a |
263 | if ($@) { |
3fa64c79 |
264 | carp "$filename failed to compile: $@"; |
9faec51a |
265 | } elsif (ref $fn eq 'CODE') { |
fc4b7602 |
266 | $fn->() |
267 | } else { |
5b5defbc |
268 | carp "$filename should define an anonymous sub but it didn't!"; |
fc4b7602 |
269 | } |
270 | } else { |
271 | croak "A file ($filename) got to preinstall_scripts that wasn't sql or perl!"; |
272 | } |
273 | } |
274 | } |
275 | |
c8a2f7bd |
276 | sub _prepare_install { |
73caa630 |
277 | my $self = shift; |
02a7b8ac |
278 | my $sqltargs = { %{$self->sql_translator_args}, %{shift @_} }; |
e62add58 |
279 | my $from_file = shift; |
c8a2f7bd |
280 | my $to_file = shift; |
2e68a8e1 |
281 | my $schema = $self->schema; |
282 | my $databases = $self->databases; |
91adde75 |
283 | my $dir = $self->script_directory; |
73caa630 |
284 | my $version = $self->schema_version; |
d54b8d69 |
285 | |
9600776d |
286 | my $sqlt = SQL::Translator->new({ |
d54b8d69 |
287 | add_drop_table => 1, |
7e08eddd |
288 | parser => 'SQL::Translator::Parser::YAML', |
3aaf766f |
289 | %{$sqltargs} |
9600776d |
290 | }); |
2e68a8e1 |
291 | |
e62add58 |
292 | my $yaml_filename = $self->$from_file($version); |
2e68a8e1 |
293 | |
294 | foreach my $db (@$databases) { |
295 | $sqlt->reset; |
2e68a8e1 |
296 | $sqlt->producer($db); |
297 | |
c8a2f7bd |
298 | my $filename = $self->$to_file($db, $version, $dir); |
9600776d |
299 | if (-e $filename ) { |
2e68a8e1 |
300 | carp "Overwriting existing DDL file - $filename"; |
301 | unlink $filename; |
302 | } |
303 | |
7e08eddd |
304 | my $sql = $sqlt->translate($yaml_filename); |
305 | if(!$sql) { |
2e68a8e1 |
306 | carp("Failed to translate to $db, skipping. (" . $sqlt->error . ")"); |
307 | next; |
308 | } |
387b11d2 |
309 | open my $file, q(>), $filename; |
7e08eddd |
310 | print {$file} $sql; |
2e68a8e1 |
311 | close $file; |
312 | } |
313 | } |
314 | |
c8a2f7bd |
315 | sub _resultsource_install_filename { |
316 | my ($self, $source_name) = @_; |
317 | return sub { |
318 | my ($self, $type, $version) = @_; |
91adde75 |
319 | my $dirname = catfile( $self->script_directory, $type, 'schema', $version ); |
c8a2f7bd |
320 | mkpath($dirname) unless -d $dirname; |
321 | |
09bc35e3 |
322 | return catfile( $dirname, "001-auto-$source_name.sql" ); |
c8a2f7bd |
323 | } |
324 | } |
325 | |
e62add58 |
326 | sub _resultsource_protoschema_filename { |
327 | my ($self, $source_name) = @_; |
328 | return sub { |
329 | my ($self, $version) = @_; |
330 | my $dirname = catfile( $self->script_directory, '_protoschema', $version ); |
331 | mkpath($dirname) unless -d $dirname; |
332 | |
333 | return catfile( $dirname, "001-auto-$source_name.yml" ); |
334 | } |
335 | } |
336 | |
c8a2f7bd |
337 | sub install_resultsource { |
be140a5f |
338 | my ($self, $args) = @_; |
339 | my $source = $args->{result_source}; |
340 | my $version = $args->{version}; |
f4075791 |
341 | log_info { 'installing_resultsource ' . $source->source_name . ", version $version" }; |
c8a2f7bd |
342 | my $rs_install_file = |
343 | $self->_resultsource_install_filename($source->source_name); |
344 | |
345 | my $files = [ |
346 | $self->$rs_install_file( |
347 | $self->storage->sqlt_type, |
348 | $version, |
349 | ) |
350 | ]; |
351 | $self->_run_sql_and_perl($files); |
352 | } |
353 | |
354 | sub prepare_resultsource_install { |
355 | my $self = shift; |
be140a5f |
356 | my $source = (shift @_)->{result_source}; |
f4075791 |
357 | log_info { 'preparing install for resultsource ' . $source->source_name }; |
c8a2f7bd |
358 | |
e62add58 |
359 | my $install_filename = $self->_resultsource_install_filename($source->source_name); |
360 | my $proto_filename = $self->_resultsource_protoschema_filename($source->source_name); |
361 | $self->_generate_protoschema({ |
c8a2f7bd |
362 | parser_args => { sources => [$source->source_name], } |
e62add58 |
363 | }, $proto_filename); |
364 | $self->_prepare_install({}, $proto_filename, $install_filename); |
c8a2f7bd |
365 | } |
366 | |
91557c90 |
367 | sub prepare_deploy { |
f4075791 |
368 | log_info { 'preparing deploy' }; |
c8a2f7bd |
369 | my $self = shift; |
e62add58 |
370 | $self->_generate_protoschema({}, '_ddl_protoschema_produce_filename'); |
371 | $self->_prepare_install({}, '_ddl_protoschema_produce_filename', '_ddl_schema_produce_filename'); |
c8a2f7bd |
372 | } |
373 | |
a41a04e5 |
374 | sub prepare_upgrade { |
be140a5f |
375 | my ($self, $args) = @_; |
0df68524 |
376 | log_info { |
f4075791 |
377 | "preparing upgrade from $args->{from_version} to $args->{to_version}" |
0df68524 |
378 | }; |
be140a5f |
379 | $self->_prepare_changegrade( |
380 | $args->{from_version}, $args->{to_version}, $args->{version_set}, 'up' |
381 | ); |
76d311e7 |
382 | } |
383 | |
384 | sub prepare_downgrade { |
be140a5f |
385 | my ($self, $args) = @_; |
0df68524 |
386 | log_info { |
f4075791 |
387 | "preparing downgrade from $args->{from_version} to $args->{to_version}" |
0df68524 |
388 | }; |
be140a5f |
389 | $self->_prepare_changegrade( |
390 | $args->{from_version}, $args->{to_version}, $args->{version_set}, 'down' |
391 | ); |
76d311e7 |
392 | } |
393 | |
394 | method _prepare_changegrade($from_version, $to_version, $version_set, $direction) { |
2e68a8e1 |
395 | my $schema = $self->schema; |
396 | my $databases = $self->databases; |
91adde75 |
397 | my $dir = $self->script_directory; |
02a7b8ac |
398 | my $sqltargs = $self->sql_translator_args; |
2e68a8e1 |
399 | |
73caa630 |
400 | my $schema_version = $self->schema_version; |
2e68a8e1 |
401 | |
402 | $sqltargs = { |
403 | add_drop_table => 1, |
404 | ignore_constraint_names => 1, |
405 | ignore_index_names => 1, |
406 | %{$sqltargs} |
407 | }; |
408 | |
e62add58 |
409 | my $diff_file_method = "_ddl_schema_${direction}_produce_filename"; |
410 | my $source_schema; |
411 | { |
412 | my $prefilename = $self->_ddl_protoschema_produce_filename($from_version, $dir); |
2e68a8e1 |
413 | |
e62add58 |
414 | # should probably be a croak |
415 | carp("No previous schema file found ($prefilename)") |
416 | unless -e $prefilename; |
2e68a8e1 |
417 | |
e62add58 |
418 | my $t = SQL::Translator->new({ |
419 | %{$sqltargs}, |
420 | debug => 0, |
421 | trace => 0, |
422 | parser => 'SQL::Translator::Parser::YAML', |
423 | }); |
2e68a8e1 |
424 | |
e62add58 |
425 | my $out = $t->translate( $prefilename ) |
426 | or croak($t->error); |
2e68a8e1 |
427 | |
e62add58 |
428 | $source_schema = $t->schema; |
2e68a8e1 |
429 | |
e62add58 |
430 | $source_schema->name( $prefilename ) |
431 | unless $source_schema->name; |
432 | } |
2e68a8e1 |
433 | |
e62add58 |
434 | my $dest_schema; |
435 | { |
436 | my $filename = $self->_ddl_protoschema_produce_filename($to_version, $dir); |
2e68a8e1 |
437 | |
e62add58 |
438 | # should probably be a croak |
439 | carp("No next schema file found ($filename)") |
440 | unless -e $filename; |
2e68a8e1 |
441 | |
e62add58 |
442 | my $t = SQL::Translator->new({ |
443 | %{$sqltargs}, |
444 | debug => 0, |
445 | trace => 0, |
446 | parser => 'SQL::Translator::Parser::YAML', |
447 | }); |
2e68a8e1 |
448 | |
e62add58 |
449 | my $out = $t->translate( $filename ) |
450 | or croak($t->error); |
2e68a8e1 |
451 | |
e62add58 |
452 | $dest_schema = $t->schema; |
2e68a8e1 |
453 | |
e62add58 |
454 | $dest_schema->name( $filename ) |
455 | unless $dest_schema->name; |
456 | } |
457 | foreach my $db (@$databases) { |
458 | my $diff_file = $self->$diff_file_method($db, $version_set, $dir ); |
459 | if(-e $diff_file) { |
460 | carp("Overwriting existing $direction-diff file - $diff_file"); |
461 | unlink $diff_file; |
2e68a8e1 |
462 | } |
463 | |
09bc35e3 |
464 | my $diff = SQL::Translator::Diff::schema_diff( |
465 | $source_schema, $db, |
466 | $dest_schema, $db, |
467 | $sqltargs |
468 | ); |
387b11d2 |
469 | open my $file, q(>), $diff_file; |
09bc35e3 |
470 | print {$file} $diff; |
2e68a8e1 |
471 | close $file; |
472 | } |
473 | } |
474 | |
334bced5 |
475 | method _read_sql_file($file) { |
476 | return unless $file; |
477 | |
aabd4237 |
478 | open my $fh, '<', $file; |
0d19af1d |
479 | my @data = split /;\n/, join '', <$fh>; |
334bced5 |
480 | close $fh; |
481 | |
09bc35e3 |
482 | @data = grep { |
483 | $_ && # remove blank lines |
484 | !/^(BEGIN|BEGIN TRANSACTION|COMMIT)/ # strip txn's |
485 | } map { |
486 | s/^\s+//; s/\s+$//; # trim whitespace |
487 | join '', grep { !/^--/ } split /\n/ # remove comments |
488 | } @data; |
334bced5 |
489 | |
09bc35e3 |
490 | return \@data; |
1f0d0633 |
491 | } |
492 | |
7d2a6974 |
493 | sub downgrade_single_step { |
76d311e7 |
494 | my $self = shift; |
be140a5f |
495 | my $version_set = (shift @_)->{version_set}; |
f4075791 |
496 | Dlog_info { "downgrade_single_step'ing $_" } $version_set; |
41219a5d |
497 | |
498 | my $sql = $self->_run_sql_and_perl($self->_ddl_schema_down_consume_filenames( |
76d311e7 |
499 | $self->storage->sqlt_type, |
627581cd |
500 | $version_set, |
41219a5d |
501 | )); |
3249629f |
502 | |
41219a5d |
503 | return ['', $sql]; |
76d311e7 |
504 | } |
505 | |
7d2a6974 |
506 | sub upgrade_single_step { |
7521a845 |
507 | my $self = shift; |
be140a5f |
508 | my $version_set = (shift @_)->{version_set}; |
f4075791 |
509 | Dlog_info { "upgrade_single_step'ing $_" } $version_set; |
41219a5d |
510 | |
511 | my $sql = $self->_run_sql_and_perl($self->_ddl_schema_up_consume_filenames( |
334bced5 |
512 | $self->storage->sqlt_type, |
627581cd |
513 | $version_set, |
41219a5d |
514 | )); |
515 | return ['', $sql]; |
334bced5 |
516 | } |
517 | |
7e08eddd |
518 | sub _generate_protoschema { |
519 | my $self = shift; |
e62add58 |
520 | my $sqltargs = { %{$self->sql_translator_args}, %{shift @_} }; |
521 | my $to_file = shift; |
7e08eddd |
522 | my $filename |
e62add58 |
523 | = $self->$to_file($self->schema_version); |
7e08eddd |
524 | |
e62add58 |
525 | # we do this because the code that uses this sets parser args, |
526 | # so we just need to merge in the package |
527 | $sqltargs->{parser_args}{package} = $self->schema; |
7e08eddd |
528 | my $sqlt = SQL::Translator->new({ |
529 | parser => 'SQL::Translator::Parser::DBIx::Class', |
530 | producer => 'SQL::Translator::Producer::YAML', |
e62add58 |
531 | %{ $sqltargs }, |
7e08eddd |
532 | }); |
533 | |
534 | my $yml = $sqlt->translate; |
535 | |
536 | croak("Failed to translate to YAML: " . $sqlt->error) |
537 | unless $yml; |
538 | |
539 | if (-e $filename ) { |
540 | carp "Overwriting existing DDL-YML file - $filename"; |
541 | unlink $filename; |
542 | } |
543 | |
544 | open my $file, q(>), $filename; |
545 | print {$file} $yml; |
546 | close $file; |
547 | } |
548 | |
aabd4237 |
549 | __PACKAGE__->meta->make_immutable; |
550 | |
2e68a8e1 |
551 | 1; |
e051bb00 |
552 | |
e52174e3 |
553 | # vim: ts=2 sw=2 expandtab |
554 | |
e051bb00 |
555 | __END__ |
556 | |
bcc72297 |
557 | =head1 DESCRIPTION |
558 | |
e62add58 |
559 | This class is the meat of L<DBIx::Class::DeploymentHandler>. It takes care |
560 | of generating serialized schemata as well as sql files to move from one |
561 | version of a schema to the rest. One of the hallmark features of this class |
562 | is that it allows for multiple sql files for deploy and upgrade, allowing |
563 | developers to fine tune deployment. In addition it also allows for perl |
564 | files to be run at any stage of the process. |
bcc72297 |
565 | |
566 | For basic usage see L<DBIx::Class::DeploymentHandler::HandlesDeploy>. What's |
567 | documented here is extra fun stuff or private methods. |
568 | |
569 | =head1 DIRECTORY LAYOUT |
570 | |
92c34cab |
571 | Arguably this is the best feature of L<DBIx::Class::DeploymentHandler>. It's |
572 | heavily based upon L<DBIx::Migration::Directories>, but has some extensions and |
573 | modifications, so even if you are familiar with it, please read this. I feel |
574 | like the best way to describe the layout is with the following example: |
575 | |
576 | $sql_migration_dir |
577 | |- SQLite |
578 | | |- down |
4f85efc6 |
579 | | | `- 2-1 |
e62add58 |
580 | | | `- 001-auto.sql |
92c34cab |
581 | | |- schema |
582 | | | `- 1 |
e62add58 |
583 | | | `- 001-auto.sql |
92c34cab |
584 | | `- up |
585 | | |- 1-2 |
e62add58 |
586 | | | `- 001-auto.sql |
92c34cab |
587 | | `- 2-3 |
e62add58 |
588 | | `- 001-auto.sql |
92c34cab |
589 | |- _common |
590 | | |- down |
4f85efc6 |
591 | | | `- 2-1 |
92c34cab |
592 | | | `- 002-remove-customers.pl |
593 | | `- up |
594 | | `- 1-2 |
595 | | `- 002-generate-customers.pl |
596 | |- _generic |
597 | | |- down |
4f85efc6 |
598 | | | `- 2-1 |
e62add58 |
599 | | | `- 001-auto.sql |
92c34cab |
600 | | |- schema |
601 | | | `- 1 |
e62add58 |
602 | | | `- 001-auto.sql |
92c34cab |
603 | | `- up |
604 | | `- 1-2 |
e62add58 |
605 | | |- 001-auto.sql |
92c34cab |
606 | | `- 002-create-stored-procedures.sql |
607 | `- MySQL |
608 | |- down |
4f85efc6 |
609 | | `- 2-1 |
e62add58 |
610 | | `- 001-auto.sql |
80ff6f6d |
611 | |- preinstall |
612 | | `- 1 |
613 | | |- 001-create_database.pl |
614 | | `- 002-create_users_and_permissions.pl |
92c34cab |
615 | |- schema |
616 | | `- 1 |
e62add58 |
617 | | `- 001-auto.sql |
92c34cab |
618 | `- up |
619 | `- 1-2 |
e62add58 |
620 | `- 001-auto.sql |
92c34cab |
621 | |
622 | So basically, the code |
623 | |
624 | $dm->deploy(1) |
625 | |
626 | on an C<SQLite> database that would simply run |
e62add58 |
627 | C<$sql_migration_dir/SQLite/schema/1/001-auto.sql>. Next, |
92c34cab |
628 | |
629 | $dm->upgrade_single_step([1,2]) |
630 | |
e62add58 |
631 | would run C<$sql_migration_dir/SQLite/up/1-2/001-auto.sql> followed by |
92c34cab |
632 | C<$sql_migration_dir/_common/up/1-2/002-generate-customers.pl>. |
633 | |
0824f31f |
634 | C<.pl> files don't have to be in the C<_common> directory, but most of the time |
635 | they should be, because perl scripts are generally be database independent. |
92c34cab |
636 | |
637 | C<_generic> exists for when you for some reason are sure that your SQL is |
638 | generic enough to run on all databases. Good luck with that one. |
639 | |
80ff6f6d |
640 | Note that unlike most steps in the process, C<preinstall> will not run SQL, as |
641 | there may not even be an database at preinstall time. It will run perl scripts |
642 | just like the other steps in the process, but nothing is passed to them. |
643 | Until people have used this more it will remain freeform, but a recommended use |
644 | of preinstall is to have it prompt for username and password, and then call the |
645 | appropriate C<< CREATE DATABASE >> commands etc. |
646 | |
92c34cab |
647 | =head1 PERL SCRIPTS |
648 | |
7d0b0f2b |
649 | A perl script for this tool is very simple. It merely needs to contain an |
650 | anonymous sub that takes a L<DBIx::Class::Schema> as it's only argument. |
92c34cab |
651 | A very basic perl script might look like: |
652 | |
653 | #!perl |
654 | |
655 | use strict; |
656 | use warnings; |
657 | |
7d0b0f2b |
658 | sub { |
92c34cab |
659 | my $schema = shift; |
660 | |
661 | $schema->resultset('Users')->create({ |
662 | name => 'root', |
663 | password => 'root', |
664 | }) |
665 | } |
bcc72297 |
666 | |
eb28403b |
667 | =attr schema |
a65184c8 |
668 | |
bcc72297 |
669 | The L<DBIx::Class::Schema> (B<required>) that is used to talk to the database |
670 | and generate the DDL. |
671 | |
eb28403b |
672 | =attr storage |
a65184c8 |
673 | |
bcc72297 |
674 | The L<DBIx::Class::Storage> that is I<actually> used to talk to the database |
675 | and generate the DDL. This is automatically created with L</_build_storage>. |
676 | |
02a7b8ac |
677 | =attr sql_translator_args |
cfc9edf9 |
678 | |
02a7b8ac |
679 | The arguments that get passed to L<SQL::Translator> when it's used. |
a65184c8 |
680 | |
91adde75 |
681 | =attr script_directory |
cfc9edf9 |
682 | |
91adde75 |
683 | The directory (default C<'sql'>) that scripts are stored in |
cfc9edf9 |
684 | |
eb28403b |
685 | =attr databases |
cfc9edf9 |
686 | |
687 | The types of databases (default C<< [qw( MySQL SQLite PostgreSQL )] >>) to |
688 | generate files for |
689 | |
eb28403b |
690 | =attr txn_wrap |
691 | |
bcc72297 |
692 | Set to true (which is the default) to wrap all upgrades and deploys in a single |
693 | transaction. |
694 | |
73caa630 |
695 | =attr schema_version |
696 | |
697 | The version the schema on your harddrive is at. Defaults to |
698 | C<< $self->schema->schema_version >>. |
699 | |
db223aff |
700 | =begin comment |
701 | |
702 | =head2 __ddl_consume_with_prefix |
a65184c8 |
703 | |
bcc72297 |
704 | $dm->__ddl_consume_with_prefix( 'SQLite', [qw( 1.00 1.01 )], 'up' ) |
705 | |
706 | This is the meat of the multi-file upgrade/deploy stuff. It returns a list of |
707 | files in the order that they should be run for a generic "type" of upgrade. |
708 | You should not be calling this in user code. |
709 | |
db223aff |
710 | =head2 _ddl_schema_consume_filenames |
a65184c8 |
711 | |
bcc72297 |
712 | $dm->__ddl_schema_consume_filenames( 'SQLite', [qw( 1.00 )] ) |
713 | |
714 | Just a curried L</__ddl_consume_with_prefix>. Get's a list of files for an |
715 | initial deploy. |
716 | |
db223aff |
717 | =head2 _ddl_schema_produce_filename |
a65184c8 |
718 | |
bcc72297 |
719 | $dm->__ddl_schema_produce_filename( 'SQLite', [qw( 1.00 )] ) |
720 | |
721 | Returns a single file in which an initial schema will be stored. |
722 | |
db223aff |
723 | =head2 _ddl_schema_up_consume_filenames |
a65184c8 |
724 | |
bcc72297 |
725 | $dm->_ddl_schema_up_consume_filenames( 'SQLite', [qw( 1.00 )] ) |
726 | |
727 | Just a curried L</__ddl_consume_with_prefix>. Get's a list of files for an |
728 | upgrade. |
729 | |
db223aff |
730 | =head2 _ddl_schema_down_consume_filenames |
a65184c8 |
731 | |
bcc72297 |
732 | $dm->_ddl_schema_down_consume_filenames( 'SQLite', [qw( 1.00 )] ) |
733 | |
734 | Just a curried L</__ddl_consume_with_prefix>. Get's a list of files for a |
735 | downgrade. |
736 | |
db223aff |
737 | =head2 _ddl_schema_up_produce_filenames |
a65184c8 |
738 | |
bcc72297 |
739 | $dm->_ddl_schema_up_produce_filename( 'SQLite', [qw( 1.00 1.01 )] ) |
740 | |
741 | Returns a single file in which the sql to upgrade from one schema to another |
742 | will be stored. |
743 | |
db223aff |
744 | =head2 _ddl_schema_down_produce_filename |
bcc72297 |
745 | |
746 | $dm->_ddl_schema_down_produce_filename( 'SQLite', [qw( 1.00 1.01 )] ) |
747 | |
748 | Returns a single file in which the sql to downgrade from one schema to another |
749 | will be stored. |
a65184c8 |
750 | |
db223aff |
751 | =head2 _resultsource_install_filename |
a65184c8 |
752 | |
bcc72297 |
753 | my $filename_fn = $dm->_resultsource_install_filename('User'); |
754 | $dm->$filename_fn('SQLite', '1.00') |
755 | |
756 | Returns a function which in turn returns a single filename used to install a |
757 | single resultsource. Weird interface is convenient for me. Deal with it. |
758 | |
db223aff |
759 | =head2 _run_sql_and_perl |
eb28403b |
760 | |
bcc72297 |
761 | $dm->_run_sql_and_perl([qw( list of filenames )]) |
a65184c8 |
762 | |
bcc72297 |
763 | Simply put, this runs the list of files passed to it. If the file ends in |
764 | C<.sql> it runs it as sql and if it ends in C<.pl> it runs it as a perl file. |
a65184c8 |
765 | |
bcc72297 |
766 | Depending on L</txn_wrap> all of the files run will be wrapped in a single |
767 | transaction. |
eb28403b |
768 | |
db223aff |
769 | =head2 _prepare_install |
a65184c8 |
770 | |
bcc72297 |
771 | $dm->_prepare_install({ add_drop_table => 0 }, sub { 'file_to_create' }) |
a65184c8 |
772 | |
bcc72297 |
773 | Generates the sql file for installing the database. First arg is simply |
774 | L<SQL::Translator> args and the second is a coderef that returns the filename |
775 | to store the sql in. |
a65184c8 |
776 | |
db223aff |
777 | =head2 _prepare_changegrade |
bcc72297 |
778 | |
779 | $dm->_prepare_changegrade('1.00', '1.01', [qw( 1.00 1.01)], 'up') |
a65184c8 |
780 | |
bcc72297 |
781 | Generates the sql file for migrating from one schema version to another. First |
782 | arg is the version to start from, second is the version to go to, third is the |
783 | L<version set|DBIx::Class::DeploymentHandler/VERSION SET>, and last is the |
784 | direction of the changegrade, be it 'up' or 'down'. |
a65184c8 |
785 | |
db223aff |
786 | =head2 _read_sql_file |
a65184c8 |
787 | |
bcc72297 |
788 | $dm->_read_sql_file('foo.sql') |
a65184c8 |
789 | |
bcc72297 |
790 | Reads a sql file and returns lines in an C<ArrayRef>. Strips out comments, |
791 | transactions, and blank lines. |
eb28403b |
792 | |
db223aff |
793 | =end comment |