60124e4b6e59f06489c5d82ca15229233b866911
[dbsrgits/DBIx-Class.git] / script / dbicadmin
1 #!/usr/bin/env 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 =q|
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 my ($opts, $usage) = describe_options(
32     "%c: %o",
33   (
34     ['Actions'],
35     ["action" => hidden => { one_of => [
36       ['create' => 'Create version diffs needs preversion'],
37       ['upgrade' => 'Upgrade the database to the current schema'],
38       ['install' => 'Install the schema version tables to an existing database'],
39       ['deploy' => 'Deploy the schema to the database'],
40       ['select'   => 'Select data from the schema'],
41       ['insert'   => 'Insert data into the schema'],
42       ['update'   => 'Update data in the schema'],
43       ['delete'   => 'Delete data from the schema'],
44       ['op:s' => 'compatiblity option all of the above can be suppied as --op=<action>'],
45       ['help' => 'display this help', { implies => { schema_class => '__dummy__' } } ],
46       ['selfinject-pod' => 'hidden', { implies => { schema_class => '__dummy__' } } ],
47     ], required => 1 }],
48     ['Arguments'],
49     ["configuration" => hidden => { one_of => [
50       ['config-file|config:s' => 'Supply the config file for parsing by Config::Any', { depends => 'config_stanza'} ],
51       ['connect-info:s%' => 'Supply the connect info as trailing options e.g. --connect-info dsn=<dsn> user=<user> password=<pass>' ],
52       ['connect:s' => 'Supply the connect info as a JSON-encoded structure, e.g. an --connect=["dsn","user","pass"]'],
53     ] }],
54     ['schema-class:s' => 'The class of the schema to load', { required => 1 } ],
55     ['config-stanza:s' => 'Where in the config to find the connection_info, supply in form MyApp::Model::DB',],
56     ['resultset|resultset-class|class:s' => 'The resultset to operate on for data manipulation' ],
57     ['sql-dir:s' => 'The directory where sql diffs will be created'],
58     ['sql-type:s' => 'The RDBMs flavour you wish to use'],
59     ['version:i' => 'Supply a version install'],
60     ['preversion:s' => 'The previous version to diff against',],
61     ['set:s' => 'JSON data used to perform data operations' ],
62     ['attrs:s' => 'JSON string to be used for the second argument for search'],
63     ['where:s' => 'JSON string to be used for the where clause of search'],
64     ['force' => 'Be forceful with some operations'],
65     ['trace' => 'Turn on DBIx::Class trace output'],
66     ['quiet' => 'Be less verbose'],
67     ['I:s@' => 'Same as perl\'s -I, prepended to current @INC'],
68   )
69 );
70
71 if($opts->{selfinject_pod}) {
72
73     die "This is an internal method, do not call!!!\n"
74       unless $ENV{MAKELEVEL};
75
76     $usage->synopsis($synopsis_text);
77     $usage->short_description($short_description);
78     exec (
79       $^X,
80       qw/-p -0777 -i -e/,
81       (
82         's/^# auto_pod_begin.*^# auto_pod_end/'
83       . quotemeta($usage->pod)
84       . '/ms'
85       ),
86       __FILE__
87     );
88 }
89
90 # FIXME - lowercasing will eventually go away when Getopt::Long::Descriptive is fixed
91 if($opts->{i}) {
92   require lib;
93   lib->import( @{delete $opts->{i}} );
94 }
95
96 if($opts->{help}) {
97   $usage->die();
98 }
99
100 # option compatability mangle
101 # (can not be joined in the spec, one is s% the other is s)
102 if($opts->{connect}) {
103   $opts->{connect_info} = delete $opts->{connect};
104 }
105
106 my $admin = DBIx::Class::Admin->new( %$opts );
107
108 my $action = $opts->{action};
109
110 $action = $opts->{op} if ($action eq 'op');
111
112 print "Performing action $action...\n";
113
114 my $res = $admin->$action();
115 if ($action eq 'select') {
116
117   my $format = $opts->{format} || 'tsv';
118   die('Invalid format') if ($format!~/^tsv|csv$/s);
119
120   require Text::CSV;
121
122   my $csv = Text::CSV->new({
123     sep_char => ( $format eq 'tsv' ? "\t" : ',' ),
124   });
125
126   foreach my $row (@$res) {
127     $csv->combine( @$row );
128     print $csv->string()."\n";
129   }
130 }
131
132
133 __END__
134
135 # auto_pod_begin
136 #
137 # This will be replaced by the actual pod when selfinject-pod is invoked
138 #
139 # auto_pod_end
140
141 # vim: et ft=perl