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