Merge 'sqlt_tests_refactor' into 'DBIx-Class-current'
Justin Guenther [Sat, 24 Jun 2006 03:26:53 +0000 (03:26 +0000)]
lib/DBIx/Class/InflateColumn/DateTime.pm
lib/DBIx/Class/Manual/Troubleshooting.pod
lib/DBIx/Class/Relationship/BelongsTo.pm
lib/DBIx/Class/Relationship/HasMany.pm
lib/DBIx/Class/Relationship/HasOne.pm
lib/DBIx/Class/Storage/DBI.pm
t/89inflate_datetime.t
t/lib/DBICTest.pm
t/lib/DBICTest/Schema/Event.pm
t/lib/sqlite.sql

index db6d3a7..f05523c 100644 (file)
@@ -11,7 +11,7 @@ DBIx::Class::InflateColumn::DateTime - Auto-create DateTime objects from date an
 =head1 SYNOPSIS
 
 Load this component and then declare one or more 
-columns to be of the datetime or date datatype.
+columns to be of the datetime, timestamp or date datatype.
 
   package Event;
   __PACKAGE__->load_components(qw/InflateColumn::DateTime/);
@@ -52,6 +52,7 @@ sub register_column {
   $self->next::method($column, $info, @rest);
   return unless defined($info->{data_type});
   my $type = lc($info->{data_type});
+  $type = 'datetime' if ($type eq 'timestamp');
   if ($type eq 'datetime' || $type eq 'date') {
     my ($parse, $format) = ("parse_${type}", "format_${type}");
     $self->inflate_column(
index 643add5..366bdf2 100644 (file)
@@ -15,7 +15,7 @@ SQL tracing, so to see what is happening try
 
   export DBIX_CLASS_STORAGE_DBI_DEBUG=1
 
-Alternatively use the C<storage->debug> class method:-
+Alternatively use the C<< storage->debug >> class method:-
 
   $class->storage->debug(1);
 
index e32dd6d..b871266 100644 (file)
@@ -9,22 +9,30 @@ use warnings;
 
 sub belongs_to {
   my ($class, $rel, $f_class, $cond, $attrs) = @_;
-  $class->ensure_class_loaded($f_class);
   # no join condition or just a column name
   if (!ref $cond) {
+    $class->ensure_class_loaded($f_class);
     my %f_primaries = map { $_ => 1 } eval { $f_class->primary_columns };
-    $class->throw_exception("Can't infer join condition for ${rel} on ${class}; unable to load ${f_class}")
-      if $@;
+    $class->throw_exception(
+      "Can't infer join condition for ${rel} on ${class}; ".
+      "unable to load ${f_class}: $@"
+    ) if $@;
 
     my ($pri, $too_many) = keys %f_primaries;
-    $class->throw_exception("Can't infer join condition for ${rel} on ${class}; ${f_class} has no primary keys")
-      unless defined $pri;
-    $class->throw_exception("Can't infer join condition for ${rel} on ${class}; ${f_class} has multiple primary keys")
-      if $too_many;
+    $class->throw_exception(
+      "Can't infer join condition for ${rel} on ${class}; ".
+      "${f_class} has no primary keys"
+    ) unless defined $pri;
+    $class->throw_exception(
+      "Can't infer join condition for ${rel} on ${class}; ".
+      "${f_class} has multiple primary keys"
+    ) if $too_many;
 
     my $fk = defined $cond ? $cond : $rel;
-    $class->throw_exception("Can't infer join condition for ${rel} on ${class}; $fk is not a column")
-      unless $class->has_column($fk);
+    $class->throw_exception(
+      "Can't infer join condition for ${rel} on ${class}; ".
+      "$fk is not a column of $class"
+    ) unless $class->has_column($fk);
 
     my $acc_type = $class->has_column($rel) ? 'filter' : 'single';
     $class->add_relationship($rel, $f_class,
@@ -42,14 +50,19 @@ sub belongs_to {
       }
       $cond_rel->{"foreign.$_"} = "self.".$cond->{$_};
     }
-    my $acc_type = (keys %$cond_rel == 1 and $class->has_column($rel)) ? 'filter' : 'single';
+    my $acc_type = (keys %$cond_rel == 1 and $class->has_column($rel))
+      ? 'filter'
+      : 'single';
     $class->add_relationship($rel, $f_class,
       $cond_rel,
       { accessor => $acc_type, %{$attrs || {}} }
     );
   }
   else {
-    $class->throw_exception('third argument for belongs_to must be undef, a column name, or a join condition');
+    $class->throw_exception(
+      'third argument for belongs_to must be undef, a column name, '.
+      'or a join condition'
+    );
   }
   return 1;
 }
index eeead26..2c9a3bb 100644 (file)
@@ -7,13 +7,14 @@ use warnings;
 sub has_many {
   my ($class, $rel, $f_class, $cond, $attrs) = @_;
 
-  $class->ensure_class_loaded($f_class);
-
   unless (ref $cond) {
+    $class->ensure_class_loaded($f_class);
     my ($pri, $too_many) = $class->primary_columns;
-    $class->throw_exception( "has_many can only infer join for a single ".
-                            "primary key; ${class} has more" )
-      if $too_many;
+
+    $class->throw_exception(
+      "has_many can only infer join for a single primary key; ".
+      "${class} has more"
+    ) if $too_many;
 
     my ($f_key,$guess);
     if (defined $cond && length $cond) {
index aa94a08..568078c 100644 (file)
@@ -14,11 +14,13 @@ sub has_one {
 
 sub _has_one {
   my ($class, $join_type, $rel, $f_class, $cond, $attrs) = @_;
-  $class->ensure_class_loaded($f_class);
   unless (ref $cond) {
+    $class->ensure_class_loaded($f_class);
     my ($pri, $too_many) = $class->primary_columns;
-    $class->throw_exception( "might_have/has_one can only infer join for a single primary key; ${class} has more" )
-      if $too_many;
+    $class->throw_exception(
+      "might_have/has_one can only infer join for a single primary key; ".
+      "${class} has more"
+    ) if $too_many;
     my $f_class_loaded = eval { $f_class->columns };
     my ($f_key,$guess);
     if (defined $cond && length $cond) {
@@ -29,12 +31,15 @@ sub _has_one {
       $guess = "using given relationship '$rel' for foreign key";
     } else {
       ($f_key, $too_many) = $f_class->primary_columns;
-      $class->throw_exception( "might_have/has_one can only infer join for a single primary key; ${f_class} has more" )
-        if $too_many;
+      $class->throw_exception(
+        "might_have/has_one can only infer join for a single primary key; ".
+        "${f_class} has more"
+      ) if $too_many;
       $guess = "using primary key of foreign class for foreign key";
     }
-    $class->throw_exception("No such column ${f_key} on foreign class ${f_class} ($guess)")
-      if $f_class_loaded && !$f_class->has_column($f_key);
+    $class->throw_exception(
+      "No such column ${f_key} on foreign class ${f_class} ($guess)"
+    ) if $f_class_loaded && !$f_class->has_column($f_key);
     $cond = { "foreign.${f_key}" => "self.${pri}" };
   }
   $class->add_relationship($rel, $f_class,
index fb0fbca..247a989 100644 (file)
@@ -915,7 +915,7 @@ L<DBIx::Class::Schema/deploy>.
 sub deployment_statements {
   my ($self, $schema, $type, $version, $dir, $sqltargs) = @_;
   # Need to be connected to get the correct sqlt_type
-  $self->ensure_connected();
+  $self->ensure_connected() unless $type;
   $type ||= $self->sqlt_type;
   $version ||= $schema->VERSION || '1.x';
   $dir ||= './';
index 4145749..85bddeb 100644 (file)
@@ -10,7 +10,7 @@ my $schema = DBICTest->init_schema();
 eval { require DateTime::Format::MySQL };
 plan skip_all => "Need DateTime::Format::MySQL for inflation tests" if $@;
 
-plan tests => 4;
+plan tests => 8;
 
 # inflation test
 my $event = $schema->resultset("Event")->find(1);
@@ -23,9 +23,22 @@ is($starts, '2006-04-25T22:24:33', 'Correct date/time');
 
 # create using DateTime
 my $created = $schema->resultset('Event')->create({
-    starts_at => DateTime->new(year=>2006, month=>6, day=>18)
+    starts_at => DateTime->new(year=>2006, month=>6, day=>18),
+    created_on => DateTime->new(year=>2006, month=>6, day=>23)
 });
 my $created_start = $created->starts_at;
 
 isa_ok($created->starts_at, 'DateTime', 'DateTime returned');
 is($created_start, '2006-06-18T00:00:00', 'Correct date/time');
+
+## timestamp field
+isa_ok($event->created_on, 'DateTime', 'DateTime returned');
+
+# klunky, but makes older Test::More installs happy
+my $createo = $event->created_on . '';
+is($createo, '2006-06-22T21:00:05', 'Correct date/time');
+
+my $created_cron = $created->created_on;
+
+isa_ok($created->created_on, 'DateTime', 'DateTime returned');
+is($created_cron, '2006-06-23T00:00:00', 'Correct date/time');
index 963eb4c..e3c739e 100755 (executable)
@@ -219,8 +219,8 @@ sub populate_schema {
     ]);
 
     $schema->populate('Event', [
-        [ qw/id starts_at/ ],
-        [ 1, '2006-04-25 22:24:33' ],
+        [ qw/id starts_at created_on/ ],
+        [ 1, '2006-04-25 22:24:33', '2006-06-22 21:00:05'],
     ]);
 
     $schema->populate('Link', [
index 937d782..bce3e34 100644 (file)
@@ -10,7 +10,8 @@ __PACKAGE__->table('event');
 
 __PACKAGE__->add_columns(
   id => { data_type => 'integer', is_auto_increment => 1 },
-  starts_at => { data_type => 'datetime' }
+  starts_at => { data_type => 'datetime' },
+  created_on => { data_type => 'timestamp' }
 );
 
 __PACKAGE__->set_primary_key('id');
index 551d159..868aba1 100644 (file)
@@ -1,6 +1,6 @@
 -- 
 -- Created by SQL::Translator::Producer::SQLite
--- Created on Sat Jun 17 07:46:56 2006
+-- Created on Thu Jun 22 22:47:36 2006
 -- 
 BEGIN TRANSACTION;
 
@@ -150,7 +150,8 @@ CREATE TABLE tags (
 --
 CREATE TABLE event (
   id INTEGER PRIMARY KEY NOT NULL,
-  starts_at datetime NOT NULL
+  starts_at datetime NOT NULL,
+  created_on timestamp NOT NULL
 );
 
 --