Add moritz to CONTRIBUTORS
[dbsrgits/DBIx-Class-Schema-Loader.git] / script / dbicdump
CommitLineData
ff746964 1#!/usr/bin/perl
2
a0f1a565 3=encoding UTF-8
4
ff746964 5=head1 NAME
6
7dbicdump - Dump a schema using DBIx::Class::Schema::Loader
8
9=head1 SYNOPSIS
10
07f39b47 11 dbicdump <configuration_file>
112415f1 12 dbicdump [-I <lib-path>] [-o <loader_option>=<value> ] \
13 <schema_class> <connect_info>
ff746964 14
667f1a0b 15Examples:
16
07f39b47 17 $ dbicdump schema.conf
18
667f1a0b 19 $ dbicdump -o dump_directory=./lib \
20 -o components='["InflateColumn::DateTime"]' \
c4a69b87 21 MyApp::Schema dbi:SQLite:./foo.db
22
23 $ dbicdump -o dump_directory=./lib \
24 -o components='["InflateColumn::DateTime"]' \
667f1a0b 25 MyApp::Schema dbi:SQLite:./foo.db '{ quote_char => "\"" }'
26
112415f1 27 $ dbicdump -Ilib -o dump_directory=./lib \
667f1a0b 28 -o components='["InflateColumn::DateTime"]' \
29 -o preserve_case=1 \
30 MyApp::Schema dbi:mysql:database=foo user pass '{ quote_char => "`" }'
31
eb40e254 32 $ dbicdump -o dump_directory=./lib \
33 -o components='["InflateColumn::DateTime"]' \
34 MyApp::Schema 'dbi:mysql:database=foo;host=domain.tld;port=3306' user pass
35
93acebe1 36On Windows that would be:
37
38 $ dbicdump -o dump_directory=.\lib ^
39 -o components="[q{InflateColumn::DateTime}]" ^
40 -o preserve_case=1 ^
41 MyApp::Schema dbi:mysql:database=foo user pass "{ quote_char => q{`} }"
07f39b47 42
0322c5b3 43Configuration files must have schema_class and connect_info sections,
07f39b47 44an example of a general config file is as follows:
45
46 schema_class MyApp::Schema
112415f1 47
48 lib /extra/perl/libs
07f39b47 49
50 # connection string
51 <connect_info>
52 dsn dbi:mysql:example
53 user root
54 pass secret
55 </connect_info>
56
57 # dbic loader options
58 <loader_options>
e946f659 59 dump_directory ./lib
60 components InflateColumn::DateTime
61 components TimeStamp
07f39b47 62 </loader_options>
93acebe1 63
0322c5b3 64Using a config file requires L<Config::Any> installed.
65
112415f1 66The optional C<lib> key is equivalent to the C<-I> option.
67
ff746964 68=head1 DESCRIPTION
69
70Dbicdump generates a L<DBIx::Class> schema using
71L<DBIx::Class::Schema::Loader/make_schema_at> and dumps it to disk.
72
e83cb149 73You can pass any L<DBIx::Class::Schema::Loader::Base> constructor option using
ff746964 74C<< -o <option>=<value> >>. For convenience, option names will have C<->
75replaced with C<_> and values that look like references or quote-like
76operators will be C<eval>-ed before being passed to the constructor.
77
78The C<dump_directory> option defaults to the current directory if not
79specified.
80
81=head1 SEE ALSO
82
83L<DBIx::Class::Schema::Loader>, L<DBIx::Class>.
84
85=head1 AUTHOR
86
a0f1a565 87Dagfinn Ilmari Mannsåker C<< <ilmari@ilmari.org> >>
ff746964 88
667f1a0b 89=head1 CONTRIBUTORS
90
91Caelum: Rafael Kitover <rkitover@cpan.org>
92
112415f1 93alnewkirk: Al Newkirk <awncorp@cpan.org>
94
ff746964 95=head1 LICENSE
96
97This program is free software; you can redistribute it and/or modify it
98under the same terms as Perl itself.
99
100=cut
101
102use strict;
103use warnings;
104use Getopt::Long;
707fb247 105use Pod::Usage;
0322c5b3 106use DBIx::Class::Schema::Loader 'make_schema_at';
112415f1 107use namespace::clean;
0322c5b3 108use DBIx::Class::Schema::Loader::Base ();
109use DBIx::Class::Schema::Loader::Optional::Dependencies ();
112415f1 110require lib;
ff746964 111
112my $loader_options;
113
112415f1 114Getopt::Long::Configure('gnu_getopt');
115
116GetOptions(
117 'I=s' => sub { shift; lib->import(shift) },
118 'loader-option|o=s%' => \&handle_option,
119);
120
ff746964 121$loader_options->{dump_directory} ||= '.';
122
07f39b47 123if (@ARGV == 1) {
0322c5b3 124 if (not DBIx::Class::Schema::Loader::Optional::Dependencies->req_ok_for('dbicdump_config')) {
125 die sprintf "You must install the following CPAN modules to use a config file with dbicdump: %s.\n",
126 DBIx::Class::Schema::Loader::Optional::Dependencies->req_missing_for('dbicdump_config');
127 }
128
07f39b47 129 my $configuration_file = shift @ARGV;
0322c5b3 130
07f39b47 131 my $configurations =
132 Config::Any->load_files( {
133 use_ext => 1,
134 flatten_to_hash => 1,
135 files => [$configuration_file] } );
136
137 my $c = (values %$configurations)[0];
138
139 unless (keys %{$c->{connect_info}} && $c->{schema_class}) {
140 pod2usage(1);
141 }
112415f1 142
143 my @libs;
144
145 if ($c->{lib}) {
146 if (ref $c->{lib}) {
147 @libs = @{ $c->{lib} };
148 }
149
150 @libs = ($c->{lib});
151 }
152
153 lib->import($_) for @libs;
07f39b47 154
155 my ($dsn, $user, $pass, $options) =
156 map { $c->{connect_info}->{$_} } qw/dsn user pass options/;
157 $options ||= {};
158 $c->{loader_options}->{dump_directory} ||=
159 $loader_options->{dump_directory};
160
161 make_schema_at(
162 $c->{schema_class},
163 $c->{loader_options} || {},
164 [ $dsn, $user, $pass, %{$options} ],
165 );
166}
167else {
168 my ($schema_class, @loader_connect_info) = @ARGV
169 or pod2usage(1);
170
171 my $dsn = shift @loader_connect_info;
172
173 my ($user, $pass) = $dsn =~ /sqlite/i ? ('', '')
174 : splice @loader_connect_info, 0, 2;
175
176 my @extra_connect_info_opts = map parse_value($_), @loader_connect_info;
177
178 make_schema_at(
179 $schema_class,
180 $loader_options,
181 [ $dsn, $user, $pass, @extra_connect_info_opts ],
182 );
183}
667f1a0b 184
185exit 0;
186
187sub parse_value {
188 my $value = shift;
189
190 $value = eval $value if $value =~ /^\s*(?:sub\s*\{|q\w?\s*[^\w\s]|[[{])/;
191
192 return $value;
193}
194
ff746964 195sub handle_option {
196 my ($self, $key, $value) = @_;
197
198 $key =~ tr/-/_/;
199 die "Unknown option: $key\n"
200 unless DBIx::Class::Schema::Loader::Base->can($key);
201
667f1a0b 202 $value = parse_value $value;
ff746964 203
204 $loader_options->{$key} = $value;
205}
206
667f1a0b 2071;
208
209__END__