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