[Bast-commits] r3161 - in branches/DBIx-Class-Schema-Loader/current: . lib/DBIx/Class/Schema lib/DBIx/Class/Schema/Loader

blblack at dev.catalyst.perl.org blblack at dev.catalyst.perl.org
Fri Mar 30 23:17:37 GMT 2007


Author: blblack
Date: 2007-03-30 23:17:33 +0100 (Fri, 30 Mar 2007)
New Revision: 3161

Modified:
   branches/DBIx-Class-Schema-Loader/current/TODO
   branches/DBIx-Class-Schema-Loader/current/lib/DBIx/Class/Schema/Loader.pm
   branches/DBIx-Class-Schema-Loader/current/lib/DBIx/Class/Schema/Loader/Base.pm
   branches/DBIx-Class-Schema-Loader/current/lib/DBIx/Class/Schema/Loader/RelBuilder.pm
Log:
added rescan method to pick up newly created tables at runtime

Modified: branches/DBIx-Class-Schema-Loader/current/TODO
===================================================================
--- branches/DBIx-Class-Schema-Loader/current/TODO	2007-03-30 21:44:30 UTC (rev 3160)
+++ branches/DBIx-Class-Schema-Loader/current/TODO	2007-03-30 22:17:33 UTC (rev 3161)
@@ -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.

Modified: branches/DBIx-Class-Schema-Loader/current/lib/DBIx/Class/Schema/Loader/Base.pm
===================================================================
--- branches/DBIx-Class-Schema-Loader/current/lib/DBIx/Class/Schema/Loader/Base.pm	2007-03-30 21:44:30 UTC (rev 3160)
+++ branches/DBIx-Class-Schema-Loader/current/lib/DBIx/Class/Schema/Loader/Base.pm	2007-03-30 22:17:33 UTC (rev 3161)
@@ -308,27 +308,45 @@
 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;
+    @tables = grep { /$constraint/ } @tables if $constraint;
+    @tables = grep { ! /$exclude/ } @tables if $exclude;
 
-        warn "All tables excluded by constraint/exclude, nothing to load"
-            if !@tables;
-    }
+    # Save the new tables to the tables list
+    push(@{$self->{_tables}}, @tables);
 
-    # Save the tables list
-    $self->{_tables} = \@tables;
-
     # Set up classes/monikers
     {
         no warnings 'redefine';
@@ -592,7 +610,7 @@
 sub tables {
     my $self = shift;
 
-    return @{$self->_tables};
+    return keys %{$self->_tables};
 }
 
 # Make a moniker from a table

Modified: branches/DBIx-Class-Schema-Loader/current/lib/DBIx/Class/Schema/Loader/RelBuilder.pm
===================================================================
--- branches/DBIx-Class-Schema-Loader/current/lib/DBIx/Class/Schema/Loader/RelBuilder.pm	2007-03-30 21:44:30 UTC (rev 3160)
+++ branches/DBIx-Class-Schema-Loader/current/lib/DBIx/Class/Schema/Loader/RelBuilder.pm	2007-03-30 22:17:33 UTC (rev 3161)
@@ -63,8 +63,8 @@
 
   {
       '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' => [
           # ...

Modified: branches/DBIx-Class-Schema-Loader/current/lib/DBIx/Class/Schema/Loader.pm
===================================================================
--- branches/DBIx-Class-Schema-Loader/current/lib/DBIx/Class/Schema/Loader.pm	2007-03-30 21:44:30 UTC (rev 3160)
+++ branches/DBIx-Class-Schema-Loader/current/lib/DBIx/Class/Schema/Loader.pm	2007-03-30 22:17:33 UTC (rev 3161)
@@ -17,6 +17,7 @@
 __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 @@
       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 @@
     $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<DBIx::Class::Manual::ExampleSchema> as a basis




More information about the Bast-commits mailing list