DBIx-Class-Schema-Loader
view release on metacpan or search on metacpan
lib/DBIx/Class/Schema/Loader.pm view on Meta::CPAN
=head2 dump_to_dir
=over 4
=item Argument: $directory
=back
Calling this as a class method on either L<DBIx::Class::Schema::Loader>
or any derived schema class will cause all schemas to dump
manual versions of themselves to the named directory when they are
loaded. In order to be effective, this must be set before defining a
connection on this schema class or any derived object (as the loading
happens as soon as both a connection and loader_options are set, and
only once per class).
See L<DBIx::Class::Schema::Loader::Base/dump_directory> for more
details on the dumping mechanism.
This can also be set at module import time via the import option
C<dump_to_dir:/foo/bar> to L<DBIx::Class::Schema::Loader>, where
C</foo/bar> is the target directory.
Examples:
# My::Schema isa DBIx::Class::Schema::Loader, and has connection info
# hardcoded in the class itself:
perl -MDBIx::Class::Schema::Loader=dump_to_dir:/foo/bar -MMy::Schema -e1
# Same, but no hard-coded connection, so we must provide one:
perl -MDBIx::Class::Schema::Loader=dump_to_dir:/foo/bar -MMy::Schema -e 'My::Schema->connection("dbi:Pg:dbname=foo", ...)'
# Or as a class method, as long as you get it done *before* defining a
# connection on this schema class or any derived object:
use My::Schema;
My::Schema->dump_to_dir('/foo/bar');
My::Schema->connection(........);
# Or as a class method on the DBIx::Class::Schema::Loader itself, which affects all
# derived schemas
use My::Schema;
use My::OtherSchema;
DBIx::Class::Schema::Loader->dump_to_dir('/foo/bar');
My::Schema->connection(.......);
My::OtherSchema->connection(.......);
# Another alternative to the above:
use DBIx::Class::Schema::Loader qw| dump_to_dir:/foo/bar |;
use My::Schema;
use My::OtherSchema;
My::Schema->connection(.......);
My::OtherSchema->connection(.......);
=cut
sub import {
my $self = shift;
return if !@_;
my $cpkg = (caller)[0];
foreach my $opt (@_) {
if($opt =~ m{^dump_to_dir:(.*)$}) {
$self->dump_to_dir($1)
}
elsif($opt eq 'make_schema_at') {
no strict 'refs';
*{"${cpkg}::make_schema_at"} = \&make_schema_at;
}
elsif($opt eq 'naming') {
no strict 'refs';
*{"${cpkg}::naming"} = $self->curry::naming;
}
elsif($opt eq 'use_namespaces') {
no strict 'refs';
*{"${cpkg}::use_namespaces"} = $self->curry::use_namespaces,
}
}
}
=head2 make_schema_at
=over 4
=item Arguments: $schema_class_name, \%loader_options, \@connect_info
=item Return Value: $schema_class_name
=back
This function creates a DBIx::Class schema from an existing RDBMS
schema. With the C<dump_directory> option, generates a set of
DBIx::Class classes from an existing database schema read from the
given dsn. Without a C<dump_directory>, creates schema classes in
memory at runtime without generating on-disk class files.
For a complete list of supported loader_options, see
L<DBIx::Class::Schema::Loader::Base>
The last hashref in the C<\@connect_info> can specify the L</loader_class>.
This function can be imported in the usual way, as illustrated in
these Examples:
# Simple example, creates as a new class 'New::Schema::Name' in
# memory in the running perl interpreter.
use DBIx::Class::Schema::Loader qw/ make_schema_at /;
make_schema_at(
'New::Schema::Name',
{ debug => 1 },
[ 'dbi:Pg:dbname="foo"','postgres','',
{ loader_class => 'MyLoader' } # optionally
],
);
# Inside a script, specifying a dump directory in which to write
# class files
use DBIx::Class::Schema::Loader qw/ make_schema_at /;
make_schema_at(
'New::Schema::Name',
( run in 0.477 second using v1.01-cache-2.11-cpan-63c85eba8c4 )