From: Luke Saunders Date: Tue, 10 Jun 2008 19:40:42 +0000 (+0000) Subject: allow add_fk_index param to be specified in rel def X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=2581038c9cf626bdf53a518429a1fe3ecbf42603;p=dbsrgits%2FDBIx-Class-Historic.git allow add_fk_index param to be specified in rel def --- diff --git a/lib/DBIx/Class/Relationship/Base.pm b/lib/DBIx/Class/Relationship/Base.pm index 3a95b3c..b8f2467 100644 --- a/lib/DBIx/Class/Relationship/Base.pm +++ b/lib/DBIx/Class/Relationship/Base.pm @@ -116,6 +116,12 @@ 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. +=item add_fk_index + +Tells L to add an index for this constraint. Can also be +specified globally in the args to L or +L. Default is on, set to 0 to disable. + =back =head2 register_relationship diff --git a/lib/SQL/Translator/Parser/DBIx/Class.pm b/lib/SQL/Translator/Parser/DBIx/Class.pm index ad314b2..155d35c 100644 --- a/lib/SQL/Translator/Parser/DBIx/Class.pm +++ b/lib/SQL/Translator/Parser/DBIx/Class.pm @@ -112,6 +112,9 @@ sub parse { my @rels = $source->relationships(); my %created_FK_rels; + + # global add_fk_index set in parser_args + my $add_fk_index = (exists $args->{add_fk_index} && ($args->{add_fk_index} == 0)) ? 0 : 1; foreach my $rel (sort @rels) { @@ -142,6 +145,9 @@ sub parse { } my $is_deferrable = $rel_info->{attrs}{is_deferrable}; + + # global parser_args add_fk_index param can be overridden on the rel def + my $add_fk_index_rel = (exists $rel_info->{attrs}{add_fk_index}) ? $rel_info->{attrs}{add_fk_index} : $add_fk_index; # Make sure we dont create the same foreign key constraint twice my $key_test = join("\x00", @keys); @@ -176,7 +182,7 @@ sub parse { (defined $is_deferrable ? ( deferrable => $is_deferrable ) : ()), ); - unless (exists $args->{add_fk_index} && ($args->{add_fk_index} == 0)) { + if ($add_fk_index_rel) { my $index = $table->add_index( name => join('_', $table->name, 'idx', @keys), fields => \@keys, diff --git a/t/99dbic_sqlt_parser.t b/t/99dbic_sqlt_parser.t index 900e124..34547db 100644 --- a/t/99dbic_sqlt_parser.t +++ b/t/99dbic_sqlt_parser.t @@ -23,6 +23,7 @@ my $schema = DBICTest->init_schema(); my $fk_count = scalar(grep { $_->type eq 'FOREIGN KEY' } $table->get_constraints); my @indices = $table->get_indices; my $index_count = scalar(@indices); + $index_count++ if ($source eq 'TwoKeys'); # TwoKeys has the index turned off on the rel def is($index_count, $fk_count, "correct number of indices for $source with no args"); } } @@ -36,6 +37,7 @@ my $schema = DBICTest->init_schema(); my $fk_count = scalar(grep { $_->type eq 'FOREIGN KEY' } $table->get_constraints); my @indices = $table->get_indices; my $index_count = scalar(@indices); + $index_count++ if ($source eq 'TwoKeys'); # TwoKeys has the index turned off on the rel def is($index_count, $fk_count, "correct number of indices for $source with add_fk_index => 1"); } } diff --git a/t/lib/DBICTest/Schema/TwoKeys.pm b/t/lib/DBICTest/Schema/TwoKeys.pm index b6dedf0..c504ae3 100755 --- a/t/lib/DBICTest/Schema/TwoKeys.pm +++ b/t/lib/DBICTest/Schema/TwoKeys.pm @@ -11,7 +11,7 @@ __PACKAGE__->add_columns( __PACKAGE__->set_primary_key(qw/artist cd/); __PACKAGE__->belongs_to( artist => 'DBICTest::Schema::Artist' ); -__PACKAGE__->belongs_to( cd => 'DBICTest::Schema::CD', undef, { is_deferrable => 0 } ); +__PACKAGE__->belongs_to( cd => 'DBICTest::Schema::CD', undef, { is_deferrable => 0, add_fk_index => 0 } ); __PACKAGE__->has_many( 'fourkeys_to_twokeys', 'DBICTest::Schema::FourKeys_to_TwoKeys', {