Forgotten reenable of syntax test to guard for 37af2968e
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Admin.pm
CommitLineData
9f3849c3 1package DBIx::Class::Admin;
2
71ef99d5 3# check deps
4BEGIN {
a4a02f15 5 use DBIx::Class;
70c28808 6 die('The following modules are required for DBIx::Class::Admin ' . DBIx::Class::Optional::Dependencies->req_missing_for ('admin') )
a4a02f15 7 unless DBIx::Class::Optional::Dependencies->req_ok_for ('admin');
71ef99d5 8}
585072bb 9
be855469 10use JSON::Any qw(DWIW PP JSON CPANEL XS);
71ef99d5 11use Moose;
a62dec82 12use MooseX::Types::Moose qw/Int Str Any Bool/;
13use DBIx::Class::Admin::Types qw/DBICConnectInfo DBICHashRef/;
71ef99d5 14use MooseX::Types::JSON qw(JSON);
15use MooseX::Types::Path::Class qw(Dir File);
d401ab6b 16use MooseX::Types::LoadableClass qw(LoadableClass);
71ef99d5 17use Try::Tiny;
8aa16237 18use namespace::autoclean;
bb464677 19
595cb2c7 20=head1 NAME
21
22DBIx::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 59The Admin interface has additional requirements not currently part of
60L<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 66the class of the schema to load
e81f0fe2 67
595cb2c7 68=cut
e81f0fe2 69
9f3849c3 70has 'schema_class' => (
3b27cdac 71 is => 'ro',
d401ab6b 72 isa => LoadableClass,
9f3849c3 73);
74
e81f0fe2 75
595cb2c7 76=head2 schema
9f3849c3 77
595cb2c7 78A pre-connected schema object can be provided for manipulation
e81f0fe2 79
595cb2c7 80=cut
e81f0fe2 81
9f3849c3 82has 'schema' => (
3b27cdac 83 is => 'ro',
84 isa => 'DBIx::Class::Schema',
a705b175 85 lazy_build => 1,
9f3849c3 86);
87
9f3849c3 88sub _build_schema {
a705b175 89 my ($self) = @_;
312eef08 90
8c96bbc2 91 $self->connect_info->[3]{ignore_version} = 1;
92 return $self->schema_class->connect(@{$self->connect_info});
9f3849c3 93}
94
bb464677 95=head2 resultset
96
97a resultset from the schema to operate on
e81f0fe2 98
bb464677 99=cut
e81f0fe2 100
bb464677 101has 'resultset' => (
3b27cdac 102 is => 'rw',
103 isa => Str,
bb464677 104);
105
e81f0fe2 106
bb464677 107=head2 where
108
109a hash ref or json string to be used for identifying data to manipulate
e81f0fe2 110
bb464677 111=cut
112
113has 'where' => (
a705b175 114 is => 'rw',
3b27cdac 115 isa => DBICHashRef,
116 coerce => 1,
bb464677 117);
118
e81f0fe2 119
bb464677 120=head2 set
e81f0fe2 121
bb464677 122a hash ref or json string to be used for inserting or updating data
e81f0fe2 123
bb464677 124=cut
125
126has 'set' => (
a705b175 127 is => 'rw',
3b27cdac 128 isa => DBICHashRef,
129 coerce => 1,
bb464677 130);
131
e81f0fe2 132
bb464677 133=head2 attrs
e81f0fe2 134
4d1e63f4 135a hash ref or json string to be used for passing additional info to the ->search call
e81f0fe2 136
bb464677 137=cut
e81f0fe2 138
bb464677 139has 'attrs' => (
3b27cdac 140 is => 'rw',
141 isa => DBICHashRef,
142 coerce => 1,
bb464677 143);
e81f0fe2 144
145
595cb2c7 146=head2 connect_info
147
148connect_info the arguments to provide to the connect call of the schema_class
bb464677 149
e81f0fe2 150=cut
bb464677 151
9f3849c3 152has 'connect_info' => (
3b27cdac 153 is => 'ro',
154 isa => DBICConnectInfo,
a705b175 155 lazy_build => 1,
3b27cdac 156 coerce => 1,
9f3849c3 157);
158
159sub _build_connect_info {
a705b175 160 my ($self) = @_;
161 return $self->_find_stanza($self->config, $self->config_stanza);
9f3849c3 162}
163
e81f0fe2 164
595cb2c7 165=head2 config_file
166
167config_file provide a config_file to read connect_info from, if this is provided
168config_stanze should also be provided to locate where the connect_info is in the config
7b71391b 169The config file should be in a format readable by Config::Any.
e81f0fe2 170
595cb2c7 171=cut
e81f0fe2 172
595cb2c7 173has config_file => (
a705b175 174 is => 'ro',
3b27cdac 175 isa => File,
176 coerce => 1,
595cb2c7 177);
178
e81f0fe2 179
595cb2c7 180=head2 config_stanza
181
4d1e63f4 182config_stanza for use with config_file should be a '::' delimited 'path' to the connection information
595cb2c7 183designed for use with catalyst config files
e81f0fe2 184
595cb2c7 185=cut
e81f0fe2 186
595cb2c7 187has 'config_stanza' => (
3b27cdac 188 is => 'ro',
189 isa => Str,
595cb2c7 190);
191
e81f0fe2 192
595cb2c7 193=head2 config
194
a03b396b 195Instead of loading from a file the configuration can be provided directly as a hash ref. Please note
595cb2c7 196config_stanza will still be required.
e81f0fe2 197
595cb2c7 198=cut
e81f0fe2 199
9f3849c3 200has config => (
3b27cdac 201 is => 'ro',
202 isa => DBICHashRef,
a705b175 203 lazy_build => 1,
9f3849c3 204);
205
206sub _build_config {
a705b175 207 my ($self) = @_;
71766122 208
9780718f 209 try { require Config::Any }
210 catch { die ("Config::Any is required to parse the config file.\n") };
9f3849c3 211
a705b175 212 my $cfg = Config::Any->load_files ( {files => [$self->config_file], use_ext =>1, flatten_to_hash=>1});
9f3849c3 213
a705b175 214 # just grab the config from the config file
215 $cfg = $cfg->{$self->config_file};
216 return $cfg;
9f3849c3 217}
218
e81f0fe2 219
595cb2c7 220=head2 sql_dir
9f3849c3 221
595cb2c7 222The location where sql ddl files should be created or found for an upgrade.
e81f0fe2 223
595cb2c7 224=cut
e81f0fe2 225
9f3849c3 226has 'sql_dir' => (
a705b175 227 is => 'ro',
3b27cdac 228 isa => Dir,
229 coerce => 1,
9f3849c3 230);
231
e81f0fe2 232
f3386204 233=head2 sql_type
234
235The type of sql dialect to use for creating sql files from schema
236
237=cut
238
239has 'sql_type' => (
240 is => 'ro',
241 isa => Str,
242);
243
595cb2c7 244=head2 version
9f3849c3 245
595cb2c7 246Used for install, the version which will be 'installed' in the schema
e81f0fe2 247
595cb2c7 248=cut
e81f0fe2 249
9f3849c3 250has version => (
3b27cdac 251 is => 'rw',
252 isa => Str,
9f3849c3 253);
254
e81f0fe2 255
595cb2c7 256=head2 preversion
257
4d1e63f4 258Previous 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 259
595cb2c7 260=cut
e81f0fe2 261
9f3849c3 262has preversion => (
3b27cdac 263 is => 'rw',
264 isa => Str,
9f3849c3 265);
266
e81f0fe2 267
595cb2c7 268=head2 force
269
270Try and force certain operations.
e81f0fe2 271
595cb2c7 272=cut
e81f0fe2 273
912e2d5a 274has force => (
3b27cdac 275 is => 'rw',
276 isa => Bool,
912e2d5a 277);
278
e81f0fe2 279
c57f1cf7 280=head2 quiet
595cb2c7 281
282Be less verbose about actions
e81f0fe2 283
595cb2c7 284=cut
e81f0fe2 285
64c012f4 286has quiet => (
3b27cdac 287 is => 'rw',
288 isa => Bool,
64c012f4 289);
290
912e2d5a 291has '_confirm' => (
3b27cdac 292 is => 'bare',
293 isa => Bool,
912e2d5a 294);
295
e81f0fe2 296
f3386204 297=head2 trace
298
299Toggle DBIx::Class debug output
300
301=cut
302
303has trace => (
304 is => 'rw',
305 isa => Bool,
306 trigger => \&_trigger_trace,
307);
308
309sub _trigger_trace {
310 my ($self, $new, $old) = @_;
311 $self->schema->storage->debug($new);
312}
313
314
595cb2c7 315=head1 METHODS
316
317=head2 create
318
319=over 4
320
321=item Arguments: $sqlt_type, \%sqlt_args, $preversion
322
323=back
324
f92a9d79 325C<create> will generate sql for the supplied schema_class in sql_dir. The
8f987bd5 326flavour of sql to generate can be controlled by supplying a sqlt_type which
327should be a L<SQL::Translator> name.
595cb2c7 328
329Arguments for L<SQL::Translator> can be supplied in the sqlt_args hashref.
330
331Optional preversion can be supplied to generate a diff to be used by upgrade.
e81f0fe2 332
595cb2c7 333=cut
334
9f3849c3 335sub create {
a705b175 336 my ($self, $sqlt_type, $sqlt_args, $preversion) = @_;
595cb2c7 337
a705b175 338 $preversion ||= $self->preversion();
f3386204 339 $sqlt_type ||= $self->sql_type();
595cb2c7 340
a705b175 341 my $schema = $self->schema();
342 # create the dir if does not exist
343 $self->sql_dir->mkpath() if ( ! -d $self->sql_dir);
9f3849c3 344
a705b175 345 $schema->create_ddl_dir( $sqlt_type, (defined $schema->schema_version ? $schema->schema_version : ""), $self->sql_dir->stringify, $preversion, $sqlt_args );
9f3849c3 346}
347
e81f0fe2 348
595cb2c7 349=head2 upgrade
350
351=over 4
352
353=item Arguments: <none>
354
355=back
356
357upgrade will attempt to upgrade the connected database to the same version as the schema_class.
358B<MAKE SURE YOU BACKUP YOUR DB FIRST>
e81f0fe2 359
595cb2c7 360=cut
361
9f3849c3 362sub upgrade {
a705b175 363 my ($self) = @_;
364 my $schema = $self->schema();
15de9f06 365
a705b175 366 if (!$schema->get_db_version()) {
367 # schema is unversioned
b718fd0a 368 $schema->throw_exception ("Could not determin current schema version, please either install() or deploy().\n");
a705b175 369 } else {
23737393 370 $schema->upgrade_directory ($self->sql_dir) if $self->sql_dir; # this will override whatever default the schema has
a705b175 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 385install is here to help when you want to move to L<DBIx::Class::Schema::Versioned> and have an existing
386database. install will take a version and add the version tracking tables and 'install' the version. No
387further ddl modification takes place. Setting the force attribute to a true value will allow overriding of
595cb2c7 388already versioned databases.
e81f0fe2 389
595cb2c7 390=cut
e81f0fe2 391
9f3849c3 392sub 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
15de9f06 399 print "Going to install schema version\n" if (!$self->quiet);
a705b175 400 my $ret = $schema->install($version);
15de9f06 401 print "return is $ret\n" if (!$self->quiet);
a705b175 402 }
403 elsif ($schema->get_db_version() and $self->force ) {
70c28808 404 warn "Forcing install may not be a good idea\n";
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 424deploy will create the schema at the connected database. C<$args> are passed straight to
e81f0fe2 425L<DBIx::Class::Schema/deploy>.
426
595cb2c7 427=cut
e81f0fe2 428
9f3849c3 429sub 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 443insert takes the name of a resultset from the schema_class and a hashref of data to insert
595cb2c7 444into that resultset
445
446=cut
e81f0fe2 447
bb464677 448sub 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 467update takes the name of a resultset from the schema_class, a hashref of data to update and
468a where hash used to form the search for the rows to update.
469
595cb2c7 470=cut
e81f0fe2 471
bb464677 472sub 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 498delete takes the name of a resultset from the schema_class, a where hashref and a attrs to pass to ->search.
595cb2c7 499The found data is deleted and cannot be recovered.
e81f0fe2 500
595cb2c7 501=cut
e81f0fe2 502
bb464677 503sub 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 529select takes the name of a resultset from the schema_class, a where hashref and a attrs to pass to ->search.
595cb2c7 530The found data is returned in a array ref where the first row will be the columns list.
531
532=cut
e81f0fe2 533
bb464677 534sub 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 558sub _confirm {
a705b175 559 my ($self) = @_;
15de9f06 560
a705b175 561 # mainly here for testing
562 return 1 if ($self->meta->get_attribute('_confirm')->get_value($self));
15de9f06 563
564 print "Are you sure you want to do this? (type YES to confirm) \n";
a705b175 565 my $response = <STDIN>;
15de9f06 566
567 return ($response=~/^YES/);
9f3849c3 568}
569
595cb2c7 570sub _find_stanza {
a705b175 571 my ($self, $cfg, $stanza) = @_;
572 my @path = split /::/, $stanza;
573 while (my $path = shift @path) {
574 if (exists $cfg->{$path}) {
575 $cfg = $cfg->{$path};
576 }
577 else {
b718fd0a 578 die ("Could not find $stanza in config, $path does not seem to exist.\n");
9c34993a 579 }
a705b175 580 }
7b71391b 581 $cfg = $cfg->{connect_info} if exists $cfg->{connect_info};
a705b175 582 return $cfg;
595cb2c7 583}
bb464677 584
47442cea 585=head1 AUTHOR
bb464677 586
e81f0fe2 587See L<DBIx::Class/CONTRIBUTORS>.
bb464677 588
589=head1 LICENSE
590
e81f0fe2 591You may distribute this code under the same terms as Perl itself
592
bb464677 593=cut
e81f0fe2 594
9f3849c3 5951;