Commit | Line | Data |
9274250d |
1 | #!/usr/bin/perl |
a705b175 |
2 | |
a94aa524 |
3 | use strict; |
4 | use warnings; |
5 | |
a4a02f15 |
6 | BEGIN { |
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 |
14 | use DBIx::Class::Admin::Descriptive; |
15 | #use Getopt::Long::Descriptive; |
fd27648a |
16 | use DBIx::Class::Admin; |
17 | |
e5279977 |
18 | my $short_description = "utility for administrating DBIx::Class schemata"; |
82b08554 |
19 | my $synopsis_text =q| |
e5279977 |
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" }' |
82b08554 |
29 | |; |
e5279977 |
30 | |
fd27648a |
31 | my ($opts, $usage) = describe_options( |
e5279977 |
32 | "%c: %o", |
a705b175 |
33 | ( |
34 | ['Actions'], |
35 | ["action" => hidden => { one_of => [ |
19a59b9e |
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',], |
a705b175 |
44 | ['op:s' => 'compatiblity option all of the above can be suppied as --op=<action>'], |
19a59b9e |
45 | ['help' => 'display this help', { implies => { schema_class => '__dummy__' } } ], |
7d6f6a6e |
46 | ['selfinject-pod' => 'hidden', { implies => { schema_class => '__dummy__' } } ], |
a705b175 |
47 | ], required=> 1 }], |
d26b9726 |
48 | ['Arguments'], |
19a59b9e |
49 | ['schema-class:s' => 'The class of the schema to load', { required => 1 } ], |
50 | ['resultset|resultset-class|class:s' => 'The resultset to operate on for data manipulation' ], |
51 | ['config-stanza:s' => 'Where in the config to find the connection_info, supply in form MyApp::Model::DB',], |
52 | ['config:s' => 'Supply the config file for parsing by Config::Any', { depends => 'config_stanza'} ], |
53 | ['connect-info:s%' => 'Supply the connect info as additonal options ie -I dsn=<dsn> user=<user> password=<pass> '], |
a705b175 |
54 | ['connect:s' => 'Supply the connect info as a json string' ], |
19a59b9e |
55 | ['sql-dir:s' => 'The directory where sql diffs will be created'], |
56 | ['sql-type:s' => 'The RDBMs flavour you wish to use'], |
57 | ['version:i' => 'Supply a version install'], |
58 | ['preversion:s' => 'The previous version to diff against',], |
a705b175 |
59 | ['set:s' => 'JSON data used to perform data operations' ], |
a705b175 |
60 | ['attrs:s' => 'JSON string to be used for the second argument for search'], |
61 | ['where:s' => 'JSON string to be used for the where clause of search'], |
62 | ['force' => 'Be forceful with some operations'], |
63 | ['trace' => 'Turn on DBIx::Class trace output'], |
a705b175 |
64 | ['quiet' => 'Be less verbose'], |
65 | ) |
2bbc85c9 |
66 | ); |
a94aa524 |
67 | |
ad81fe7d |
68 | die "please only use one of --config or --connect-info\n" if ($opts->{config} and $opts->{connect_info}); |
a94aa524 |
69 | |
7d6f6a6e |
70 | if($opts->{selfinject_pod}) { |
a2a769ba |
71 | |
72 | die "This is an internal method, do not call!!!\n" |
73 | unless $ENV{MAKELEVEL}; |
74 | |
e5279977 |
75 | $usage->synopsis($synopsis_text); |
76 | $usage->short_description($short_description); |
7d6f6a6e |
77 | exec ( |
78 | $^X, |
79 | qw/-p -0777 -i -e/, |
80 | ( |
81 | 's/^# auto_pod_begin.*^# auto_pod_end/' |
82 | . quotemeta($usage->pod) |
83 | . '/ms' |
84 | ), |
85 | __FILE__ |
86 | ); |
e5279977 |
87 | } |
88 | |
89 | if($opts->{help}) { |
90 | $usage->die(); |
91 | } |
92 | |
fd27648a |
93 | # option compatability mangle |
94 | if($opts->{connect}) { |
a705b175 |
95 | $opts->{connect_info} = delete $opts->{connect}; |
b04e5d3e |
96 | } |
a94aa524 |
97 | |
fd27648a |
98 | my $admin = DBIx::Class::Admin->new( %$opts ); |
99 | |
100 | |
101 | my $action = $opts->{action}; |
c57f1cf7 |
102 | |
103 | $action = $opts->{op} if ($action eq 'op'); |
fd27648a |
104 | |
ad81fe7d |
105 | print "Performig action $action...\n"; |
106 | |
107 | my $res = $admin->$action(); |
fd27648a |
108 | if ($action eq 'select') { |
109 | |
a705b175 |
110 | my $format = $opts->{format} || 'tsv'; |
111 | die('Invalid format') if ($format!~/^tsv|csv$/s); |
a705b175 |
112 | |
ad81fe7d |
113 | require Text::CSV; |
114 | |
115 | my $csv = Text::CSV->new({ |
116 | sep_char => ( $format eq 'tsv' ? "\t" : ',' ), |
117 | }); |
118 | |
a705b175 |
119 | foreach my $row (@$res) { |
120 | $csv->combine( @$row ); |
121 | print $csv->string()."\n"; |
122 | } |
a94aa524 |
123 | } |
e5279977 |
124 | |
e5279977 |
125 | |
7d6f6a6e |
126 | __END__ |
127 | |
128 | # auto_pod_begin |
6f720a19 |
129 | # |
7d6f6a6e |
130 | # This will be replaced by the actual pod when selfinject-pod is invoked |
6f720a19 |
131 | # |
7d6f6a6e |
132 | # auto_pod_end |
e5279977 |
133 | |
134 | # vim: et ft=perl |