X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=script%2Fdbicdump;h=c9e4da6674e5904001730aa925f578645afe67b9;hb=b87ab3912a3fd6e7b3eb435d492247961a040c7f;hp=628aa3ef918922806a42b0ed10520e14cb1b669e;hpb=667f1a0b6967917848f772066253dc4404fa9d32;p=dbsrgits%2FDBIx-Class-Schema-Loader.git diff --git a/script/dbicdump b/script/dbicdump index 628aa3e..c9e4da6 100644 --- a/script/dbicdump +++ b/script/dbicdump @@ -1,24 +1,70 @@ #!/usr/bin/perl +=encoding UTF-8 + =head1 NAME dbicdump - Dump a schema using DBIx::Class::Schema::Loader =head1 SYNOPSIS - dbicdump [-o = ] + dbicdump + dbicdump [-I ] [-o = ] \ + Examples: + $ dbicdump schema.conf + $ dbicdump -o dump_directory=./lib \ -o components='["InflateColumn::DateTime"]' \ - MyApp::Schema dbi:SQLite:./foo.db '{ quote_char => "\"" }' + MyApp::Schema dbi:SQLite:./foo.db $ dbicdump -o dump_directory=./lib \ -o components='["InflateColumn::DateTime"]' \ + MyApp::Schema dbi:SQLite:./foo.db '{ quote_char => "\"" }' + + $ dbicdump -Ilib -o dump_directory=./lib \ + -o components='["InflateColumn::DateTime"]' \ -o preserve_case=1 \ MyApp::Schema dbi:mysql:database=foo user pass '{ quote_char => "`" }' + $ dbicdump -o dump_directory=./lib \ + -o components='["InflateColumn::DateTime"]' \ + MyApp::Schema 'dbi:mysql:database=foo;host=domain.tld;port=3306' user pass + +On Windows that would be: + + $ dbicdump -o dump_directory=.\lib ^ + -o components="[q{InflateColumn::DateTime}]" ^ + -o preserve_case=1 ^ + MyApp::Schema dbi:mysql:database=foo user pass "{ quote_char => q{`} }" + +Configuration files must have schema_class and connect_info sections, +an example of a general config file is as follows: + + schema_class MyApp::Schema + + lib /extra/perl/libs + + # connection string + + dsn dbi:mysql:example + user root + pass secret + + + # dbic loader options + + dump_directory ./lib + components InflateColumn::DateTime + components TimeStamp + + +Using a config file requires L installed. + +The optional C key is equivalent to the C<-I> option. + =head1 DESCRIPTION Dbicdump generates a L schema using @@ -36,13 +82,9 @@ specified. L, L. -=head1 AUTHOR - -Dagfinn Ilmari Mannsåker C<< >> - -=head1 CONTRIBUTORS +=head1 AUTHORS -Caelum: Rafael Kitover +See L. =head1 LICENSE @@ -54,32 +96,85 @@ under the same terms as Perl itself. use strict; use warnings; use Getopt::Long; - use Pod::Usage; - -use DBIx::Class::Schema::Loader qw/ make_schema_at /; -require DBIx::Class::Schema::Loader::Base; +use DBIx::Class::Schema::Loader 'make_schema_at'; +use namespace::clean; +use DBIx::Class::Schema::Loader::Base (); +use DBIx::Class::Schema::Loader::Optional::Dependencies (); +require lib; my $loader_options; -GetOptions( 'loader-option|o=s%' => \&handle_option ); -$loader_options->{dump_directory} ||= '.'; - -my ($schema_class, @loader_connect_info) = @ARGV - or pod2usage(1); - -my $dsn = shift @loader_connect_info; +Getopt::Long::Configure('gnu_getopt'); -my ($user, $pass) = $dsn =~ /sqlite/i ? ('', '') - : splice @loader_connect_info, 0, 2; +GetOptions( + 'I=s' => sub { shift; lib->import(shift) }, + 'loader-option|o=s%' => \&handle_option, +); -my @extra_connect_info_opts = map parse_value($_), @loader_connect_info; +$loader_options->{dump_directory} ||= '.'; -make_schema_at( - $schema_class, - $loader_options, - [ $dsn, $user, $pass, @extra_connect_info_opts ], -); +if (@ARGV == 1) { + if (not DBIx::Class::Schema::Loader::Optional::Dependencies->req_ok_for('dbicdump_config')) { + die sprintf "You must install the following CPAN modules to use a config file with dbicdump: %s.\n", + DBIx::Class::Schema::Loader::Optional::Dependencies->req_missing_for('dbicdump_config'); + } + + my $configuration_file = shift @ARGV; + + my $configurations = + Config::Any->load_files( { + use_ext => 1, + flatten_to_hash => 1, + files => [$configuration_file] } ); + + my $c = (values %$configurations)[0]; + + unless (keys %{$c->{connect_info}} && $c->{schema_class}) { + pod2usage(1); + } + + my @libs; + + if ($c->{lib}) { + if (ref $c->{lib}) { + @libs = @{ $c->{lib} }; + } + + @libs = ($c->{lib}); + } + + lib->import($_) for @libs; + + my ($dsn, $user, $pass, $options) = + map { $c->{connect_info}->{$_} } qw/dsn user pass options/; + $options ||= {}; + $c->{loader_options}->{dump_directory} ||= + $loader_options->{dump_directory}; + + make_schema_at( + $c->{schema_class}, + $c->{loader_options} || {}, + [ $dsn, $user, $pass, %{$options} ], + ); +} +else { + my ($schema_class, @loader_connect_info) = @ARGV + or pod2usage(1); + + my $dsn = shift @loader_connect_info; + + my ($user, $pass) = $dsn =~ /sqlite/i ? ('', '') + : splice @loader_connect_info, 0, 2; + + my @extra_connect_info_opts = map parse_value($_), @loader_connect_info; + + make_schema_at( + $schema_class, + $loader_options, + [ $dsn, $user, $pass, @extra_connect_info_opts ], + ); +} exit 0;