c50c50b9273bab7c61dd0d8dac53f94c9be64db9
[dbsrgits/DBIx-Class.git] / script / dbicadmin
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Getopt::Long::Descriptive;
7 use DBIx::Class::Admin;
8
9 my ($opts, $usage) = describe_options(
10   "%c: %o",
11   (
12     ['Actions'],
13     ["action" => hidden => { one_of => [
14       ['create|c' => 'Create version diffs needs preversion',],
15       ['upgrade|u' => 'Upgrade the database to the current schema '],
16       ['install|i' => 'Install the schema to the database',],
17       ['deploy|d' => 'Deploy the schema to the database',],
18       ['select|s'   => 'Select data from the schema', ],
19       ['insert|i'   => 'Insert data into the schema', ],
20       ['update|u'   => 'Update data in the schema', ], 
21       ['delete|D'   => 'Delete data from the schema',],
22       ['op:s' => 'compatiblity option all of the above can be suppied as --op=<action>'],
23       ['help|h' => 'display this help'],
24     ], required=> 1 }],
25     ['Options'],
26     ['schema-class|schema|C:s' => 'The class of the schema to load', { required => 1 } ],
27     ['resultset|resultset_class|class|r:s' => 'The resultset to operate on for data manipulation' ],
28     ['config-stanza|S:s' => 'Where in the config to find the connection_info, supply in form MyApp::Model::DB',],
29     ['config|f:s' => 'Supply the config file for parsing by Config::Any', { depends => 'config_stanza'} ],
30     ['connect-info|n:s%' => 'Supply the connect info as additonal options ie -I dsn=<dsn> user=<user> password=<pass> '],
31     ['connect:s' => 'Supply the connect info as a json string' ],
32     ['sql-dir|q:s' => 'The directory where sql diffs will be created'],
33     ['sql-type|t:s' => 'The RDBMs flavour you wish to use'],
34     ['version|v:i' => 'Supply a version install'],
35     ['preversion|p:s' => 'The previous version to diff against',],
36     ['set:s' => 'JSON data used to perform data operations' ],
37     ['lib|I:s' => 'Additonal library path to search in'], 
38     ['attrs:s' => 'JSON string to be used for the second argument for search'],
39     ['where:s' => 'JSON string to be used for the where clause of search'],
40     ['force' => 'Be forceful with some operations'],
41     ['trace' => 'Turn on DBIx::Class trace output'],
42     ['quiet' => 'Be less verbose'],
43   )
44 );
45
46 die "please only use one of --config or --connect-info\n" if ($opts->{config} and $opts->{connect_info});
47
48 # option compatability mangle
49 if($opts->{connect}) {
50   $opts->{connect_info} = delete $opts->{connect};
51 }
52
53 my $admin = DBIx::Class::Admin->new( %$opts );
54
55
56 my $action = $opts->{action};
57
58 $action = $opts->{op} if ($action eq 'op');
59
60 print "Performig action $action...\n";
61
62 my $res = $admin->$action();
63 if ($action eq 'select') {
64
65   my $format = $opts->{format} || 'tsv';
66   die('Invalid format') if ($format!~/^tsv|csv$/s);
67
68   require Text::CSV;
69
70   my $csv = Text::CSV->new({
71     sep_char => ( $format eq 'tsv' ? "\t" : ',' ),
72   });
73
74   foreach my $row (@$res) {
75     $csv->combine( @$row );
76     print $csv->string()."\n";
77   }
78 }
79
80
81
82 =head1 AUTHOR
83
84 Aran Deltac <bluefeet@cpan.org>
85
86 refactored by
87 Gordon Irving <goraxe@cpan.org>
88
89 =head1 LICENSE
90
91 You may distribute this code under the same terms as Perl itself.