added rescan method to pick up newly created tables at runtime
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader.pm
CommitLineData
18fca96a 1package DBIx::Class::Schema::Loader;
a78e3fed 2
3use strict;
a4a19f3c 4use warnings;
8a6b44ef 5use base qw/DBIx::Class::Schema/;
6use base qw/Class::Data::Accessor/;
fa994d3c 7use Carp::Clan qw/^DBIx::Class/;
457eb8a6 8use UNIVERSAL::require;
996be9ee 9use Class::C3;
c9f1d7b0 10use Scalar::Util qw/ weaken /;
3980d69c 11
a4a19f3c 12# Always remember to do all digits for the version even if they're 0
13# i.e. first release of 0.XX *must* be 0.XX000. This avoids fBSD ports
14# brain damage and presumably various other packaging systems too
32f784fc 15our $VERSION = '0.03999_01';
457eb8a6 16
996be9ee 17__PACKAGE__->mk_classaccessor('dump_to_dir');
59cfa251 18__PACKAGE__->mk_classaccessor('_loader_args' => {});
19__PACKAGE__->mk_classaccessor('_loader_invoked');
b97c2c1e 20__PACKAGE__->mk_classaccessor('_loader');
a78e3fed 21
22=head1 NAME
23
18fca96a 24DBIx::Class::Schema::Loader - Dynamic definition of a DBIx::Class::Schema
a78e3fed 25
26=head1 SYNOPSIS
27
a4a19f3c 28 package My::Schema;
29 use base qw/DBIx::Class::Schema::Loader/;
a78e3fed 30
996be9ee 31 __PACKAGE__->loader_options(
996be9ee 32 constraint => '^foo.*',
33 # debug => 1,
a78e3fed 34 );
af6c2665 35
a4a19f3c 36 # in seperate application code ...
a78e3fed 37
a4a19f3c 38 use My::Schema;
a78e3fed 39
a4a19f3c 40 my $schema1 = My::Schema->connect( $dsn, $user, $password, $attrs);
41 # -or-
996be9ee 42 my $schema1 = "My::Schema"; $schema1->connection(as above);
074e81cd 43
996be9ee 44=head1 DESCRIPTION
074e81cd 45
fbd83464 46DBIx::Class::Schema::Loader automates the definition of a
996be9ee 47L<DBIx::Class::Schema> by scanning database table definitions and
d65cda9e 48setting up the columns, primary keys, and relationships.
a78e3fed 49
d65cda9e 50DBIx::Class::Schema::Loader currently supports only the DBI storage type.
51It has explicit support for L<DBD::Pg>, L<DBD::mysql>, L<DBD::DB2>, and
52L<DBD::SQLite>. Other DBI drivers may function to a greater or lesser
53degree with this loader, depending on how much of the DBI spec they
54implement, and how standard their implementation is. Patches to make
55other DBDs work correctly welcome.
a78e3fed 56
996be9ee 57See L<DBIx::Class::Schema::Loader::DBI::Writing> for notes on writing
58your own vendor-specific subclass for an unsupported DBD driver.
a78e3fed 59
996be9ee 60This module requires L<DBIx::Class> 0.06 or later, and obsoletes
61the older L<DBIx::Class::Loader>.
89ecd854 62
996be9ee 63This module is designed more to get you up and running quickly against
64an existing database, or to be effective for simple situations, rather
65than to be what you use in the long term for a complex database/project.
89ecd854 66
67That being said, transitioning your code from a Schema generated by this
68module to one that doesn't use this module should be straightforward and
59cfa251 69painless, so don't shy away from it just for fears of the transition down
70the road.
89ecd854 71
a78e3fed 72=head1 METHODS
73
996be9ee 74=head2 loader_options
a78e3fed 75
996be9ee 76Example in Synopsis above demonstrates a few common arguments. For
77detailed information on all of the arguments, most of which are
78only useful in fairly complex scenarios, see the
79L<DBIx::Class::Schema::Loader::Base> documentation.
a78e3fed 80
59cfa251 81One must call C<loader_options> before any connection is made,
82or embed the C<loader_options> in the connection information itself
83as shown below. Setting C<loader_options> after the connection has
84already been made is useless.
a78e3fed 85
996be9ee 86=cut
1031d4f6 87
996be9ee 88sub loader_options {
89 my $self = shift;
90
d65cda9e 91 my %args = (ref $_[0] eq 'HASH') ? %{$_[0]} : @_;
996be9ee 92 $self->_loader_args(\%args);
996be9ee 93
94 $self;
95}
96
97sub _invoke_loader {
98 my $self = shift;
99 my $class = ref $self || $self;
100
59cfa251 101 my $args = $self->_loader_args;
102
103 # set up the schema/schema_class arguments
104 $args->{schema} = $self;
105 $args->{schema_class} = $class;
106 weaken($args->{schema}) if ref $self;
107 $args->{dump_directory} ||= $self->dump_to_dir;
af6c2665 108
996be9ee 109 # XXX this only works for relative storage_type, like ::DBI ...
110 my $impl = "DBIx::Class::Schema::Loader" . $self->storage_type;
a78e3fed 111 $impl->require or
996be9ee 112 croak qq/Could not load storage_type loader "$impl": / .
3385ac62 113 qq/"$UNIVERSAL::require::ERROR"/;
af6c2665 114
b97c2c1e 115 $self->_loader($impl->new(%$args));
116 $self->_loader->load;
59cfa251 117 $self->_loader_invoked(1);
996be9ee 118
996be9ee 119 $self;
120}
121
122=head2 connection
123
d65cda9e 124See L<DBIx::Class::Schema> for basic usage.
125
126If the final argument is a hashref, and it contains a key C<loader_options>,
127that key will be deleted, and its value will be used for the loader options,
128just as if set via the L</loader_options> method above.
129
130The actual auto-loading operation (the heart of this module) will be invoked
131as soon as the connection information is defined.
996be9ee 132
133=cut
134
135sub connection {
d65cda9e 136 my $self = shift;
137
138 if($_[-1] && ref $_[-1] eq 'HASH') {
139 if(my $loader_opts = delete $_[-1]->{loader_options}) {
140 $self->loader_options($loader_opts);
fd64d90d 141 pop @_ if !keys %{$_[-1]};
d65cda9e 142 }
143 }
144
145 $self = $self->next::method(@_);
996be9ee 146
147 my $class = ref $self || $self;
59cfa251 148 if(!$class->_loader_invoked) {
fa994d3c 149 $self->_invoke_loader
150 }
996be9ee 151
152 return $self;
153}
154
155=head2 clone
156
074e81cd 157See L<DBIx::Class::Schema>.
996be9ee 158
159=cut
160
161sub clone {
162 my $self = shift;
163
164 my $clone = $self->next::method(@_);
165
fa994d3c 166 if($clone->_loader_args) {
167 $clone->_loader_args->{schema} = $clone;
168 weaken($clone->_loader_args->{schema});
169 }
996be9ee 170
171 $clone;
172}
173
174=head2 dump_to_dir
175
176Argument: directory name.
177
178Calling this as a class method on either L<DBIx::Class::Schema::Loader>
179or any derived schema class will cause all affected schemas to dump
180manual versions of themselves to the named directory when they are
181loaded. In order to be effective, this must be set before defining a
182connection on this schema class or any derived object (as the loading
074e81cd 183happens as soon as both a connection and loader_options are set, and
184only once per class).
996be9ee 185
186See L<DBIx::Class::Schema::Loader::Base/dump_directory> for more
187details on the dumping mechanism.
188
189This can also be set at module import time via the import option
190C<dump_to_dir:/foo/bar> to L<DBIx::Class::Schema::Loader>, where
191C</foo/bar> is the target directory.
192
193Examples:
194
195 # My::Schema isa DBIx::Class::Schema::Loader, and has connection info
196 # hardcoded in the class itself:
197 perl -MDBIx::Class::Schema::Loader=dump_to_dir:/foo/bar -MMy::Schema -e1
198
199 # Same, but no hard-coded connection, so we must provide one:
200 perl -MDBIx::Class::Schema::Loader=dump_to_dir:/foo/bar -MMy::Schema -e 'My::Schema->connection("dbi:Pg:dbname=foo", ...)'
201
202 # Or as a class method, as long as you get it done *before* defining a
203 # connection on this schema class or any derived object:
204 use My::Schema;
205 My::Schema->dump_to_dir('/foo/bar');
206 My::Schema->connection(........);
207
208 # Or as a class method on the DBIx::Class::Schema::Loader itself, which affects all
209 # derived schemas
210 use My::Schema;
211 use My::OtherSchema;
212 DBIx::Class::Schema::Loader->dump_to_dir('/foo/bar');
213 My::Schema->connection(.......);
214 My::OtherSchema->connection(.......);
215
216 # Another alternative to the above:
217 use DBIx::Class::Schema::Loader qw| dump_to_dir:/foo/bar |;
218 use My::Schema;
219 use My::OtherSchema;
220 My::Schema->connection(.......);
221 My::OtherSchema->connection(.......);
222
223=cut
224
225sub import {
226 my $self = shift;
227 return if !@_;
228 foreach my $opt (@_) {
229 if($opt =~ m{^dump_to_dir:(.*)$}) {
230 $self->dump_to_dir($1)
231 }
232 elsif($opt eq 'make_schema_at') {
233 no strict 'refs';
234 my $cpkg = (caller)[0];
235 *{"${cpkg}::make_schema_at"} = \&make_schema_at;
236 }
237 }
238}
239
240=head2 make_schema_at
241
242This simple function allows one to create a Loader-based schema
243in-memory on the fly without any on-disk class files of any
244kind. When used with the C<dump_directory> option, you can
8f9d7ce5 245use this to generate a rough draft manual schema from a dsn
996be9ee 246without the intermediate step of creating a physical Loader-based
247schema class.
248
483987b9 249The return value is the input class name.
250
996be9ee 251This function can be exported/imported by the normal means, as
252illustrated in these Examples:
253
5223f24a 254 # Simple example, creates as a new class 'New::Schema::Name' in
255 # memory in the running perl interpreter.
996be9ee 256 use DBIx::Class::Schema::Loader qw/ make_schema_at /;
257 make_schema_at(
258 'New::Schema::Name',
59cfa251 259 { debug => 1 },
996be9ee 260 [ 'dbi:Pg:dbname="foo"','postgres' ],
261 );
262
263 # Complex: dump loaded schema to disk, all from the commandline:
59cfa251 264 perl -MDBIx::Class::Schema::Loader=make_schema_at,dump_to_dir:./lib -e 'make_schema_at("New::Schema::Name", { debug => 1 }, [ "dbi:Pg:dbname=foo","postgres" ])'
996be9ee 265
266 # Same, but inside a script, and using a different way to specify the
267 # dump directory:
268 use DBIx::Class::Schema::Loader qw/ make_schema_at /;
269 make_schema_at(
270 'New::Schema::Name',
59cfa251 271 { debug => 1, dump_directory => './lib' },
996be9ee 272 [ 'dbi:Pg:dbname="foo"','postgres' ],
273 );
274
275=cut
276
277sub make_schema_at {
278 my ($target, $opts, $connect_info) = @_;
279
483987b9 280 {
281 no strict 'refs';
282 @{$target . '::ISA'} = qw/DBIx::Class::Schema::Loader/;
283 }
284
285 $target->loader_options($opts);
286 $target->connection(@$connect_info);
996be9ee 287}
288
b97c2c1e 289=head2 rescan
290
291Re-scans the database for newly added tables since the initial
292load, and adds them to the schema at runtime, including relationships,
293etc. Does not process drops or changes.
294
295=cut
296
297sub rescan { shift->_loader->rescan }
298
996be9ee 299=head1 EXAMPLE
300
301Using the example in L<DBIx::Class::Manual::ExampleSchema> as a basis
302replace the DB::Main with the following code:
303
304 package DB::Main;
305
306 use base qw/DBIx::Class::Schema::Loader/;
307
308 __PACKAGE__->loader_options(
996be9ee 309 debug => 1,
310 );
311 __PACKAGE__->connection('dbi:SQLite:example.db');
312
313 1;
314
315and remove the Main directory tree (optional). Every thing else
316should work the same
317
996be9ee 318=head1 KNOWN ISSUES
319
320=head2 Multiple Database Schemas
321
322Currently the loader is limited to working within a single schema
323(using the database vendors' definition of "schema"). If you
324have a multi-schema database with inter-schema relationships (which
8f9d7ce5 325is easy to do in PostgreSQL or DB2 for instance), you only get to
996be9ee 326automatically load the tables of one schema, and any relationships
327to tables in other schemas will be silently ignored.
328
329At some point in the future, an intelligent way around this might be
330devised, probably by allowing the C<db_schema> option to be an
d65cda9e 331arrayref of schemas to load.
89ecd854 332
996be9ee 333In "normal" L<DBIx::Class::Schema> usage, manually-defined
334source classes and relationships have no problems crossing vendor schemas.
89ecd854 335
a78e3fed 336=head1 AUTHOR
337
f654c972 338Brandon Black, C<blblack@gmail.com>
fbd83464 339
8a6b44ef 340Based on L<DBIx::Class::Loader> by Sebastian Riedel
a78e3fed 341
342Based upon the work of IKEBE Tomohiro
343
344=head1 THANK YOU
345
d65cda9e 346Matt S Trout, all of the #dbix-class folks, and everyone who's ever sent
347in a bug report or suggestion.
a78e3fed 348
349=head1 LICENSE
350
351This library is free software; you can redistribute it and/or modify it under
352the same terms as Perl itself.
353
354=head1 SEE ALSO
355
996be9ee 356L<DBIx::Class>, L<DBIx::Class::Manual::ExampleSchema>
a78e3fed 357
358=cut
359
3601;