Commit | Line | Data |
45d0d9d5 |
1 | package DBIx::Class::DeploymentHandler::DeployMethod::SQL::Translator; |
334bced5 |
2 | use Moose; |
2e68a8e1 |
3 | use Method::Signatures::Simple; |
7f50d101 |
4 | use Try::Tiny; |
d23c7c77 |
5 | use SQL::Translator; |
6 | require SQL::Translator::Diff; |
7 | require DBIx::Class::Storage; # loaded for type constraint |
2e68a8e1 |
8 | |
7521a845 |
9 | with 'DBIx::Class::DeploymentHandler::HandlesDeploy'; |
2e68a8e1 |
10 | use Carp 'carp'; |
11 | |
334bced5 |
12 | has storage => ( |
13 | isa => 'DBIx::Class::Storage', |
14 | is => 'ro', |
15 | lazy_build => 1, |
16 | ); |
17 | |
2eaf903b |
18 | method _build_storage { |
19 | my $s = $self->schema->storage; |
20 | $s->_determine_driver; |
21 | $s |
22 | } |
23 | |
334bced5 |
24 | has sqltargs => ( |
25 | isa => 'HashRef', |
26 | is => 'ro', |
27 | default => sub { {} }, |
28 | ); |
29 | has upgrade_directory => ( |
30 | isa => 'Str', |
31 | is => 'ro', |
32 | required => 1, |
33 | default => 'sql', |
34 | ); |
35 | |
334bced5 |
36 | has databases => ( |
37 | coerce => 1, |
38 | isa => 'DBIx::Class::DeploymentHandler::Databases', |
39 | is => 'ro', |
40 | default => sub { [qw( MySQL SQLite PostgreSQL )] }, |
41 | ); |
42 | |
43 | has schema => ( |
44 | isa => 'DBIx::Class::Schema', |
45 | is => 'ro', |
46 | required => 1, |
24f4524b |
47 | handles => [qw( schema_version )], |
334bced5 |
48 | ); |
49 | |
50 | has _filedata => ( |
51 | isa => 'ArrayRef[Str]', |
52 | is => 'rw', |
53 | ); |
54 | |
8a7847f1 |
55 | method _ddl_filename($type, $versions, $dir) { |
a912450b |
56 | my $filename = ref $self->schema; |
24f4524b |
57 | $filename =~ s/::/-/g; |
58 | |
59 | $filename = File::Spec->catfile( |
60 | $dir, "$filename-" . join( q(-), @{$versions} ) . "-$type.sql" |
61 | ); |
62 | |
63 | return $filename; |
64 | } |
65 | |
8a7847f1 |
66 | method _deployment_statements { |
2e68a8e1 |
67 | my $dir = $self->upgrade_directory; |
68 | my $schema = $self->schema; |
69 | my $type = $self->storage->sqlt_type; |
70 | my $sqltargs = $self->sqltargs; |
71 | my $version = $self->schema_version || '1.x'; |
72 | |
8a7847f1 |
73 | my $filename = $self->_ddl_filename($type, [ $version ], $dir); |
2e68a8e1 |
74 | if(-f $filename) { |
75 | my $file; |
76 | open $file, q(<), $filename |
77 | or carp "Can't open $filename ($!)"; |
78 | my @rows = <$file>; |
79 | close $file; |
80 | return join '', @rows; |
81 | } |
82 | |
83 | # sources needs to be a parser arg, but for simplicty allow at top level |
84 | # coming in |
85 | $sqltargs->{parser_args}{sources} = delete $sqltargs->{sources} |
86 | if exists $sqltargs->{sources}; |
87 | |
88 | my $tr = SQL::Translator->new( |
89 | producer => "SQL::Translator::Producer::${type}", |
90 | %$sqltargs, |
91 | parser => 'SQL::Translator::Parser::DBIx::Class', |
92 | data => $schema, |
93 | ); |
94 | |
95 | my @ret; |
96 | my $wa = wantarray; |
97 | if ($wa) { |
98 | @ret = $tr->translate; |
99 | } |
100 | else { |
101 | $ret[0] = $tr->translate; |
102 | } |
103 | |
104 | $schema->throw_exception( 'Unable to produce deployment statements: ' . $tr->error) |
105 | unless (@ret && defined $ret[0]); |
106 | |
107 | return $wa ? @ret : $ret[0]; |
108 | } |
109 | |
7521a845 |
110 | sub _deploy { |
111 | my $self = shift; |
2e68a8e1 |
112 | my $storage = $self->storage; |
113 | |
114 | my $deploy = sub { |
115 | my $line = shift; |
116 | return if(!$line || $line =~ /^--|^BEGIN TRANSACTION|^COMMIT|^\s+$/); |
117 | $storage->_query_start($line); |
118 | try { |
119 | # do a dbh_do cycle here, as we need some error checking in |
120 | # place (even though we will ignore errors) |
121 | $storage->dbh_do (sub { $_[1]->do($line) }); |
122 | } |
123 | catch { |
124 | carp "$_ (running '${line}')" |
125 | } |
126 | $storage->_query_end($line); |
127 | }; |
8a7847f1 |
128 | my @statements = $self->_deployment_statements(); |
2e68a8e1 |
129 | if (@statements > 1) { |
130 | foreach my $statement (@statements) { |
131 | $deploy->( $statement ); |
132 | } |
133 | } |
134 | elsif (@statements == 1) { |
135 | foreach my $line ( split(";\n", $statements[0])) { |
136 | $deploy->( $line ); |
137 | } |
138 | } |
139 | } |
140 | |
7521a845 |
141 | sub prepare_install { |
142 | my $self = shift; |
2e68a8e1 |
143 | my $schema = $self->schema; |
144 | my $databases = $self->databases; |
145 | my $dir = $self->upgrade_directory; |
146 | my $sqltargs = $self->sqltargs; |
147 | unless( -d $dir ) { |
148 | carp "Upgrade directory $dir does not exist, using ./\n"; |
149 | $dir = "./"; |
150 | } |
151 | |
152 | my $version = $schema->schema_version || '1.x'; |
153 | my $schema_version = $schema->schema_version || '1.x'; |
154 | $version ||= $schema_version; |
155 | |
156 | $sqltargs = { |
157 | add_drop_table => 1, |
158 | ignore_constraint_names => 1, |
159 | ignore_index_names => 1, |
160 | %{$sqltargs || {}} |
161 | }; |
162 | |
163 | my $sqlt = SQL::Translator->new( $sqltargs ); |
164 | |
165 | $sqlt->parser('SQL::Translator::Parser::DBIx::Class'); |
166 | my $sqlt_schema = $sqlt->translate({ data => $schema }) |
167 | or $self->throw_exception ($sqlt->error); |
168 | |
169 | foreach my $db (@$databases) { |
170 | $sqlt->reset; |
171 | $sqlt->{schema} = $sqlt_schema; |
172 | $sqlt->producer($db); |
173 | |
8a7847f1 |
174 | my $filename = $self->_ddl_filename($db, [ $version ], $dir); |
2e68a8e1 |
175 | if (-e $filename && ($version eq $schema_version )) { |
176 | # if we are dumping the current version, overwrite the DDL |
177 | carp "Overwriting existing DDL file - $filename"; |
178 | unlink $filename; |
179 | } |
180 | |
181 | my $output = $sqlt->translate; |
182 | if(!$output) { |
183 | carp("Failed to translate to $db, skipping. (" . $sqlt->error . ")"); |
184 | next; |
185 | } |
186 | my $file; |
187 | unless( open $file, q(>), $filename ) { |
188 | $self->throw_exception("Can't open $filename for writing ($!)"); |
189 | next; |
190 | } |
191 | print {$file} $output; |
192 | close $file; |
193 | } |
194 | } |
195 | |
7521a845 |
196 | sub prepare_update { |
197 | my ($self, $version, $preversion) = @_; |
63e95f13 |
198 | # this should be: |
199 | # |
200 | # $from_version ||= $db_version |
201 | # $to_version ||= $schema_version |
202 | # $version_set ||= [$from_version, $to_version]; |
203 | # |
204 | # for updates prepared automatically (rob's stuff) |
205 | # one would want to explicitly set $version_set to |
206 | # [$to_version] |
2e68a8e1 |
207 | my $schema = $self->schema; |
208 | my $databases = $self->databases; |
209 | my $dir = $self->upgrade_directory; |
210 | my $sqltargs = $self->sqltargs; |
211 | |
212 | unless( -d $dir ) { |
213 | carp "Upgrade directory $dir does not exist, using ./\n"; |
214 | $dir = "./"; |
215 | } |
216 | |
217 | my $schema_version = $schema->schema_version || '1.x'; |
218 | $version ||= $schema_version; |
219 | |
220 | $sqltargs = { |
221 | add_drop_table => 1, |
222 | ignore_constraint_names => 1, |
223 | ignore_index_names => 1, |
224 | %{$sqltargs} |
225 | }; |
226 | |
227 | my $sqlt = SQL::Translator->new( $sqltargs ); |
228 | |
229 | $sqlt->parser('SQL::Translator::Parser::DBIx::Class'); |
230 | my $sqlt_schema = $sqlt->translate({ data => $schema }) |
231 | or $self->throw_exception ($sqlt->error); |
232 | |
233 | foreach my $db (@$databases) { |
234 | $sqlt->reset; |
235 | $sqlt->{schema} = $sqlt_schema; |
236 | $sqlt->producer($db); |
237 | |
8a7847f1 |
238 | my $prefilename = $self->_ddl_filename($db, [ $preversion ], $dir); |
2e68a8e1 |
239 | unless(-e $prefilename) { |
240 | carp("No previous schema file found ($prefilename)"); |
241 | next; |
242 | } |
243 | |
8a7847f1 |
244 | my $diff_file = $self->_ddl_filename($db, [ $preversion, $version ], $dir ); |
2e68a8e1 |
245 | if(-e $diff_file) { |
246 | carp("Overwriting existing diff file - $diff_file"); |
247 | unlink $diff_file; |
248 | } |
249 | |
250 | my $source_schema; |
251 | { |
252 | my $t = SQL::Translator->new({ |
253 | %{$sqltargs}, |
254 | debug => 0, |
255 | trace => 0, |
256 | }); |
257 | |
258 | $t->parser( $db ) # could this really throw an exception? |
259 | or $self->throw_exception ($t->error); |
260 | |
261 | my $out = $t->translate( $prefilename ) |
262 | or $self->throw_exception ($t->error); |
263 | |
264 | $source_schema = $t->schema; |
265 | |
266 | $source_schema->name( $prefilename ) |
267 | unless $source_schema->name; |
268 | } |
269 | |
270 | # The "new" style of producers have sane normalization and can support |
271 | # diffing a SQL file against a DBIC->SQLT schema. Old style ones don't |
272 | # And we have to diff parsed SQL against parsed SQL. |
273 | my $dest_schema = $sqlt_schema; |
274 | |
275 | unless ( "SQL::Translator::Producer::$db"->can('preprocess_schema') ) { |
276 | my $t = SQL::Translator->new({ |
277 | %{$sqltargs}, |
278 | debug => 0, |
279 | trace => 0, |
280 | }); |
281 | |
282 | $t->parser( $db ) # could this really throw an exception? |
283 | or $self->throw_exception ($t->error); |
284 | |
8a7847f1 |
285 | my $filename = $self->_ddl_filename($db, [ $version ], $dir); |
2e68a8e1 |
286 | my $out = $t->translate( $filename ) |
287 | or $self->throw_exception ($t->error); |
288 | |
289 | $dest_schema = $t->schema; |
290 | |
291 | $dest_schema->name( $filename ) |
292 | unless $dest_schema->name; |
293 | } |
294 | |
295 | my $diff = SQL::Translator::Diff::schema_diff( |
296 | $source_schema, $db, |
297 | $dest_schema, $db, |
298 | $sqltargs |
299 | ); |
300 | my $file; |
301 | unless(open $file, q(>), $diff_file) { |
302 | $self->throw_exception("Can't write to $diff_file ($!)"); |
303 | next; |
304 | } |
305 | print {$file} $diff; |
306 | close $file; |
307 | } |
308 | } |
309 | |
334bced5 |
310 | method _read_sql_file($file) { |
311 | return unless $file; |
312 | |
313 | open my $fh, '<', $file or carp("Can't open upgrade file, $file ($!)"); |
314 | my @data = split /\n/, join '', <$fh>; |
315 | close $fh; |
316 | |
317 | @data = grep { |
318 | $_ && |
319 | !/^--/ && |
320 | !/^(BEGIN|BEGIN TRANSACTION|COMMIT)/m |
321 | } split /;/, |
322 | join '', @data; |
323 | |
324 | return \@data; |
325 | } |
326 | |
7521a845 |
327 | sub _upgrade_single_step { |
328 | my $self = shift; |
24f4524b |
329 | my @version_set = @{ shift @_ }; |
8a7847f1 |
330 | my $upgrade_file = $self->_ddl_filename( |
334bced5 |
331 | $self->storage->sqlt_type, |
24f4524b |
332 | \@version_set, |
334bced5 |
333 | $self->upgrade_directory, |
334bced5 |
334 | ); |
335 | |
334bced5 |
336 | unless (-f $upgrade_file) { |
337 | # croak? |
338 | carp "Upgrade not possible, no upgrade file found ($upgrade_file), please create one\n"; |
339 | return; |
340 | } |
341 | |
334bced5 |
342 | $self->_filedata($self->_read_sql_file($upgrade_file)); # I don't like this --fREW 2010-02-22 |
8a7847f1 |
343 | $self->schema->txn_do(sub { $self->_do_upgrade }); |
334bced5 |
344 | } |
345 | |
8a7847f1 |
346 | method _do_upgrade { $self->_run_upgrade(qr/.*?/) } |
334bced5 |
347 | |
8a7847f1 |
348 | method _run_upgrade($stm) { |
334bced5 |
349 | return unless $self->_filedata; |
350 | my @statements = grep { $_ =~ $stm } @{$self->_filedata}; |
351 | |
352 | for (@statements) { |
353 | $self->storage->debugobj->query_start($_) if $self->storage->debug; |
8a7847f1 |
354 | $self->_apply_statement($_); |
334bced5 |
355 | $self->storage->debugobj->query_end($_) if $self->storage->debug; |
356 | } |
357 | } |
358 | |
8a7847f1 |
359 | method _apply_statement($statement) { |
334bced5 |
360 | # croak? |
361 | $self->storage->dbh->do($_) or carp "SQL was: $_" |
362 | } |
363 | |
334bced5 |
364 | __PACKAGE__->meta->make_immutable; |
e051bb00 |
365 | |
2e68a8e1 |
366 | 1; |
e051bb00 |
367 | |
368 | __END__ |
369 | |
2eaf903b |
370 | vim: ts=2 sw=2 expandtab |