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