From: Nigel Metheringham Date: Thu, 12 Feb 2009 20:27:22 +0000 (+0000) Subject: Made update() on a rs that includes joins complain in the same way that delete()... X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=9f2d17e9dc71dabc3263cc7c2ad794f7eb24d223;p=dbsrgits%2FDBIx-Class-Historic.git Made update() on a rs that includes joins complain in the same way that delete() does. Added tests for both update and delete checks. --- diff --git a/lib/DBIx/Class/ResultSet.pm b/lib/DBIx/Class/ResultSet.pm index 1610eb1..24b8981 100644 --- a/lib/DBIx/Class/ResultSet.pm +++ b/lib/DBIx/Class/ResultSet.pm @@ -1269,6 +1269,11 @@ sub update { $self->throw_exception("Values for update must be a hash") unless ref $values eq 'HASH'; + carp( 'WARNING! Currently $rs->update() does not generate proper SQL' + . ' on joined resultsets, and may affect rows well outside of the' + . ' contents of $rs. Use at your own risk' ) + if ( $self->{attrs}{seen_join} ); + my $cond = $self->_cond_for_update_delete; return $self->result_source->storage->update( diff --git a/t/76joins.t b/t/76joins.t index 5d90d21..84d8ba5 100644 --- a/t/76joins.t +++ b/t/76joins.t @@ -17,7 +17,7 @@ BEGIN { eval "use DBD::SQLite"; plan $@ ? ( skip_all => 'needs DBD::SQLite for testing' ) - : ( tests => 16 ); + : ( tests => 18 ); } # figure out if we've got a version of sqlite that is older than 3.2.6, in @@ -179,3 +179,28 @@ cmp_ok( $rs->count, '==', 1, "Single record in resultset"); is($rs->first->name, 'We Are Goth', 'Correct record returned'); +# test for warnings on delete of joined resultset +$rs = $schema->resultset("CD")->search( + { 'artist.name' => 'Caterwauler McCrae' }, + { join => [qw/artist/]} +); +my $tst_delete_warning; +eval { + local $SIG{__WARN__} = sub { $tst_delete_warning = shift }; + $rs->delete(); +}; + +ok( ($@ || $tst_delete_warning), 'fail/warning on attempt to delete a join-ed resultset'); + +# test for warnings on update of joined resultset +$rs = $schema->resultset("CD")->search( + { 'artist.name' => 'Random Boy Band' }, + { join => [qw/artist/]} +); +my $tst_update_warning; +eval { + local $SIG{__WARN__} = sub { $tst_update_warning = shift }; + $rs->update({ 'artist' => 1 }); +}; + +ok( ($@ || $tst_update_warning), 'fail/warning on attempt to update a join-ed resultset');