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