d1daa382f147055a1a70646fe90fa054a536ce80
[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' => 'Create version diffs needs preversion',],
38       ['upgrade' => 'Upgrade the database to the current schema '],
39       ['install' => 'Install the schema version tables to an existing database',],
40       ['deploy' => 'Deploy the schema to the database',],
41       ['select'   => 'Select data from the schema', ],
42       ['insert'   => 'Insert data into the schema', ],
43       ['update'   => 'Update data in the schema', ], 
44       ['delete'   => 'Delete data from the schema',],
45       ['op:s' => 'compatiblity option all of the above can be suppied as --op=<action>'],
46       ['help' => 'display this help', { implies => { schema_class => '__dummy__' } } ],
47       ['selfinject-pod' => 'hidden', { implies => { schema_class => '__dummy__' } } ],
48     ], required=> 1 }],
49     ['Arguments'],
50     ['schema-class:s' => 'The class of the schema to load', { required => 1 } ],
51     ['resultset|resultset-class|class:s' => 'The resultset to operate on for data manipulation' ],
52     ['config-stanza:s' => 'Where in the config to find the connection_info, supply in form MyApp::Model::DB',],
53     ['config:s' => 'Supply the config file for parsing by Config::Any', { depends => 'config_stanza'} ],
54     ['connect-info: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:s' => 'The directory where sql diffs will be created'],
57     ['sql-type:s' => 'The RDBMs flavour you wish to use'],
58     ['version:i' => 'Supply a version install'],
59     ['preversion: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->{selfinject_pod}) {
72     $usage->synopsis($synopsis_text);
73     $usage->short_description($short_description);
74     exec (
75       $^X,
76       qw/-p -0777 -i -e/,
77       (
78         's/^# auto_pod_begin.*^# auto_pod_end/'
79       . quotemeta($usage->pod)
80       . '/ms'
81       ),
82       __FILE__
83     );
84 }
85
86 if($opts->{help}) {
87     $usage->die();
88 }
89
90 # option compatability mangle
91 if($opts->{connect}) {
92   $opts->{connect_info} = delete $opts->{connect};
93 }
94
95 my $admin = DBIx::Class::Admin->new( %$opts );
96
97
98 my $action = $opts->{action};
99
100 $action = $opts->{op} if ($action eq 'op');
101
102 print "Performig action $action...\n";
103
104 my $res = $admin->$action();
105 if ($action eq 'select') {
106
107   my $format = $opts->{format} || 'tsv';
108   die('Invalid format') if ($format!~/^tsv|csv$/s);
109
110   require Text::CSV;
111
112   my $csv = Text::CSV->new({
113     sep_char => ( $format eq 'tsv' ? "\t" : ',' ),
114   });
115
116   foreach my $row (@$res) {
117     $csv->combine( @$row );
118     print $csv->string()."\n";
119   }
120 }
121
122
123 __END__
124
125 # auto_pod_begin
126 #
127 # This will be replaced by the actual pod when selfinject-pod is invoked
128 #
129 # auto_pod_end
130
131 # vim: et ft=perl