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