From: Dagfinn Ilmari Mannsåker Date: Tue, 26 Nov 2013 17:36:07 +0000 (+0000) Subject: Add dry-run mode for static schema creation X-Git-Tag: 0.07039~2 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=28310f24a72d93ca7fc3852c7458e4c05d84fe2b;p=dbsrgits%2FDBIx-Class-Schema-Loader.git Add dry-run mode for static schema creation --- diff --git a/Changes b/Changes index d66a9d1..5300348 100644 --- a/Changes +++ b/Changes @@ -2,6 +2,7 @@ Revision history for Perl extension DBIx::Class::Schema::Loader - Fix table listing with DBD::DB2 >= 1.85 (RT#91764) - Add accessor for the list of (re)generated classes + - Add dry-run mode for static schema creation 0.07038 2013-11-20 - Allow coderef maps to call back into the hashref mapping code diff --git a/lib/DBIx/Class/Schema/Loader/Base.pm b/lib/DBIx/Class/Schema/Loader/Base.pm index 2c7cfb0..c7f9bd7 100644 --- a/lib/DBIx/Class/Schema/Loader/Base.pm +++ b/lib/DBIx/Class/Schema/Loader/Base.pm @@ -61,6 +61,7 @@ __PACKAGE__->mk_group_ro_accessors('simple', qw/ use_moose only_autoclean overwrite_modifications + dry_run generated_classes relationship_attrs @@ -306,6 +307,11 @@ If true, will not print the usual C messages. Does not affect warnings (except for warnings related to L.) +=head2 dry_run + +If true, don't actually write out the generated files. This can only be +used with static schema generation. + =head2 generate_pod By default POD will be generated for columns and relationships, using database @@ -1136,6 +1142,10 @@ sub new { if $self->{dump_overwrite}; $self->{dynamic} = ! $self->{dump_directory}; + + croak "dry_run can only be used with static schema generation" + if $self->dynamic and $self->dry_run; + $self->{temp_directory} ||= File::Temp::tempdir( 'dbicXXXX', TMPDIR => 1, CLEANUP => 1, @@ -1750,6 +1760,7 @@ sub _load_tables { local $self->{quiet} = 1; local $self->{dump_directory} = $self->{temp_directory}; local $self->{generated_classes} = []; + local $self->{dry_run} = 0; $self->_reload_classes(\@tables); $self->_load_relationships(\@tables); @@ -1762,7 +1773,7 @@ sub _load_tables { # Reload without unloading first to preserve any symbols from external # packages. - $self->_reload_classes(\@tables, { unload => 0 }); + $self->_reload_classes(\@tables, { unload => 0 }) unless $self->dry_run; # Drop temporary cache delete $self->{_cache}; @@ -1880,6 +1891,8 @@ sub get_dump_filename { sub _ensure_dump_subdirs { my ($self, $class) = (@_); + return if $self->dry_run; + my @name_parts = split(/::/, $class); pop @name_parts; # we don't care about the very last element, # which is a filename @@ -2027,7 +2040,7 @@ sub _write_classfile { my $filename = $self->_get_dump_filename($class); $self->_ensure_dump_subdirs($class); - if (-f $filename && $self->really_erase_my_files) { + if (-f $filename && $self->really_erase_my_files && !$self->dry_run) { warn "Deleting existing file '$filename' due to " . "'really_erase_my_files' setting\n" unless $self->quiet; unlink($filename); @@ -2051,7 +2064,7 @@ sub _write_classfile { if (-f $old_filename) { $custom_content = ($self->_parse_generated_file ($old_filename))[4]; - unlink $old_filename; + unlink $old_filename unless $self->dry_run; } } @@ -2136,6 +2149,8 @@ sub _write_classfile { push @{$self->generated_classes}, $class; + return if $self->dry_run; + $text .= $self->_sig_comment( $self->version_to_dump, POSIX::strftime('%Y-%m-%d %H:%M:%S', localtime) diff --git a/t/23dumpmore.t b/t/23dumpmore.t index babe3f0..f94cc3f 100644 --- a/t/23dumpmore.t +++ b/t/23dumpmore.t @@ -583,5 +583,18 @@ $t->dump_test( }, ); +# test dry-run mode +$t->dump_test( + classname => 'DBICTest::DumpMore::DryRun', + options => { + dry_run => 1, + }, +); + +my $schema_file = $t->class_file('DBICTest::DumpMore::DryRun'); +ok( !-e $schema_file, "dry-run doesn't create file for schema class" ); +(my $schema_dir = $schema_file) =~ s/\.pm\z//; +ok( !-e $schema_dir, "dry-run doesn't create subdirectory for schema namespace" ); + done_testing; # vim:et sts=4 sw=4 tw=0: