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