From: Ash Berlin Date: Mon, 5 Oct 2009 13:17:40 +0000 (+0000) Subject: Only redump the files when something has actually changed X-Git-Tag: 0.04999_09~2 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=79193756e714416ff1686955affdebdfe7580885;p=dbsrgits%2FDBIx-Class-Schema-Loader.git Only redump the files when something has actually changed --- diff --git a/Changes b/Changes index 9853269..af47a0d 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,7 @@ Revision history for Perl extension DBIx::Class::Schema::Loader + - Only redump the files when something has actually changed + 0.04999_08 2009-08-28 - Replace UNIVERSAL::require with Class::C3::Componentised - Add Sybase/MSSQL support through DBD::Sybase diff --git a/lib/DBIx/Class/Schema/Loader/Base.pm b/lib/DBIx/Class/Schema/Loader/Base.pm index 42b0515..fab01f5 100644 --- a/lib/DBIx/Class/Schema/Loader/Base.pm +++ b/lib/DBIx/Class/Schema/Loader/Base.pm @@ -265,11 +265,13 @@ sub new { if $self->{dump_overwrite}; $self->{dynamic} = ! $self->{dump_directory}; - $self->{dump_directory} ||= File::Temp::tempdir( 'dbicXXXX', + $self->{temp_directory} ||= File::Temp::tempdir( 'dbicXXXX', TMPDIR => 1, CLEANUP => 1, ); + $self->{dump_directory} ||= $self->{temp_directory}; + $self->{relbuilder} = DBIx::Class::Schema::Loader::RelBuilder->new( $self->schema, $self->inflect_plural, $self->inflect_singular ) if !$self->{skip_relationships}; @@ -398,9 +400,13 @@ sub _load_tables { if(!$self->skip_relationships) { # The relationship loader needs a working schema $self->{quiet} = 1; + local $self->{dump_directory} = $self->{temp_directory}; $self->_reload_classes(@tables); $self->_load_relationships($_) for @tables; $self->{quiet} = 0; + + # Remove that temp dir from INC so it doesn't get reloaded + @INC = grep { $_ ne $self->{dump_directory} } @INC; } $self->_load_external($_) @@ -529,6 +535,14 @@ sub _dump_to_dir { } +sub _sig_comment { + my ($self, $version, $ts) = @_; + return qq|\n\n# Created by DBIx::Class::Schema::Loader| + . qq| v| . $version + . q| @ | . $ts + . qq|\n# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:|; +} + sub _write_classfile { my ($self, $class, $text) = @_; @@ -541,18 +555,27 @@ sub _write_classfile { unlink($filename); } - my $custom_content = $self->_get_custom_content($class, $filename); - $custom_content ||= qq|\n\n# You can replace this text with custom| - . qq| content, and it will be preserved on regeneration| - . qq|\n1;\n|; + my ($custom_content, $old_md5, $old_ver, $old_ts) = $self->_get_custom_content($class, $filename); $text .= qq|$_\n| for @{$self->{_dump_storage}->{$class} || []}; - $text .= qq|\n\n# Created by DBIx::Class::Schema::Loader| - . qq| v| . $DBIx::Class::Schema::Loader::VERSION - . q| @ | . POSIX::strftime('%Y-%m-%d %H:%M:%S', localtime) - . qq|\n# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:|; + # Check and see if the dump is infact differnt + + my $compare_to; + if ($old_md5) { + $compare_to = $text . $self->_sig_comment($old_ver, $old_ts); + + + if (Digest::MD5::md5_base64($compare_to) eq $old_md5) { + return; + } + } + + $text .= $self->_sig_comment( + $DBIx::Class::Schema::Loader::VERSION, + POSIX::strftime('%Y-%m-%d %H:%M:%S', localtime) + ); open(my $fh, '>', $filename) or croak "Cannot open '$filename' for writing: $!"; @@ -571,24 +594,36 @@ sub _write_classfile { or croak "Error closing '$filename': $!"; } +sub _default_custom_content { + return qq|\n\n# You can replace this text with custom| + . qq| content, and it will be preserved on regeneration| + . qq|\n1;\n|; +} + sub _get_custom_content { my ($self, $class, $filename) = @_; - return if ! -f $filename; + return ($self->_default_custom_content) if ! -f $filename; + open(my $fh, '<', $filename) or croak "Cannot open '$filename' for reading: $!"; my $mark_re = qr{^(# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:)([A-Za-z0-9/+]{22})\n}; - my $found = 0; my $buffer = ''; + my ($md5, $ts, $ver); while(<$fh>) { - if(!$found && /$mark_re/) { - $found = 1; - $buffer .= $1; + if(!$md5 && /$mark_re/) { + $md5 = $2; + my $line = $1; + + # Pull out the previous version and timestamp + ($ver, $ts) = $buffer =~ m/# Created by DBIx::Class::Schema::Loader v(.*?) @ (.*?)$/s; + + $buffer .= $line; croak "Checksum mismatch in '$filename'" - if Digest::MD5::md5_base64($buffer) ne $2; + if Digest::MD5::md5_base64($buffer) ne $md5; $buffer = ''; } @@ -599,9 +634,12 @@ sub _get_custom_content { croak "Cannot not overwrite '$filename' without 'really_erase_my_files'," . " it does not appear to have been generated by Loader" - if !$found; + if !$md5; + + # Default custom content: + $buffer ||= $self->_default_custom_content; - return $buffer; + return ($buffer, $md5, $ver, $ts); } sub _use {