Fix exception text
[dbsrgits/DBIx-Class.git] / script / dbicadmin
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 BEGIN {
7   use DBIx::Class;
8   die (  'The following modules are required for the dbicadmin utility: '
9        . DBIx::Class::Optional::Dependencies->req_missing_for ('admin_script')
10        . "\n"
11   ) unless DBIx::Class::Optional::Dependencies->req_ok_for ('admin_script');
12 }
13
14 use DBIx::Class::Admin::Descriptive;
15 #use Getopt::Long::Descriptive;
16 use DBIx::Class::Admin;
17
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
32 my ($opts, $usage) = describe_options(
33     "%c: %o",
34   (
35     ['Actions'],
36     ["action" => hidden => { one_of => [
37       ['create|c' => 'Create version diffs needs preversion',],
38       ['upgrade|U' => 'Upgrade the database to the current schema '],
39       ['install|I' => 'Install the schema version tables to an existing database',],
40       ['deploy|d' => 'Deploy the schema to the database',],
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>'],
46       ['help|h' => 'display this help', { implies => { schema_class => 'main' } } ],
47       ['pod' => 'Output this usage as pod', { implies => { schema_class => 'main' } } ],
48     ], required=> 1 }],
49     ['Arguments'],
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' ],
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'],
65     ['quiet' => 'Be less verbose'],
66   )
67 );
68
69 die "please only use one of --config or --connect-info\n" if ($opts->{config} and $opts->{connect_info});
70
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
82 # option compatability mangle
83 if($opts->{connect}) {
84   $opts->{connect_info} = delete $opts->{connect};
85 }
86
87 my $admin = DBIx::Class::Admin->new( %$opts );
88
89
90 my $action = $opts->{action};
91
92 $action = $opts->{op} if ($action eq 'op');
93
94 print "Performig action $action...\n";
95
96 my $res = $admin->$action();
97 if ($action eq 'select') {
98
99   my $format = $opts->{format} || 'tsv';
100   die('Invalid format') if ($format!~/^tsv|csv$/s);
101
102   require Text::CSV;
103
104   my $csv = Text::CSV->new({
105     sep_char => ( $format eq 'tsv' ? "\t" : ',' ),
106   });
107
108   foreach my $row (@$res) {
109     $csv->combine( @$row );
110     print $csv->string()."\n";
111   }
112 }
113
114
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
138
139 # vim: et ft=perl