0.04002 - disable oracle.pm indexing and fix rescan return values
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader / Base.pm
index 05bad6a..a723beb 100644 (file)
@@ -14,7 +14,7 @@ use Cwd qw//;
 use Digest::MD5 qw//;
 require DBIx::Class;
 
-our $VERSION = '0.03999_01';
+our $VERSION = '0.04002';
 
 __PACKAGE__->mk_ro_accessors(qw/
                                 schema
@@ -34,6 +34,7 @@ __PACKAGE__->mk_ro_accessors(qw/
                                 debug
                                 dump_directory
                                 dump_overwrite
+                                really_erase_my_files
 
                                 db_schema
                                 _tables
@@ -162,6 +163,11 @@ recommended way to access this functionality.
 
 =head2 dump_overwrite
 
+Deprecated.  See L</really_erase_my_files> below, which does *not* mean
+the same thing as the old C<dump_overwrite> setting from previous releases.
+
+=head2 really_erase_my_files
+
 Default false.  If true, Loader will unconditionally delete any existing
 files before creating the new ones from scratch when dumping a schema to disk.
 
@@ -170,11 +176,15 @@ file, up to and including the final stanza which contains
 C<# DO NOT MODIFY THIS OR ANYTHING ABOVE!>
 leaving any customizations you placed after that as they were.
 
-When C<dump_overwrite> is not set, if the output file already exists,
+When C<really_erase_my_files> is not set, if the output file already exists,
 but the aforementioned final stanza is not found, or the checksum
 contained there does not match the generated contents, Loader will
 croak and not touch the file.
 
+You should really be using version control on your schema classes (and all
+of the rest of your code for that matter).  Don't blame me if a bug in this
+code wipes something out when it shouldn't have, you've been warned.
+
 =head1 METHODS
 
 None of these methods are intended for direct invocation by regular
@@ -209,7 +219,6 @@ sub new {
 
     bless $self => $class;
 
-    $self->{db_schema}  ||= '';
     $self->_ensure_arrayref(qw/additional_classes
                                additional_base_classes
                                left_base_classes
@@ -226,6 +235,10 @@ sub new {
     $self->{schema_class} ||= ( ref $self->{schema} || $self->{schema} );
     $self->{schema} ||= $self->{schema_class};
 
+    croak "dump_overwrite is deprecated.  Please read the"
+        . " DBIx::Class::Schema::Loader::Base documentation"
+            if $self->{dump_overwrite};
+
     $self->{relbuilder} = DBIx::Class::Schema::Loader::RelBuilder->new(
         $self->schema_class, $self->inflect_plural, $self->inflect_singular
     ) if !$self->{skip_relationships};
@@ -293,7 +306,7 @@ sub _load_external {
         $self->_ext_stmt($class, $_);
     }
     $self->_ext_stmt($class,
-        q|# End of lines loaded from '$real_inc_path' |
+        qq|# End of lines loaded from '$real_inc_path' |
     );
     close($fh)
         or croak "Failed to close $real_inc_path: $!";
@@ -308,27 +321,59 @@ Does the actual schema-construction work.
 sub load {
     my $self = shift;
 
+    $self->_load_tables($self->_tables_list);
+}
+
+=head2 rescan
+
+Arguments: schema
+
+Rescan the database for newly added tables.  Does
+not process drops or changes.  Returns a list of
+the newly added table monikers.
+
+The schema argument should be the schema class
+or object to be affected.  It should probably
+be derived from the original schema_class used
+during L</load>.
+
+=cut
+
+sub rescan {
+    my ($self, $schema) = @_;
+
+    $self->{schema} = $schema;
+
+    my @created;
+    my @current = $self->_tables_list;
+    foreach my $table ($self->_tables_list) {
+        if(!exists $self->{_tables}->{$table}) {
+            push(@created, $table);
+        }
+    }
+
+    my $loaded = $self->_load_tables(@created);
+
+    return map { $self->monikers->{$_} } @$loaded;
+}
+
+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
+    foreach (@tables) {
+        $self->{_tables}->{$_} = 1;
     }
 
-    # Save the tables list
-    $self->{_tables} = \@tables;
-
     # Set up classes/monikers
     {
         no warnings 'redefine';
@@ -347,14 +392,14 @@ sub load {
     }
 
     $self->_load_external($_)
-        for ($self->schema_class, values %{$self->classes});
+        for map { $self->classes->{$_} } @tables;
 
     $self->_dump_to_dir if $self->dump_directory;
 
     # Drop temporary cache
     delete $self->{_cache};
 
-    1;
+    return \@tables;
 }
 
 sub _get_dump_filename {
@@ -418,9 +463,9 @@ sub _write_classfile {
     my $filename = $self->_get_dump_filename($class);
     $self->_ensure_dump_subdirs($class);
 
-    if (-f $filename && $self->dump_overwrite) {
+    if (-f $filename && $self->really_erase_my_files) {
         warn "Deleting existing file '$filename' due to "
-            . "'dump_overwrite' setting\n";
+            . "'really_erase_my_files' setting\n";
         unlink($filename);
     }    
 
@@ -480,7 +525,7 @@ sub _get_custom_content {
         }
     }
 
-    croak "Cannot not overwrite '$filename' without 'dump_overwrite',"
+    croak "Cannot not overwrite '$filename' without 'really_erase_my_files',"
         . " it does not appear to have been generated by Loader"
             if !$found;
 
@@ -537,7 +582,7 @@ sub _make_src_class {
     $self->_use   ($table_class, @{$self->additional_classes});
     $self->_inject($table_class, @{$self->additional_base_classes});
 
-    $self->_dbic_stmt($table_class, 'load_components', @{$self->components}, qw/PK::Auto Core/);
+    $self->_dbic_stmt($table_class, 'load_components', @{$self->components}, 'Core');
 
     $self->_dbic_stmt($table_class, 'load_resultset_components', @{$self->resultset_components})
         if @{$self->resultset_components};
@@ -592,7 +637,7 @@ names.
 sub tables {
     my $self = shift;
 
-    return @{$self->_tables};
+    return keys %{$self->_tables};
 }
 
 # Make a moniker from a table