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; |
3abee566 |
8 | die ( 'The following modules are required for the dbicadmin utility: ' |
a4a02f15 |
9 | . DBIx::Class::Optional::Dependencies->req_missing_for ('admin_script') |
3abee566 |
10 | . "\n" |
a4a02f15 |
11 | ) unless DBIx::Class::Optional::Dependencies->req_ok_for ('admin_script'); |
12 | } |
13 | |
e5279977 |
14 | use DBIx::Class::Admin::Descriptive; |
15 | #use Getopt::Long::Descriptive; |
fd27648a |
16 | use DBIx::Class::Admin; |
17 | |
e5279977 |
18 | my $short_description = "utility for administrating DBIx::Class schemata"; |
19 | my $synopsis_text =qq{ |
20 | deploy a schema to a database |
21 | %c --schema=MyApp::Schema \ |
22 | --connect='["dbi:SQLite:my.db", "", ""]' \ |
23 | --deploy |
24 | |
25 | update an existing record |
26 | %c --schema=MyApp::Schema --class=Employee \ |
27 | --connect='["dbi:SQLite:my.db", "", ""]' \ |
28 | --op=update --set='{ "name": "New_Employee" }' |
29 | } |
30 | ; |
31 | |
fd27648a |
32 | my ($opts, $usage) = describe_options( |
e5279977 |
33 | "%c: %o", |
a705b175 |
34 | ( |
35 | ['Actions'], |
36 | ["action" => hidden => { one_of => [ |
37 | ['create|c' => 'Create version diffs needs preversion',], |
e5279977 |
38 | ['upgrade|U' => 'Upgrade the database to the current schema '], |
39 | ['install|I' => 'Install the schema version tables to an existing database',], |
71ef99d5 |
40 | ['deploy|d' => 'Deploy the schema to the database',], |
a705b175 |
41 | ['select|s' => 'Select data from the schema', ], |
42 | ['insert|i' => 'Insert data into the schema', ], |
43 | ['update|u' => 'Update data in the schema', ], |
44 | ['delete|D' => 'Delete data from the schema',], |
45 | ['op:s' => 'compatiblity option all of the above can be suppied as --op=<action>'], |
e5279977 |
46 | ['help|h' => 'display this help', { implies => { schema_class => 'main' } } ], |
47 | ['pod' => 'Output this usage as pod', { implies => { schema_class => 'main' } } ], |
a705b175 |
48 | ], required=> 1 }], |
d26b9726 |
49 | ['Arguments'], |
a705b175 |
50 | ['schema-class|schema|C:s' => 'The class of the schema to load', { required => 1 } ], |
51 | ['resultset|resultset_class|class|r:s' => 'The resultset to operate on for data manipulation' ], |
52 | ['config-stanza|S:s' => 'Where in the config to find the connection_info, supply in form MyApp::Model::DB',], |
53 | ['config|f:s' => 'Supply the config file for parsing by Config::Any', { depends => 'config_stanza'} ], |
54 | ['connect-info|n:s%' => 'Supply the connect info as additonal options ie -I dsn=<dsn> user=<user> password=<pass> '], |
55 | ['connect:s' => 'Supply the connect info as a json string' ], |
56 | ['sql-dir|q:s' => 'The directory where sql diffs will be created'], |
57 | ['sql-type|t:s' => 'The RDBMs flavour you wish to use'], |
58 | ['version|v:i' => 'Supply a version install'], |
59 | ['preversion|p:s' => 'The previous version to diff against',], |
60 | ['set:s' => 'JSON data used to perform data operations' ], |
a705b175 |
61 | ['attrs:s' => 'JSON string to be used for the second argument for search'], |
62 | ['where:s' => 'JSON string to be used for the where clause of search'], |
63 | ['force' => 'Be forceful with some operations'], |
64 | ['trace' => 'Turn on DBIx::Class trace output'], |
a705b175 |
65 | ['quiet' => 'Be less verbose'], |
66 | ) |
2bbc85c9 |
67 | ); |
a94aa524 |
68 | |
ad81fe7d |
69 | die "please only use one of --config or --connect-info\n" if ($opts->{config} and $opts->{connect_info}); |
a94aa524 |
70 | |
e5279977 |
71 | if($opts->{pod}) { |
72 | $usage->synopsis($synopsis_text); |
73 | $usage->short_description($short_description); |
74 | print $usage->pod(); |
75 | exit 0; |
76 | } |
77 | |
78 | if($opts->{help}) { |
79 | $usage->die(); |
80 | } |
81 | |
fd27648a |
82 | # option compatability mangle |
83 | if($opts->{connect}) { |
a705b175 |
84 | $opts->{connect_info} = delete $opts->{connect}; |
b04e5d3e |
85 | } |
a94aa524 |
86 | |
fd27648a |
87 | my $admin = DBIx::Class::Admin->new( %$opts ); |
88 | |
89 | |
90 | my $action = $opts->{action}; |
c57f1cf7 |
91 | |
92 | $action = $opts->{op} if ($action eq 'op'); |
fd27648a |
93 | |
ad81fe7d |
94 | print "Performig action $action...\n"; |
95 | |
96 | my $res = $admin->$action(); |
fd27648a |
97 | if ($action eq 'select') { |
98 | |
a705b175 |
99 | my $format = $opts->{format} || 'tsv'; |
100 | die('Invalid format') if ($format!~/^tsv|csv$/s); |
a705b175 |
101 | |
ad81fe7d |
102 | require Text::CSV; |
103 | |
104 | my $csv = Text::CSV->new({ |
105 | sep_char => ( $format eq 'tsv' ? "\t" : ',' ), |
106 | }); |
107 | |
a705b175 |
108 | foreach my $row (@$res) { |
109 | $csv->combine( @$row ); |
110 | print $csv->string()."\n"; |
111 | } |
a94aa524 |
112 | } |
e5279977 |
113 | |
e5279977 |
114 | |
6f720a19 |
115 | #__END__ |
116 | # |
117 | #=begin pod_begin |
118 | # |
119 | #BEGIN MARKER FOR DYNAMIC POD |
120 | # |
121 | #=end pod_begin |
122 | # |
123 | #=begin pod_end |
124 | # |
125 | #END MARKER FOR DYNAMIC POD |
126 | # |
127 | #=end pod_end |
128 | # |
129 | #=head1 AUTHOR |
130 | # |
131 | #See L<DBIx::Class/CONTRIBUTORS>. |
132 | # |
133 | #=head1 LICENSE |
134 | # |
135 | #You may distribute this code under the same terms as Perl itself |
136 | # |
137 | #=cut |
e5279977 |
138 | |
139 | # vim: et ft=perl |