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