Commit | Line | Data |
b974984a |
1 | package DBIx::Class::DeploymentHandler; |
2 | |
3 | use Moose; |
4 | use Method::Signatures::Simple; |
12fdd461 |
5 | require DBIx::Class::Schema; # loaded for type constraint |
6 | require DBIx::Class::Storage; # loaded for type constraint |
7 | require DBIx::Class::ResultSet; # loaded for type constraint |
e1f67607 |
8 | use Carp::Clan '^DBIx::Class::DeploymentHandler'; |
ecc3b6be |
9 | use SQL::Translator; |
b974984a |
10 | |
cf400f48 |
11 | BEGIN { |
12 | use Moose::Util::TypeConstraints; |
13 | subtype 'DBIx::Class::DeploymentHandler::Databases' |
14 | => as 'ArrayRef[Str]'; |
15 | |
16 | coerce 'DBIx::Class::DeploymentHandler::Databases' |
17 | => from 'Str' |
18 | => via { [$_] }; |
19 | no Moose::Util::TypeConstraints; |
20 | } |
21 | |
b974984a |
22 | has schema => ( |
61847972 |
23 | isa => 'DBIx::Class::Schema', |
24 | is => 'ro', |
25 | required => 1, |
4ea147c6 |
26 | handles => [qw( ddl_filename schema_version )], |
b974984a |
27 | ); |
28 | |
29 | has upgrade_directory => ( |
61847972 |
30 | isa => 'Str', |
31 | is => 'ro', |
32 | required => 1, |
4ea147c6 |
33 | default => 'sql', |
b974984a |
34 | ); |
35 | |
36 | has backup_directory => ( |
61847972 |
37 | isa => 'Str', |
38 | is => 'ro', |
b974984a |
39 | ); |
40 | |
41 | has storage => ( |
61847972 |
42 | isa => 'DBIx::Class::Storage', |
43 | is => 'ro', |
44 | lazy_build => 1, |
b974984a |
45 | ); |
46 | |
7eec7eb7 |
47 | method _build_storage { |
61847972 |
48 | my $s = $self->schema->storage; |
49 | $s->_determine_driver; |
50 | $s |
7eec7eb7 |
51 | } |
12fdd461 |
52 | |
b974984a |
53 | has _filedata => ( |
4ea147c6 |
54 | isa => 'ArrayRef[Str]', |
61847972 |
55 | is => 'rw', |
b974984a |
56 | ); |
57 | |
58 | has do_backup => ( |
61847972 |
59 | isa => 'Bool', |
60 | is => 'ro', |
61 | default => undef, |
b974984a |
62 | ); |
63 | |
64 | has do_diff_on_init => ( |
61847972 |
65 | isa => 'Bool', |
66 | is => 'ro', |
67 | default => undef, |
b974984a |
68 | ); |
69 | |
12fdd461 |
70 | has version_rs => ( |
61847972 |
71 | isa => 'DBIx::Class::ResultSet', |
72 | is => 'ro', |
73 | lazy_build => 1, |
74 | handles => [qw( is_installed db_version )], |
12fdd461 |
75 | ); |
76 | |
9e401dc2 |
77 | has databases => ( |
cf400f48 |
78 | coerce => 1, |
79 | isa => 'DBIx::Class::DeploymentHandler::Databases', |
80 | is => 'ro', |
9e401dc2 |
81 | default => sub { [qw( MySQL SQLite PostgreSQL )] }, |
82 | ); |
83 | |
ecc3b6be |
84 | has sqltargs => ( |
85 | isa => 'HashRef', |
86 | is => 'ro', |
87 | default => sub { {} }, |
88 | ); |
89 | |
741620a6 |
90 | sub deployment_statements { |
91 | my ($self, $schema, $type, $version, $dir, $sqltargs) = @_; |
92 | $type ||= $self->storage->sqlt_type; |
93 | $version ||= $schema->schema_version || '1.x'; |
94 | $dir ||= './'; |
95 | my $filename = $schema->ddl_filename($type, $version, $dir); |
96 | if(-f $filename) |
97 | { |
98 | my $file; |
99 | open($file, "<$filename") |
100 | or $self->throw_exception("Can't open $filename ($!)"); |
101 | my @rows = <$file>; |
102 | close($file); |
103 | return join('', @rows); |
104 | } |
105 | |
106 | # sources needs to be a parser arg, but for simplicty allow at top level |
107 | # coming in |
108 | $sqltargs->{parser_args}{sources} = delete $sqltargs->{sources} |
109 | if exists $sqltargs->{sources}; |
110 | |
111 | my $tr = SQL::Translator->new( |
112 | producer => "SQL::Translator::Producer::${type}", |
113 | %$sqltargs, |
114 | parser => 'SQL::Translator::Parser::DBIx::Class', |
115 | data => $schema, |
116 | ); |
117 | |
118 | my @ret; |
119 | my $wa = wantarray; |
120 | if ($wa) { |
121 | @ret = $tr->translate; |
122 | } |
123 | else { |
124 | $ret[0] = $tr->translate; |
125 | } |
126 | |
127 | $schema->throw_exception( 'Unable to produce deployment statements: ' . $tr->error) |
128 | unless (@ret && defined $ret[0]); |
129 | |
130 | return $wa ? @ret : $ret[0]; |
131 | } |
132 | |
133 | method deploy { |
134 | my $schema = $self->schema; |
135 | my $type = undef; |
136 | my $sqltargs = $self->sqltargs; |
137 | my $dir = $self->upgrade_directory; |
138 | my $storage = $self->storage; |
139 | |
140 | my $deploy = sub { |
141 | my $line = shift; |
142 | return if($line =~ /^--/); |
143 | return if(!$line); |
144 | # next if($line =~ /^DROP/m); |
145 | return if($line =~ /^BEGIN TRANSACTION/m); |
146 | return if($line =~ /^COMMIT/m); |
147 | return if $line =~ /^\s+$/; # skip whitespace only |
148 | $storage->_query_start($line); |
149 | eval { |
150 | # do a dbh_do cycle here, as we need some error checking in |
151 | # place (even though we will ignore errors) |
152 | $storage->dbh_do (sub { $_[1]->do($line) }); |
153 | }; |
154 | if ($@) { |
155 | carp qq{$@ (running "${line}")} |
156 | } |
157 | $storage->_query_end($line); |
158 | }; |
159 | my @statements = $self->deployment_statements($schema, $type, undef, $dir, { %{ $sqltargs || {} }, no_comments => 1 } ); |
160 | if (@statements > 1) { |
161 | foreach my $statement (@statements) { |
162 | $deploy->( $statement ); |
163 | } |
164 | } |
165 | elsif (@statements == 1) { |
166 | foreach my $line ( split(";\n", $statements[0])) { |
167 | $deploy->( $line ); |
168 | } |
169 | } |
170 | } |
171 | |
30749dbf |
172 | method _build_version_rs { |
173 | $self->schema->set_us_up_the_bomb; |
174 | $self->schema->resultset('__VERSION') |
175 | } |
12fdd461 |
176 | |
177 | method backup { $self->storage->backup($self->backup_directory) } |
b974984a |
178 | |
179 | method install($new_version) { |
12fdd461 |
180 | carp 'Install not possible as versions table already exists in database' |
ceef4ff5 |
181 | if $self->is_installed; |
b974984a |
182 | |
12fdd461 |
183 | $new_version ||= $self->schema_version; |
b974984a |
184 | |
185 | if ($new_version) { |
741620a6 |
186 | $self->deploy(); |
12fdd461 |
187 | |
188 | $self->version_rs->create({ |
61847972 |
189 | version => $new_version, |
190 | # ddl => $ddl, |
191 | # upgrade_sql => $upgrade_sql, |
12fdd461 |
192 | }); |
b974984a |
193 | } |
194 | } |
195 | |
12fdd461 |
196 | method create_upgrade_path { } |
b974984a |
197 | |
8636376a |
198 | method ordered_schema_versions { undef } |
b974984a |
199 | |
200 | method upgrade { |
12fdd461 |
201 | my $db_version = $self->db_version; |
202 | my $schema_version = $self->schema_version; |
b974984a |
203 | |
b974984a |
204 | unless ($db_version) { |
61847972 |
205 | # croak? |
206 | carp 'Upgrade not possible as database is unversioned. Please call install first.'; |
207 | return; |
b974984a |
208 | } |
209 | |
12fdd461 |
210 | if ( $db_version eq $schema_version ) { |
61847972 |
211 | # croak? |
212 | carp "Upgrade not necessary\n"; |
213 | return; |
b974984a |
214 | } |
215 | |
12fdd461 |
216 | my @version_list = $self->ordered_schema_versions || |
217 | ( $db_version, $schema_version ); |
b974984a |
218 | |
219 | # remove all versions in list above the required version |
12fdd461 |
220 | while ( @version_list && ( $version_list[-1] ne $schema_version ) ) { |
61847972 |
221 | pop @version_list; |
b974984a |
222 | } |
223 | |
224 | # remove all versions in list below the current version |
12fdd461 |
225 | while ( @version_list && ( $version_list[0] ne $db_version ) ) { |
61847972 |
226 | shift @version_list; |
b974984a |
227 | } |
228 | |
229 | # check we have an appropriate list of versions |
12fdd461 |
230 | die if @version_list < 2; |
b974984a |
231 | |
232 | # do sets of upgrade |
12fdd461 |
233 | while ( @version_list >= 2 ) { |
61847972 |
234 | $self->upgrade_single_step( $version_list[0], $version_list[1] ); |
235 | shift @version_list; |
b974984a |
236 | } |
237 | } |
238 | |
239 | method upgrade_single_step($db_version, $target_version) { |
b974984a |
240 | if ($db_version eq $target_version) { |
e1f67607 |
241 | # croak? |
b974984a |
242 | carp "Upgrade not necessary\n"; |
243 | return; |
244 | } |
245 | |
b974984a |
246 | my $upgrade_file = $self->ddl_filename( |
12fdd461 |
247 | $self->storage->sqlt_type, |
248 | $target_version, |
249 | $self->upgrade_directory, |
250 | $db_version, |
251 | ); |
b974984a |
252 | |
253 | $self->create_upgrade_path({ upgrade_file => $upgrade_file }); |
254 | |
255 | unless (-f $upgrade_file) { |
e1f67607 |
256 | # croak? |
b974984a |
257 | carp "Upgrade not possible, no upgrade file found ($upgrade_file), please create one\n"; |
258 | return; |
259 | } |
260 | |
261 | carp "DB version ($db_version) is lower than the schema version (".$self->schema_version."). Attempting upgrade.\n"; |
262 | |
e1f67607 |
263 | $self->_filedata($self->_read_sql_file($upgrade_file)); # I don't like this --fREW 2010-02-22 |
12fdd461 |
264 | $self->backup if $self->do_backup; |
265 | $self->schema->txn_do(sub { $self->do_upgrade }); |
b974984a |
266 | |
12fdd461 |
267 | $self->version_rs->create({ |
61847972 |
268 | version => $target_version, |
269 | # ddl => $ddl, |
270 | # upgrade_sql => $upgrade_sql, |
12fdd461 |
271 | }); |
b974984a |
272 | } |
273 | |
ecc3b6be |
274 | method create_ddl_dir($version, $preversion) { |
275 | my $schema = $self->schema; |
9e401dc2 |
276 | my $databases = $self->databases; |
ecc3b6be |
277 | my $dir = $self->upgrade_directory; |
278 | my $sqltargs = $self->sqltargs; |
9e401dc2 |
279 | unless( -d $dir ) { |
280 | carp "Upgrade directory $dir does not exist, using ./\n"; |
8164e4d0 |
281 | $dir = "./"; |
282 | } |
8164e4d0 |
283 | |
284 | my $schema_version = $schema->schema_version || '1.x'; |
285 | $version ||= $schema_version; |
286 | |
287 | $sqltargs = { |
288 | add_drop_table => 1, |
289 | ignore_constraint_names => 1, |
290 | ignore_index_names => 1, |
291 | %{$sqltargs || {}} |
292 | }; |
293 | |
8164e4d0 |
294 | my $sqlt = SQL::Translator->new( $sqltargs ); |
295 | |
296 | $sqlt->parser('SQL::Translator::Parser::DBIx::Class'); |
297 | my $sqlt_schema = $sqlt->translate({ data => $schema }) |
298 | or $self->throw_exception ($sqlt->error); |
299 | |
300 | foreach my $db (@$databases) { |
c88c9aaa |
301 | $sqlt->reset; |
8164e4d0 |
302 | $sqlt->{schema} = $sqlt_schema; |
303 | $sqlt->producer($db); |
304 | |
c88c9aaa |
305 | my $filename = $self->ddl_filename($db, $version, $dir); |
8164e4d0 |
306 | if (-e $filename && ($version eq $schema_version )) { |
307 | # if we are dumping the current version, overwrite the DDL |
308 | carp "Overwriting existing DDL file - $filename"; |
c88c9aaa |
309 | unlink $filename; |
8164e4d0 |
310 | } |
311 | |
312 | my $output = $sqlt->translate; |
313 | if(!$output) { |
314 | carp("Failed to translate to $db, skipping. (" . $sqlt->error . ")"); |
315 | next; |
316 | } |
c88c9aaa |
317 | my $file; |
318 | unless( open $file, q(>), $filename ) { |
8164e4d0 |
319 | $self->throw_exception("Can't open $filename for writing ($!)"); |
320 | next; |
321 | } |
c88c9aaa |
322 | print {$file} $output; |
323 | close $file; |
8164e4d0 |
324 | |
c88c9aaa |
325 | next unless $preversion; |
8164e4d0 |
326 | |
327 | require SQL::Translator::Diff; |
328 | |
c88c9aaa |
329 | my $prefilename = $self->ddl_filename($db, $preversion, $dir); |
330 | unless(-e $prefilename) { |
8164e4d0 |
331 | carp("No previous schema file found ($prefilename)"); |
332 | next; |
333 | } |
334 | |
c88c9aaa |
335 | my $diff_file = $self->ddl_filename($db, $version, $dir, $preversion); |
336 | if(-e $diff_file) { |
337 | carp("Overwriting existing diff file - $diff_file"); |
338 | unlink $diff_file; |
8164e4d0 |
339 | } |
340 | |
341 | my $source_schema; |
342 | { |
c88c9aaa |
343 | my $t = SQL::Translator->new({ |
344 | %{$sqltargs}, |
345 | debug => 0, |
346 | trace => 0, |
347 | }); |
8164e4d0 |
348 | |
c88c9aaa |
349 | $t->parser( $db ) # could this really throw an exception? |
8164e4d0 |
350 | or $self->throw_exception ($t->error); |
351 | |
352 | my $out = $t->translate( $prefilename ) |
353 | or $self->throw_exception ($t->error); |
354 | |
355 | $source_schema = $t->schema; |
356 | |
357 | $source_schema->name( $prefilename ) |
c88c9aaa |
358 | unless $source_schema->name; |
8164e4d0 |
359 | } |
360 | |
361 | # The "new" style of producers have sane normalization and can support |
362 | # diffing a SQL file against a DBIC->SQLT schema. Old style ones don't |
363 | # And we have to diff parsed SQL against parsed SQL. |
364 | my $dest_schema = $sqlt_schema; |
365 | |
366 | unless ( "SQL::Translator::Producer::$db"->can('preprocess_schema') ) { |
c88c9aaa |
367 | my $t = SQL::Translator->new({ |
368 | %{$sqltargs}, |
369 | debug => 0, |
370 | trace => 0, |
371 | }); |
8164e4d0 |
372 | |
c88c9aaa |
373 | $t->parser( $db ) # could this really throw an exception? |
8164e4d0 |
374 | or $self->throw_exception ($t->error); |
375 | |
376 | my $out = $t->translate( $filename ) |
377 | or $self->throw_exception ($t->error); |
378 | |
379 | $dest_schema = $t->schema; |
380 | |
381 | $dest_schema->name( $filename ) |
382 | unless $dest_schema->name; |
383 | } |
384 | |
c88c9aaa |
385 | my $diff = SQL::Translator::Diff::schema_diff( |
386 | $source_schema, $db, |
387 | $dest_schema, $db, |
388 | $sqltargs |
389 | ); |
390 | unless(open $file, q(>), $diff_file) { |
391 | $self->throw_exception("Can't write to $diff_file ($!)"); |
8164e4d0 |
392 | next; |
393 | } |
c88c9aaa |
394 | print {$file} $diff; |
395 | close $file; |
8164e4d0 |
396 | } |
397 | } |
398 | |
12fdd461 |
399 | method do_upgrade { $self->run_upgrade(qr/.*?/) } |
b974984a |
400 | |
401 | method run_upgrade($stm) { |
12fdd461 |
402 | return unless $self->_filedata; |
403 | my @statements = grep { $_ =~ $stm } @{$self->_filedata}; |
b974984a |
404 | |
12fdd461 |
405 | for (@statements) { |
406 | $self->storage->debugobj->query_start($_) if $self->storage->debug; |
407 | $self->apply_statement($_); |
408 | $self->storage->debugobj->query_end($_) if $self->storage->debug; |
409 | } |
b974984a |
410 | } |
411 | |
412 | method apply_statement($statement) { |
e1f67607 |
413 | # croak? |
12fdd461 |
414 | $self->storage->dbh->do($_) or carp "SQL was: $_" |
b974984a |
415 | } |
416 | |
8cf0010a |
417 | method _read_sql_file($file) { |
418 | return unless $file; |
b974984a |
419 | |
420 | open my $fh, '<', $file or carp("Can't open upgrade file, $file ($!)"); |
421 | my @data = split /\n/, join '', <$fh>; |
422 | close $fh; |
423 | |
424 | @data = grep { |
8cf0010a |
425 | $_ && |
426 | !/^--/ && |
427 | !/^(BEGIN|BEGIN TRANSACTION|COMMIT)/m |
b974984a |
428 | } split /;/, |
8cf0010a |
429 | join '', @data; |
b974984a |
430 | |
431 | return \@data; |
432 | } |
433 | |
b974984a |
434 | 1; |
61847972 |
435 | |
436 | __END__ |
437 | |
438 | vim: ts=2,sw=2,expandtab |