[Bast-commits] r4483 - in DBIx-Class/0.08/branches/parser_fk_index: lib/DBIx/Class lib/SQL/Translator/Parser/DBIx t

lukes at dev.catalyst.perl.org lukes at dev.catalyst.perl.org
Tue Jun 10 20:09:24 BST 2008


Author: lukes
Date: 2008-06-10 20:09:24 +0100 (Tue, 10 Jun 2008)
New Revision: 4483

Added:
   DBIx-Class/0.08/branches/parser_fk_index/t/99dbic_sqlt_parser.t
Modified:
   DBIx-Class/0.08/branches/parser_fk_index/lib/DBIx/Class/Schema.pm
   DBIx-Class/0.08/branches/parser_fk_index/lib/SQL/Translator/Parser/DBIx/Class.pm
   DBIx-Class/0.08/branches/parser_fk_index/t/94versioning.t
Log:
added some perldoc

Modified: DBIx-Class/0.08/branches/parser_fk_index/lib/DBIx/Class/Schema.pm
===================================================================
--- DBIx-Class/0.08/branches/parser_fk_index/lib/DBIx/Class/Schema.pm	2008-06-10 16:25:34 UTC (rev 4482)
+++ DBIx-Class/0.08/branches/parser_fk_index/lib/DBIx/Class/Schema.pm	2008-06-10 19:09:24 UTC (rev 4483)
@@ -1028,7 +1028,9 @@
 
 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
 
@@ -1082,6 +1084,8 @@
 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

Modified: DBIx-Class/0.08/branches/parser_fk_index/lib/SQL/Translator/Parser/DBIx/Class.pm
===================================================================
--- DBIx-Class/0.08/branches/parser_fk_index/lib/SQL/Translator/Parser/DBIx/Class.pm	2008-06-10 16:25:34 UTC (rev 4482)
+++ DBIx-Class/0.08/branches/parser_fk_index/lib/SQL/Translator/Parser/DBIx/Class.pm	2008-06-10 19:09:24 UTC (rev 4483)
@@ -23,6 +23,9 @@
 # -------------------------------------------------------------------
 # 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.
 # -------------------------------------------------------------------
@@ -173,15 +176,17 @@
                                     (defined $is_deferrable ? ( deferrable => $is_deferrable ) : ()),
                   );
                     
-                  my $index = $table->add_index(
-                                    name   => join('_', $table->name, 'idx', @keys),
-                                    fields => \@keys,
-                                    type   => 'NORMAL',
-                  );
-                }
+                  unless (exists $args->{add_fk_index} && ($args->{add_fk_index} == 0)) {
+                      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);
         }

Modified: DBIx-Class/0.08/branches/parser_fk_index/t/94versioning.t
===================================================================
--- DBIx-Class/0.08/branches/parser_fk_index/t/94versioning.t	2008-06-10 16:25:34 UTC (rev 4482)
+++ DBIx-Class/0.08/branches/parser_fk_index/t/94versioning.t	2008-06-10 19:09:24 UTC (rev 4483)
@@ -15,9 +15,9 @@
     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 );
 }
 

Added: DBIx-Class/0.08/branches/parser_fk_index/t/99dbic_sqlt_parser.t
===================================================================
--- DBIx-Class/0.08/branches/parser_fk_index/t/99dbic_sqlt_parser.t	                        (rev 0)
+++ DBIx-Class/0.08/branches/parser_fk_index/t/99dbic_sqlt_parser.t	2008-06-10 19:09:24 UTC (rev 4483)
@@ -0,0 +1,72 @@
+#!/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);
+		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);
+		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;
+}




More information about the Bast-commits mailing list