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