From: Michael G Schwern Date: Thu, 14 Feb 2008 10:23:57 +0000 (-0800) Subject: Implement cascade => "None" X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=deff792ed0b7f80ca7a388067e835a6d9ddace9d;p=dbsrgits%2FDBIx-Class-Historic.git Implement cascade => "None" --- diff --git a/lib/DBIx/Class/CDBICompat/Relationships.pm b/lib/DBIx/Class/CDBICompat/Relationships.pm index c6768a5..6893bc9 100644 --- a/lib/DBIx/Class/CDBICompat/Relationships.pm +++ b/lib/DBIx/Class/CDBICompat/Relationships.pm @@ -70,9 +70,16 @@ sub has_many { if (ref $f_key eq 'HASH' && !$args) { $args = $f_key; undef $f_key; }; $args ||= {}; - if (delete $args->{no_cascade_delete}) { + my $cascade = delete $args->{cascade} || ''; + if (delete $args->{no_cascade_delete} || $cascade eq 'None') { $args->{cascade_delete} = 0; } + elsif( $cascade eq 'Delete' ) { + $args->{cascade_delete} = 1; + } + elsif( length $cascade ) { + warn "Unemulated cascade option '$cascade' in $class->has_many($rel => $f_class)"; + } if( !$f_key and !@f_method ) { my $f_source = $f_class->result_source_instance; diff --git a/t/cdbi-t/23-cascade.t b/t/cdbi-t/23-cascade.t index 7e17419..50a1647 100644 --- a/t/cdbi-t/23-cascade.t +++ b/t/cdbi-t/23-cascade.t @@ -2,27 +2,75 @@ use strict; use Test::More; BEGIN { - eval "use DBD::SQLite"; - plan $@ ? (skip_all => 'needs DBD::SQLite for testing') : (tests => 5); + eval "use DBIx::Class::CDBICompat;"; + if ($@) { + plan (skip_all => 'Class::Trigger and DBIx::ContextualFetch required'); + next; + } + eval "use DBD::SQLite"; + plan $@ ? (skip_all => 'needs DBD::SQLite for testing') : (tests => 12); } -use lib 't/testlib'; -use Film; -use Director; - -{ # Cascade Strategies - Director->has_many(nasties => Film => { cascade => 'Fail' }); - - my $dir = Director->insert({ name => "Nasty Noddy" }); - my $kk = $dir->add_to_nasties({ Title => 'Killer Killers' }); - is $kk->director, $dir, "Director set OK"; - is $dir->nasties, 1, "We have one nasty"; - eval { $dir->delete }; - like $@, qr/1/, "Can't delete while films exist"; - my $rr = $dir->add_to_nasties({ Title => 'Revenge of the Revengers' }); - eval { $dir->delete }; - like $@, qr/2/, "Still can't delete"; - $dir->nasties->delete_all; - eval { $dir->delete }; - is $@, '', "Can delete once films are gone"; +INIT { + use lib 't/testlib'; + use Film; + use Director; } + +{ # Cascade on delete + Director->has_many(nasties => 'Film'); + + my $dir = Director->insert({ + name => "Lewis Teague", + }); + my $kk = $dir->add_to_nasties({ + Title => 'Alligator' + }); + is $kk->director, $dir, "Director set OK"; + is $dir->nasties, 1, "We have one nasty"; + + ok $dir->delete; + ok !Film->retrieve("Alligator"), "has_many cascade deletes by default"; +} + + +# Two ways of saying not to cascade +for my $args ({ no_cascade_delete => 1 }, { cascade => "None" }) { + Director->has_many(nasties => 'Film', $args); + + my $dir = Director->insert({ + name => "Lewis Teague", + }); + my $kk = $dir->add_to_nasties({ + Title => 'Alligator' + }); + is $kk->director, $dir, "Director set OK"; + is $dir->nasties, 1, "We have one nasty"; + + ok $dir->delete; + ok +Film->retrieve("Alligator"), "has_many with @{[ keys %$args ]} => @{[ values %$args ]}"; + $kk->delete; +} + + +#{ # Fail on cascade +# local $TODO = 'cascade => "Fail" unimplemented'; +# +# Director->has_many(nasties => Film => { cascade => 'Fail' }); +# +# my $dir = Director->insert({ name => "Nasty Noddy" }); +# my $kk = $dir->add_to_nasties({ Title => 'Killer Killers' }); +# is $kk->director, $dir, "Director set OK"; +# is $dir->nasties, 1, "We have one nasty"; +# +# ok !eval { $dir->delete }; +# like $@, qr/1/, "Can't delete while films exist"; +# +# my $rr = $dir->add_to_nasties({ Title => 'Revenge of the Revengers' }); +# ok !eval { $dir->delete }; +# like $@, qr/2/, "Still can't delete"; +# +# $dir->nasties->delete_all; +# ok eval { $dir->delete }; +# is $@, '', "Can delete once films are gone"; +#}