From: Brandon Black Date: Fri, 30 Mar 2007 22:17:33 +0000 (+0000) Subject: added rescan method to pick up newly created tables at runtime X-Git-Tag: 0.03999_01~11 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=b97c2c1e790cc4c18370238fd98eddac4e7de2d8;p=dbsrgits%2FDBIx-Class-Schema-Loader.git added rescan method to pick up newly created tables at runtime --- diff --git a/TODO b/TODO index 00330a2..ab176b0 100644 --- a/TODO +++ b/TODO @@ -1,11 +1,4 @@ -immediate stuff for 0.04: --------------------------- - -avinash240 wants a rescan method to pick up new tables at runtime - -------- - support multiple/all schemas, instead of just one support pk/uk/fk info on views, possibly. May or may not be a sane thing to try to do. diff --git a/lib/DBIx/Class/Schema/Loader.pm b/lib/DBIx/Class/Schema/Loader.pm index bbc8fd3..47b666b 100644 --- a/lib/DBIx/Class/Schema/Loader.pm +++ b/lib/DBIx/Class/Schema/Loader.pm @@ -17,6 +17,7 @@ our $VERSION = '0.03999_01'; __PACKAGE__->mk_classaccessor('dump_to_dir'); __PACKAGE__->mk_classaccessor('_loader_args' => {}); __PACKAGE__->mk_classaccessor('_loader_invoked'); +__PACKAGE__->mk_classaccessor('_loader'); =head1 NAME @@ -111,7 +112,8 @@ sub _invoke_loader { croak qq/Could not load storage_type loader "$impl": / . qq/"$UNIVERSAL::require::ERROR"/; - $impl->new(%$args)->load; + $self->_loader($impl->new(%$args)); + $self->_loader->load; $self->_loader_invoked(1); $self; @@ -284,6 +286,16 @@ sub make_schema_at { $target->connection(@$connect_info); } +=head2 rescan + +Re-scans the database for newly added tables since the initial +load, and adds them to the schema at runtime, including relationships, +etc. Does not process drops or changes. + +=cut + +sub rescan { shift->_loader->rescan } + =head1 EXAMPLE Using the example in L as a basis diff --git a/lib/DBIx/Class/Schema/Loader/Base.pm b/lib/DBIx/Class/Schema/Loader/Base.pm index 05bad6a..2413960 100644 --- a/lib/DBIx/Class/Schema/Loader/Base.pm +++ b/lib/DBIx/Class/Schema/Loader/Base.pm @@ -308,26 +308,44 @@ Does the actual schema-construction work. sub load { my $self = shift; + $self->_load_tables($self->_tables_list); +} + +=head2 rescan + +Rescan the database for newly added tables. Does +not process drops or changes. + +=cut + +sub rescan { + my $self = shift; + + my @created; + my @current = $self->_tables_list; + foreach my $table ($self->_tables_list) { + if(!exists $self->{_tables}->{$table}) { + push(@created, $table); + } + } + + $self->_load_tables(@created); +} + +sub _load_tables { + my ($self, @tables) = @_; + # First, use _tables_list with constraint and exclude # to get a list of tables to operate on my $constraint = $self->constraint; my $exclude = $self->exclude; - my @tables = sort $self->_tables_list; - if(!@tables) { - warn "No tables found in database, nothing to load"; - } - else { - @tables = grep { /$constraint/ } @tables if $constraint; - @tables = grep { ! /$exclude/ } @tables if $exclude; - - warn "All tables excluded by constraint/exclude, nothing to load" - if !@tables; - } + @tables = grep { /$constraint/ } @tables if $constraint; + @tables = grep { ! /$exclude/ } @tables if $exclude; - # Save the tables list - $self->{_tables} = \@tables; + # Save the new tables to the tables list + push(@{$self->{_tables}}, @tables); # Set up classes/monikers { @@ -592,7 +610,7 @@ names. sub tables { my $self = shift; - return @{$self->_tables}; + return keys %{$self->_tables}; } # Make a moniker from a table diff --git a/lib/DBIx/Class/Schema/Loader/RelBuilder.pm b/lib/DBIx/Class/Schema/Loader/RelBuilder.pm index 1ffb244..35a50a8 100644 --- a/lib/DBIx/Class/Schema/Loader/RelBuilder.pm +++ b/lib/DBIx/Class/Schema/Loader/RelBuilder.pm @@ -63,8 +63,8 @@ arguments, like so: { 'Some::Source::Class' => [ - { method => 'belongs_to', arguments => [ 'col1', 'AnotherTableMoniker' ], - { method => 'has_many', arguments => [ 'anothers', 'AnotherTableMoniker', 'col15' ], + { method => 'belongs_to', arguments => [ 'col1', 'Another::Source::Class' ], + { method => 'has_many', arguments => [ 'anothers', 'Yet::Another::Source::Class', 'col15' ], ], 'Another::Source::Class' => [ # ...