From: Dagfinn Ilmari Mannsåker Date: Fri, 8 May 2015 17:20:41 +0000 (+0100) Subject: Use the actual md5sum of the old version when deciding whether to overwrite X-Git-Tag: 0.07043~3 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=c939b4acb9a74ccfac75d58510bc241fcaf39b02;p=dbsrgits%2FDBIx-Class-Schema-Loader.git Use the actual md5sum of the old version when deciding whether to overwrite Otherwise overwrite_modifications => 1 won't work if the generated content has been modified but the schema hasn't changed (so the md5sum in the marker matches the newly-generated one). --- diff --git a/Changes b/Changes index d5ccd94..0d28eb5 100644 --- a/Changes +++ b/Changes @@ -6,6 +6,7 @@ Revision history for Perl extension DBIx::Class::Schema::Loader - Fix DB2 foreign-key introspection - Remove dependency on List::MoreUtils and Sub::Name - Ensure schema files are generated as binary files on Windows + - Fix overwrite_modifications not overwriting if the table hasn't changed 0.07042 2014-08-20 - Fix unescaped left braces in regexes in tests diff --git a/lib/DBIx/Class/Schema/Loader/Base.pm b/lib/DBIx/Class/Schema/Loader/Base.pm index da4f91b..6f12915 100644 --- a/lib/DBIx/Class/Schema/Loader/Base.pm +++ b/lib/DBIx/Class/Schema/Loader/Base.pm @@ -2236,12 +2236,12 @@ sub _parse_generated_file { my $mark_re = qr{^(# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:)([A-Za-z0-9/+]{22})\r?\n}; - my ($md5, $ts, $ver, $gen); + my ($real_md5, $ts, $ver, $gen); local $_; while(<$fh>) { if(/$mark_re/) { my $pre_md5 = $1; - $md5 = $2; + 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; @@ -2249,8 +2249,9 @@ sub _parse_generated_file { $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 && Digest::MD5::md5_base64(encode 'UTF-8', $gen) ne $md5; + if !$self->overwrite_modifications && $real_md5 ne $mark_md5; last; } @@ -2260,14 +2261,14 @@ sub _parse_generated_file { } my $custom = do { local $/; <$fh> } - if $md5; + if $real_md5; $custom ||= ''; $custom =~ s/$CRLF|$LF/\n/g; close $fh; - return ($gen, $md5, $ver, $ts, $custom); + return ($gen, $real_md5, $ver, $ts, $custom); } sub _use { diff --git a/t/40overwrite_modifications.t b/t/40overwrite_modifications.t index 20af920..05130f9 100644 --- a/t/40overwrite_modifications.t +++ b/t/40overwrite_modifications.t @@ -1,5 +1,5 @@ use strict; -use Test::More tests => 5; +use Test::More; use Test::Exception; use Test::Warn; use lib qw(t/lib); @@ -10,6 +10,7 @@ use File::Spec; use File::Temp qw/ tempdir tempfile /; use DBIx::Class::Schema::Loader; +use DBIx::Class::Schema::Loader::Utils qw/ slurp_file /; my $tempdir = tempdir( CLEANUP => 1 ); my $foopm = File::Spec->catfile( $tempdir, @@ -41,6 +42,9 @@ lives_ok { dump_schema( overwrite_modifications => 1 ); } 'does not throw when dumping with overwrite_modifications'; + +unlike slurp_file $foopm, qr/"somethingelse"/, "Modifications actually overwritten"; + sub dump_schema { # need to poke _loader_invoked in order to be able to rerun the @@ -57,3 +61,5 @@ sub dump_schema { ); } [qr/^Dumping manual schema/, qr/^Schema dump completed/ ]; } + +done_testing();