add some pod to DBIx::Class::Admin
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Admin.pm
1 #
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 use MooseX::Types::Moose qw/Int HashRef ArrayRef Str Any/;
22 use MooseX::Types::Path::Class qw(Dir File);
23 use Try::Tiny;
24 use parent 'Class::C3::Componentised';
25
26 use Data::Dumper;
27 #
28 #               ['lib|I:s' => 'Additonal library path to search in'], 
29 #               ['schema|s:s' => 'The class of the schema to load', { required => 1 } ],
30 #               ['config-stanza|S:s' => 'Where in the config to find the connection_info, supply in form MyApp::Model::DB',],
31 #               ['config|C:s' => 'Supply the config file for parsing by Config::Any', { depends => 'config_stanza'} ],
32 #               ['connect-info|n:s%' => ' supply the connect info as additonal options ie -I dsn=<dsn> user=<user> password=<pass> '],
33 #               ['sql-dir|q:s' => 'The directory where sql diffs will be created'],
34 #               ['sql-type|t:s' => 'The RDBMs falvour you wish to use'],
35 #               ['version|v:i' => 'Supply a version install'],
36 #               ['preversion|p:s' => 'The previous version to diff against',],
37 #
38 #    'schema=s'  => \my $schema_class,
39 #    'class=s'   => \my $resultset_class,
40 #    'connect=s' => \my $connect,
41 #    'op=s'      => \my $op,
42 #    'set=s'     => \my $set,
43 #    'where=s'   => \my $where,
44 #    'attrs=s'   => \my $attrs,
45 #    'format=s'  => \my $format,
46 #    'force'     => \my $force,
47 #    'trace'     => \my $trace,
48 #    'quiet'     => \my $quiet,
49 #    'help'      => \my $help,
50 #    'tlibs'      => \my $t_libs,
51 #=cut
52
53 =head1 NAME
54
55 DBIx::Class::Admin - Administration object for schemas
56
57 =head1 SYNOPSIS
58
59         use DBIx::Class::Admin;
60
61         # ddl manipulation
62         my $admin = DBIx::Class::Admin->new(
63                 schema_class=> 'MY::Schema',
64                 sql_dir=> $sql_dir,
65                 connect_info => { dsn => $dsn, user => $user, password => $pass },
66         );
67
68         # create SQLite sql
69         $admin->create('SQLite');
70
71         # create SQL diff for an upgrade
72         $admin->create('SQLite', {} , "1.0");
73
74         # upgrade a database
75         $admin->upgrade();
76
77         # install a version for an unversioned schema
78         $admin->install("3.0");
79
80 =head1 Attributes
81
82 =head2 lib
83
84 add a library search path
85 =cut
86 has lib => (
87         is              => 'ro',
88         isa             => Dir,
89         coerce  => 1,
90         trigger => \&_set_inc,
91 );
92
93 sub _set_inc {
94         my ($self, $lib) = @_;
95         push @INC, $lib->stringify;
96 }
97
98 =head2 schema_class
99
100 the class of the schema to load
101 =cut
102 has 'schema_class' => (
103         is              => 'ro',
104         isa             => 'Str',
105         coerce  => 1,
106 );
107
108 =head2 schema
109
110 A pre-connected schema object can be provided for manipulation
111 =cut
112 has 'schema' => (
113         is                      => 'ro',
114         isa                     => 'DBIx::Class::Schema',
115         lazy_build      => 1,
116 );
117
118
119
120 sub _build_schema {
121         my ($self)  = @_;
122         $self->ensure_class_loaded($self->schema_class);
123
124         $self->connect_info->[3]->{ignore_version} =1;
125         return $self->schema_class->connect(@{$self->connect_info()} ); # ,  $self->connect_info->[3], { ignore_version => 1} );
126 }
127
128 =head2 connect_info
129
130 connect_info the arguments to provide to the connect call of the schema_class
131 =cut
132 has 'connect_info' => (
133         is                      => 'ro',
134         isa                     => ArrayRef,
135         lazy_build      => 1,
136 );
137
138 sub _build_connect_info {
139         my ($self) = @_;
140         return $self->_find_stanza($self->config, $self->config_stanza);
141 }
142
143 =head2 config_file
144
145 config_file provide a config_file to read connect_info from, if this is provided
146 config_stanze should also be provided to locate where the connect_info is in the config
147 The config file should be in a format readable by Config::General
148 =cut
149 has config_file => (
150         is                      => 'ro',
151         isa                     => File,
152         coerce          => 1,
153 );
154
155 =head2 config_stanza
156
157 config_stanza for use with config_file should be a '::' deliminated 'path' to the connection information
158 designed for use with catalyst config files
159 =cut
160 has 'config_stanza' => (
161         is                      => 'ro',
162         isa                     => 'Str',
163 );
164
165 =head2 config
166
167 Instead of loading from a file the configuration can be provided directly as a hash ref.  Please note 
168 config_stanza will still be required.
169 =cut
170 has config => (
171         is                      => 'ro',
172         isa                     => HashRef,
173         lazy_build      => 1,
174 );
175
176 sub _build_config {
177         my ($self) = @_;
178         try { require 'Config::Any'; } catch { die "Config::Any is required to parse the config file"; };
179
180         my $cfg = Config::Any->load_files ( {files => [$self->config_file], use_ext =>1, flatten_to_hash=>1});
181
182         # just grab the config from the config file
183         $cfg = $cfg->{$self->config_file};
184         return $cfg;
185 }
186
187 =head2 sql_dir
188
189 The location where sql ddl files should be created or found for an upgrade.
190 =cut
191 has 'sql_dir' => (
192         is                      => 'ro',
193         isa                     => Dir,
194         coerce          => 1,
195 );
196
197 =head2 version
198
199 Used for install, the version which will be 'installed' in the schema
200 =cut
201 has version => (
202         is                      => 'rw',
203         isa                     => 'Str',
204 );
205
206 =head2 preversion
207
208 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
209 =cut
210 has preversion => (
211         is                      => 'rw',
212         isa                     => 'Str',
213 );
214
215 =head2 force
216
217 Try and force certain operations.
218 =cut
219 has force => (
220         is                      => 'rw',
221         isa                     => 'Bool',
222 );
223
224 =head2 quite
225
226 Be less verbose about actions
227 =cut
228 has quiet => (
229         is                      => 'rw',
230         isa                     => 'Bool',
231 );
232
233 has '_confirm' => (
234         is              => 'bare',
235         isa             => 'Bool',
236 );
237
238 =head1 METHODS
239
240 =head2 create
241
242 =over 4
243
244 =item Arguments: $sqlt_type, \%sqlt_args, $preversion
245
246 =back
247
248 L<create> will generate sql for the supplied schema_class in sql_dir.  The flavour of sql to 
249 generate can be controlled by suppling a sqlt_type which should be a L<SQL::Translator> name.  
250
251 Arguments for L<SQL::Translator> can be supplied in the sqlt_args hashref.
252
253 Optional preversion can be supplied to generate a diff to be used by upgrade.
254 =cut
255
256 sub create {
257         my ($self, $sqlt_type, $sqlt_args, $preversion) = @_;
258
259         $preversion ||= $self->preversion();
260
261         my $schema = $self->schema();
262         # create the dir if does not exist
263         $self->sql_dir->mkpath() if ( ! -d $self->sql_dir);
264
265         $schema->create_ddl_dir( $sqlt_type, (defined $schema->schema_version ? $schema->schema_version : ""), $self->sql_dir->stringify, $preversion, $sqlt_args );
266 }
267
268 =head2 upgrade
269
270 =over 4
271
272 =item Arguments: <none>
273
274 =back
275
276 upgrade will attempt to upgrade the connected database to the same version as the schema_class.
277 B<MAKE SURE YOU BACKUP YOUR DB FIRST>
278 =cut
279
280 sub upgrade {
281         my ($self) = @_;
282         my $schema = $self->schema();
283         if (!$schema->get_db_version()) {
284                 # schema is unversioned
285                 die "could not determin current schema version, please either install or deploy";
286         } else {
287                 my $ret = $schema->upgrade();
288                 return $ret;
289         }
290 }
291
292 =head2 install
293
294 =over 4
295
296 =item Arguments: $version
297
298 =back
299
300 install is here to help when you want to move to L<DBIx::Class::Schema::Versioned> and have an existing 
301 database.  install will take a version and add the version tracking tables and 'install' the version.  No 
302 further ddl modification takes place.  Setting the force attribute to a true value will allow overriding of 
303 already versioned databases.
304 =cut
305 sub install {
306         my ($self, $version) = @_;
307
308         my $schema = $self->schema();
309         $version ||= $self->version();
310         if (!$schema->get_db_version() ) {
311                 # schema is unversioned
312                 print "Going to install schema version\n";
313                 my $ret = $schema->install($version);
314                 print "retun is $ret\n";
315         }
316         elsif ($schema->get_db_version() and $self->force ) {
317                 warn "forcing install may not be a good idea";
318                 if($self->_confirm() ) {
319                         # FIXME private api
320                         $self->schema->_set_db_version({ version => $version});
321                 }
322         }
323         else {
324                 die "schema already has a version not installing, try upgrade instead";
325         }
326
327 }
328
329 =head2 deploy
330
331 =over 4
332
333 =item Arguments: $args
334
335 =back
336
337 deploy will create the schema at the connected database.  C<$args> are passed straight to 
338 L<DBIx::Class::Schema/deploy>.  
339 =cut
340 sub deploy {
341         my ($self, $args) = @_;
342         my $schema = $self->schema();
343         if (!$schema->get_db_version() ) {
344                 # schema is unversioned
345                 $schema->deploy( $args, $self->sql_dir)
346                         or die "could not deploy schema";
347         } else {
348                 die "there already is a database with a version here, try upgrade instead";
349         }
350 }
351
352
353 # FIXME ensure option spec compatability
354 #die('Do not use the where option with the insert op') if ($where);
355 #die('Do not use the attrs option with the insert op') if ($attrs);
356
357 =head2 insert_data
358
359 =over 4
360
361 =item Arguments: $rs, $set
362
363 =back
364
365 insert_data takes the name of a resultset from the schema_class and a hashref of data to insert
366 into that resultset
367
368 =cut
369 sub insert_data {
370         my ($self, $rs, $set) = @_;
371         my $resultset = $self->schema->resultset($rs);
372         my $obj = $resultset->create( $set );
373         print ''.ref($resultset).' ID: '.join(',',$obj->id())."\n" if (!$self->quiet);
374 }
375
376
377 =head2 update_data
378
379 =over 4 
380
381 =item Arguments: $rs, $set, $where
382
383 =back
384
385 update_data takes the name of a resultset from the schema_class, a hashref of data to update and 
386 a where hash used to form the search for the rows to update. 
387 =cut
388 sub update_data {
389         my ($self, $rs, $set, $where) = @_;
390
391         my $resultset = $self->schema->resultset($rs);
392         $resultset = $resultset->search( ($where||{}) );
393
394         my $count = $resultset->count();
395         print "This action will modify $count ".ref($resultset)." records.\n" if (!$self->quiet);
396
397         if ( $self->force || $self->_confirm() ) {
398                 $resultset->update_all( $set );
399         }
400 }
401
402 # FIXME
403 #die('Do not use the set option with the delete op') if ($set);
404 =head2 delete_data
405
406 =over 4
407
408 =item Arguments: $rs, $where, $attrs
409
410 =back
411
412 delete_data takes the name of a resultset from the schema_class, a where hashref and a attrs to pass to ->search. 
413 The found data is deleted and cannot be recovered.
414 =cut
415 sub delete_data {
416         my ($self, $rs, $where, $attrs) = @_;
417
418         my $resultset = $self->schema->resultset($rs);
419         $resultset = $resultset->search( ($where||{}), ($attrs||()) );
420
421         my $count = $resultset->count();
422         print "This action will delete $count ".ref($resultset)." records.\n" if (!$self->quiet);
423
424         if ( $self->force || $self->_confirm() ) {
425                 $resultset->delete_all();
426         }
427 }
428
429 =head2 select_data
430
431 =over 4
432
433 =item Arguments: $rs, $where, $attrs
434
435 =back
436
437 select_data takes the name of a resultset from the schema_class, a where hashref and a attrs to pass to ->search. 
438 The found data is returned in a array ref where the first row will be the columns list.
439
440 =cut
441 sub select_data {
442         my ($self, $rs, $where, $attrs) = @_;
443
444         my $resultset = $self->schema->resultset($rs);
445         $resultset = $resultset->search( ($where||{}), ($attrs||()) );
446
447         my @data;
448         my @columns = $resultset->result_source->columns();
449         push @data, [@columns];# 
450
451         while (my $row = $resultset->next()) {
452                 my @fields;
453                 foreach my $column (@columns) {
454                         push( @fields, $row->get_column($column) );
455                 }
456                 push @data, [@fields];
457         }
458
459         return \@data;
460 }
461
462 sub _confirm {
463         my ($self) = @_;
464         print "Are you sure you want to do this? (type YES to confirm) \n";
465         # mainly here for testing
466         return 1 if ($self->meta->get_attribute('_confirm')->get_value($self));
467         my $response = <STDIN>;
468         return 1 if ($response=~/^YES/);
469         return;
470 }
471
472 sub _find_stanza {
473         my ($self, $cfg, $stanza) = @_;
474         my @path = split /::/, $stanza;
475         while (my $path = shift @path) {
476                 if (exists $cfg->{$path}) {
477                         $cfg = $cfg->{$path};
478                 }
479                 else {
480                         die "could not find $stanza in config, $path did not seem to exist";
481                 }
482         }
483         return $cfg;
484 }
485 1;