Merge 'trunk' into 'replication_dedux'
John Napiorkowski [Wed, 11 Jun 2008 14:41:07 +0000 (14:41 +0000)]
r13798@dev (orig r4486):  lukes | 2008-06-10 15:08:17 -0500
 r8139@luke-mbp (orig r4482):  lukes | 2008-06-10 17:25:34 +0100
 new branch for making the FK index optional
 r8140@luke-mbp (orig r4483):  lukes | 2008-06-10 20:09:24 +0100
 added some perldoc
 r8141@luke-mbp (orig r4484):  lukes | 2008-06-10 20:40:42 +0100
 allow add_fk_index param to be specified in rel def
 r8142@luke-mbp (orig r4485):  lukes | 2008-06-10 21:07:56 +0100
 fixed failing test

r13800@dev (orig r4488):  castaway | 2008-06-11 07:25:15 -0500
Point at "prefetch" in the get/set cache docs

r13801@dev (orig r4489):  castaway | 2008-06-11 07:54:58 -0500
Add docs to update mentioning scalar refs and discard_changes

lib/DBIx/Class/Relationship/Base.pm
lib/DBIx/Class/ResultSet.pm
lib/DBIx/Class/Row.pm
lib/DBIx/Class/Schema.pm
lib/SQL/Translator/Parser/DBIx/Class.pm
t/86sqlt.t
t/94versioning.t
t/99dbic_sqlt_parser.t [new file with mode: 0644]
t/lib/DBICTest/Schema/TwoKeys.pm

index 3a95b3c..b8f2467 100644 (file)
@@ -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<SQL::Translator> to add an index for this constraint. Can also be
+specified globally in the args to L<DBIx::Class::Schema/deploy> or
+L<DBIx::Class::Schema/create_ddl_dir>. Default is on, set to 0 to disable.
+
 =back
 
 =head2 register_relationship
index af97ebb..bcb27a4 100644 (file)
@@ -22,8 +22,8 @@ DBIx::Class::ResultSet - Responsible for fetching and creating resultset.
 
 =head1 SYNOPSIS
 
-  my $rs   = $schema->resultset('User')->search(registered => 1);
-  my @rows = $schema->resultset('CD')->search(year => 2005);
+  my $rs   = $schema->resultset('User')->search({ registered => 1 });
+  my @rows = $schema->resultset('CD')->search({ year => 2005 })->all();
 
 =head1 DESCRIPTION
 
@@ -53,7 +53,10 @@ In the examples below, the following table classes are used:
 
 =head1 OVERLOADING
 
-If a resultset is used as a number it returns the C<count()>.  However, if it is used as a boolean it is always true.  So if you want to check if a result set has any results use C<if $rs != 0>.  C<if $rs> will always be true.
+If a resultset is used in a numeric context it returns the L</count>.
+However, if it is used in a booleand context it is always true.  So if
+you want to check if a resultset has any results use C<if $rs != 0>.
+C<if $rs> will always be true.
 
 =head1 METHODS
 
@@ -1802,6 +1805,9 @@ sub update_or_create {
 
 Gets the contents of the cache for the resultset, if the cache is set.
 
+The cache is populated either by using the L</prefetch> attribute to
+L</search> or by calling L</set_cache>.
+
 =cut
 
 sub get_cache {
@@ -1823,6 +1829,9 @@ of objects of the same class as those produced by the resultset. Note that
 if the cache is set the resultset will return the cached objects rather
 than re-querying the database even if the cache attr is not set.
 
+The contents of the cache can also be populated by using the
+L</prefetch> attribute to L</search>.
+
 =cut
 
 sub set_cache {
index d2def31..ffe0359 100644 (file)
@@ -295,6 +295,21 @@ C<set_inflated_columns>, which might edit it in place, so dont rely on it being
 the same after a call to C<update>.  If you need to preserve the hashref, it is
 sufficient to pass a shallow copy to C<update>, e.g. ( { %{ $href } } )
 
+If the values passed or any of the column values set on the object
+contain scalar references, eg:
+
+  $obj->last_modified(\'NOW()');
+  # OR
+  $obj->update({ last_modified => \'NOW()' });
+
+The update will pass the values verbatim into SQL. (See
+L<SQL::Abstract> docs).  The values in your Row object will NOT change
+as a result of the update call, if you want the object to be updated
+with the actual values from the database, call L</discard_changes>
+after the update.
+
+  $obj->update()->discard_changes();
+
 =cut
 
 sub update {
index 55cf8fc..7159558 100644 (file)
@@ -1049,7 +1049,9 @@ produced include a DROP TABLE statement for each table created.
 
 Additionally, the DBIx::Class parser accepts a C<sources> parameter as a hash 
 ref or an array ref, containing a list of source to deploy. If present, then 
-only the sources listed will get deployed.
+only the sources listed will get deployed. Furthermore, you can use the
+C<add_fk_index> parser parameter to prevent the parser from creating an index for each
+FK.
 
 =cut
 
@@ -1103,6 +1105,8 @@ override this method in your schema if you would like a different file
 name format. For the ALTER file, the same format is used, replacing
 $version in the name with "$preversion-$version".
 
+See L<DBIx::Class::Schema/deploy> for details of $sqlt_args.
+
 If no arguments are passed, then the following default values are used:
 
 =over 4
index adab8cc..9335509 100644 (file)
@@ -23,6 +23,9 @@ use base qw(Exporter);
 # -------------------------------------------------------------------
 # parse($tr, $data)
 #
+# setting parser_args => { add_fk_index => 0 } will prevent
+# the auto-generation of an index for each FK.
+#
 # Note that $data, in the case of this parser, is not useful.
 # We're working with DBIx::Class Schemas, not data streams.
 # -------------------------------------------------------------------
@@ -109,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)
         {
@@ -143,6 +149,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);
@@ -177,15 +186,17 @@ sub parse {
                                     (defined $is_deferrable ? ( deferrable => $is_deferrable ) : ()),
                   );
                     
-                  my $index = $table->add_index(
-                                    name   => join('_', $table->name, 'idx', @keys),
-                                    fields => \@keys,
-                                    type   => 'NORMAL',
-                  );
-                }
+                  if ($add_fk_index_rel) {
+                      my $index = $table->add_index(
+                                                    name   => join('_', $table->name, 'idx', @keys),
+                                                    fields => \@keys,
+                                                    type   => 'NORMAL',
+                                                    );
+                  }
+              }
             }
         }
-
+               
         if ($source->result_class->can('sqlt_deploy_hook')) {
           $source->result_class->sqlt_deploy_hook($table);
         }
index cf29a88..8211c90 100644 (file)
@@ -10,7 +10,7 @@ plan skip_all => 'SQL::Translator required' if $@;
 
 my $schema = DBICTest->init_schema;
 
-plan tests => 131;
+plan tests => 130;
 
 my $translator = SQL::Translator->new( 
   parser_args => {
@@ -43,6 +43,7 @@ my %fk_constraints = (
       'name' => 'twokeys_fk_cd', 'index_name' => 'twokeys_idx_cd',
       'selftable' => 'twokeys', 'foreigntable' => 'cd', 
       'selfcols'  => ['cd'], 'foreigncols' => ['cdid'], 
+      'noindex'  => 1,
       on_delete => '', on_update => '', deferrable => 0,
     },
     {
@@ -388,8 +389,13 @@ sub test_fk {
       "is_deferrable parameter correct for `$desc'" );
 
   my $index = get_index( $got->table, { fields => $expected->{selfcols} } );
-  ok( defined $index, "index exists for `$desc'" );
-  is( $index->name, $expected->{index_name}, "index has correct name for `$desc'" );
+
+  if ($expected->{noindex}) {
+      ok( !defined $index, "index doesn't for `$desc'" );
+  } else {
+      ok( defined $index, "index exists for `$desc'" );
+      is( $index->name, $expected->{index_name}, "index has correct name for `$desc'" );
+  }
 }
 
 sub test_unique {
index 3667c52..3324745 100644 (file)
@@ -15,9 +15,9 @@ BEGIN {
     unless ($dsn);
 
 
-    eval "use DBD::mysql; use SQL::Translator 0.08;";
+    eval "use DBD::mysql; use SQL::Translator 0.09;";
     plan $@
-        ? ( skip_all => 'needs DBD::mysql and SQL::Translator 0.08 for testing' )
+        ? ( skip_all => 'needs DBD::mysql and SQL::Translator 0.09 for testing' )
         : ( tests => 13 );
 }
 
diff --git a/t/99dbic_sqlt_parser.t b/t/99dbic_sqlt_parser.t
new file mode 100644 (file)
index 0000000..34547db
--- /dev/null
@@ -0,0 +1,74 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+use Test::More;
+use lib qw(t/lib);
+use DBICTest;
+
+BEGIN {
+    eval "use DBD::mysql; use SQL::Translator 0.09;";
+    plan $@
+        ? ( skip_all => 'needs SQL::Translator 0.09 for testing' )
+        : ( tests => 99 );
+}
+
+my $schema = DBICTest->init_schema();
+
+{ 
+       my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { } } });
+
+       foreach my $source ($schema->sources) {
+               my $table = $sqlt_schema->get_table($schema->source($source)->from);
+
+               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");
+       }
+}
+
+{ 
+       my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { add_fk_index => 1 } } });
+
+       foreach my $source ($schema->sources) {
+               my $table = $sqlt_schema->get_table($schema->source($source)->from);
+
+               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");
+       }
+}
+
+{ 
+       my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { add_fk_index => 0 } } });
+
+       foreach my $source ($schema->sources) {
+               my $table = $sqlt_schema->get_table($schema->source($source)->from);
+
+               my @indices = $table->get_indices;
+               my $index_count = scalar(@indices);
+               is($index_count, 0, "correct number of indices for $source with add_fk_index => 0");
+       }
+}
+
+sub create_schema {
+       my $args = shift;
+
+       my $schema = $args->{schema};
+       my $additional_sqltargs = $args->{args} || {};
+
+       my $sqltargs = {
+               add_drop_table => 1, 
+               ignore_constraint_names => 1,
+               ignore_index_names => 1,
+               %{$additional_sqltargs}
+               };
+
+       my $sqlt = SQL::Translator->new( $sqltargs );
+
+       $sqlt->parser('SQL::Translator::Parser::DBIx::Class');
+       return $sqlt->translate({ data => $schema }) or die $sqlt->error;
+}
index beced31..69af2e6 100755 (executable)
@@ -15,7 +15,7 @@ __PACKAGE__->belongs_to(
     {'foreign.artistid'=>'self.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', {