Commit | Line | Data |
9f3849c3 |
1 | package DBIx::Class::Admin; |
2 | |
71ef99d5 |
3 | # check deps |
4 | BEGIN { |
a4a02f15 |
5 | use Carp::Clan qw/^DBIx::Class/; |
6 | use DBIx::Class; |
7 | croak('The following modules are required for DBIx::Class::Admin ' . DBIx::Class::Optional::Dependencies->req_missing_for ('admin') ) |
8 | unless DBIx::Class::Optional::Dependencies->req_ok_for ('admin'); |
71ef99d5 |
9 | } |
585072bb |
10 | |
71ef99d5 |
11 | use Moose; |
a62dec82 |
12 | use MooseX::Types::Moose qw/Int Str Any Bool/; |
13 | use DBIx::Class::Admin::Types qw/DBICConnectInfo DBICHashRef/; |
71ef99d5 |
14 | use MooseX::Types::JSON qw(JSON); |
15 | use MooseX::Types::Path::Class qw(Dir File); |
16 | use Try::Tiny; |
cbde5b15 |
17 | use JSON::Any qw(DWIW XS JSON); |
8aa16237 |
18 | use namespace::autoclean; |
bb464677 |
19 | |
595cb2c7 |
20 | =head1 NAME |
21 | |
22 | DBIx::Class::Admin - Administration object for schemas |
23 | |
24 | =head1 SYNOPSIS |
25 | |
47442cea |
26 | $ dbicadmin --help |
27 | |
28 | $ dbicadmin --schema=MyApp::Schema \ |
29 | --connect='["dbi:SQLite:my.db", "", ""]' \ |
30 | --deploy |
31 | |
32 | $ dbicadmin --schema=MyApp::Schema --class=Employee \ |
33 | --connect='["dbi:SQLite:my.db", "", ""]' \ |
cbde5b15 |
34 | --op=update --set='{ "name": "New_Employee" }' |
47442cea |
35 | |
9c34993a |
36 | use DBIx::Class::Admin; |
595cb2c7 |
37 | |
9c34993a |
38 | # ddl manipulation |
39 | my $admin = DBIx::Class::Admin->new( |
40 | schema_class=> 'MY::Schema', |
41 | sql_dir=> $sql_dir, |
42 | connect_info => { dsn => $dsn, user => $user, password => $pass }, |
43 | ); |
595cb2c7 |
44 | |
9c34993a |
45 | # create SQLite sql |
46 | $admin->create('SQLite'); |
595cb2c7 |
47 | |
9c34993a |
48 | # create SQL diff for an upgrade |
49 | $admin->create('SQLite', {} , "1.0"); |
595cb2c7 |
50 | |
9c34993a |
51 | # upgrade a database |
52 | $admin->upgrade(); |
595cb2c7 |
53 | |
9c34993a |
54 | # install a version for an unversioned schema |
55 | $admin->install("3.0"); |
9f3849c3 |
56 | |
47442cea |
57 | =head1 REQUIREMENTS |
58 | |
a4a02f15 |
59 | The Admin interface has additional requirements not currently part of |
60 | L<DBIx::Class>. See L<DBIx::Class::Optional::Dependencies> for more details. |
47442cea |
61 | |
a4a02f15 |
62 | =head1 ATTRIBUTES |
9f3849c3 |
63 | |
595cb2c7 |
64 | =head2 schema_class |
9f3849c3 |
65 | |
595cb2c7 |
66 | the class of the schema to load |
e81f0fe2 |
67 | |
595cb2c7 |
68 | =cut |
e81f0fe2 |
69 | |
9f3849c3 |
70 | has 'schema_class' => ( |
3b27cdac |
71 | is => 'ro', |
72 | isa => Str, |
9f3849c3 |
73 | ); |
74 | |
e81f0fe2 |
75 | |
595cb2c7 |
76 | =head2 schema |
9f3849c3 |
77 | |
595cb2c7 |
78 | A pre-connected schema object can be provided for manipulation |
e81f0fe2 |
79 | |
595cb2c7 |
80 | =cut |
e81f0fe2 |
81 | |
9f3849c3 |
82 | has 'schema' => ( |
3b27cdac |
83 | is => 'ro', |
84 | isa => 'DBIx::Class::Schema', |
a705b175 |
85 | lazy_build => 1, |
9f3849c3 |
86 | ); |
87 | |
9f3849c3 |
88 | sub _build_schema { |
a705b175 |
89 | my ($self) = @_; |
a77f5ab1 |
90 | require Class::MOP; |
91 | Class::MOP::load_class($self->schema_class); |
9f3849c3 |
92 | |
a705b175 |
93 | $self->connect_info->[3]->{ignore_version} =1; |
94 | return $self->schema_class->connect(@{$self->connect_info()} ); # , $self->connect_info->[3], { ignore_version => 1} ); |
9f3849c3 |
95 | } |
96 | |
e81f0fe2 |
97 | |
bb464677 |
98 | =head2 resultset |
99 | |
100 | a resultset from the schema to operate on |
e81f0fe2 |
101 | |
bb464677 |
102 | =cut |
e81f0fe2 |
103 | |
bb464677 |
104 | has 'resultset' => ( |
3b27cdac |
105 | is => 'rw', |
106 | isa => Str, |
bb464677 |
107 | ); |
108 | |
e81f0fe2 |
109 | |
bb464677 |
110 | =head2 where |
111 | |
112 | a hash ref or json string to be used for identifying data to manipulate |
e81f0fe2 |
113 | |
bb464677 |
114 | =cut |
115 | |
116 | has 'where' => ( |
a705b175 |
117 | is => 'rw', |
3b27cdac |
118 | isa => DBICHashRef, |
119 | coerce => 1, |
bb464677 |
120 | ); |
121 | |
e81f0fe2 |
122 | |
bb464677 |
123 | =head2 set |
e81f0fe2 |
124 | |
bb464677 |
125 | a hash ref or json string to be used for inserting or updating data |
e81f0fe2 |
126 | |
bb464677 |
127 | =cut |
128 | |
129 | has 'set' => ( |
a705b175 |
130 | is => 'rw', |
3b27cdac |
131 | isa => DBICHashRef, |
132 | coerce => 1, |
bb464677 |
133 | ); |
134 | |
e81f0fe2 |
135 | |
bb464677 |
136 | =head2 attrs |
e81f0fe2 |
137 | |
bb464677 |
138 | a hash ref or json string to be used for passing additonal info to the ->search call |
e81f0fe2 |
139 | |
bb464677 |
140 | =cut |
e81f0fe2 |
141 | |
bb464677 |
142 | has 'attrs' => ( |
3b27cdac |
143 | is => 'rw', |
144 | isa => DBICHashRef, |
145 | coerce => 1, |
bb464677 |
146 | ); |
e81f0fe2 |
147 | |
148 | |
595cb2c7 |
149 | =head2 connect_info |
150 | |
151 | connect_info the arguments to provide to the connect call of the schema_class |
bb464677 |
152 | |
e81f0fe2 |
153 | =cut |
bb464677 |
154 | |
9f3849c3 |
155 | has 'connect_info' => ( |
3b27cdac |
156 | is => 'ro', |
157 | isa => DBICConnectInfo, |
a705b175 |
158 | lazy_build => 1, |
3b27cdac |
159 | coerce => 1, |
9f3849c3 |
160 | ); |
161 | |
162 | sub _build_connect_info { |
a705b175 |
163 | my ($self) = @_; |
164 | return $self->_find_stanza($self->config, $self->config_stanza); |
9f3849c3 |
165 | } |
166 | |
e81f0fe2 |
167 | |
595cb2c7 |
168 | =head2 config_file |
169 | |
170 | config_file provide a config_file to read connect_info from, if this is provided |
171 | config_stanze should also be provided to locate where the connect_info is in the config |
172 | The config file should be in a format readable by Config::General |
e81f0fe2 |
173 | |
595cb2c7 |
174 | =cut |
e81f0fe2 |
175 | |
595cb2c7 |
176 | has config_file => ( |
a705b175 |
177 | is => 'ro', |
3b27cdac |
178 | isa => File, |
179 | coerce => 1, |
595cb2c7 |
180 | ); |
181 | |
e81f0fe2 |
182 | |
595cb2c7 |
183 | =head2 config_stanza |
184 | |
185 | config_stanza for use with config_file should be a '::' deliminated 'path' to the connection information |
186 | designed for use with catalyst config files |
e81f0fe2 |
187 | |
595cb2c7 |
188 | =cut |
e81f0fe2 |
189 | |
595cb2c7 |
190 | has 'config_stanza' => ( |
3b27cdac |
191 | is => 'ro', |
192 | isa => Str, |
595cb2c7 |
193 | ); |
194 | |
e81f0fe2 |
195 | |
595cb2c7 |
196 | =head2 config |
197 | |
198 | Instead of loading from a file the configuration can be provided directly as a hash ref. Please note |
199 | config_stanza will still be required. |
e81f0fe2 |
200 | |
595cb2c7 |
201 | =cut |
e81f0fe2 |
202 | |
9f3849c3 |
203 | has config => ( |
3b27cdac |
204 | is => 'ro', |
205 | isa => DBICHashRef, |
a705b175 |
206 | lazy_build => 1, |
9f3849c3 |
207 | ); |
208 | |
209 | sub _build_config { |
a705b175 |
210 | my ($self) = @_; |
71766122 |
211 | |
212 | eval { require Config::Any } |
b718fd0a |
213 | or die ("Config::Any is required to parse the config file.\n"); |
9f3849c3 |
214 | |
a705b175 |
215 | my $cfg = Config::Any->load_files ( {files => [$self->config_file], use_ext =>1, flatten_to_hash=>1}); |
9f3849c3 |
216 | |
a705b175 |
217 | # just grab the config from the config file |
218 | $cfg = $cfg->{$self->config_file}; |
219 | return $cfg; |
9f3849c3 |
220 | } |
221 | |
e81f0fe2 |
222 | |
595cb2c7 |
223 | =head2 sql_dir |
9f3849c3 |
224 | |
595cb2c7 |
225 | The location where sql ddl files should be created or found for an upgrade. |
e81f0fe2 |
226 | |
595cb2c7 |
227 | =cut |
e81f0fe2 |
228 | |
9f3849c3 |
229 | has 'sql_dir' => ( |
a705b175 |
230 | is => 'ro', |
3b27cdac |
231 | isa => Dir, |
232 | coerce => 1, |
9f3849c3 |
233 | ); |
234 | |
e81f0fe2 |
235 | |
595cb2c7 |
236 | =head2 version |
9f3849c3 |
237 | |
595cb2c7 |
238 | Used for install, the version which will be 'installed' in the schema |
e81f0fe2 |
239 | |
595cb2c7 |
240 | =cut |
e81f0fe2 |
241 | |
9f3849c3 |
242 | has version => ( |
3b27cdac |
243 | is => 'rw', |
244 | isa => Str, |
9f3849c3 |
245 | ); |
246 | |
e81f0fe2 |
247 | |
595cb2c7 |
248 | =head2 preversion |
249 | |
250 | Previouse version of the schema to create an upgrade diff for, the full sql for that version of the sql must be in the sql_dir |
e81f0fe2 |
251 | |
595cb2c7 |
252 | =cut |
e81f0fe2 |
253 | |
9f3849c3 |
254 | has preversion => ( |
3b27cdac |
255 | is => 'rw', |
256 | isa => Str, |
9f3849c3 |
257 | ); |
258 | |
e81f0fe2 |
259 | |
595cb2c7 |
260 | =head2 force |
261 | |
262 | Try and force certain operations. |
e81f0fe2 |
263 | |
595cb2c7 |
264 | =cut |
e81f0fe2 |
265 | |
912e2d5a |
266 | has force => ( |
3b27cdac |
267 | is => 'rw', |
268 | isa => Bool, |
912e2d5a |
269 | ); |
270 | |
e81f0fe2 |
271 | |
c57f1cf7 |
272 | =head2 quiet |
595cb2c7 |
273 | |
274 | Be less verbose about actions |
e81f0fe2 |
275 | |
595cb2c7 |
276 | =cut |
e81f0fe2 |
277 | |
64c012f4 |
278 | has quiet => ( |
3b27cdac |
279 | is => 'rw', |
280 | isa => Bool, |
64c012f4 |
281 | ); |
282 | |
912e2d5a |
283 | has '_confirm' => ( |
3b27cdac |
284 | is => 'bare', |
285 | isa => Bool, |
912e2d5a |
286 | ); |
287 | |
e81f0fe2 |
288 | |
595cb2c7 |
289 | =head1 METHODS |
290 | |
291 | =head2 create |
292 | |
293 | =over 4 |
294 | |
295 | =item Arguments: $sqlt_type, \%sqlt_args, $preversion |
296 | |
297 | =back |
298 | |
299 | L<create> will generate sql for the supplied schema_class in sql_dir. The flavour of sql to |
300 | generate can be controlled by suppling a sqlt_type which should be a L<SQL::Translator> name. |
301 | |
302 | Arguments for L<SQL::Translator> can be supplied in the sqlt_args hashref. |
303 | |
304 | Optional preversion can be supplied to generate a diff to be used by upgrade. |
e81f0fe2 |
305 | |
595cb2c7 |
306 | =cut |
307 | |
9f3849c3 |
308 | sub create { |
a705b175 |
309 | my ($self, $sqlt_type, $sqlt_args, $preversion) = @_; |
595cb2c7 |
310 | |
a705b175 |
311 | $preversion ||= $self->preversion(); |
595cb2c7 |
312 | |
a705b175 |
313 | my $schema = $self->schema(); |
314 | # create the dir if does not exist |
315 | $self->sql_dir->mkpath() if ( ! -d $self->sql_dir); |
9f3849c3 |
316 | |
a705b175 |
317 | $schema->create_ddl_dir( $sqlt_type, (defined $schema->schema_version ? $schema->schema_version : ""), $self->sql_dir->stringify, $preversion, $sqlt_args ); |
9f3849c3 |
318 | } |
319 | |
e81f0fe2 |
320 | |
595cb2c7 |
321 | =head2 upgrade |
322 | |
323 | =over 4 |
324 | |
325 | =item Arguments: <none> |
326 | |
327 | =back |
328 | |
329 | upgrade will attempt to upgrade the connected database to the same version as the schema_class. |
330 | B<MAKE SURE YOU BACKUP YOUR DB FIRST> |
e81f0fe2 |
331 | |
595cb2c7 |
332 | =cut |
333 | |
9f3849c3 |
334 | sub upgrade { |
a705b175 |
335 | my ($self) = @_; |
336 | my $schema = $self->schema(); |
337 | if (!$schema->get_db_version()) { |
338 | # schema is unversioned |
b718fd0a |
339 | $schema->throw_exception ("Could not determin current schema version, please either install() or deploy().\n"); |
a705b175 |
340 | } else { |
341 | my $ret = $schema->upgrade(); |
342 | return $ret; |
343 | } |
9f3849c3 |
344 | } |
345 | |
e81f0fe2 |
346 | |
595cb2c7 |
347 | =head2 install |
348 | |
349 | =over 4 |
350 | |
351 | =item Arguments: $version |
352 | |
353 | =back |
354 | |
355 | install is here to help when you want to move to L<DBIx::Class::Schema::Versioned> and have an existing |
356 | database. install will take a version and add the version tracking tables and 'install' the version. No |
357 | further ddl modification takes place. Setting the force attribute to a true value will allow overriding of |
358 | already versioned databases. |
e81f0fe2 |
359 | |
595cb2c7 |
360 | =cut |
e81f0fe2 |
361 | |
9f3849c3 |
362 | sub install { |
a705b175 |
363 | my ($self, $version) = @_; |
364 | |
365 | my $schema = $self->schema(); |
366 | $version ||= $self->version(); |
367 | if (!$schema->get_db_version() ) { |
368 | # schema is unversioned |
369 | print "Going to install schema version\n"; |
370 | my $ret = $schema->install($version); |
371 | print "retun is $ret\n"; |
372 | } |
373 | elsif ($schema->get_db_version() and $self->force ) { |
585072bb |
374 | carp "Forcing install may not be a good idea"; |
a705b175 |
375 | if($self->_confirm() ) { |
a705b175 |
376 | $self->schema->_set_db_version({ version => $version}); |
9c34993a |
377 | } |
a705b175 |
378 | } |
379 | else { |
b718fd0a |
380 | $schema->throw_exception ("Schema already has a version. Try upgrade instead.\n"); |
a705b175 |
381 | } |
9f3849c3 |
382 | |
383 | } |
384 | |
e81f0fe2 |
385 | |
595cb2c7 |
386 | =head2 deploy |
387 | |
388 | =over 4 |
389 | |
390 | =item Arguments: $args |
391 | |
392 | =back |
393 | |
394 | deploy will create the schema at the connected database. C<$args> are passed straight to |
e81f0fe2 |
395 | L<DBIx::Class::Schema/deploy>. |
396 | |
595cb2c7 |
397 | =cut |
e81f0fe2 |
398 | |
9f3849c3 |
399 | sub deploy { |
a705b175 |
400 | my ($self, $args) = @_; |
401 | my $schema = $self->schema(); |
402 | if (!$schema->get_db_version() ) { |
403 | # schema is unversioned |
404 | $schema->deploy( $args, $self->sql_dir) |
b718fd0a |
405 | or $schema->throw_exception ("Could not deploy schema.\n"); # FIXME deploy() does not return 1/0 on success/fail |
a705b175 |
406 | } else { |
b718fd0a |
407 | $schema->throw_exception("A versioned schema has already been deployed, try upgrade instead.\n"); |
a705b175 |
408 | } |
9f3849c3 |
409 | } |
410 | |
bb464677 |
411 | =head2 insert |
595cb2c7 |
412 | |
413 | =over 4 |
414 | |
415 | =item Arguments: $rs, $set |
416 | |
417 | =back |
418 | |
bb464677 |
419 | insert takes the name of a resultset from the schema_class and a hashref of data to insert |
595cb2c7 |
420 | into that resultset |
421 | |
422 | =cut |
e81f0fe2 |
423 | |
bb464677 |
424 | sub insert { |
a705b175 |
425 | my ($self, $rs, $set) = @_; |
bb464677 |
426 | |
a705b175 |
427 | $rs ||= $self->resultset(); |
428 | $set ||= $self->set(); |
429 | my $resultset = $self->schema->resultset($rs); |
430 | my $obj = $resultset->create( $set ); |
431 | print ''.ref($resultset).' ID: '.join(',',$obj->id())."\n" if (!$self->quiet); |
9f3849c3 |
432 | } |
433 | |
595cb2c7 |
434 | |
bb464677 |
435 | =head2 update |
595cb2c7 |
436 | |
e81f0fe2 |
437 | =over 4 |
595cb2c7 |
438 | |
439 | =item Arguments: $rs, $set, $where |
440 | |
441 | =back |
442 | |
e81f0fe2 |
443 | update takes the name of a resultset from the schema_class, a hashref of data to update and |
444 | a where hash used to form the search for the rows to update. |
445 | |
595cb2c7 |
446 | =cut |
e81f0fe2 |
447 | |
bb464677 |
448 | sub update { |
a705b175 |
449 | my ($self, $rs, $set, $where) = @_; |
882931aa |
450 | |
a705b175 |
451 | $rs ||= $self->resultset(); |
452 | $where ||= $self->where(); |
453 | $set ||= $self->set(); |
454 | my $resultset = $self->schema->resultset($rs); |
455 | $resultset = $resultset->search( ($where||{}) ); |
882931aa |
456 | |
a705b175 |
457 | my $count = $resultset->count(); |
458 | print "This action will modify $count ".ref($resultset)." records.\n" if (!$self->quiet); |
882931aa |
459 | |
a705b175 |
460 | if ( $self->force || $self->_confirm() ) { |
461 | $resultset->update_all( $set ); |
462 | } |
9f3849c3 |
463 | } |
464 | |
e81f0fe2 |
465 | |
bb464677 |
466 | =head2 delete |
595cb2c7 |
467 | |
468 | =over 4 |
469 | |
470 | =item Arguments: $rs, $where, $attrs |
471 | |
472 | =back |
473 | |
e81f0fe2 |
474 | delete takes the name of a resultset from the schema_class, a where hashref and a attrs to pass to ->search. |
595cb2c7 |
475 | The found data is deleted and cannot be recovered. |
e81f0fe2 |
476 | |
595cb2c7 |
477 | =cut |
e81f0fe2 |
478 | |
bb464677 |
479 | sub delete { |
a705b175 |
480 | my ($self, $rs, $where, $attrs) = @_; |
9f3849c3 |
481 | |
a705b175 |
482 | $rs ||= $self->resultset(); |
483 | $where ||= $self->where(); |
484 | $attrs ||= $self->attrs(); |
485 | my $resultset = $self->schema->resultset($rs); |
486 | $resultset = $resultset->search( ($where||{}), ($attrs||()) ); |
9f3849c3 |
487 | |
a705b175 |
488 | my $count = $resultset->count(); |
489 | print "This action will delete $count ".ref($resultset)." records.\n" if (!$self->quiet); |
9f3849c3 |
490 | |
a705b175 |
491 | if ( $self->force || $self->_confirm() ) { |
492 | $resultset->delete_all(); |
493 | } |
9f3849c3 |
494 | } |
495 | |
e81f0fe2 |
496 | |
bb464677 |
497 | =head2 select |
595cb2c7 |
498 | |
499 | =over 4 |
500 | |
501 | =item Arguments: $rs, $where, $attrs |
502 | |
503 | =back |
504 | |
bb464677 |
505 | select takes the name of a resultset from the schema_class, a where hashref and a attrs to pass to ->search. |
595cb2c7 |
506 | The found data is returned in a array ref where the first row will be the columns list. |
507 | |
508 | =cut |
e81f0fe2 |
509 | |
bb464677 |
510 | sub select { |
a705b175 |
511 | my ($self, $rs, $where, $attrs) = @_; |
512 | |
513 | $rs ||= $self->resultset(); |
514 | $where ||= $self->where(); |
515 | $attrs ||= $self->attrs(); |
516 | my $resultset = $self->schema->resultset($rs); |
517 | $resultset = $resultset->search( ($where||{}), ($attrs||()) ); |
518 | |
519 | my @data; |
520 | my @columns = $resultset->result_source->columns(); |
521 | push @data, [@columns];# |
522 | |
523 | while (my $row = $resultset->next()) { |
524 | my @fields; |
525 | foreach my $column (@columns) { |
526 | push( @fields, $row->get_column($column) ); |
9c34993a |
527 | } |
a705b175 |
528 | push @data, [@fields]; |
529 | } |
9c34993a |
530 | |
a705b175 |
531 | return \@data; |
9f3849c3 |
532 | } |
533 | |
595cb2c7 |
534 | sub _confirm { |
a705b175 |
535 | my ($self) = @_; |
536 | print "Are you sure you want to do this? (type YES to confirm) \n"; |
537 | # mainly here for testing |
538 | return 1 if ($self->meta->get_attribute('_confirm')->get_value($self)); |
539 | my $response = <STDIN>; |
540 | return 1 if ($response=~/^YES/); |
541 | return; |
9f3849c3 |
542 | } |
543 | |
595cb2c7 |
544 | sub _find_stanza { |
a705b175 |
545 | my ($self, $cfg, $stanza) = @_; |
546 | my @path = split /::/, $stanza; |
547 | while (my $path = shift @path) { |
548 | if (exists $cfg->{$path}) { |
549 | $cfg = $cfg->{$path}; |
550 | } |
551 | else { |
b718fd0a |
552 | die ("Could not find $stanza in config, $path does not seem to exist.\n"); |
9c34993a |
553 | } |
a705b175 |
554 | } |
555 | return $cfg; |
595cb2c7 |
556 | } |
bb464677 |
557 | |
47442cea |
558 | =head1 AUTHOR |
bb464677 |
559 | |
e81f0fe2 |
560 | See L<DBIx::Class/CONTRIBUTORS>. |
bb464677 |
561 | |
562 | =head1 LICENSE |
563 | |
e81f0fe2 |
564 | You may distribute this code under the same terms as Perl itself |
565 | |
bb464677 |
566 | =cut |
e81f0fe2 |
567 | |
9f3849c3 |
568 | 1; |