added rescan method to pick up newly created tables at runtime
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader / Base.pm
index 6de7d2f..2413960 100644 (file)
@@ -226,6 +226,10 @@ sub new {
     $self->{schema_class} ||= ( ref $self->{schema} || $self->{schema} );
     $self->{schema} ||= $self->{schema_class};
 
+    $self->{relbuilder} = DBIx::Class::Schema::Loader::RelBuilder->new(
+        $self->schema_class, $self->inflect_plural, $self->inflect_singular
+    ) if !$self->{skip_relationships};
+
     $self;
 }
 
@@ -304,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
     {
@@ -338,7 +360,10 @@ sub load {
 
     $self->_setup_src_meta($_) for @tables;
 
-    $self->_load_relationships if ! $self->skip_relationships;
+    if(!$self->skip_relationships) {
+        $self->_load_relationships($_) for @tables;
+    }
+
     $self->_load_external($_)
         for ($self->schema_class, values %{$self->classes});
 
@@ -585,7 +610,7 @@ names.
 sub tables {
     my $self = shift;
 
-    return @{$self->_tables};
+    return keys %{$self->_tables};
 }
 
 # Make a moniker from a table
@@ -607,27 +632,17 @@ sub _table2moniker {
 }
 
 sub _load_relationships {
-    my $self = shift;
+    my ($self, $table) = @_;
 
-    # Construct the fk_info RelBuilder wants to see, by
-    # translating table names to monikers in the _fk_info output
-    my %fk_info;
-    foreach my $table ($self->tables) {
-        my $tbl_fk_info = $self->_table_fk_info($table);
-        foreach my $fkdef (@$tbl_fk_info) {
-            $fkdef->{remote_source} =
-                $self->monikers->{delete $fkdef->{remote_table}};
-        }
-        my $moniker = $self->monikers->{$table};
-        $fk_info{$moniker} = $tbl_fk_info;
+    my $tbl_fk_info = $self->_table_fk_info($table);
+    foreach my $fkdef (@$tbl_fk_info) {
+        $fkdef->{remote_source} =
+            $self->monikers->{delete $fkdef->{remote_table}};
     }
 
-    my $relbuilder = DBIx::Class::Schema::Loader::RelBuilder->new(
-        $self->schema_class, \%fk_info, $self->inflect_plural,
-        $self->inflect_singular
-    );
+    my $local_moniker = $self->monikers->{$table};
+    my $rel_stmts = $self->{relbuilder}->generate_code($local_moniker, $tbl_fk_info);
 
-    my $rel_stmts = $relbuilder->generate_code;
     foreach my $src_class (sort keys %$rel_stmts) {
         my $src_stmts = $rel_stmts->{$src_class};
         foreach my $stmt (@$src_stmts) {