From: Ash Berlin Date: Tue, 4 Mar 2008 12:06:34 +0000 (+0000) Subject: is_deferrable patch from Debolaz X-Git-Tag: v0.08240~550 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=13de943d1aef0b4c2ff28505fc61c90b463302fb;p=dbsrgits%2FDBIx-Class.git is_deferrable patch from Debolaz --- diff --git a/Changes b/Changes index d75bf9b..23d6e46 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,8 @@ Revision history for DBIx::Class + - is_deferable support on relations used by the SQL::Translator + parser (Anders Nor Berle) + 0.08010 2008-03-01 10:30 - Fix t/94versioning.t so it passes with latest SQL::Translator diff --git a/lib/DBIx/Class.pm b/lib/DBIx/Class.pm index 1e9a7bd..b2689df 100644 --- a/lib/DBIx/Class.pm +++ b/lib/DBIx/Class.pm @@ -218,6 +218,8 @@ clkao: CL Kao da5id: David Jack Olrik +debolaz: Anders Nor Berle + dkubb: Dan Kubb dnm: Justin Wheeler diff --git a/lib/DBIx/Class/Relationship/Base.pm b/lib/DBIx/Class/Relationship/Base.pm index 1fbcf52..e64a0e4 100644 --- a/lib/DBIx/Class/Relationship/Base.pm +++ b/lib/DBIx/Class/Relationship/Base.pm @@ -109,6 +109,13 @@ is creating constraints where it shouldn't, or not creating them where it should, set this attribute to a true or false value to override the detection of when to create constraints. +=item is_deferrable + +Tells L that the foreign key constraint it creates should be +deferrable. In other words, the user may request that the constraint be ignored +until the end of the transaction. Currently, only the PostgreSQL producer +actually supports this. + =back =head2 register_relationship diff --git a/lib/SQL/Translator/Parser/DBIx/Class.pm b/lib/SQL/Translator/Parser/DBIx/Class.pm index ac91446..194ae0b 100644 --- a/lib/SQL/Translator/Parser/DBIx/Class.pm +++ b/lib/SQL/Translator/Parser/DBIx/Class.pm @@ -139,6 +139,8 @@ sub parse { $on_update = $otherrelationship->{'attrs'}->{cascade_copy} ? 'CASCADE' : ''; } + my $is_deferrable = $rel_info->{attrs}{is_deferrable} || 0; + # Make sure we dont create the same foreign key constraint twice my $key_test = join("\x00", @keys); @@ -163,7 +165,8 @@ sub parse { reference_fields => \@refkeys, reference_table => $rel_table, on_delete => $on_delete, - on_update => $on_update + on_update => $on_update, + deferrable => $is_deferrable, ); } } diff --git a/t/86sqlt.t b/t/86sqlt.t index ad9c480..079a252 100644 --- a/t/86sqlt.t +++ b/t/86sqlt.t @@ -10,7 +10,7 @@ plan skip_all => 'SQL::Translator required' if $@; my $schema = DBICTest->init_schema; -plan tests => 60; +plan tests => 77; my $translator = SQL::Translator->new( parser_args => { @@ -42,13 +42,13 @@ my %fk_constraints = ( 'display' => 'twokeys->cd', 'selftable' => 'twokeys', 'foreigntable' => 'cd', 'selfcols' => ['cd'], 'foreigncols' => ['cdid'], - on_delete => '', on_update => '', + on_delete => '', on_update => '', deferrable => 0, }, { 'display' => 'twokeys->artist', 'selftable' => 'twokeys', 'foreigntable' => 'artist', 'selfcols' => ['artist'], 'foreigncols' => ['artistid'], - on_delete => 'CASCADE', on_update => 'CASCADE', + on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 0, }, ], @@ -58,14 +58,14 @@ my %fk_constraints = ( 'display' => 'fourkeys_to_twokeys->twokeys', 'selftable' => 'fourkeys_to_twokeys', 'foreigntable' => 'twokeys', 'selfcols' => ['t_artist', 't_cd'], 'foreigncols' => ['artist', 'cd'], - on_delete => 'CASCADE', on_update => 'CASCADE', + on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 0, }, { 'display' => 'fourkeys_to_twokeys->fourkeys', 'selftable' => 'fourkeys_to_twokeys', 'foreigntable' => 'fourkeys', 'selfcols' => [qw(f_foo f_bar f_hello f_goodbye)], 'foreigncols' => [qw(foo bar hello goodbye)], - on_delete => 'CASCADE', on_update => 'CASCADE', + on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 0, }, ], @@ -75,13 +75,13 @@ my %fk_constraints = ( 'display' => 'cd_to_producer->cd', 'selftable' => 'cd_to_producer', 'foreigntable' => 'cd', 'selfcols' => ['cd'], 'foreigncols' => ['cdid'], - on_delete => 'CASCADE', on_update => 'CASCADE', + on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 0, }, { 'display' => 'cd_to_producer->producer', 'selftable' => 'cd_to_producer', 'foreigntable' => 'producer', 'selfcols' => ['producer'], 'foreigncols' => ['producerid'], - on_delete => '', on_update => '', + on_delete => '', on_update => '', deferrable => 0, }, ], @@ -91,13 +91,13 @@ my %fk_constraints = ( 'display' => 'self_ref_alias->self_ref for self_ref', 'selftable' => 'self_ref_alias', 'foreigntable' => 'self_ref', 'selfcols' => ['self_ref'], 'foreigncols' => ['id'], - on_delete => 'CASCADE', on_update => 'CASCADE', + on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 0, }, { 'display' => 'self_ref_alias->self_ref for alias', 'selftable' => 'self_ref_alias', 'foreigntable' => 'self_ref', 'selfcols' => ['alias'], 'foreigncols' => ['id'], - on_delete => '', on_update => '', + on_delete => '', on_update => '', deferrable => 0, }, ], @@ -107,7 +107,7 @@ my %fk_constraints = ( 'display' => 'cd->artist', 'selftable' => 'cd', 'foreigntable' => 'artist', 'selfcols' => ['artist'], 'foreigncols' => ['artistid'], - on_delete => 'CASCADE', on_update => 'CASCADE', + on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1, }, ], @@ -117,13 +117,13 @@ my %fk_constraints = ( 'display' => 'artist_undirected_map->artist for id1', 'selftable' => 'artist_undirected_map', 'foreigntable' => 'artist', 'selfcols' => ['id1'], 'foreigncols' => ['artistid'], - on_delete => 'CASCADE', on_update => '', + on_delete => 'CASCADE', on_update => '', deferrable => 0, }, { 'display' => 'artist_undirected_map->artist for id2', 'selftable' => 'artist_undirected_map', 'foreigntable' => 'artist', 'selfcols' => ['id2'], 'foreigncols' => ['artistid'], - on_delete => 'CASCADE', on_update => '', + on_delete => 'CASCADE', on_update => '', deferrable => 0, }, ], @@ -133,7 +133,7 @@ my %fk_constraints = ( 'display' => 'track->cd', 'selftable' => 'track', 'foreigntable' => 'cd', 'selfcols' => ['cd'], 'foreigncols' => ['cdid'], - on_delete => 'CASCADE', on_update => 'CASCADE', + on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 0, }, ], @@ -143,7 +143,7 @@ my %fk_constraints = ( 'display' => 'treelike->treelike for parent', 'selftable' => 'treelike', 'foreigntable' => 'treelike', 'selfcols' => ['parent'], 'foreigncols' => ['id'], - on_delete => 'CASCADE', on_update => 'CASCADE', + on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 0, }, ], @@ -153,7 +153,7 @@ my %fk_constraints = ( 'display' => 'twokeytreelike->twokeytreelike for parent1,parent2', 'selftable' => 'twokeytreelike', 'foreigntable' => 'twokeytreelike', 'selfcols' => ['parent1', 'parent2'], 'foreigncols' => ['id1','id2'], - on_delete => '', on_update => '', + on_delete => '', on_update => '', deferrable => 0, }, ], @@ -163,7 +163,7 @@ my %fk_constraints = ( 'display' => 'tags->cd', 'selftable' => 'tags', 'foreigntable' => 'cd', 'selfcols' => ['cd'], 'foreigncols' => ['cdid'], - on_delete => 'CASCADE', on_update => 'CASCADE', + on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 0, }, ], @@ -173,7 +173,7 @@ my %fk_constraints = ( 'display' => 'bookmark->link', 'selftable' => 'bookmark', 'foreigntable' => 'link', 'selfcols' => ['link'], 'foreigncols' => ['id'], - on_delete => '', on_update => '', + on_delete => '', on_update => '', deferrable => 0, }, ], # ForceForeign @@ -182,7 +182,7 @@ my %fk_constraints = ( 'display' => 'forceforeign->artist', 'selftable' => 'forceforeign', 'foreigntable' => 'artist', 'selfcols' => ['artist'], 'foreigncols' => ['artist_id'], - on_delete => '', on_update => '', + on_delete => '', on_update => '', deferrable => 0, }, ], @@ -359,4 +359,6 @@ sub test_fk { "on_delete parameter correct for `$desc'" ); is( $got->on_update, $expected->{on_update}, "on_update parameter correct for `$desc'" ); + is( $got->deferrable, $expected->{deferrable}, + "is_deferrable parameter correct for `$desc'" ); } diff --git a/t/lib/DBICTest/Schema/CD.pm b/t/lib/DBICTest/Schema/CD.pm index 7ba727c..2530c7c 100644 --- a/t/lib/DBICTest/Schema/CD.pm +++ b/t/lib/DBICTest/Schema/CD.pm @@ -24,7 +24,7 @@ __PACKAGE__->add_columns( __PACKAGE__->set_primary_key('cdid'); __PACKAGE__->add_unique_constraint([ qw/artist title/ ]); -__PACKAGE__->belongs_to( artist => 'DBICTest::Schema::Artist' ); +__PACKAGE__->belongs_to( artist => 'DBICTest::Schema::Artist', undef, { is_deferrable => 1 } ); __PACKAGE__->has_many( tracks => 'DBICTest::Schema::Track' ); __PACKAGE__->has_many(