From: Peter Rabbitson Date: Wed, 13 May 2009 23:41:31 +0000 (+0000) Subject: Better testing and heuristics for subqueried _cond_for_update_delete X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=ecef7b5e63ab290cb2e8b120a65f178cb871397f;p=dbsrgits%2FDBIx-Class-Historic.git Better testing and heuristics for subqueried _cond_for_update_delete --- diff --git a/lib/DBIx/Class/ResultSet.pm b/lib/DBIx/Class/ResultSet.pm index 4804dc7..9fd07e4 100644 --- a/lib/DBIx/Class/ResultSet.pm +++ b/lib/DBIx/Class/ResultSet.pm @@ -1315,7 +1315,7 @@ sub _cond_for_update_delete { # Some attributes when present require a subquery # This might not work on some database (mysql), but... # it won't work without the subquery either so who cares - if (grep { defined $self->{attrs}{$_} } qw/join rows group_by/) { + if (grep { defined $self->{attrs}{$_} } qw/seen_join from rows group_by/) { foreach my $pk ($self->result_source->primary_columns) { $cond->{$pk} = { IN => $self->get_column($pk)->as_query }; diff --git a/t/delete/related.t b/t/delete/related.t index f3c8d9b..f3fb78b 100644 --- a/t/delete/related.t +++ b/t/delete/related.t @@ -12,29 +12,34 @@ my $ars = $schema->resultset('Artist'); my $cdrs = $schema->resultset('CD'); # create some custom entries -$ars->create ({ artistid => 9, name => 'dead man walking' }); +$ars->populate ([ + [qw/artistid name/], + [qw/71 a1/], + [qw/72 a2/], + [qw/73 a3/], +]); $cdrs->populate ([ [qw/cdid artist title year/], - [qw/70 2 delete0 2005/], - [qw/71 3 delete1 2005/], - [qw/72 3 delete2 2005/], - [qw/73 3 delete3 2006/], - [qw/74 3 delete4 2007/], - [qw/75 9 delete5 2008/], + [qw/70 71 delete0 2005/], + [qw/71 72 delete1 2005/], + [qw/72 72 delete2 2005/], + [qw/73 72 delete3 2006/], + [qw/74 72 delete4 2007/], + [qw/75 73 delete5 2008/], ]); my $total_cds = $cdrs->count; # test that delete_related w/o conditions deletes all related records only -$ars->find (9)->delete_related ('cds'); +$ars->search ({name => 'a3' })->search_related ('cds')->delete; is ($cdrs->count, $total_cds -= 1, 'related delete ok'); -my $a3_cds = $ars->find(3)->cds; +my $a2_cds = $ars->search ({ name => 'a2' })->search_related ('cds'); # test that related deletion w/conditions deletes just the matched related records only -$a3_cds->search ({ year => 2005 })->delete; +$a2_cds->search ({ year => 2005 })->delete; is ($cdrs->count, $total_cds -= 2, 'related + condition delete ok'); # test that related deletion with limit condition works -$a3_cds->search ({}, { rows => 1})->delete; +$a2_cds->search ({}, { rows => 1})->delete; is ($cdrs->count, $total_cds -= 1, 'related + limit delete ok');