Commit | Line | Data |
a705b175 |
1 | #!/usr/bin/perl |
2 | |
a94aa524 |
3 | use strict; |
4 | use warnings; |
5 | |
fd27648a |
6 | use Getopt::Long::Descriptive; |
fd27648a |
7 | use DBIx::Class::Admin; |
8 | |
9 | |
10 | my ($opts, $usage) = describe_options( |
a705b175 |
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 | ) |
2bbc85c9 |
46 | ); |
a94aa524 |
47 | |
d8d6276a |
48 | |
fd27648a |
49 | if ($opts->{help}) { |
a705b175 |
50 | print $usage->text; |
51 | exit 0; |
b04e5d3e |
52 | } |
a94aa524 |
53 | |
fd27648a |
54 | if ($opts->{tlibs}) { |
55 | unshift( @INC, 't/lib', 'lib' ); |
56 | } |
a94aa524 |
57 | |
fd27648a |
58 | die "please only use one of --config or --connect-info" if ($opts->{config} and $opts->{connect_info}); |
a94aa524 |
59 | |
fd27648a |
60 | # option compatability mangle |
61 | if($opts->{connect}) { |
a705b175 |
62 | $opts->{connect_info} = delete $opts->{connect}; |
b04e5d3e |
63 | } |
a94aa524 |
64 | |
fd27648a |
65 | my $admin = DBIx::Class::Admin->new( %$opts ); |
66 | |
67 | |
68 | my $action = $opts->{action}; |
c57f1cf7 |
69 | |
70 | $action = $opts->{op} if ($action eq 'op'); |
fd27648a |
71 | my $res = $admin->$action(); |
72 | |
c57f1cf7 |
73 | print "going to perform action $action\n"; |
fd27648a |
74 | if ($action eq 'select') { |
75 | |
a705b175 |
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 | } |
a94aa524 |
94 | } |
95 | |
d8d6276a |
96 | |
6717e3a8 |
97 | |
a94aa524 |
98 | =head1 AUTHOR |
99 | |
100 | Aran Deltac <bluefeet@cpan.org> |
101 | |
fd27648a |
102 | refactored by |
103 | Gordon Irving <goraxe@cpan.org> |
104 | |
a94aa524 |
105 | =head1 LICENSE |
106 | |
107 | You may distribute this code under the same terms as Perl itself. |