From: Arthur Axel 'fREW' Schmidt Date: Sat, 10 Jul 2010 08:20:10 +0000 (-0500) Subject: add force_overwrite attribute to SQLTDM X-Git-Tag: v0.001003~9 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=dbsrgits%2FDBIx-Class-DeploymentHandler.git;a=commitdiff_plain;h=92624ee506c3671a80da4acb71148091e5c27128 add force_overwrite attribute to SQLTDM --- diff --git a/Changes b/Changes index 0659681..b2862c7 100644 --- a/Changes +++ b/Changes @@ -1,6 +1,7 @@ Revision history for {{$dist->name}} {{$NEXT}} + - add force_overwrite attribute to SQLTDM 0.001002 2010-07-07 21:11:45 CST6CDT - Add basic intro (norkakn) diff --git a/lib/DBIx/Class/DeploymentHandler/DeployMethod/SQL/Translator.pm b/lib/DBIx/Class/DeploymentHandler/DeployMethod/SQL/Translator.pm index 9664009..92fb92d 100644 --- a/lib/DBIx/Class/DeploymentHandler/DeployMethod/SQL/Translator.pm +++ b/lib/DBIx/Class/DeploymentHandler/DeployMethod/SQL/Translator.pm @@ -31,6 +31,12 @@ has ignore_ddl => ( default => undef, ); +has force_overwrite => ( + isa => 'Bool', + is => 'ro', + default => undef, +); + has schema => ( isa => 'DBIx::Class::Schema', is => 'ro', @@ -450,8 +456,12 @@ sub _prepare_install { my $filename = $self->$to_file($db, $version, $dir); if (-e $filename ) { - carp "Overwriting existing DDL file - $filename"; - unlink $filename; + if ($self->force_overwrite) { + carp "Overwriting existing DDL file - $filename"; + unlink $filename; + } else { + die "Cannot overwrite '$filename', either enable force_overwrite or delete it" + } } open my $file, q(>), $filename; print {$file} join ";\n", @$sql; @@ -556,8 +566,12 @@ method _prepare_changegrade($from_version, $to_version, $version_set, $direction foreach my $db (@$databases) { my $diff_file = $self->$diff_file_method($db, $version_set, $dir ); if(-e $diff_file) { - carp("Overwriting existing $direction-diff file - $diff_file"); - unlink $diff_file; + if ($self->force_overwrite) { + carp("Overwriting existing $direction-diff file - $diff_file"); + unlink $diff_file; + } else { + die "Cannot overwrite '$diff_file', either enable force_overwrite or delete it" + } } open my $file, q(>), $diff_file; @@ -645,8 +659,12 @@ sub prepare_protoschema { unless $yml; if (-e $filename ) { - carp "Overwriting existing DDL-YML file - $filename"; - unlink $filename; + if ($self->force_overwrite) { + carp "Overwriting existing DDL-YML file - $filename"; + unlink $filename; + } else { + die "Cannot overwrite '$filename', either enable force_overwrite or delete it" + } } open my $file, q(>), $filename; @@ -846,6 +864,12 @@ instead of any pregenerated SQL. If you have a development server this is probably the best plan of action as you will not be putting as many generated files in your version control. Goes well with with C of C<[]>. +=attr force_overwrite + +When this attribute is true generated files will be overwritten when the +methods which create such files are run again. The default is false, in which +case the program will die with a message saying which file needs to be deleted. + =attr schema The L (B) that is used to talk to the database diff --git a/t/deploy_methods/sql_translator.t b/t/deploy_methods/sql_translator.t index ef2dad7..3bfdce1 100644 --- a/t/deploy_methods/sql_translator.t +++ b/t/deploy_methods/sql_translator.t @@ -38,12 +38,7 @@ VERSION1: { ok -e 'foobar'; - { - my $warned = 0; - local $SIG{__WARN__} = sub{$warned = 1}; - $dm->prepare_deploy; - ok( $warned, 'prepare_deploy warns if you run it twice' ); - } + dies_ok {$dm->prepare_deploy} 'prepare_deploy dies if you run it twice' ; ok( -f catfile(qw( t sql SQLite deploy 1.0 001-auto.sql )), @@ -214,16 +209,14 @@ VERSION3: { to_version => $version, version_set => ['2.0', $version] }); - { - my $warned = 0; - local $SIG{__WARN__} = sub{$warned = 1}; + dies_ok { $dm->prepare_upgrade({ from_version => '2.0', to_version => $version, version_set => ['2.0', $version] }); - ok( $warned, 'prepare_upgrade warns if you clobber an existing upgrade file' ); - } + } + 'prepare_upgrade dies if you clobber an existing upgrade file' ; ok( -f catfile(qw( t sql SQLite upgrade 1.0-2.0 001-auto.sql )), '2.0-3.0 diff gets generated properly'