THROWAWAY: Don't load unmodified generated external classes
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader / Base.pm
index 089adc0..0e3c555 100644 (file)
@@ -158,6 +158,9 @@ of relationships.
 Skip loading of other classes in @INC. The default is to merge all other classes
 with the same name found in @INC into the schema file we are creating.
 
+Even if this is not set, code generated by this module and not
+subsequently modified is never included.
+
 =head2 naming
 
 Static schemas (ones dumped to disk) will, by default, use the new-style
@@ -1561,24 +1564,34 @@ sub _load_external {
 
         my $code = $self->_rewrite_old_classnames(slurp_file $real_inc_path);
 
-        if ($self->dynamic) { # load the class too
-            eval_package_without_redefine_warnings($class, $code);
+        if (my ($gen, $real_md5, $ver, $ts, $custom) = try {
+            local $self->{overwrite_modifications} = 0;
+            $self->_parse_generated_code($real_inc_path, $code);
+        }) {
+            # Ignore unmodified generated code.
+            $code = $custom eq $self->_default_custom_content ? '' : $custom;
         }
 
-        $self->_ext_stmt($class,
-            qq|# These lines were loaded from '$real_inc_path' found in \@INC.\n|
-           .qq|# They are now part of the custom portion of this file\n|
-           .qq|# for you to hand-edit.  If you do not either delete\n|
-           .qq|# this section or remove that file from \@INC, this section\n|
-           .qq|# will be repeated redundantly when you re-create this\n|
-           .qq|# file again via Loader!  See skip_load_external to disable\n|
-           .qq|# this feature.\n|
-        );
-        chomp $code;
-        $self->_ext_stmt($class, $code);
-        $self->_ext_stmt($class,
-            qq|# End of lines loaded from '$real_inc_path'|
-        );
+        if ($code) {
+            if ($self->dynamic) { # load the class too
+                eval_package_without_redefine_warnings($class, $code);
+            }
+
+            $self->_ext_stmt($class,
+                qq|# These lines were loaded from '$real_inc_path' found in \@INC.\n|
+               .qq|# They are now part of the custom portion of this file\n|
+               .qq|# for you to hand-edit.  If you do not either delete\n|
+               .qq|# this section or remove that file from \@INC, this section\n|
+               .qq|# will be repeated redundantly when you re-create this\n|
+               .qq|# file again via Loader!  See skip_load_external to disable\n|
+               .qq|# this feature.\n|
+            );
+            chomp $code;
+            $self->_ext_stmt($class, $code);
+            $self->_ext_stmt($class,
+                qq|# End of lines loaded from '$real_inc_path'|
+            );
+        }
     }
 
     if ($old_real_inc_path) {
@@ -1623,9 +1636,7 @@ Does the actual schema-construction work.
 sub load {
     my $self = shift;
 
-    $self->_load_tables(
-        $self->_tables_list({ constraint => $self->constraint, exclude => $self->exclude })
-    );
+    $self->_load_tables($self->_tables_list);
 }
 
 =head2 rescan
@@ -1647,7 +1658,7 @@ sub rescan {
     $self->_relbuilder->{schema} = $schema;
 
     my @created;
-    my @current = $self->_tables_list({ constraint => $self->constraint, exclude => $self->exclude });
+    my @current = $self->_tables_list;
 
     foreach my $table (@current) {
         if(!exists $self->_tables->{$table->sql_name}) {
@@ -2230,43 +2241,33 @@ sub _parse_generated_file {
 
     return unless -f $fn;
 
-    open(my $fh, '<:encoding(UTF-8)', $fn)
-        or croak "Cannot open '$fn' for reading: $!";
-
-    my $mark_re =
-        qr{^(# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:)([A-Za-z0-9/+]{22})\r?\n};
-
-    my ($real_md5, $ts, $ver, $gen);
-    local $_;
-    while(<$fh>) {
-        if(/$mark_re/) {
-            my $pre_md5 = $1;
-            my $mark_md5 = $2;
-
-            # Pull out the version and timestamp from the line above
-            ($ver, $ts) = $gen =~ m/^# Created by DBIx::Class::Schema::Loader( v[\d.]+)?( @ [\d-]+ [\d:]+)?\r?\Z/m;
-            $ver =~ s/^ v// if $ver;
-            $ts =~ s/^ @ // if $ts;
-
-            $gen .= $pre_md5;
-            $real_md5 = Digest::MD5::md5_base64(encode 'UTF-8', $gen);
-            croak "Checksum mismatch in '$fn', the auto-generated part of the file has been modified outside of this loader.  Aborting.\nIf you want to overwrite these modifications, set the 'overwrite_modifications' loader option.\n"
-                if !$self->overwrite_modifications && $real_md5 ne $mark_md5;
-
-            last;
-        }
-        else {
-            $gen .= $_;
-        }
-    }
-
-    my $custom = do { local $/; <$fh> }
-        if $real_md5;
-
-    $custom ||= '';
-    $custom =~ s/$CRLF|$LF/\n/g;
-
-    close $fh;
+    return $self->_parse_generated_code($fn, slurp_file $fn);
+}
+
+sub _parse_generated_code {
+    my ($self, $fn, $code) = @_;
+
+    my ($gen, $ver, $ts, $mark_md5, $custom) = (
+        $code =~ m{
+            \A
+            (
+                .*                   # generated code
+                ^\# \Q Created by DBIx::Class::Schema::Loader\E
+                (\ v [\d.]+ )? (\ @\ [\d-]+\ [\d:]+)?\r?\n # verison/time stamp
+                ^\# \Q DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:\E
+            )
+            ([A-Za-z0-9/+]{22})\r?\n # checksum
+            (.*)                     # custom code
+            \z
+        }xms
+    ) or return;
+
+    $ver =~ s/^ v// if $ver;
+    $ts =~ s/^ @ // if $ts;
+
+    my $real_md5 = Digest::MD5::md5_base64(encode 'UTF-8', $gen);
+    croak "Checksum mismatch in '$fn', the auto-generated part of the file has been modified outside of this loader.  Aborting.\nIf you want to overwrite these modifications, set the 'overwrite_modifications' loader option.\n"
+        if !$self->overwrite_modifications && $real_md5 ne $mark_md5;
 
     return ($gen, $real_md5, $ver, $ts, $custom);
 }