update Changes
[dbsrgits/DBIx-Class.git] / script / dbicadmin
CommitLineData
9274250d 1#!/usr/bin/perl
a705b175 2
a94aa524 3use strict;
4use warnings;
5
a4a02f15 6BEGIN {
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 14use DBIx::Class::Admin::Descriptive;
15#use Getopt::Long::Descriptive;
fd27648a 16use DBIx::Class::Admin;
312eef08 17require lib;
fd27648a 18
e5279977 19my $short_description = "utility for administrating DBIx::Class schemata";
82b08554 20my $synopsis_text =q|
e5279977 21 deploy a schema to a database
22 %c --schema=MyApp::Schema \
23 --connect='["dbi:SQLite:my.db", "", ""]' \
24 --deploy
25
26 update an existing record
27 %c --schema=MyApp::Schema --class=Employee \
28 --connect='["dbi:SQLite:my.db", "", ""]' \
29 --op=update --set='{ "name": "New_Employee" }'
82b08554 30|;
e5279977 31
fd27648a 32my ($opts, $usage) = describe_options(
e5279977 33 "%c: %o",
a705b175 34 (
35 ['Actions'],
36 ["action" => hidden => { one_of => [
19a59b9e 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', ],
b13aab4f 43 ['update' => 'Update data in the schema', ],
19a59b9e 44 ['delete' => 'Delete data from the schema',],
a705b175 45 ['op:s' => 'compatiblity option all of the above can be suppied as --op=<action>'],
19a59b9e 46 ['help' => 'display this help', { implies => { schema_class => '__dummy__' } } ],
7d6f6a6e 47 ['selfinject-pod' => 'hidden', { implies => { schema_class => '__dummy__' } } ],
a705b175 48 ], required=> 1 }],
d26b9726 49 ['Arguments'],
19a59b9e 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> '],
a705b175 55 ['connect:s' => 'Supply the connect info as a json string' ],
19a59b9e 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',],
a705b175 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'],
099f10d1 66 ['I:s@' => 'Same as perl\'s -I, prepended to current @INC'],
a705b175 67 )
2bbc85c9 68);
a94aa524 69
ad81fe7d 70die "please only use one of --config or --connect-info\n" if ($opts->{config} and $opts->{connect_info});
a94aa524 71
7d6f6a6e 72if($opts->{selfinject_pod}) {
a2a769ba 73
74 die "This is an internal method, do not call!!!\n"
75 unless $ENV{MAKELEVEL};
76
e5279977 77 $usage->synopsis($synopsis_text);
78 $usage->short_description($short_description);
7d6f6a6e 79 exec (
80 $^X,
81 qw/-p -0777 -i -e/,
82 (
83 's/^# auto_pod_begin.*^# auto_pod_end/'
84 . quotemeta($usage->pod)
85 . '/ms'
86 ),
87 __FILE__
88 );
e5279977 89}
90
6875b150 91# FIXME - lowercasing will eventually go away when Getopt::Long::Descriptive is fixed
4e2873b1 92if($opts->{i}) {
312eef08 93 lib->import(delete $opts->{i});
b13aab4f 94}
95
e5279977 96if($opts->{help}) {
97 $usage->die();
98}
99
fd27648a 100# option compatability mangle
101if($opts->{connect}) {
a705b175 102 $opts->{connect_info} = delete $opts->{connect};
b04e5d3e 103}
fd27648a 104my $admin = DBIx::Class::Admin->new( %$opts );
105
fd27648a 106my $action = $opts->{action};
c57f1cf7 107
108$action = $opts->{op} if ($action eq 'op');
fd27648a 109
b13aab4f 110print "Performing action $action...\n";
ad81fe7d 111
112my $res = $admin->$action();
fd27648a 113if ($action eq 'select') {
114
a705b175 115 my $format = $opts->{format} || 'tsv';
116 die('Invalid format') if ($format!~/^tsv|csv$/s);
a705b175 117
ad81fe7d 118 require Text::CSV;
119
120 my $csv = Text::CSV->new({
121 sep_char => ( $format eq 'tsv' ? "\t" : ',' ),
122 });
123
a705b175 124 foreach my $row (@$res) {
125 $csv->combine( @$row );
126 print $csv->string()."\n";
127 }
a94aa524 128}
e5279977 129
e5279977 130
7d6f6a6e 131__END__
132
133# auto_pod_begin
6f720a19 134#
7d6f6a6e 135# This will be replaced by the actual pod when selfinject-pod is invoked
6f720a19 136#
7d6f6a6e 137# auto_pod_end
e5279977 138
139# vim: et ft=perl