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