Merge 'trunk' into 'multiple_version_upgrade'
Peter Rabbitson [Tue, 19 Jan 2010 12:47:48 +0000 (12:47 +0000)]
r8307@Thesaurus (orig r8295):  abraxxa | 2010-01-13 17:28:05 +0100
added the sources parser arg to the example code

r8327@Thesaurus (orig r8315):  ribasushi | 2010-01-15 01:25:39 +0100
 r8167@Thesaurus (orig r8155):  ribasushi | 2009-12-19 12:50:13 +0100
 New branch for null-only-result fix
 r8168@Thesaurus (orig r8156):  ribasushi | 2009-12-19 12:51:21 +0100
 Failing test
 r8322@Thesaurus (orig r8310):  ribasushi | 2010-01-15 00:48:09 +0100
 Correct test order
 r8323@Thesaurus (orig r8311):  ribasushi | 2010-01-15 01:15:33 +0100
 Generalize the to-node inner-join-er to apply to all related_resultset calls, not just counts
 r8324@Thesaurus (orig r8312):  ribasushi | 2010-01-15 01:16:05 +0100
 Adjust sql-emitter tests
 r8326@Thesaurus (orig r8314):  ribasushi | 2010-01-15 01:25:10 +0100
 One more sql-test fix and changes

r8328@Thesaurus (orig r8316):  ribasushi | 2010-01-15 01:31:58 +0100
Strict mysql bugfix
r8329@Thesaurus (orig r8317):  ribasushi | 2010-01-15 01:38:53 +0100
Better description of mysql strict option
r8331@Thesaurus (orig r8319):  ribasushi | 2010-01-15 03:12:13 +0100
Update troubleshooting doc
r8337@Thesaurus (orig r8325):  ribasushi | 2010-01-15 17:13:28 +0100
RT52674
r8346@Thesaurus (orig r8334):  ribasushi | 2010-01-17 09:41:49 +0100
No method aliasing in OO code, *ever*
r8373@Thesaurus (orig r8360):  ribasushi | 2010-01-18 11:54:51 +0100
Adjust my email
r8387@Thesaurus (orig r8374):  ribasushi | 2010-01-19 13:07:07 +0100
 r8340@Thesaurus (orig r8328):  abraxxa | 2010-01-15 19:21:20 +0100
 added branch no_duplicate_indexes_for_pk_cols with test and fix

 r8343@Thesaurus (orig r8331):  abraxxa | 2010-01-15 19:32:16 +0100
 don't use eq_set in test

 r8344@Thesaurus (orig r8332):  abraxxa | 2010-01-15 19:44:04 +0100
 don't sort the primary columns because order matters for indexes

 r8345@Thesaurus (orig r8333):  abraxxa | 2010-01-15 19:56:46 +0100
 don't sort the key columns because the order of columns is important for indexes

 r8372@Thesaurus (orig r8359):  abraxxa | 2010-01-18 10:22:09 +0100
 don't sort the columns in the tests either

 r8378@Thesaurus (orig r8365):  abraxxa | 2010-01-18 15:39:28 +0100
 added pod section for parser args

 r8379@Thesaurus (orig r8366):  abraxxa | 2010-01-18 15:53:08 +0100
 better pod thanks to ribasushi

 r8380@Thesaurus (orig r8367):  abraxxa | 2010-01-18 16:04:34 +0100
 test and pod fixes

 r8383@Thesaurus (orig r8370):  abraxxa | 2010-01-19 12:38:44 +0100
 fixed Authors section
 added License section
 fixed t/86sqlt.t tests

 r8384@Thesaurus (orig r8371):  ribasushi | 2010-01-19 12:59:52 +0100
 Regenaretd under new parser
 r8385@Thesaurus (orig r8372):  ribasushi | 2010-01-19 13:03:51 +0100
 Minor style change and white space trim
 r8386@Thesaurus (orig r8373):  ribasushi | 2010-01-19 13:06:54 +0100
 Changes abraxxa++

r8390@Thesaurus (orig r8377):  ribasushi | 2010-01-19 13:41:03 +0100
Some minor test refactor and tab cleanups

25 files changed:
Changes
lib/DBIx/Class.pm
lib/DBIx/Class/Manual/Troubleshooting.pod
lib/DBIx/Class/Relationship/ManyToMany.pm
lib/DBIx/Class/ResultSet.pm
lib/DBIx/Class/ResultSourceProxy.pm
lib/DBIx/Class/Row.pm
lib/DBIx/Class/Schema/Versioned.pm
lib/DBIx/Class/Storage/DBI/AmbiguousGlob.pm
lib/DBIx/Class/Storage/DBI/Oracle/Generic.pm
lib/DBIx/Class/Storage/DBI/Replicated.pm
lib/DBIx/Class/Storage/DBI/Replicated/Pool.pm
lib/DBIx/Class/Storage/DBI/mysql.pm
lib/SQL/Translator/Parser/DBIx/Class.pm
t/71mysql.t
t/86sqlt.t
t/94versioning.t
t/99dbic_sqlt_parser.t
t/inflate/hri.t
t/lib/DBICTest.pm
t/lib/sqlite.sql
t/prefetch/double_prefetch.t
t/relationship/core.t
t/resultset/nulls_only.t [new file with mode: 0644]
t/search/related_strip_prefetch.t

diff --git a/Changes b/Changes
index cf08609..45462eb 100644 (file)
--- a/Changes
+++ b/Changes
@@ -9,10 +9,16 @@ Revision history for DBIx::Class
         - Cookbook POD fix for add_drop_table instead of add_drop_tables
         - Views without a view_definition will throw an exception when
           parsed by SQL::Translator::Parser::DBIx::Class
+        - Stop the SQLT parser from auto-adding indexes identical to the
+          Primary Key
         - Schema POD improvement for dclone
         - Fix regression in context sensitiveness of deployment_statements
         - Fix regression resulting in overcomplicated query on
           search_related from prefetching resultsets
+        - Fix regression on all-null returning searches (properly switch
+          LEFT JOIN to JOIN in order to distinguish between both cases)
+        - Fix regression in groupedresultset count() used on strict-mode
+          MySQL connections
         - Better isolation of RNO-limited queries from the rest of a
           prefetching resultset
         - New MSSQL specific resultset attribute to allow hacky ordered
index d5e742c..4298620 100644 (file)
@@ -319,7 +319,7 @@ rbuels: Robert Buels <rmb32@cornell.edu>
 
 rdj: Ryan D Johnson <ryan@innerfence.com>
 
-ribasushi: Peter Rabbitson <rabbit+dbic@rabbit.us>
+ribasushi: Peter Rabbitson <ribasushi@cpan.org>
 
 rjbs: Ricardo Signes <rjbs@cpan.org>
 
index 56bcc01..5d46805 100644 (file)
@@ -100,28 +100,20 @@ The solution is to enable quoting - see
 L<DBIx::Class::Manual::Cookbook/Setting_quoting_for_the_generated_SQL> for
 details.
 
-Note that quoting may lead to problems with C<order_by> clauses, see
-L<... column "foo DESC" does not exist ...> for info on avoiding those.
-
 =head2 column "foo DESC" does not exist ...
 
-This can happen if you've turned on quoting and then done something like
-this:
+This can happen if you are still using the obsolete order hack, and also
+happen to turn on sql-quoting.
 
   $rs->search( {}, { order_by => [ 'name DESC' ] } );
 
-This results in SQL like this:
-
-  ... ORDER BY "name DESC"
-
-The solution is to pass your order_by items as scalar references to avoid
-quoting:
-
-  $rs->search( {}, { order_by => [ \'name DESC' ] } );
+Since L<DBIx::Class> >= 0.08100 and L<SQL::Abstract> >= 1.50 the above
+should be written as:
 
-Now you'll get SQL like this:
+  $rs->search( {}, { order_by => { -desc => 'name' } } );
 
-  ... ORDER BY name DESC
+For more ways to express order clauses refer to
+L<SQL::Abstract/ORDER_BY_CLAUSES>
 
 =head2 Perl Performance Issues on Red Hat Systems
 
index 07a244a..137fb30 100644 (file)
@@ -64,15 +64,15 @@ EOW
       my $rs = $self->search_related($rel)->search_related(
         $f_rel, @_ > 0 ? @_ : undef, { %{$rel_attrs||{}}, %$attrs }
       );
-         return $rs;
+      return $rs;
     };
 
     my $meth_name = join '::', $class, $meth;
     *$meth_name = Sub::Name::subname $meth_name, sub {
-               my $self = shift;
-               my $rs = $self->$rs_meth( @_ );
-               return (wantarray ? $rs->all : $rs);
-       };
+      my $self = shift;
+      my $rs = $self->$rs_meth( @_ );
+      return (wantarray ? $rs->all : $rs);
+    };
 
     my $add_meth_name = join '::', $class, $add_meth;
     *$add_meth_name = Sub::Name::subname $add_meth_name, sub {
@@ -102,7 +102,7 @@ EOW
       my $link = $self->search_related($rel)->new_result($link_vals);
       $link->set_from_related($f_rel, $obj);
       $link->insert();
-         return $obj;
+      return $obj;
     };
 
     my $set_meth_name = join '::', $class, $set_meth;
index baf6be2..5827fb4 100644 (file)
@@ -974,19 +974,6 @@ sub _construct_object {
 sub _collapse_result {
   my ($self, $as_proto, $row) = @_;
 
-  # if the first row that ever came in is totally empty - this means we got
-  # hit by a smooth^Wempty left-joined resultset. Just noop in that case
-  # instead of producing a {}
-  #
-  my $has_def;
-  for (@$row) {
-    if (defined $_) {
-      $has_def++;
-      last;
-    }
-  }
-  return undef unless $has_def;
-
   my @copy = @$row;
 
   # 'foo'         => [ undef, 'foo' ]
@@ -1247,11 +1234,6 @@ sub _count_rs {
   $tmp_attrs->{select} = $rsrc->storage->_count_select ($rsrc, $tmp_attrs);
   $tmp_attrs->{as} = 'count';
 
-  # read the comment on top of the actual function to see what this does
-  $tmp_attrs->{from} = $self->result_source->schema->storage->_straight_join_to_node (
-    $tmp_attrs->{from}, $tmp_attrs->{alias}
-  );
-
   my $tmp_rs = $rsrc->resultset_class->new($rsrc, $tmp_attrs)->get_column ('count');
 
   return $tmp_rs;
@@ -1279,11 +1261,6 @@ sub _count_subq_rs {
 
   $sub_attrs->{select} = $rsrc->storage->_subq_count_select ($rsrc, $sub_attrs);
 
-  # read the comment on top of the actual function to see what this does
-  $sub_attrs->{from} = $self->result_source->schema->storage->_straight_join_to_node (
-    $sub_attrs->{from}, $sub_attrs->{alias}
-  );
-
   # this is so that the query can be simplified e.g.
   # * non-limiting joins can be pruned
   # * ordering can be thrown away in things like Top limit
@@ -2510,10 +2487,11 @@ sub related_resultset {
 
   $self->{related_resultsets} ||= {};
   return $self->{related_resultsets}{$rel} ||= do {
-    my $rel_info = $self->result_source->relationship_info($rel);
+    my $rsrc = $self->result_source;
+    my $rel_info = $rsrc->relationship_info($rel);
 
     $self->throw_exception(
-      "search_related: result source '" . $self->result_source->source_name .
+      "search_related: result source '" . $rsrc->source_name .
         "' has no such relationship $rel")
       unless $rel_info;
 
@@ -2524,6 +2502,13 @@ sub related_resultset {
     my $alias = $self->result_source->storage
         ->relname_to_table_alias($rel, $join_count);
 
+    # since this is search_related, and we already slid the select window inwards
+    # (the select/as attrs were deleted in the beginning), we need to flip all 
+    # left joins to inner, so we get the expected results
+    # read the comment on top of the actual function to see what this does
+    $attrs->{from} = $rsrc->schema->storage->_straight_join_to_node ($attrs->{from}, $alias);
+
+
     #XXX - temp fix for result_class bug. There likely is a more elegant fix -groditi
     delete @{$attrs}{qw(result_class alias)};
 
@@ -2536,7 +2521,7 @@ sub related_resultset {
       }
     }
 
-    my $rel_source = $self->result_source->related_source($rel);
+    my $rel_source = $rsrc->related_source($rel);
 
     my $new = do {
 
@@ -2687,7 +2672,6 @@ sub _chain_relationship {
   # the join in question so we could tell it *is* the search_related)
   my $already_joined;
 
-
   # we consider the last one thus reverse
   for my $j (reverse @requested_joins) {
     if ($rel eq $j->[0]{-join_path}[-1]) {
@@ -2696,7 +2680,6 @@ sub _chain_relationship {
       last;
     }
   }
-
 # alternative way to scan the entire chain - not backwards compatible
 #  for my $j (reverse @$from) {
 #    next unless ref $j eq 'ARRAY';
index 696c9a5..ffef623 100644 (file)
@@ -41,7 +41,9 @@ sub add_columns {
   }
 }
 
-*add_column = \&add_columns;
+sub add_column {
+  shift->add_columns(@_);
+}
 
 sub has_column {
   shift->result_source_instance->has_column(@_);
index ac496f8..60e4776 100644 (file)
@@ -527,7 +527,9 @@ attempt is made to delete all the related objects as well. To turn
 this behaviour off, pass C<< cascade_delete => 0 >> in the C<$attr>
 hashref of the relationship, see L<DBIx::Class::Relationship>. Any
 database-level cascade or restrict will take precedence over a
-DBIx-Class-based cascading delete.
+DBIx-Class-based cascading delete, since DBIx-Class B<deletes the
+main row first> and only then attempts to delete any remaining related
+rows.
 
 If you delete an object within a txn_do() (see L<DBIx::Class::Storage/txn_do>)
 and the transaction subsequently fails, the row object will remain marked as
index c7a89d7..929e79b 100644 (file)
@@ -268,7 +268,7 @@ and the schema_version which is retrieved via $self->schema_version
 =cut
 
 sub create_upgrade_path {
-       ## override this method
+  ## override this method
 }
 
 =head2 ordered_schema_versions
@@ -418,7 +418,7 @@ sub upgrade_single_step
     return;
   }
 
-  carp "\nDB version ($db_version) is lower than the schema version (".$self->schema_version."). Attempting upgrade.\n";
+  carp "DB version ($db_version) is lower than the schema version (".$self->schema_version."). Attempting upgrade.\n";
 
   # backup if necessary then apply upgrade
   $self->_filedata($self->_read_sql_file($upgrade_file));
@@ -488,7 +488,7 @@ differently.
 sub apply_statement {
     my ($self, $statement) = @_;
 
-    $self->storage->dbh->do($_) or carp "SQL was:\n $_";
+    $self->storage->dbh->do($_) or carp "SQL was: $_";
 }
 
 =head2 get_db_version
@@ -599,7 +599,7 @@ sub _on_connect
         return 1;
     }
 
-  carp "Versions out of sync. This is " . $self->schema_version . 
+  carp "Versions out of sync. This is " . $self->schema_version .
     ", your database contains version $pversion, please call upgrade on your Schema.\n";
 }
 
index d883d0b..2008c54 100644 (file)
@@ -27,6 +27,9 @@ At this point the only overriden method is C<_subq_count_select()>
 
 sub _subq_count_select {
   my ($self, $source, $rs_attrs) = @_;
+
+  return $rs_attrs->{group_by} if $rs_attrs->{group_by};
+
   my @pcols = map { join '.', $rs_attrs->{alias}, $_ } ($source->primary_columns);
   return @pcols ? \@pcols : [ 1 ];
 }
index fe81851..55cad0e 100644 (file)
@@ -209,11 +209,15 @@ sub connect_call_datetime_setup {
   my $timestamp_tz_format = $ENV{NLS_TIMESTAMP_TZ_FORMAT} ||=
     'YYYY-MM-DD HH24:MI:SS.FF TZHTZM';
 
-  $self->_do_query("alter session set nls_date_format = '$date_format'");
   $self->_do_query(
-"alter session set nls_timestamp_format = '$timestamp_format'");
+    "alter session set nls_date_format = '$date_format'"
+  );
   $self->_do_query(
-"alter session set nls_timestamp_tz_format='$timestamp_tz_format'");
+    "alter session set nls_timestamp_format = '$timestamp_format'"
+  );
+  $self->_do_query(
+    "alter session set nls_timestamp_tz_format='$timestamp_tz_format'"
+  );
 }
 
 =head2 source_bind_attributes
@@ -235,35 +239,35 @@ table with more than one LOB column.
 
 sub source_bind_attributes
 {
-       require DBD::Oracle;
-       my $self = shift;
-       my($source) = @_;
+  require DBD::Oracle;
+  my $self = shift;
+  my($source) = @_;
 
-       my %bind_attributes;
+  my %bind_attributes;
 
-       foreach my $column ($source->columns) {
-               my $data_type = $source->column_info($column)->{data_type} || '';
-               next unless $data_type;
+  foreach my $column ($source->columns) {
+    my $data_type = $source->column_info($column)->{data_type} || '';
+    next unless $data_type;
 
-               my %column_bind_attrs = $self->bind_attribute_by_data_type($data_type);
+    my %column_bind_attrs = $self->bind_attribute_by_data_type($data_type);
 
-               if ($data_type =~ /^[BC]LOB$/i) {
-                       $column_bind_attrs{'ora_type'} = uc($data_type) eq 'CLOB' ?
-                               DBD::Oracle::ORA_CLOB() :
-                               DBD::Oracle::ORA_BLOB();
-                       $column_bind_attrs{'ora_field'} = $column;
-               }
+    if ($data_type =~ /^[BC]LOB$/i) {
+      $column_bind_attrs{'ora_type'} = uc($data_type) eq 'CLOB'
+        ? DBD::Oracle::ORA_CLOB()
+        : DBD::Oracle::ORA_BLOB()
+      ;
+      $column_bind_attrs{'ora_field'} = $column;
+    }
 
-               $bind_attributes{$column} = \%column_bind_attrs;
-       }
+    $bind_attributes{$column} = \%column_bind_attrs;
+  }
 
-       return \%bind_attributes;
+  return \%bind_attributes;
 }
 
 sub _svp_begin {
-    my ($self, $name) = @_;
-
-    $self->_get_dbh->do("SAVEPOINT $name");
+  my ($self, $name) = @_;
+  $self->_get_dbh->do("SAVEPOINT $name");
 }
 
 # Oracle automatically releases a savepoint when you start another one with the
@@ -271,9 +275,8 @@ sub _svp_begin {
 sub _svp_release { 1 }
 
 sub _svp_rollback {
-    my ($self, $name) = @_;
-
-    $self->_get_dbh->do("ROLLBACK TO SAVEPOINT $name")
+  my ($self, $name) = @_;
+  $self->_get_dbh->do("ROLLBACK TO SAVEPOINT $name")
 }
 
 =head2 relname_to_table_alias
index 200483d..f8958da 100644 (file)
@@ -409,7 +409,7 @@ bits get put into the correct places.
 =cut
 
 sub BUILDARGS {
-  my ($class, $schema, $storage_type_args, @args) = @_;        
+  my ($class, $schema, $storage_type_args, @args) = @_;  
 
   return {
     schema=>$schema,
index a7a1dfa..a496512 100644 (file)
@@ -280,16 +280,15 @@ sub _safely {
 
   eval {
     $code->()
-  }; 
+  };
   if ($@) {
-    $replicant
-      ->debugobj
-      ->print(
-        sprintf( "Exception trying to $name for replicant %s, error is %s",
-          $replicant->_dbi_connect_info->[0], $@)
-        );
-       return;
+    $replicant->debugobj->print(sprintf(
+      "Exception trying to $name for replicant %s, error is %s",
+      $replicant->_dbi_connect_info->[0], $@)
+    );
+    return undef;
   }
+
   return 1;
 }
 
index 9fa6d31..20e4f7f 100644 (file)
@@ -106,6 +106,19 @@ It also provides a one-stop on-connect macro C<set_strict_mode> which sets
 session variables such that MySQL behaves more predictably as far as the
 SQL standard is concerned.
 
+=head1 STORAGE OPTIONS
+
+=head2 set_strict_mode
+
+Enables session-wide strict options upon connecting. Equivalent to:
+
+  ->connect ( ... , {
+    on_connect_do => [
+      q|SET SQL_MODE = CONCAT('ANSI,TRADITIONAL,ONLY_FULL_GROUP_BY,', @@sql_mode)|,
+      q|SET SQL_AUTO_IS_NULL = 0|,
+    ]
+  });
+
 =head1 AUTHORS
 
 See L<DBIx::Class/CONTRIBUTORS>
index 5d146f8..2efc5b1 100644 (file)
@@ -206,8 +206,7 @@ sub parse {
                 }
             }
 
-            if($rel_table)
-            {
+            if($rel_table) {
                 # Constraints are added only if applicable
                 next unless $fk_constraint;
 
@@ -216,7 +215,6 @@ sub parse {
                 next if $created_FK_rels{$rel_table}->{$key_test};
 
                 if (scalar(@keys)) {
-
                   $created_FK_rels{$rel_table}->{$key_test} = 1;
 
                   my $is_deferrable = $rel_info->{attrs}{is_deferrable};
@@ -228,25 +226,33 @@ sub parse {
                   }
 
                   $table->add_constraint(
-                                    type             => 'foreign_key',
-                                    name             => join('_', $table_name, 'fk', @keys),
-                                    fields           => \@keys,
-                                    reference_fields => \@refkeys,
-                                    reference_table  => $rel_table,
-                                    on_delete        => uc ($cascade->{delete} || ''),
-                                    on_update        => uc ($cascade->{update} || ''),
-                                    (defined $is_deferrable ? ( deferrable => $is_deferrable ) : ()),
+                    type             => 'foreign_key',
+                    name             => join('_', $table_name, 'fk', @keys),
+                    fields           => \@keys,
+                    reference_fields => \@refkeys,
+                    reference_table  => $rel_table,
+                    on_delete        => uc ($cascade->{delete} || ''),
+                    on_update        => uc ($cascade->{update} || ''),
+                    (defined $is_deferrable ? ( deferrable => $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;
 
+                  # Check that we do not create an index identical to the PK index
+                  # (some RDBMS croak on this, and it generally doesn't make much sense)
+                  # NOTE: we do not sort the key columns because the order of
+                  # columns is important for indexes and two indexes with the
+                  # same cols but different order are allowed and sometimes
+                  # needed
+                  next if join("\x00", @keys) eq join("\x00", @primary);
+
                   if ($add_fk_index_rel) {
                       my $index = $table->add_index(
-                                                    name   => join('_', $table_name, 'idx', @keys),
-                                                    fields => \@keys,
-                                                    type   => 'NORMAL',
-                                                    );
+                          name   => join('_', $table_name, 'idx', @keys),
+                          fields => \@keys,
+                          type   => 'NORMAL',
+                      );
                   }
               }
             }
@@ -379,7 +385,14 @@ from a DBIx::Class::Schema instance
  my $schema = MyApp::Schema->connect;
  my $trans  = SQL::Translator->new (
       parser      => 'SQL::Translator::Parser::DBIx::Class',
-      parser_args => { package => $schema },
+      parser_args => {
+          package => $schema,
+          add_fk_index => 0,
+          sources => [qw/
+            Artist
+            CD
+          /],
+      },
       producer    => 'SQLite',
      ) or die SQL::Translator->error;
  my $out = $trans->translate() or die $trans->error;
@@ -401,14 +414,34 @@ other machines that need to have your application installed but don't
 have SQL::Translator installed. To do this see
 L<DBIx::Class::Schema/create_ddl_dir>.
 
+=head1 PARSER OPTIONS
+
+=head2 add_fk_index
+
+Create an index for each foreign key.
+Enabled by default, as having indexed foreign key columns is normally the
+sensible thing to do.
+
+=head2 sources
+
+=over 4
+
+=item Arguments: \@class_names
+
+=back
+
+Limit the amount of parsed sources by supplying an explicit list of source names.
+
 =head1 SEE ALSO
 
 L<SQL::Translator>, L<DBIx::Class::Schema>
 
 =head1 AUTHORS
 
-Jess Robinson
+See L<DBIx::Class/CONTRIBUTORS>.
+
+=head1 LICENSE
 
-Matt S Trout
+You may distribute this code under the same terms as Perl itself.
 
-Ash Berlin
+=cut
index aa2db86..b51947c 100644 (file)
@@ -225,6 +225,23 @@ NULLINSEARCH: {
       => 'Nothing Found!';
 }
 
+# check for proper grouped counts
+{
+  my $ansi_schema = DBICTest::Schema->connect ($dsn, $user, $pass, { on_connect_call => 'set_strict_mode' });
+  my $rs = $ansi_schema->resultset('CD');
+
+  my $years;
+  $years->{$_->year|| scalar keys %$years}++ for $rs->all;  # NULL != NULL, thus the keys eval
+
+  lives_ok ( sub {
+    is (
+      $rs->search ({}, { group_by => 'year'})->count,
+      scalar keys %$years,
+      'grouped count correct',
+    );
+  }, 'Grouped count does not throw');
+}
+
 ZEROINSEARCH: {
   my $cds_per_year = {
     2001 => 2,
index e710ec1..26e1fc2 100644 (file)
@@ -269,6 +269,7 @@ my %fk_constraints = (
       'name' => 'forceforeign_fk_artist', 'index_name' => 'forceforeign_idx_artist',
       'selftable' => 'forceforeign', 'foreigntable' => 'artist', 
       'selfcols'  => ['artist'], 'foreigncols' => ['artistid'], 
+      'noindex'  => 1,
       on_delete => '', on_update => '', deferrable => 1,
     },
   ],
@@ -464,21 +465,21 @@ sub test_fk {
   my ($expected, $got) = @_;
   my $desc = $expected->{display};
   is( $got->name, $expected->{name},
-      "name parameter correct for `$desc'" );
+      "name parameter correct for '$desc'" );
   is( $got->on_delete, $expected->{on_delete},
-      "on_delete parameter correct for `$desc'" );
+      "on_delete parameter correct for '$desc'" );
   is( $got->on_update, $expected->{on_update},
-      "on_update parameter correct for `$desc'" );
+      "on_update parameter correct for '$desc'" );
   is( $got->deferrable, $expected->{deferrable},
-      "is_deferrable parameter correct for `$desc'" );
+      "is_deferrable parameter correct for '$desc'" );
 
   my $index = get_index( $got->table, { fields => $expected->{selfcols} } );
 
   if ($expected->{noindex}) {
-      ok( !defined $index, "index doesn't for `$desc'" );
+      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'" );
+      ok( defined $index, "index exists for '$desc'" );
+      is( $index->name, $expected->{index_name}, "index has correct name for '$desc'" );
   }
 }
 
@@ -486,7 +487,7 @@ sub test_unique {
   my ($expected, $got) = @_;
   my $desc = $expected->{display};
   is( $got->name, $expected->{name},
-      "name parameter correct for `$desc'" );
+      "name parameter correct for '$desc'" );
 }
 
 done_testing;
index 4d3c780..f901965 100644 (file)
@@ -3,7 +3,10 @@
 use strict;
 use warnings;
 use Test::More;
-use File::Spec;
+use Test::Warn;
+use Test::Exception;
+
+use Path::Class;
 use File::Copy;
 
 #warn "$dsn $user $pass";
@@ -28,13 +31,13 @@ BEGIN {
 my $version_table_name = 'dbix_class_schema_versions';
 my $old_table_name = 'SchemaVersions';
 
-my $ddl_dir = File::Spec->catdir ('t', 'var');
+my $ddl_dir = dir ('t', 'var');
 my $fn = {
-    v1 => File::Spec->catfile($ddl_dir, 'DBICVersion-Schema-1.0-MySQL.sql'),
-    v2 => File::Spec->catfile($ddl_dir, 'DBICVersion-Schema-2.0-MySQL.sql'),
-    v3 => File::Spec->catfile($ddl_dir, 'DBICVersion-Schema-3.0-MySQL.sql'),
-    trans_v12 => File::Spec->catfile($ddl_dir, 'DBICVersion-Schema-1.0-2.0-MySQL.sql'),
-    trans_v23 => File::Spec->catfile($ddl_dir, 'DBICVersion-Schema-2.0-3.0-MySQL.sql'),
+    v1 => $ddl_dir->file ('DBICVersion-Schema-1.0-MySQL.sql'),
+    v2 => $ddl_dir->file ('DBICVersion-Schema-2.0-MySQL.sql'),
+    v3 => $ddl_dir->file ('DBICVersion-Schema-3.0-MySQL.sql'),
+    trans_v12 => $ddl_dir-> ('DBICVersion-Schema-1.0-2.0-MySQL.sql'),
+    trans_v23 => $ddl_dir-> ('DBICVersion-Schema-2.0-3.0-MySQL.sql'),
 };
 
 use lib qw(t/lib);
@@ -70,58 +73,46 @@ my $schema_v2 = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_versio
   $schema_v2->create_ddl_dir('MySQL', '2.0', $ddl_dir, '1.0');
   ok(-f $fn->{trans_v12}, 'Created DDL file');
 
-  {
-    my $w;
-    local $SIG{__WARN__} = sub { $w = shift };
-
-    $schema_v2->upgrade();
-    like ($w, qr/Attempting upgrade\.$/, 'Warn before upgrade');
-  }
+  sleep 1;    # remove this when TODO below is completed
+  warnings_like (
+    sub { $schema_v2->upgrade() },
+    qr/DB version .+? is lower than the schema version/,
+    'Warn before upgrade',
+  );
 
   is($schema_v2->get_db_version(), '2.0', 'db version number upgraded');
 
-  eval {
+  lives_ok ( sub {
     $schema_v2->storage->dbh->do('select NewVersionName from TestVersion');
-  };
-  is($@, '', 'new column created');
-
-  # should overwrite files and warn about it
-  my @w;
-  local $SIG{__WARN__} = sub { 
-    if ($_[0] =~ /Overwriting existing/) {
-      push @w, $_[0];
-    }
-    else {
-      warn @_;
-    }
-  };
-  $schema_v2->create_ddl_dir('MySQL', '2.0', $ddl_dir, '1.0');
-
-  is (2, @w, 'A warning generated for both the DDL and the diff');
-  like ($w[0], qr/Overwriting existing DDL file - $fn->{v2}/, 'New version DDL overwrite warning');
-  like ($w[1], qr/Overwriting existing diff file - $fn->{trans_v12}/, 'Upgrade diff overwrite warning');
+  }, 'new column created' );
+
+  warnings_exist (
+    sub { $schema_v2->create_ddl_dir('MySQL', '2.0', $ddl_dir, '1.0') },
+    [
+      qr/Overwriting existing DDL file - $fn->{v2}/,
+      qr/Overwriting existing diff file - $fn->{trans}/,
+    ],
+    'An overwrite warning generated for both the DDL and the diff',
+  );
 }
 
 {
   my $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass);
-  eval {
+  lives_ok (sub {
     $schema_version->storage->dbh->do('select * from ' . $version_table_name);
-  };
-  is($@, '', 'version table exists');
+  }, 'version table exists');
 
-  eval {
+  lives_ok (sub {
     $schema_version->storage->dbh->do("DROP TABLE IF EXISTS $old_table_name");
     $schema_version->storage->dbh->do("RENAME TABLE $version_table_name TO $old_table_name");
-  };
-  is($@, '', 'versions table renamed to old style table');
+  }, 'versions table renamed to old style table');
 
   $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass);
   is($schema_version->get_db_version, '2.0', 'transition from old table name to new okay');
 
-  eval {
+  dies_ok (sub {
     $schema_version->storage->dbh->do('select * from ' . $old_table_name);
-  };
-  ok($@, 'old version table gone');
+  }, 'old version table gone');
 
 }
 
@@ -190,28 +181,23 @@ my $schema_v3 = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_versio
   };
 
 
-  my $warn = '';
-  local $SIG{__WARN__} = sub { $warn = shift };
-  $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass);
-  like($warn, qr/Your DB is currently unversioned/, 'warning detected without env var or attr');
+  warnings_like ( sub {
+    $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass);
+  }, qr/Your DB is currently unversioned/, 'warning detected without env var or attr' );
 
+  warnings_like ( sub {
+    $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 1 });
+  },  [], 'warning not detected with attr set');
 
-  # should warn
-  $warn = '';
-  $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 1 });
-  is($warn, '', 'warning not detected with attr set');
-  # should not warn
 
   local $ENV{DBIC_NO_VERSION_CHECK} = 1;
-  $warn = '';
-  $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass);
-  is($warn, '', 'warning not detected with env var set');
-  # should not warn
+  warnings_like ( sub {
+    $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass);
+  }, [], 'warning not detected with env var set');
 
-  $warn = '';
-  $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 0 });
-  like($warn, qr/Your DB is currently unversioned/, 'warning detected without env var or attr');
-  # should warn
+  warnings_like ( sub {
+    $schema_version = DBICVersion::Schema->connect($dsn, $user, $pass, { ignore_version => 0 });
+  }, qr/Your DB is currently unversioned/, 'warning detected without env var or attr');
 }
 
 # attempt a deploy/upgrade cycle within one second
index 4d1ddac..628f3cf 100644 (file)
@@ -35,35 +35,50 @@ my @sources = grep
   $schema->sources
 ;
 
-{ 
+my $idx_exceptions = {
+    'Artwork'       => -1,
+    'ForceForeign'  => -1,
+    'LinerNotes'    => -1,
+    'TwoKeys'       => -1, # TwoKeys has the index turned off on the rel def
+};
+
+{
   my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { } } });
 
-  foreach my $source (@sources) {
-    my $table = get_table($sqlt_schema, $schema, $source);
+  foreach my $source_name (@sources) {
+    my $table = get_table($sqlt_schema, $schema, $source_name);
 
     my $fk_count = scalar(grep { $_->type eq 'FOREIGN KEY' } $table->get_constraints);
+    $fk_count += $idx_exceptions->{$source_name} || 0;
     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");
+    is($index_count, $fk_count, "correct number of indices for $source_name with no args");
+
+    for my $index (@indices) {
+        my $source = $schema->source($source_name);
+        my $pk_test = join("\x00", $source->primary_columns);
+        my $idx_test = join("\x00", $index->fields);
+        isnt ( $pk_test, $idx_test, "no additional index for the primary columns exists in $source_name");
+    }
   }
 }
 
-{ 
+{
   my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { add_fk_index => 1 } } });
 
-  foreach my $source (@sources) {
-    my $table = get_table($sqlt_schema, $schema, $source);
+  foreach my $source_name (@sources) {
+    my $table = get_table($sqlt_schema, $schema, $source_name);
 
     my $fk_count = scalar(grep { $_->type eq 'FOREIGN KEY' } $table->get_constraints);
+    $fk_count += $idx_exceptions->{$source_name} || 0;
     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");
+    is($index_count, $fk_count, "correct number of indices for $source_name with add_fk_index => 1");
   }
 }
 
-{ 
+{
   my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { add_fk_index => 0 } } });
 
   foreach my $source (@sources) {
@@ -75,7 +90,7 @@ my @sources = grep
   }
 }
 
-{ 
+{
     {
         package # hide from PAUSE
             DBICTest::Schema::NoViewDefinition;
index 292c943..69fa4ff 100644 (file)
@@ -45,7 +45,7 @@ sub check_cols_of {
             my @dbic_reltable = $dbic_obj->$col;
             my @hashref_reltable = @{$datahashref->{$col}};
   
-            is (scalar @hashref_reltable, scalar @dbic_reltable, 'number of related entries');
+            is (scalar @dbic_reltable, scalar @hashref_reltable, 'number of related entries');
 
             # for my $index (0..scalar @hashref_reltable) {
             for my $index (0..scalar @dbic_reltable) {
index 66a79e8..8006961 100644 (file)
@@ -32,7 +32,7 @@ DBIx::Class.
     no_populate=>1,
     storage_type=>'::DBI::Replicated',
     storage_type_args=>{
-       balancer_type=>'DBIx::Class::Storage::DBI::Replicated::Balancer::Random'
+      balancer_type=>'DBIx::Class::Storage::DBI::Replicated::Balancer::Random'
     },
   );
 
@@ -48,7 +48,7 @@ default, unless the no_deploy or no_populate flags are set.
 =cut
 
 sub has_custom_dsn {
-       return $ENV{"DBICTEST_DSN"} ? 1:0;
+    return $ENV{"DBICTEST_DSN"} ? 1:0;
 }
 
 sub _sqlite_dbfilename {
@@ -59,7 +59,7 @@ sub _sqlite_dbname {
     my $self = shift;
     my %args = @_;
     return $self->_sqlite_dbfilename if $args{sqlite_use_file} or $ENV{"DBICTEST_SQLITE_USE_FILE"};
-       return ":memory:";
+    return ":memory:";
 }
 
 sub _database {
@@ -85,7 +85,7 @@ sub init_schema {
     my %args = @_;
 
     my $schema;
-    
+
     if ($args{compose_connection}) {
       $schema = DBICTest::Schema->compose_connection(
                   'DBICTest', $self->_database(%args)
@@ -94,8 +94,8 @@ sub init_schema {
       $schema = DBICTest::Schema->compose_namespace('DBICTest');
     }
     if( $args{storage_type}) {
-       $schema->storage_type($args{storage_type});
-    }    
+      $schema->storage_type($args{storage_type});
+    }
     if ( !$args{no_connect} ) {
       $schema = $schema->connect($self->_database(%args));
       $schema->storage->on_connect_do(['PRAGMA synchronous = OFF'])
index cfb4e33..a4836f5 100644 (file)
@@ -1,6 +1,6 @@
 -- 
 -- Created by SQL::Translator::Producer::SQLite
--- Created on Sun Nov 15 14:13:02 2009
+-- Created on Tue Jan 19 12:46:12 2010
 -- 
 
 
@@ -262,8 +262,6 @@ CREATE TABLE forceforeign (
   cd integer NOT NULL
 );
 
-CREATE INDEX forceforeign_idx_artist ON forceforeign (artist);
-
 --
 -- Table: self_ref_alias
 --
@@ -346,8 +344,6 @@ CREATE TABLE cd_artwork (
   cd_id INTEGER PRIMARY KEY NOT NULL
 );
 
-CREATE INDEX cd_artwork_idx_cd_id ON cd_artwork (cd_id);
-
 --
 -- Table: liner_notes
 --
@@ -356,8 +352,6 @@ CREATE TABLE liner_notes (
   notes varchar(100) NOT NULL
 );
 
-CREATE INDEX liner_notes_idx_liner_id ON liner_notes (liner_id);
-
 --
 -- Table: lyric_versions
 --
index 6142098..d82f4c4 100644 (file)
@@ -27,7 +27,7 @@ is_same_sql(
       single_track_2.trackid, single_track_2.cd, single_track_2.position, single_track_2.title, single_track_2.last_updated_on, single_track_2.last_updated_at, single_track_2.small_dt,
       cd.cdid, cd.artist, cd.title, cd.year, cd.genreid, cd.single_track
     FROM artist me
-      LEFT JOIN cd cds ON cds.artist = me.artistid
+      JOIN cd cds ON cds.artist = me.artistid
       LEFT JOIN track single_track ON single_track.trackid = cds.single_track
       LEFT JOIN track single_track_2 ON single_track_2.trackid = cds.single_track
       LEFT JOIN cd cd ON cd.cdid = single_track_2.cd
index 90e49a3..62776fa 100644 (file)
@@ -268,7 +268,7 @@ is_same_sql_bind (
   '(
     SELECT artist_undirected_maps.id1, artist_undirected_maps.id2
       FROM artist me
-      LEFT JOIN artist_undirected_map artist_undirected_maps
+      JOIN artist_undirected_map artist_undirected_maps
         ON artist_undirected_maps.id1 = me.artistid OR artist_undirected_maps.id2 = me.artistid
     WHERE ( artistid = ? )
   )',
diff --git a/t/resultset/nulls_only.t b/t/resultset/nulls_only.t
new file mode 100644 (file)
index 0000000..facf299
--- /dev/null
@@ -0,0 +1,29 @@
+use strict;
+use warnings;
+
+use lib qw(t/lib);
+use Test::More;
+use Test::Exception;
+use DBICTest;
+
+my $schema = DBICTest->init_schema();
+
+
+my $cd_rs = $schema->resultset('CD')->search ({ genreid => undef }, { columns => [ 'genreid' ]} );
+my $count = $cd_rs->count;
+cmp_ok ( $count, '>', 1, 'several CDs with no genre');
+
+my @objects = $cd_rs->all;
+is (scalar @objects, $count, 'Correct amount of objects without limit');
+isa_ok ($_, 'DBICTest::CD') for @objects;
+
+is_deeply (
+  [ map { values %{{$_->get_columns}} } (@objects) ],
+  [ (undef) x $count ],
+  'All values are indeed undef'
+);
+
+
+isa_ok ($cd_rs->search ({}, { rows => 1 })->single, 'DBICTest::CD');
+
+done_testing;
index 10621ae..c65e696 100644 (file)
@@ -31,7 +31,7 @@ is_same_sql_bind (
       ) me
       JOIN artist artist ON artist.artistid = me.artist
       LEFT JOIN track tracks ON tracks.cd = me.cdid
-      LEFT JOIN tags tags ON tags.cd = me.cdid
+      JOIN tags tags ON tags.cd = me.cdid
     WHERE ( tags.tag IS NOT NULL )
     GROUP BY tags.tagid, tags.cd, tags.tag
   )',