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