Commit | Line | Data |
9274250d |
1 | #!/usr/bin/perl |
a705b175 |
2 | |
a94aa524 |
3 | use strict; |
4 | use warnings; |
5 | |
fd27648a |
6 | use Getopt::Long::Descriptive; |
fd27648a |
7 | use DBIx::Class::Admin; |
8 | |
fd27648a |
9 | my ($opts, $usage) = describe_options( |
a705b175 |
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',], |
71ef99d5 |
17 | ['deploy|d' => 'Deploy the schema to the database',], |
a705b175 |
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'], |
a705b175 |
42 | ['quiet' => 'Be less verbose'], |
43 | ) |
2bbc85c9 |
44 | ); |
a94aa524 |
45 | |
d8d6276a |
46 | |
fd27648a |
47 | die "please only use one of --config or --connect-info" if ($opts->{config} and $opts->{connect_info}); |
a94aa524 |
48 | |
fd27648a |
49 | # option compatability mangle |
50 | if($opts->{connect}) { |
a705b175 |
51 | $opts->{connect_info} = delete $opts->{connect}; |
b04e5d3e |
52 | } |
a94aa524 |
53 | |
fd27648a |
54 | my $admin = DBIx::Class::Admin->new( %$opts ); |
55 | |
56 | |
57 | my $action = $opts->{action}; |
c57f1cf7 |
58 | |
59 | $action = $opts->{op} if ($action eq 'op'); |
fd27648a |
60 | my $res = $admin->$action(); |
61 | |
c57f1cf7 |
62 | print "going to perform action $action\n"; |
fd27648a |
63 | if ($action eq 'select') { |
64 | |
a705b175 |
65 | my $csv_class; |
66 | my $format = $opts->{format} || 'tsv'; |
67 | die('Invalid format') if ($format!~/^tsv|csv$/s); |
68 | $csv_class = 'Text::CSV_XS'; |
69 | eval{ require Text::CSV_XS }; |
70 | if ($@) { |
71 | $csv_class = 'Text::CSV_PP'; |
72 | eval{ require Text::CSV_PP }; |
73 | die('The select op requires either the Text::CSV_XS or the Text::CSV_PP module') if ($@); |
74 | } |
75 | |
76 | my $csv = $csv_class->new({ |
77 | sep_char => ( $format eq 'tsv' ? "\t" : ',' ), |
78 | }); |
79 | foreach my $row (@$res) { |
80 | $csv->combine( @$row ); |
81 | print $csv->string()."\n"; |
82 | } |
a94aa524 |
83 | } |
84 | |
d8d6276a |
85 | |
6717e3a8 |
86 | |
a94aa524 |
87 | =head1 AUTHOR |
88 | |
89 | Aran Deltac <bluefeet@cpan.org> |
90 | |
9274250d |
91 | refactored by |
fd27648a |
92 | Gordon Irving <goraxe@cpan.org> |
93 | |
a94aa524 |
94 | =head1 LICENSE |
95 | |
96 | You may distribute this code under the same terms as Perl itself. |