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 ); |
0df68524 |
8 | use Log::Contextual::WarnLogger; |
9 | use Log::Contextual qw(:log :dlog), -default_logger => Log::Contextual::WarnLogger->new({ |
10 | env_prefix => 'DBICDH' |
11 | }); |
12 | use Data::Dumper::Concise; |
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 | |
5020d6a2 |
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 |
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; |
41219a5d |
100 | my %files = map { $_ => "$dir/$_" } grep { /\.(?:sql|pl)$/ && -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 | |
76d311e7 |
124 | method _ddl_schema_produce_filename($type, $version) { |
91adde75 |
125 | my $dirname = catfile( $self->script_directory, $type, 'schema', $version ); |
76d08d08 |
126 | mkpath($dirname) unless -d $dirname; |
d54b8d69 |
127 | |
76d08d08 |
128 | return catfile( $dirname, '001-auto.sql' ); |
d54b8d69 |
129 | } |
130 | |
76d311e7 |
131 | method _ddl_schema_up_consume_filenames($type, $versions) { |
132 | $self->__ddl_consume_with_prefix($type, $versions, 'up') |
3c1b5ee8 |
133 | } |
134 | |
76d311e7 |
135 | method _ddl_schema_down_consume_filenames($type, $versions) { |
136 | $self->__ddl_consume_with_prefix($type, $versions, 'down') |
a41a04e5 |
137 | } |
138 | |
76d311e7 |
139 | method _ddl_schema_up_produce_filename($type, $versions) { |
91adde75 |
140 | my $dir = $self->script_directory; |
76d311e7 |
141 | |
76d08d08 |
142 | my $dirname = catfile( $dir, $type, 'up', join q(-), @{$versions}); |
143 | mkpath($dirname) unless -d $dirname; |
a41a04e5 |
144 | |
76d08d08 |
145 | return catfile( $dirname, '001-auto.sql' |
a41a04e5 |
146 | ); |
147 | } |
148 | |
76d311e7 |
149 | method _ddl_schema_down_produce_filename($type, $versions, $dir) { |
76d08d08 |
150 | my $dirname = catfile( $dir, $type, 'down', join q(-), @{$versions} ); |
151 | mkpath($dirname) unless -d $dirname; |
24f4524b |
152 | |
76d08d08 |
153 | return catfile( $dirname, '001-auto.sql'); |
24f4524b |
154 | } |
155 | |
41219a5d |
156 | method _run_sql_and_perl($filenames) { |
157 | my @files = @{$filenames}; |
158 | my $storage = $self->storage; |
2e68a8e1 |
159 | |
c8a2f7bd |
160 | |
a7d53deb |
161 | my $guard = $self->schema->txn_scope_guard if $self->txn_wrap; |
162 | |
41219a5d |
163 | my $sql; |
164 | for my $filename (@files) { |
165 | if ($filename =~ /\.sql$/) { |
0df68524 |
166 | log_debug { "[DBICDH] Running SQL from $filename" }; |
41219a5d |
167 | my @sql = @{$self->_read_sql_file($filename)}; |
168 | $sql .= join "\n", @sql; |
0df68524 |
169 | log_trace { "[DBICDH] Running SQL $sql" }; |
41219a5d |
170 | |
171 | foreach my $line (@sql) { |
172 | $storage->_query_start($line); |
173 | try { |
174 | # do a dbh_do cycle here, as we need some error checking in |
175 | # place (even though we will ignore errors) |
176 | $storage->dbh_do (sub { $_[1]->do($line) }); |
177 | } |
178 | catch { |
179 | carp "$_ (running '${line}')" |
180 | } |
181 | $storage->_query_end($line); |
182 | } |
0841a743 |
183 | } elsif ( $filename =~ /^(.+)\.pl$/ ) { |
0df68524 |
184 | log_debug { "[DBICDH] Running Perl from $filename" }; |
0841a743 |
185 | my $filedata = do { local( @ARGV, $/ ) = $filename; <> }; |
0841a743 |
186 | |
187 | no warnings 'redefine'; |
5b5defbc |
188 | my $fn = eval "$filedata"; |
0841a743 |
189 | use warnings; |
0df68524 |
190 | log_trace { '[DBICDH] Running Perl ' . Dumper($fn) }; |
0841a743 |
191 | |
9faec51a |
192 | if ($@) { |
5b5defbc |
193 | carp "$filename failed to compile: $@"; |
9faec51a |
194 | } elsif (ref $fn eq 'CODE') { |
5b5defbc |
195 | $fn->($self->schema) |
98c9484a |
196 | } else { |
5b5defbc |
197 | carp "$filename should define an anonymouse sub that takes a schema but it didn't!"; |
98c9484a |
198 | } |
41219a5d |
199 | } else { |
fc4b7602 |
200 | croak "A file ($filename) got to deploy that wasn't sql or perl!"; |
2e68a8e1 |
201 | } |
2e68a8e1 |
202 | } |
a7d53deb |
203 | |
204 | $guard->commit if $self->txn_wrap; |
41219a5d |
205 | |
206 | return $sql; |
207 | } |
208 | |
209 | sub deploy { |
210 | my $self = shift; |
be140a5f |
211 | my $version = (shift @_ || {})->{version} || $self->schema_version; |
0df68524 |
212 | log_info { "[DBICDH] deploying version $version" }; |
41219a5d |
213 | |
214 | return $self->_run_sql_and_perl($self->_ddl_schema_consume_filenames( |
215 | $self->storage->sqlt_type, |
92c34cab |
216 | $version, |
41219a5d |
217 | )); |
2e68a8e1 |
218 | } |
219 | |
80ff6f6d |
220 | sub preinstall { |
9faec51a |
221 | my $self = shift; |
222 | my $args = shift; |
223 | my $version = $args->{version} || $self->schema_version; |
0df68524 |
224 | log_info { "[DBICDH] preinstalling version $version" }; |
9faec51a |
225 | my $storage_type = $args->{storage_type} || $self->storage->sqlt_type; |
fc4b7602 |
226 | |
227 | my @files = @{$self->_ddl_preinstall_consume_filenames( |
9faec51a |
228 | $storage_type, |
fc4b7602 |
229 | $version, |
230 | )}; |
231 | |
232 | for my $filename (@files) { |
233 | # We ignore sql for now (till I figure out what to do with it) |
234 | if ( $filename =~ /^(.+)\.pl$/ ) { |
fc4b7602 |
235 | my $filedata = do { local( @ARGV, $/ ) = $filename; <> }; |
fc4b7602 |
236 | |
9faec51a |
237 | no warnings 'redefine'; |
5b5defbc |
238 | my $fn = eval "$filedata"; |
fc4b7602 |
239 | use warnings; |
5b5defbc |
240 | |
9faec51a |
241 | if ($@) { |
3fa64c79 |
242 | carp "$filename failed to compile: $@"; |
9faec51a |
243 | } elsif (ref $fn eq 'CODE') { |
fc4b7602 |
244 | $fn->() |
245 | } else { |
5b5defbc |
246 | carp "$filename should define an anonymous sub but it didn't!"; |
fc4b7602 |
247 | } |
248 | } else { |
249 | croak "A file ($filename) got to preinstall_scripts that wasn't sql or perl!"; |
250 | } |
251 | } |
252 | } |
253 | |
c8a2f7bd |
254 | sub _prepare_install { |
73caa630 |
255 | my $self = shift; |
02a7b8ac |
256 | my $sqltargs = { %{$self->sql_translator_args}, %{shift @_} }; |
c8a2f7bd |
257 | my $to_file = shift; |
2e68a8e1 |
258 | my $schema = $self->schema; |
259 | my $databases = $self->databases; |
91adde75 |
260 | my $dir = $self->script_directory; |
73caa630 |
261 | my $version = $self->schema_version; |
d54b8d69 |
262 | |
9600776d |
263 | my $sqlt = SQL::Translator->new({ |
d54b8d69 |
264 | add_drop_table => 1, |
2e68a8e1 |
265 | ignore_constraint_names => 1, |
d54b8d69 |
266 | ignore_index_names => 1, |
267 | parser => 'SQL::Translator::Parser::DBIx::Class', |
3aaf766f |
268 | %{$sqltargs} |
9600776d |
269 | }); |
2e68a8e1 |
270 | |
d53e0bfc |
271 | my $sqlt_schema = $sqlt->translate( data => $schema ) |
387b11d2 |
272 | or croak($sqlt->error); |
2e68a8e1 |
273 | |
274 | foreach my $db (@$databases) { |
275 | $sqlt->reset; |
276 | $sqlt->{schema} = $sqlt_schema; |
277 | $sqlt->producer($db); |
278 | |
c8a2f7bd |
279 | my $filename = $self->$to_file($db, $version, $dir); |
9600776d |
280 | if (-e $filename ) { |
2e68a8e1 |
281 | carp "Overwriting existing DDL file - $filename"; |
282 | unlink $filename; |
283 | } |
284 | |
285 | my $output = $sqlt->translate; |
286 | if(!$output) { |
287 | carp("Failed to translate to $db, skipping. (" . $sqlt->error . ")"); |
288 | next; |
289 | } |
387b11d2 |
290 | open my $file, q(>), $filename; |
2e68a8e1 |
291 | print {$file} $output; |
292 | close $file; |
293 | } |
294 | } |
295 | |
c8a2f7bd |
296 | sub _resultsource_install_filename { |
297 | my ($self, $source_name) = @_; |
298 | return sub { |
299 | my ($self, $type, $version) = @_; |
91adde75 |
300 | my $dirname = catfile( $self->script_directory, $type, 'schema', $version ); |
c8a2f7bd |
301 | mkpath($dirname) unless -d $dirname; |
302 | |
303 | return catfile( $dirname, "001-auto-$source_name.sql" ); |
304 | } |
305 | } |
306 | |
307 | sub install_resultsource { |
be140a5f |
308 | my ($self, $args) = @_; |
309 | my $source = $args->{result_source}; |
310 | my $version = $args->{version}; |
0df68524 |
311 | log_info { '[DBICDH] installing_resultsource ' . $source->source_name . ", version $version" }; |
c8a2f7bd |
312 | my $rs_install_file = |
313 | $self->_resultsource_install_filename($source->source_name); |
314 | |
315 | my $files = [ |
316 | $self->$rs_install_file( |
317 | $self->storage->sqlt_type, |
318 | $version, |
319 | ) |
320 | ]; |
321 | $self->_run_sql_and_perl($files); |
322 | } |
323 | |
324 | sub prepare_resultsource_install { |
325 | my $self = shift; |
be140a5f |
326 | my $source = (shift @_)->{result_source}; |
0df68524 |
327 | log_info { '[DBICDH] preparing install for resultsource ' . $source->source_name }; |
c8a2f7bd |
328 | |
329 | my $filename = $self->_resultsource_install_filename($source->source_name); |
330 | $self->_prepare_install({ |
331 | parser_args => { sources => [$source->source_name], } |
332 | }, $filename); |
333 | } |
334 | |
91557c90 |
335 | sub prepare_deploy { |
0df68524 |
336 | log_info { '[DBICDH] preparing deploy' }; |
c8a2f7bd |
337 | my $self = shift; |
338 | $self->_prepare_install({}, '_ddl_schema_produce_filename'); |
339 | } |
340 | |
a41a04e5 |
341 | sub prepare_upgrade { |
be140a5f |
342 | my ($self, $args) = @_; |
0df68524 |
343 | log_info { |
344 | '[DBICDH] preparing upgrade ' . |
345 | "from $args->{from_version} to $args->{to_version}" |
346 | }; |
be140a5f |
347 | $self->_prepare_changegrade( |
348 | $args->{from_version}, $args->{to_version}, $args->{version_set}, 'up' |
349 | ); |
76d311e7 |
350 | } |
351 | |
352 | sub prepare_downgrade { |
be140a5f |
353 | my ($self, $args) = @_; |
0df68524 |
354 | log_info { |
355 | '[DBICDH] preparing downgrade ' . |
356 | "from $args->{from_version} to $args->{to_version}" |
357 | }; |
be140a5f |
358 | $self->_prepare_changegrade( |
359 | $args->{from_version}, $args->{to_version}, $args->{version_set}, 'down' |
360 | ); |
76d311e7 |
361 | } |
362 | |
363 | method _prepare_changegrade($from_version, $to_version, $version_set, $direction) { |
2e68a8e1 |
364 | my $schema = $self->schema; |
365 | my $databases = $self->databases; |
91adde75 |
366 | my $dir = $self->script_directory; |
02a7b8ac |
367 | my $sqltargs = $self->sql_translator_args; |
2e68a8e1 |
368 | |
73caa630 |
369 | my $schema_version = $self->schema_version; |
2e68a8e1 |
370 | |
371 | $sqltargs = { |
372 | add_drop_table => 1, |
373 | ignore_constraint_names => 1, |
374 | ignore_index_names => 1, |
375 | %{$sqltargs} |
376 | }; |
377 | |
378 | my $sqlt = SQL::Translator->new( $sqltargs ); |
379 | |
380 | $sqlt->parser('SQL::Translator::Parser::DBIx::Class'); |
d53e0bfc |
381 | my $sqlt_schema = $sqlt->translate( data => $schema ) |
387b11d2 |
382 | or croak($sqlt->error); |
2e68a8e1 |
383 | |
384 | foreach my $db (@$databases) { |
385 | $sqlt->reset; |
386 | $sqlt->{schema} = $sqlt_schema; |
387 | $sqlt->producer($db); |
388 | |
76d311e7 |
389 | my $prefilename = $self->_ddl_schema_produce_filename($db, $from_version, $dir); |
2e68a8e1 |
390 | unless(-e $prefilename) { |
391 | carp("No previous schema file found ($prefilename)"); |
392 | next; |
393 | } |
76d311e7 |
394 | my $diff_file_method = "_ddl_schema_${direction}_produce_filename"; |
395 | my $diff_file = $self->$diff_file_method($db, $version_set, $dir ); |
2e68a8e1 |
396 | if(-e $diff_file) { |
76d311e7 |
397 | carp("Overwriting existing $direction-diff file - $diff_file"); |
2e68a8e1 |
398 | unlink $diff_file; |
399 | } |
400 | |
401 | my $source_schema; |
402 | { |
403 | my $t = SQL::Translator->new({ |
404 | %{$sqltargs}, |
405 | debug => 0, |
406 | trace => 0, |
407 | }); |
408 | |
409 | $t->parser( $db ) # could this really throw an exception? |
387b11d2 |
410 | or croak($t->error); |
2e68a8e1 |
411 | |
412 | my $out = $t->translate( $prefilename ) |
387b11d2 |
413 | or croak($t->error); |
2e68a8e1 |
414 | |
415 | $source_schema = $t->schema; |
416 | |
417 | $source_schema->name( $prefilename ) |
418 | unless $source_schema->name; |
419 | } |
420 | |
421 | # The "new" style of producers have sane normalization and can support |
422 | # diffing a SQL file against a DBIC->SQLT schema. Old style ones don't |
423 | # And we have to diff parsed SQL against parsed SQL. |
424 | my $dest_schema = $sqlt_schema; |
425 | |
426 | unless ( "SQL::Translator::Producer::$db"->can('preprocess_schema') ) { |
427 | my $t = SQL::Translator->new({ |
428 | %{$sqltargs}, |
429 | debug => 0, |
430 | trace => 0, |
431 | }); |
432 | |
433 | $t->parser( $db ) # could this really throw an exception? |
387b11d2 |
434 | or croak($t->error); |
2e68a8e1 |
435 | |
76d311e7 |
436 | my $filename = $self->_ddl_schema_produce_filename($db, $to_version, $dir); |
2e68a8e1 |
437 | my $out = $t->translate( $filename ) |
387b11d2 |
438 | or croak($t->error); |
2e68a8e1 |
439 | |
440 | $dest_schema = $t->schema; |
441 | |
442 | $dest_schema->name( $filename ) |
443 | unless $dest_schema->name; |
444 | } |
445 | |
446 | my $diff = SQL::Translator::Diff::schema_diff( |
447 | $source_schema, $db, |
448 | $dest_schema, $db, |
449 | $sqltargs |
450 | ); |
387b11d2 |
451 | open my $file, q(>), $diff_file; |
2e68a8e1 |
452 | print {$file} $diff; |
453 | close $file; |
454 | } |
455 | } |
456 | |
334bced5 |
457 | method _read_sql_file($file) { |
458 | return unless $file; |
459 | |
aabd4237 |
460 | open my $fh, '<', $file; |
0d19af1d |
461 | my @data = split /;\n/, join '', <$fh>; |
334bced5 |
462 | close $fh; |
463 | |
464 | @data = grep { |
0d19af1d |
465 | $_ && # remove blank lines |
466 | !/^(BEGIN|BEGIN TRANSACTION|COMMIT)/ # strip txn's |
467 | } map { |
468 | s/^\s+//; s/\s+$//; # trim whitespace |
469 | join '', grep { !/^--/ } split /\n/ # remove comments |
470 | } @data; |
334bced5 |
471 | |
472 | return \@data; |
473 | } |
474 | |
7d2a6974 |
475 | sub downgrade_single_step { |
76d311e7 |
476 | my $self = shift; |
be140a5f |
477 | my $version_set = (shift @_)->{version_set}; |
0df68524 |
478 | log_info { qq([DBICDH] downgrade_single_step'ing ) . Dumper($version_set) }; |
41219a5d |
479 | |
480 | my $sql = $self->_run_sql_and_perl($self->_ddl_schema_down_consume_filenames( |
76d311e7 |
481 | $self->storage->sqlt_type, |
627581cd |
482 | $version_set, |
41219a5d |
483 | )); |
3249629f |
484 | |
41219a5d |
485 | return ['', $sql]; |
76d311e7 |
486 | } |
487 | |
7d2a6974 |
488 | sub upgrade_single_step { |
7521a845 |
489 | my $self = shift; |
be140a5f |
490 | my $version_set = (shift @_)->{version_set}; |
0df68524 |
491 | log_info { qq([DBICDH] upgrade_single_step'ing ) . Dumper($version_set) }; |
41219a5d |
492 | |
493 | my $sql = $self->_run_sql_and_perl($self->_ddl_schema_up_consume_filenames( |
334bced5 |
494 | $self->storage->sqlt_type, |
627581cd |
495 | $version_set, |
41219a5d |
496 | )); |
497 | return ['', $sql]; |
334bced5 |
498 | } |
499 | |
aabd4237 |
500 | __PACKAGE__->meta->make_immutable; |
501 | |
2e68a8e1 |
502 | 1; |
e051bb00 |
503 | |
e52174e3 |
504 | # vim: ts=2 sw=2 expandtab |
505 | |
e051bb00 |
506 | __END__ |
507 | |
bcc72297 |
508 | =head1 DESCRIPTION |
509 | |
510 | This class is the meat of L<DBIx::Class::DeploymentHandler>. It takes care of |
511 | generating sql files representing schemata as well as sql files to move from |
512 | one version of a schema to the rest. One of the hallmark features of this |
513 | class is that it allows for multiple sql files for deploy and upgrade, allowing |
514 | developers to fine tune deployment. In addition it also allows for perl files |
515 | to be run at any stage of the process. |
516 | |
517 | For basic usage see L<DBIx::Class::DeploymentHandler::HandlesDeploy>. What's |
518 | documented here is extra fun stuff or private methods. |
519 | |
520 | =head1 DIRECTORY LAYOUT |
521 | |
92c34cab |
522 | Arguably this is the best feature of L<DBIx::Class::DeploymentHandler>. It's |
523 | heavily based upon L<DBIx::Migration::Directories>, but has some extensions and |
524 | modifications, so even if you are familiar with it, please read this. I feel |
525 | like the best way to describe the layout is with the following example: |
526 | |
527 | $sql_migration_dir |
528 | |- SQLite |
529 | | |- down |
4f85efc6 |
530 | | | `- 2-1 |
92c34cab |
531 | | | `- 001-auto.sql |
532 | | |- schema |
533 | | | `- 1 |
534 | | | `- 001-auto.sql |
535 | | `- up |
536 | | |- 1-2 |
537 | | | `- 001-auto.sql |
538 | | `- 2-3 |
539 | | `- 001-auto.sql |
540 | |- _common |
541 | | |- down |
4f85efc6 |
542 | | | `- 2-1 |
92c34cab |
543 | | | `- 002-remove-customers.pl |
544 | | `- up |
545 | | `- 1-2 |
546 | | `- 002-generate-customers.pl |
547 | |- _generic |
548 | | |- down |
4f85efc6 |
549 | | | `- 2-1 |
92c34cab |
550 | | | `- 001-auto.sql |
551 | | |- schema |
552 | | | `- 1 |
553 | | | `- 001-auto.sql |
554 | | `- up |
555 | | `- 1-2 |
556 | | |- 001-auto.sql |
557 | | `- 002-create-stored-procedures.sql |
558 | `- MySQL |
559 | |- down |
4f85efc6 |
560 | | `- 2-1 |
92c34cab |
561 | | `- 001-auto.sql |
80ff6f6d |
562 | |- preinstall |
563 | | `- 1 |
564 | | |- 001-create_database.pl |
565 | | `- 002-create_users_and_permissions.pl |
92c34cab |
566 | |- schema |
567 | | `- 1 |
568 | | `- 001-auto.sql |
569 | `- up |
570 | `- 1-2 |
571 | `- 001-auto.sql |
572 | |
573 | So basically, the code |
574 | |
575 | $dm->deploy(1) |
576 | |
577 | on an C<SQLite> database that would simply run |
578 | C<$sql_migration_dir/SQLite/schema/1/001-auto.sql>. Next, |
579 | |
580 | $dm->upgrade_single_step([1,2]) |
581 | |
582 | would run C<$sql_migration_dir/SQLite/up/1-2/001-auto.sql> followed by |
583 | C<$sql_migration_dir/_common/up/1-2/002-generate-customers.pl>. |
584 | |
585 | Now, a C<.pl> file doesn't have to be in the C<_common> directory, but most of |
586 | the time it probably should be, since perl scripts will mostly be database |
587 | independent. |
588 | |
589 | C<_generic> exists for when you for some reason are sure that your SQL is |
590 | generic enough to run on all databases. Good luck with that one. |
591 | |
80ff6f6d |
592 | Note that unlike most steps in the process, C<preinstall> will not run SQL, as |
593 | there may not even be an database at preinstall time. It will run perl scripts |
594 | just like the other steps in the process, but nothing is passed to them. |
595 | Until people have used this more it will remain freeform, but a recommended use |
596 | of preinstall is to have it prompt for username and password, and then call the |
597 | appropriate C<< CREATE DATABASE >> commands etc. |
598 | |
92c34cab |
599 | =head1 PERL SCRIPTS |
600 | |
7d0b0f2b |
601 | A perl script for this tool is very simple. It merely needs to contain an |
602 | anonymous sub that takes a L<DBIx::Class::Schema> as it's only argument. |
92c34cab |
603 | A very basic perl script might look like: |
604 | |
605 | #!perl |
606 | |
607 | use strict; |
608 | use warnings; |
609 | |
7d0b0f2b |
610 | sub { |
92c34cab |
611 | my $schema = shift; |
612 | |
613 | $schema->resultset('Users')->create({ |
614 | name => 'root', |
615 | password => 'root', |
616 | }) |
617 | } |
bcc72297 |
618 | |
eb28403b |
619 | =attr schema |
a65184c8 |
620 | |
bcc72297 |
621 | The L<DBIx::Class::Schema> (B<required>) that is used to talk to the database |
622 | and generate the DDL. |
623 | |
eb28403b |
624 | =attr storage |
a65184c8 |
625 | |
bcc72297 |
626 | The L<DBIx::Class::Storage> that is I<actually> used to talk to the database |
627 | and generate the DDL. This is automatically created with L</_build_storage>. |
628 | |
02a7b8ac |
629 | =attr sql_translator_args |
cfc9edf9 |
630 | |
02a7b8ac |
631 | The arguments that get passed to L<SQL::Translator> when it's used. |
a65184c8 |
632 | |
91adde75 |
633 | =attr script_directory |
cfc9edf9 |
634 | |
91adde75 |
635 | The directory (default C<'sql'>) that scripts are stored in |
cfc9edf9 |
636 | |
eb28403b |
637 | =attr databases |
cfc9edf9 |
638 | |
639 | The types of databases (default C<< [qw( MySQL SQLite PostgreSQL )] >>) to |
640 | generate files for |
641 | |
eb28403b |
642 | =attr txn_wrap |
643 | |
bcc72297 |
644 | Set to true (which is the default) to wrap all upgrades and deploys in a single |
645 | transaction. |
646 | |
73caa630 |
647 | =attr schema_version |
648 | |
649 | The version the schema on your harddrive is at. Defaults to |
650 | C<< $self->schema->schema_version >>. |
651 | |
db223aff |
652 | =begin comment |
653 | |
654 | =head2 __ddl_consume_with_prefix |
a65184c8 |
655 | |
bcc72297 |
656 | $dm->__ddl_consume_with_prefix( 'SQLite', [qw( 1.00 1.01 )], 'up' ) |
657 | |
658 | This is the meat of the multi-file upgrade/deploy stuff. It returns a list of |
659 | files in the order that they should be run for a generic "type" of upgrade. |
660 | You should not be calling this in user code. |
661 | |
db223aff |
662 | =head2 _ddl_schema_consume_filenames |
a65184c8 |
663 | |
bcc72297 |
664 | $dm->__ddl_schema_consume_filenames( 'SQLite', [qw( 1.00 )] ) |
665 | |
666 | Just a curried L</__ddl_consume_with_prefix>. Get's a list of files for an |
667 | initial deploy. |
668 | |
db223aff |
669 | =head2 _ddl_schema_produce_filename |
a65184c8 |
670 | |
bcc72297 |
671 | $dm->__ddl_schema_produce_filename( 'SQLite', [qw( 1.00 )] ) |
672 | |
673 | Returns a single file in which an initial schema will be stored. |
674 | |
db223aff |
675 | =head2 _ddl_schema_up_consume_filenames |
a65184c8 |
676 | |
bcc72297 |
677 | $dm->_ddl_schema_up_consume_filenames( 'SQLite', [qw( 1.00 )] ) |
678 | |
679 | Just a curried L</__ddl_consume_with_prefix>. Get's a list of files for an |
680 | upgrade. |
681 | |
db223aff |
682 | =head2 _ddl_schema_down_consume_filenames |
a65184c8 |
683 | |
bcc72297 |
684 | $dm->_ddl_schema_down_consume_filenames( 'SQLite', [qw( 1.00 )] ) |
685 | |
686 | Just a curried L</__ddl_consume_with_prefix>. Get's a list of files for a |
687 | downgrade. |
688 | |
db223aff |
689 | =head2 _ddl_schema_up_produce_filenames |
a65184c8 |
690 | |
bcc72297 |
691 | $dm->_ddl_schema_up_produce_filename( 'SQLite', [qw( 1.00 1.01 )] ) |
692 | |
693 | Returns a single file in which the sql to upgrade from one schema to another |
694 | will be stored. |
695 | |
db223aff |
696 | =head2 _ddl_schema_down_produce_filename |
bcc72297 |
697 | |
698 | $dm->_ddl_schema_down_produce_filename( 'SQLite', [qw( 1.00 1.01 )] ) |
699 | |
700 | Returns a single file in which the sql to downgrade from one schema to another |
701 | will be stored. |
a65184c8 |
702 | |
db223aff |
703 | =head2 _resultsource_install_filename |
a65184c8 |
704 | |
bcc72297 |
705 | my $filename_fn = $dm->_resultsource_install_filename('User'); |
706 | $dm->$filename_fn('SQLite', '1.00') |
707 | |
708 | Returns a function which in turn returns a single filename used to install a |
709 | single resultsource. Weird interface is convenient for me. Deal with it. |
710 | |
db223aff |
711 | =head2 _run_sql_and_perl |
eb28403b |
712 | |
bcc72297 |
713 | $dm->_run_sql_and_perl([qw( list of filenames )]) |
a65184c8 |
714 | |
bcc72297 |
715 | Simply put, this runs the list of files passed to it. If the file ends in |
716 | C<.sql> it runs it as sql and if it ends in C<.pl> it runs it as a perl file. |
a65184c8 |
717 | |
bcc72297 |
718 | Depending on L</txn_wrap> all of the files run will be wrapped in a single |
719 | transaction. |
eb28403b |
720 | |
db223aff |
721 | =head2 _prepare_install |
a65184c8 |
722 | |
bcc72297 |
723 | $dm->_prepare_install({ add_drop_table => 0 }, sub { 'file_to_create' }) |
a65184c8 |
724 | |
bcc72297 |
725 | Generates the sql file for installing the database. First arg is simply |
726 | L<SQL::Translator> args and the second is a coderef that returns the filename |
727 | to store the sql in. |
a65184c8 |
728 | |
db223aff |
729 | =head2 _prepare_changegrade |
bcc72297 |
730 | |
731 | $dm->_prepare_changegrade('1.00', '1.01', [qw( 1.00 1.01)], 'up') |
a65184c8 |
732 | |
bcc72297 |
733 | Generates the sql file for migrating from one schema version to another. First |
734 | arg is the version to start from, second is the version to go to, third is the |
735 | L<version set|DBIx::Class::DeploymentHandler/VERSION SET>, and last is the |
736 | direction of the changegrade, be it 'up' or 'down'. |
a65184c8 |
737 | |
db223aff |
738 | =head2 _read_sql_file |
a65184c8 |
739 | |
bcc72297 |
740 | $dm->_read_sql_file('foo.sql') |
a65184c8 |
741 | |
bcc72297 |
742 | Reads a sql file and returns lines in an C<ArrayRef>. Strips out comments, |
743 | transactions, and blank lines. |
eb28403b |
744 | |
db223aff |
745 | =end comment |