From: Matt S Trout Date: Fri, 12 May 2006 02:17:18 +0000 (+0000) Subject: fixup for stringify that can be false in find_or_create_related X-Git-Tag: v0.07002~99 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=9c2c91ea7f94d7981cd1c8d212a4b04751fcd023;p=dbsrgits%2FDBIx-Class.git fixup for stringify that can be false in find_or_create_related --- diff --git a/Changes b/Changes index cfc7ee3..54514fb 100644 --- a/Changes +++ b/Changes @@ -1,6 +1,7 @@ Revision history for DBIx::Class 0.06003 + - make find_or_create_related check defined() instead of truth - don't unnecessarily fetch rels for cascade_update - don't set_columns explicitly in update_or_create; instead use update($hashref) so InflateColumn works diff --git a/lib/DBIx/Class/Relationship/Base.pm b/lib/DBIx/Class/Relationship/Base.pm index c36d233..05f4c52 100644 --- a/lib/DBIx/Class/Relationship/Base.pm +++ b/lib/DBIx/Class/Relationship/Base.pm @@ -264,7 +264,8 @@ L for details. sub find_or_create_related { my $self = shift; - return $self->find_related(@_) || $self->create_related(@_); + my $obj = $self->find_related(@_); + return (defined($obj) ? $obj : $self->create_related(@_)); } =head2 set_from_related diff --git a/t/lib/DBICTest/Schema.pm b/t/lib/DBICTest/Schema.pm index fbf5383..595db5a 100644 --- a/t/lib/DBICTest/Schema.pm +++ b/t/lib/DBICTest/Schema.pm @@ -8,6 +8,8 @@ no warnings qw/qw/; __PACKAGE__->load_classes(qw/ Artist CD + Link + Bookmark #dummy Track Tag diff --git a/t/lib/DBICTest/Schema/Bookmark.pm b/t/lib/DBICTest/Schema/Bookmark.pm new file mode 100644 index 0000000..4f9ec44 --- /dev/null +++ b/t/lib/DBICTest/Schema/Bookmark.pm @@ -0,0 +1,26 @@ +package # hide from PAUSE + DBICTest::Schema::Bookmark; + + use base 'DBIx::Class::Core'; + + +use strict; +use warnings; + +__PACKAGE__->load_components(qw/PK::Auto Core/); +__PACKAGE__->table('bookmark'); +__PACKAGE__->add_columns(qw/id link/); +__PACKAGE__->add_columns( + 'id' => { + data_type => 'integer', + is_auto_increment => 1 + }, + 'link' => { + data_type => 'integer', + }, +); + +__PACKAGE__->set_primary_key('id'); +__PACKAGE__->belongs_to(link => 'DBICTest::Schema::Link' ); + +1; diff --git a/t/lib/DBICTest/Schema/Link.pm b/t/lib/DBICTest/Schema/Link.pm new file mode 100644 index 0000000..72574ea --- /dev/null +++ b/t/lib/DBICTest/Schema/Link.pm @@ -0,0 +1,31 @@ +package # hide from PAUSE + DBICTest::Schema::Link; + +use base 'DBIx::Class::Core'; + +use strict; +use warnings; + +__PACKAGE__->load_components(qw/PK::Auto Core/); +__PACKAGE__->table('link'); +__PACKAGE__->add_columns( + 'id' => { + data_type => 'integer', + is_auto_increment => 1 + }, + 'url' => { + data_type => 'varchar', + size => 100, + is_nullable => 1, + }, + 'title' => { + data_type => 'varchar', + size => 100, + is_nullable => 1, + }, +); +__PACKAGE__->set_primary_key('id'); + +use overload '""' => sub { shift->url }, fallback=> 1; + +1; diff --git a/t/lib/DBICTest/Setup.pm b/t/lib/DBICTest/Setup.pm index 816a64c..ddcad9c 100755 --- a/t/lib/DBICTest/Setup.pm +++ b/t/lib/DBICTest/Setup.pm @@ -137,4 +137,14 @@ $schema->populate('Track', [ [ 18, 1, 3, "Beehind You"], ]); +$schema->populate('Link', [ + [ qw/id title/ ], + [ 1, 'aaa' ] +]); + +$schema->populate('Bookmark', [ + [ qw/id link/ ], + [ 1, 1 ] +]); + 1; diff --git a/t/lib/sqlite.sql b/t/lib/sqlite.sql index f59aca8..ac5f9f3 100644 --- a/t/lib/sqlite.sql +++ b/t/lib/sqlite.sql @@ -1,6 +1,6 @@ -- -- Created by SQL::Translator::Producer::SQLite --- Created on Sun Mar 19 19:16:50 2006 +-- Created on Fri May 12 01:09:57 2006 -- BEGIN TRANSACTION; @@ -13,15 +13,6 @@ CREATE TABLE serialized ( ); -- --- Table: twokeys --- -CREATE TABLE twokeys ( - artist integer NOT NULL, - cd integer NOT NULL, - PRIMARY KEY (artist, cd) -); - --- -- Table: liner_notes -- CREATE TABLE liner_notes ( @@ -47,6 +38,18 @@ CREATE TABLE artist ( ); -- +-- Table: twokeytreelike +-- +CREATE TABLE twokeytreelike ( + id1 integer NOT NULL, + id2 integer NOT NULL, + parent1 integer NOT NULL, + parent2 integer NOT NULL, + name varchar(100) NOT NULL, + PRIMARY KEY (id1, id2) +); + +-- -- Table: self_ref_alias -- CREATE TABLE self_ref_alias ( @@ -56,17 +59,6 @@ CREATE TABLE self_ref_alias ( ); -- --- Table: fourkeys --- -CREATE TABLE fourkeys ( - foo integer NOT NULL, - bar integer NOT NULL, - hello integer NOT NULL, - goodbye integer NOT NULL, - PRIMARY KEY (foo, bar, hello, goodbye) -); - --- -- Table: cd -- CREATE TABLE cd ( @@ -77,21 +69,11 @@ CREATE TABLE cd ( ); -- --- Table: artist_undirected_map +-- Table: bookmark -- -CREATE TABLE artist_undirected_map ( - id1 integer NOT NULL, - id2 integer NOT NULL, - PRIMARY KEY (id1, id2) -); - --- --- Table: onekey --- -CREATE TABLE onekey ( +CREATE TABLE bookmark ( id INTEGER PRIMARY KEY NOT NULL, - artist integer NOT NULL, - cd integer NOT NULL + link integer NOT NULL ); -- @@ -105,10 +87,28 @@ CREATE TABLE track ( ); -- --- Table: producer +-- Table: link -- -CREATE TABLE producer ( - producerid INTEGER PRIMARY KEY NOT NULL, +CREATE TABLE link ( + id INTEGER PRIMARY KEY NOT NULL, + url varchar(100), + title varchar(100) +); + +-- +-- Table: self_ref +-- +CREATE TABLE self_ref ( + id INTEGER PRIMARY KEY NOT NULL, + name varchar(100) NOT NULL +); + +-- +-- Table: treelike +-- +CREATE TABLE treelike ( + id INTEGER PRIMARY KEY NOT NULL, + parent integer NOT NULL, name varchar(100) NOT NULL ); @@ -122,20 +122,49 @@ CREATE TABLE tags ( ); -- --- Table: self_ref +-- Table: twokeys -- -CREATE TABLE self_ref ( - id INTEGER PRIMARY KEY NOT NULL, +CREATE TABLE twokeys ( + artist integer NOT NULL, + cd integer NOT NULL, + PRIMARY KEY (artist, cd) +); + +-- +-- Table: fourkeys +-- +CREATE TABLE fourkeys ( + foo integer NOT NULL, + bar integer NOT NULL, + hello integer NOT NULL, + goodbye integer NOT NULL, + PRIMARY KEY (foo, bar, hello, goodbye) +); + +-- +-- Table: artist_undirected_map +-- +CREATE TABLE artist_undirected_map ( + id1 integer NOT NULL, + id2 integer NOT NULL, + PRIMARY KEY (id1, id2) +); + +-- +-- Table: producer +-- +CREATE TABLE producer ( + producerid INTEGER PRIMARY KEY NOT NULL, name varchar(100) NOT NULL ); -- --- Table: treelike +-- Table: onekey -- -CREATE TABLE treelike ( +CREATE TABLE onekey ( id INTEGER PRIMARY KEY NOT NULL, - parent integer NOT NULL, - name varchar(100) NOT NULL + artist integer NOT NULL, + cd integer NOT NULL ); COMMIT; diff --git a/t/run/01core.tl b/t/run/01core.tl index c0505c2..68d34aa 100644 --- a/t/run/01core.tl +++ b/t/run/01core.tl @@ -1,7 +1,7 @@ sub run_tests { my $schema = shift; -plan tests => 46; +plan tests => 47; # figure out if we've got a version of sqlite that is older than 3.2.6, in # which case COUNT(DISTINCT()) doesn't work @@ -213,6 +213,14 @@ is($typeinfo->{data_type}, 'INTEGER', 'column_info ok'); $schema->source("Artist")->column_info('artistid'); ok($schema->source("Artist")->{_columns_info_loaded} == 1, 'Columns info flag set'); +my $newbook = $schema->resultset( 'Bookmark' )->find(1); + +$@ = ''; +eval { +my $newlink = $newbook->link; +}; +ok(!$@, "stringify to false value doesn't cause error"); + } 1;