Introduce GOVERNANCE document and empty RESOLUTIONS file.
[dbsrgits/DBIx-Class.git] / script / dbicadmin
1 #!/usr/bin/env perl
2
3 use strict;
4 use warnings;
5
6 BEGIN {
7   require DBIx::Class::Optional::Dependencies;
8   if (my $missing = DBIx::Class::Optional::Dependencies->req_missing_for ('admin_script') ) {
9     die "The following modules are required for the dbicadmin utility: $missing\n";
10   }
11 }
12
13 use DBIx::Class::Admin::Descriptive;
14 #use Getopt::Long::Descriptive;
15 use DBIx::Class::Admin;
16
17 my $short_description = "utility for administrating DBIx::Class schemata";
18 my $synopsis_text =q|
19   deploy a schema to a database
20   %c --schema=MyApp::Schema \
21     --connect='["dbi:SQLite:my.db", "", ""]' \
22     --deploy
23
24   update an existing record
25   %c --schema=MyApp::Schema --class=Employee \
26     --connect='["dbi:SQLite:my.db", "", ""]' \
27     --op=update --set='{ "name": "New_Employee" }'
28 |;
29
30 my ($opts, $usage) = describe_options(
31     "%c: %o",
32   (
33     ['Actions'],
34     ["action" => hidden => { one_of => [
35       ['create' => 'Create version diffs needs preversion'],
36       ['upgrade' => 'Upgrade the database to the current schema'],
37       ['install' => 'Install the schema version tables to an existing database'],
38       ['deploy' => 'Deploy the schema to the database'],
39       ['select'   => 'Select data from the schema'],
40       ['insert'   => 'Insert data into the schema'],
41       ['update'   => 'Update data in the schema'],
42       ['delete'   => 'Delete data from the schema'],
43       ['op:s' => 'compatibility option all of the above can be supplied as --op=<action>'],
44       ['help' => 'display this help', { implies => { schema_class => '__dummy__' } } ],
45       ['documentation-as-pod:s' => 'hidden', { implies => { schema_class => '__dummy__' } } ],
46     ], required => 1 }],
47     ['Arguments'],
48     ["configuration" => hidden => { one_of => [
49       ['config-file|config:s' => 'Supply the config file for parsing by Config::Any', { depends => 'config_stanza'} ],
50       ['connect-info:s%' => 'Supply the connect info as trailing options e.g. --connect-info dsn=<dsn> user=<user> password=<pass>' ],
51       ['connect:s' => 'Supply the connect info as a JSON-encoded structure, e.g. an --connect=["dsn","user","pass"]'],
52     ] }],
53     ['schema-class:s' => 'The class of the schema to load', { required => 1 } ],
54     ['config-stanza:s' => 'Where in the config to find the connection_info, supply in form MyApp::Model::DB',],
55     ['resultset|resultset-class|class:s' => 'The resultset to operate on for data manipulation' ],
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     ['I:s@' => 'Same as perl\'s -I, prepended to current @INC'],
67   )
68 );
69
70 if(defined (my $fn = $opts->{documentation_as_pod}) ) {
71   $usage->synopsis($synopsis_text);
72   $usage->short_description($short_description);
73
74   my $fh;
75   if ($fn) {
76     require DBIx::Class::_Util;
77     DBIx::Class::_Util::mkdir_p( DBIx::Class::_Util::parent_dir( $fn ) );
78     open( $fh, '>', $fn ) or die "Unable to open $fn: $!\n";
79   }
80   else {
81     $fh = \*STDOUT;
82   }
83
84   print $fh "\n";
85   print $fh $usage->pod;
86   print $fh "\n";
87
88   close $fh if $fn;
89   exit 0;
90 }
91
92 # FIXME - lowercasing will eventually go away when Getopt::Long::Descriptive is fixed
93 if($opts->{i}) {
94   require lib;
95   lib->import( @{delete $opts->{i}} );
96 }
97
98 if($opts->{help}) {
99   $usage->die();
100 }
101
102 # option compatibility mangle
103 # (can not be joined in the spec, one is s% the other is s)
104 if($opts->{connect}) {
105   $opts->{connect_info} = delete $opts->{connect};
106 }
107
108 my $admin = DBIx::Class::Admin->new( %$opts );
109
110 my $action = $opts->{action};
111
112 $action = $opts->{op} if ($action eq 'op');
113
114 print "Performing action $action...\n";
115
116 my $res = $admin->$action();
117 if ($action eq 'select') {
118
119   my $format = $opts->{format} || 'tsv';
120   die('Invalid format') if ($format!~/^tsv|csv$/s);
121
122   require Text::CSV;
123
124   my $csv = Text::CSV->new({
125     sep_char => ( $format eq 'tsv' ? "\t" : ',' ),
126   });
127
128   foreach my $row (@$res) {
129     $csv->combine( @$row );
130     print $csv->string()."\n";
131   }
132 }
133
134 1;
135
136 __END__