Merge 'trunk' into 'current'
Dagfinn Ilmari Mannsåker [Wed, 12 Mar 2008 02:22:36 +0000 (02:22 +0000)]
r36739@vesla (orig r4176):  ilmari | 2008-03-12 01:49:17 +0000
Fix DB2 support:
 - foreign_key_info needs the PK schema name
 - up/down-case table names when going to/from the DB

15 files changed:
Changes
lib/DBIx/Class/Schema/Loader.pm
lib/DBIx/Class/Schema/Loader/Base.pm
lib/DBIx/Class/Schema/Loader/DBI.pm
lib/DBIx/Class/Schema/Loader/DBI/DB2.pm
lib/DBIx/Class/Schema/Loader/DBI/Oracle.pm
lib/DBIx/Class/Schema/Loader/DBI/Pg.pm
lib/DBIx/Class/Schema/Loader/DBI/SQLite.pm
lib/DBIx/Class/Schema/Loader/DBI/Writing.pm
lib/DBIx/Class/Schema/Loader/DBI/mysql.pm
lib/DBIx/Class/Schema/Loader/RelBuilder.pm
t/10sqlite_common.t
t/11mysql_common.t
t/13db2_common.t
t/lib/dbixcsl_common_tests.pm

diff --git a/Changes b/Changes
index 4c59009..b221943 100644 (file)
--- a/Changes
+++ b/Changes
@@ -1,12 +1,25 @@
 Revision history for Perl extension DBIx::Class::Schema::Loader
 
-Not yet released
+0.04999_02 Tue Feb 12, 2008
+        - Add is_auto_increment detection for Oracle
+        - Unnhide the Oracle module now that the CPAN perms are sorted
+          out. Thanks to Tsunoda Kazuya for the quick response.
+
+0.04999_01 Tue Feb 5, 2008
+        - Mark foreign key columns with is_foreign_key => 1
+        - Add support for vendor-specific extra column attributes.
+        - Add support for extra => { unsigned => 1 } for MySQL.
+        - Add support for enum value lists for MySQL
+        - Set join_type => 'LEFT OUTER' for nullable foreign keys
+          (patch from Bernhard Weißhuhn)
+        - Set is_auto_increment for auto-increment columns (RT #31473)
+          (Only SQLite, MySQL and PostgreSQL are currently supported)
+        - Generate one-to-one accessors for unique foreign keys (ilmari)
         - Fix DB2 support
         - Add support for load_namespaces-style class layout
         - Fix test skip count for main skip_rels block
         - Fix auto-inc column creation for the Oracle tests
-
-0.04004 Thu Nov 15, 2007
+        - Fix column ordering in unique constraints for Oracle
         - Fix Win32 test skip counts for good (RT #30568, Kenichi Ishigaki)
         - Default Oracle db_schema to db username (patch
           from Johannes Plunien)
index 5147aa6..b8080c9 100644 (file)
@@ -11,7 +11,7 @@ use Scalar::Util qw/ weaken /;
 # Always remember to do all digits for the version even if they're 0
 # i.e. first release of 0.XX *must* be 0.XX000. This avoids fBSD ports
 # brain damage and presumably various other packaging systems too
-our $VERSION = '0.04004';
+our $VERSION = '0.04999_02';
 
 __PACKAGE__->mk_classaccessor('_loader_args' => {});
 __PACKAGE__->mk_classaccessors(qw/dump_to_dir _loader_invoked _loader/);
index aa6e381..c7a9856 100644 (file)
@@ -14,7 +14,7 @@ use Cwd qw//;
 use Digest::MD5 qw//;
 require DBIx::Class;
 
-our $VERSION = '0.04004';
+our $VERSION = '0.04999_02';
 
 __PACKAGE__->mk_ro_accessors(qw/
                                 schema
@@ -652,6 +652,12 @@ sub _setup_src_meta {
     }
     else {
         my %col_info_lc = map { lc($_), $col_info->{$_} } keys %$col_info;
+        my $fks = $self->_table_fk_info($table);
+        for my $fkdef (@$fks) {
+            for my $col (@{ $fkdef->{local_columns} }) {
+                $col_info_lc{$col}->{is_foreign_key} = 1;
+            }
+        }
         $self->_dbic_stmt(
             $table_class,
             'add_columns',
@@ -709,9 +715,10 @@ sub _load_relationships {
         $fkdef->{remote_source} =
             $self->monikers->{delete $fkdef->{remote_table}};
     }
+    my $tbl_uniq_info = $self->_table_uniq_info($table);
 
     my $local_moniker = $self->monikers->{$table};
-    my $rel_stmts = $self->{relbuilder}->generate_code($local_moniker, $tbl_fk_info);
+    my $rel_stmts = $self->{relbuilder}->generate_code($local_moniker, $tbl_fk_info, $tbl_uniq_info);
 
     foreach my $src_class (sort keys %$rel_stmts) {
         my $src_stmts = $rel_stmts->{$src_class};
index 9d6d0ea..0dd8b25 100644 (file)
@@ -7,7 +7,7 @@ use Class::C3;
 use Carp::Clan qw/^DBIx::Class/;
 use UNIVERSAL::require;
 
-our $VERSION = '0.04004';
+our $VERSION = '0.04999_02';
 
 =head1 NAME
 
@@ -222,7 +222,9 @@ sub _columns_info_for {
                 my $col_name = $info->{COLUMN_NAME};
                 $col_name =~ s/^\"(.*)\"$/$1/;
 
-                $result{$col_name} = \%column_info;
+                my $extra_info = $self->_extra_column_info($info) || {};
+
+                $result{$col_name} = { %column_info, %$extra_info };
             }
             $sth->finish;
         };
@@ -247,7 +249,9 @@ sub _columns_info_for {
             $column_info{size}    = $2;
         }
 
-        $result{$columns[$i]} = \%column_info;
+        my $extra_info = $self->_extra_column_info($table, $columns[$i], $sth, $i) || {};
+
+        $result{$columns[$i]} = { %column_info, %$extra_info };
     }
     $sth->finish;
 
@@ -265,6 +269,10 @@ sub _columns_info_for {
     return \%result;
 }
 
+# Override this in vendor class to return any additional column
+# attributes
+sub _extra_column_info {}
+
 =head1 SEE ALSO
 
 L<DBIx::Class::Schema::Loader>
index 7d1716e..a8ef8bb 100644 (file)
@@ -6,7 +6,7 @@ use base 'DBIx::Class::Schema::Loader::DBI';
 use Carp::Clan qw/^DBIx::Class/;
 use Class::C3;
 
-our $VERSION = '0.04004';
+our $VERSION = '0.04999_02';
 
 =head1 NAME
 
index 3be38e2..de4e4a1 100644 (file)
@@ -1,6 +1,4 @@
-package # hide from pause/cpan for now, as there's a permissions
-        # issue and it's screwing the rest of the package
-  DBIx::Class::Schema::Loader::DBI::Oracle;
+package DBIx::Class::Schema::Loader::DBI::Oracle;
 
 use strict;
 use warnings;
@@ -8,7 +6,7 @@ use base 'DBIx::Class::Schema::Loader::DBI';
 use Carp::Clan qw/^DBIx::Class/;
 use Class::C3;
 
-our $VERSION = '0.04004';
+our $VERSION = '0.04999_02';
 
 =head1 NAME
 
@@ -123,6 +121,31 @@ sub _columns_info_for {
     return $self->next::method(uc $table);
 }
 
+sub _extra_column_info {
+    my ($self, $info) = @_;
+    my %extra_info;
+
+    my ($table, $column) = @$info{qw/TABLE_NAME COLUMN_NAME/};
+
+    my $dbh = $self->schema->storage->dbh;
+    my $sth = $dbh->prepare_cached(
+        q{
+            SELECT COUNT(*)
+            FROM user_triggers ut JOIN user_trigger_cols utc USING (trigger_name)
+            WHERE utc.table_name = ? AND utc.column_name = ?
+            AND column_usage LIKE '%NEW%' AND column_usage LIKE '%OUT%'
+            AND trigger_type = 'BEFORE EACH ROW' AND triggering_event LIKE '%INSERT%'
+        },
+        {}, 1);
+
+    $sth->execute($table, $column);
+    if ($sth->fetchrow_array) {
+        $extra_info{is_auto_increment} = 1;
+    }
+
+    return \%extra_info;
+}
+
 =head1 SEE ALSO
 
 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
@@ -132,6 +155,8 @@ L<DBIx::Class::Schema::Loader::DBI>
 
 TSUNODA Kazuya C<drk@drk7.jp>
 
+Dagfinn Ilmari Mannsåker C<ilmari@ilmari.org>
+
 =cut
 
 1;
index b04e0fb..5a428ec 100644 (file)
@@ -6,7 +6,7 @@ use base 'DBIx::Class::Schema::Loader::DBI';
 use Carp::Clan qw/^DBIx::Class/;
 use Class::C3;
 
-our $VERSION = '0.04004';
+our $VERSION = '0.04999_02';
 
 =head1 NAME
 
@@ -95,6 +95,17 @@ sub _table_uniq_info {
     return \@uniqs;
 }
 
+sub _extra_column_info {
+    my ($self, $info) = @_;
+    my %extra_info;
+
+    if ($info->{COLUMN_DEF} && $info->{COLUMN_DEF} =~ /\bnextval\(/i) {
+        $extra_info{is_auto_increment} = 1;
+    }
+
+    return \%extra_info;
+}
+
 =head1 SEE ALSO
 
 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
index 23db551..028fcc5 100644 (file)
@@ -7,7 +7,7 @@ use Carp::Clan qw/^DBIx::Class/;
 use Text::Balanced qw( extract_bracketed );
 use Class::C3;
 
-our $VERSION = '0.04004';
+our $VERSION = '0.04999_02';
 
 =head1 NAME
 
@@ -52,6 +52,7 @@ sub _sqlite_parse_table {
 
     my @rels;
     my @uniqs;
+    my %auto_inc;
 
     my $dbh = $self->schema->storage->dbh;
     my $sth = $self->{_cache}->{sqlite_master}
@@ -110,6 +111,11 @@ sub _sqlite_parse_table {
             push(@uniqs, [ $name => \@cols ]);
         }
 
+        if ($col =~ /AUTOINCREMENT/i) {
+            $col =~ /^(\S+)/;
+            $auto_inc{lc $1} = 1;
+        }
+
         next if $col !~ /^(.*\S)\s+REFERENCES\s+(\w+) (?: \s* \( (.*) \) )? /ix;
 
         my ($cols, $f_table, $f_cols) = ($1, $2, $3);
@@ -137,7 +143,21 @@ sub _sqlite_parse_table {
         });
     }
 
-    return { rels => \@rels, uniqs => \@uniqs };
+    return { rels => \@rels, uniqs => \@uniqs, auto_inc => \%auto_inc };
+}
+
+sub _extra_column_info {
+    my ($self, $table, $col_name, $sth, $col_num) = @_;
+    my %extra_info;
+
+    $self->{_sqlite_parse_data}->{$table} ||=
+        $self->_sqlite_parse_table($table);
+
+    if ($self->{_sqlite_parse_data}->{$table}->{auto_inc}->{$col_name}) {
+        $extra_info{is_auto_increment} = 1;
+    }
+
+    return \%extra_info;
 }
 
 sub _table_fk_info {
index 7bbb9d2..e4554cc 100644 (file)
@@ -1,7 +1,7 @@
 package DBIx::Class::Schema::Loader::DBI::Writing;
 use strict;
 
-our $VERSION = '0.04004';
+our $VERSION = '0.04999_02';
 
 # Empty. POD only.
 
index 34cca83..e8bd657 100644 (file)
@@ -6,7 +6,7 @@ use base 'DBIx::Class::Schema::Loader::DBI';
 use Carp::Clan qw/^DBIx::Class/;
 use Class::C3;
 
-our $VERSION = '0.04004';
+our $VERSION = '0.04999_02';
 
 =head1 NAME
 
@@ -121,6 +121,23 @@ sub _table_uniq_info {
     return \@uniqs;
 }
 
+sub _extra_column_info {
+    my ($self, $info) = @_;
+    my %extra_info;
+
+    if ($info->{mysql_is_auto_increment}) {
+        $extra_info{is_auto_increment} = 1
+    }
+    if ($info->{mysql_type_name} =~ /\bunsigned\b/i) {
+        $extra_info{extra}{unsigned} = 1;
+    }
+    if ($info->{mysql_values}) {
+        $extra_info{extra}{list} = $info->{mysql_values};
+    }
+
+    return \%extra_info;
+}
+
 =head1 SEE ALSO
 
 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
index 6c23177..0a29af1 100644 (file)
@@ -5,7 +5,7 @@ use warnings;
 use Carp::Clan qw/^DBIx::Class/;
 use Lingua::EN::Inflect::Number ();
 
-our $VERSION = '0.04004';
+our $VERSION = '0.04999_02';
 
 =head1 NAME
 
@@ -121,8 +121,19 @@ sub _inflect_singular {
     return Lingua::EN::Inflect::Number::to_S($relname);
 }
 
+sub _array_eq {
+    my ($a, $b) = @_;
+
+    return unless @$a == @$b;
+
+    for (my $i = 0; $i < @$a; $i++) {
+        return unless $a->[$i] eq $b->[$i];
+    }
+    return 1;
+}
+
 sub generate_code {
-    my ($self, $local_moniker, $rels) = @_;
+    my ($self, $local_moniker, $rels, $uniqs) = @_;
 
     my $all_code = {};
 
@@ -187,17 +198,32 @@ sub generate_code {
             delete $rev_cond{$_};
         }
 
+        my $remote_method = 'has_many';
+
+        # If the local columns have a UNIQUE constraint, this is a one-to-one rel
+        my $local_source = $self->{schema}->source($local_moniker);
+        if (_array_eq([ $local_source->primary_columns ], $local_cols) ||
+            grep { _array_eq($_->[1], $local_cols) } @$uniqs) {
+            $remote_method = 'might_have';
+            $local_relname = $self->_inflect_singular($local_relname);
+        }
+
+        # If the referring column is nullable, make 'belongs_to' an outer join:
+        my $nullable = grep { $local_source->column_info($_)->{is_nullable} }
+          @$local_cols;
+
         push(@{$all_code->{$local_class}},
             { method => 'belongs_to',
               args => [ $remote_relname,
                         $remote_class,
                         \%cond,
+                        $nullable ? { join_type => 'LEFT OUTER' } : ()
               ],
             }
         );
 
         push(@{$all_code->{$remote_class}},
-            { method => 'has_many',
+            { method => $remote_method,
               args => [ $local_relname,
                         $local_class,
                         \%rev_cond,
index 7899cf6..13bb045 100644 (file)
@@ -8,7 +8,7 @@ my $class = $@ ? 'SQLite2' : 'SQLite';
 {
     my $tester = dbixcsl_common_tests->new(
         vendor          => 'SQLite',
-        auto_inc_pk     => 'INTEGER NOT NULL PRIMARY KEY',
+        auto_inc_pk     => 'INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT',
         dsn             => "dbi:$class:dbname=./t/sqlite_test",
         user            => '',
         password        => '',
index c65c7b1..bd86d93 100644 (file)
@@ -1,6 +1,7 @@
 use strict;
 use lib qw(t/lib);
 use dbixcsl_common_tests;
+use Test::More;
 
 my $dsn         = $ENV{DBICTEST_MYSQL_DSN} || '';
 my $user        = $ENV{DBICTEST_MYSQL_USER} || '';
@@ -19,6 +20,32 @@ my $tester = dbixcsl_common_tests->new(
     skip_rels        => $test_innodb ? 0 : $skip_rels_msg,
     no_inline_rels   => 1,
     no_implicit_rels => 1,
+    extra            => {
+        create => [
+            qq{
+                CREATE TABLE mysql_loader_test1 (
+                    id INTEGER UNSIGNED NOT NULL PRIMARY KEY,
+                    value ENUM('foo', 'bar', 'baz')
+                )
+            },
+        ],
+        drop   => [ qw/ mysql_loader_test1 / ],
+        count  => 3,
+        run    => sub {
+            my ($schema, $monikers, $classes) = @_;
+        
+            my $rs = $schema->resultset($monikers->{mysql_loader_test1});
+            my $column_info = $rs->result_source->column_info('id');
+            
+            is($column_info->{extra}->{unsigned}, 1, 'Unsigned MySQL columns');
+
+            $column_info = $rs->result_source->column_info('value');
+
+            like($column_info->{data_type}, qr/^enum$/i, 'MySQL ENUM type');
+            is_deeply($column_info->{extra}->{list}, [qw/foo bar baz/],
+                      'MySQL ENUM values');
+        },
+    }
 );
 
 if( !$dsn || !$user ) {
index b52fa68..e616f21 100644 (file)
@@ -13,6 +13,7 @@ my $tester = dbixcsl_common_tests->new(
     user           => $user,
     password       => $password,
     db_schema      => uc $user,
+    no_auto_increment => 1
 );
 
 if( !$dsn || !$user ) {
index 75d6090..f38f693 100644 (file)
@@ -25,6 +25,9 @@ sub new {
     
     $self->{verbose} = $ENV{TEST_VERBOSE} || 0;
 
+    # Optional extra tables and tests
+    $self->{extra} ||= {};
+
     return bless $self => $class;
 }
 
@@ -43,7 +46,7 @@ sub _monikerize {
 sub run_tests {
     my $self = shift;
 
-    plan tests => 88;
+    plan tests => 134 + ($self->{extra}->{count} || 0);
 
     $self->create();
 
@@ -53,7 +56,7 @@ sub run_tests {
 
     my @connect_info = ( $self->{dsn}, $self->{user}, $self->{password} );
     my %loader_opts = (
-        constraint              => qr/^(?:\S+\.)?loader_test[0-9]+$/i,
+        constraint              => qr/^(?:\S+\.)?(?:$self->{vendor}_)?loader_test[0-9]+$/i,
         relationships           => 1,
         additional_classes      => 'TestAdditional',
         additional_base_classes => 'TestAdditionalBase',
@@ -86,16 +89,17 @@ sub run_tests {
         };
         ok(!$@, "Loader initialization") or diag $@;
         if($self->{skip_rels}) {
-            is(scalar(@loader_warnings), 0)
-              or diag "Did not get the expected 0 warnings.  Warnings are: "
-                . join('',@loader_warnings);
-            ok(1);
+            SKIP: {
+                is(scalar(@loader_warnings), 0, "No loader warnings")
+                    or diag @loader_warnings;
+                skip "No missing PK warnings without rels", 1;
+            }
         }
         else {
-            is(scalar(@loader_warnings), 1)
-              or diag "Did not get the expected 1 warning.  Warnings are: "
-                . join('',@loader_warnings);
-            like($loader_warnings[0], qr/loader_test9 has no primary key/i);
+            is(scalar(@loader_warnings), 1, "Expected loader warning")
+                or diag @loader_warnings;
+            like($loader_warnings[0], qr/loader_test9 has no primary key/i,
+                 "Missing PK warning");
         }
     }
 
@@ -130,9 +134,7 @@ sub run_tests {
     isa_ok( $rsobj24, "DBIx::Class::ResultSet" );
 
     my @columns_lt2 = $class2->columns;
-    is($columns_lt2[0], 'id', "Column Ordering 0");
-    is($columns_lt2[1], 'dat', "Column Ordering 1");
-    is($columns_lt2[2], 'dat2', "Column Ordering 2");
+    is_deeply( \@columns_lt2, [ qw/id dat dat2/ ], "Column Ordering" );
 
     my %uniq1 = $class1->unique_constraints;
     my $uniq1_test = 0;
@@ -143,7 +145,7 @@ sub run_tests {
            last;
         }
     }
-    ok($uniq1_test) or diag "Unique constraints not working";
+    ok($uniq1_test, "Unique constraint");
 
     my %uniq2 = $class2->unique_constraints;
     my $uniq2_test = 0;
@@ -156,7 +158,7 @@ sub run_tests {
             last;
         }
     }
-    ok($uniq2_test) or diag "Multi-col unique constraints not working";
+    ok($uniq2_test, "Multi-col unique constraint");
 
     is($moniker2, 'LoaderTest2X', "moniker_map testing");
 
@@ -197,7 +199,8 @@ sub run_tests {
         SKIP: {
             skip "Pre-requisite test failed", 1 if $skip_tcomp;
             is( $class1->dbix_class_testcomponent,
-                'dbix_class_testcomponent works' );
+                'dbix_class_testcomponent works',
+                'Additional Component' );
         }
 
         SKIP: {
@@ -207,44 +210,50 @@ sub run_tests {
             SKIP: {
                 skip "Pre-requisite test failed", 1 if $skip_trscomp;
                 is( $rsobj1->dbix_class_testrscomponent,
-                    'dbix_class_testrscomponent works' );
+                    'dbix_class_testrscomponent works',
+                    'ResultSet component' );
             }
         }
 
         SKIP: {
             skip "Pre-requisite test failed", 1 if $skip_cmeth;
-            is( $class1->loader_test1_classmeth, 'all is well' );
+            is( $class1->loader_test1_classmeth, 'all is well', 'Class method' );
         }
 
-        # XXX put this back in when the TODO above works...
-        #SKIP: {
-        #    skip "Pre-requisite test failed", 1 if $skip_rsmeth;
-        #    is( $rsobj1->loader_test1_rsmeth, 'all is still well' );
-        #}
+        SKIP: {
+            skip "Pre-requisite test failed", 1 if $skip_rsmeth;
+            is( $rsobj1->loader_test1_rsmeth, 'all is still well', 'Result set method' );
+        }
     }
 
+    SKIP: {
+        skip "This vendor doesn't detect auto-increment columns", 1
+            if $self->{no_auto_increment};
+
+        ok( $class1->column_info('id')->{is_auto_increment}, 'is_auto_incrment detection' );
+    }
 
     my $obj    = $rsobj1->find(1);
-    is( $obj->id,  1 );
-    is( $obj->dat, "foo" );
-    is( $rsobj2->count, 4 );
+    is( $obj->id,  1, "Find got the right row" );
+    is( $obj->dat, "foo", "Column value" );
+    is( $rsobj2->count, 4, "Count" );
     my $saved_id;
     eval {
         my $new_obj1 = $rsobj1->create({ dat => 'newthing' });
         $saved_id = $new_obj1->id;
     };
-    ok(!$@) or diag "Died during create new record using a PK::Auto key: $@";
-    ok($saved_id) or diag "Failed to get PK::Auto-generated id";
+    ok(!$@, "Inserting new record using a PK::Auto key didn't die") or diag $@;
+    ok($saved_id, "Got PK::Auto-generated id");
 
     my $new_obj1 = $rsobj1->search({ dat => 'newthing' })->first;
-    ok($new_obj1) or diag "Cannot find newly inserted PK::Auto record";
-    is($new_obj1->id, $saved_id);
+    ok($new_obj1, "Found newly inserted PK::Auto record");
+    is($new_obj1->id, $saved_id, "Correct PK::Auto-generated id");
 
     my ($obj2) = $rsobj2->search({ dat => 'bbb' })->first;
     is( $obj2->id, 2 );
 
     SKIP: {
-        skip $self->{skip_rels}, 50 if $self->{skip_rels};
+        skip $self->{skip_rels}, 96 if $self->{skip_rels};
 
         my $moniker3 = $monikers->{loader_test3};
         my $class3   = $classes->{loader_test3};
@@ -310,6 +319,34 @@ sub run_tests {
         my $class26   = $classes->{loader_test26};
         my $rsobj26   = $conn->resultset($moniker26);
 
+        my $moniker27 = $monikers->{loader_test27};
+        my $class27   = $classes->{loader_test27};
+        my $rsobj27   = $conn->resultset($moniker27);
+
+        my $moniker28 = $monikers->{loader_test28};
+        my $class28   = $classes->{loader_test28};
+        my $rsobj28   = $conn->resultset($moniker28);
+
+        my $moniker29 = $monikers->{loader_test29};
+        my $class29   = $classes->{loader_test29};
+        my $rsobj29   = $conn->resultset($moniker29);
+
+        my $moniker31 = $monikers->{loader_test31};
+        my $class31   = $classes->{loader_test31};
+        my $rsobj31   = $conn->resultset($moniker31);
+
+        my $moniker32 = $monikers->{loader_test32};
+        my $class32   = $classes->{loader_test32};
+        my $rsobj32   = $conn->resultset($moniker32);
+
+        my $moniker33 = $monikers->{loader_test33};
+        my $class33   = $classes->{loader_test33};
+        my $rsobj33   = $conn->resultset($moniker33);
+
+        my $moniker34 = $monikers->{loader_test34};
+        my $class34   = $classes->{loader_test34};
+        my $rsobj34   = $conn->resultset($moniker34);
+
         isa_ok( $rsobj3, "DBIx::Class::ResultSet" );
         isa_ok( $rsobj4, "DBIx::Class::ResultSet" );
         isa_ok( $rsobj5, "DBIx::Class::ResultSet" );
@@ -326,68 +363,149 @@ sub run_tests {
         isa_ok( $rsobj22, "DBIx::Class::ResultSet" );
         isa_ok( $rsobj25, "DBIx::Class::ResultSet" );
         isa_ok( $rsobj26, "DBIx::Class::ResultSet" );
+        isa_ok( $rsobj27, "DBIx::Class::ResultSet" );
+        isa_ok( $rsobj28, "DBIx::Class::ResultSet" );
+        isa_ok( $rsobj29, "DBIx::Class::ResultSet" );
+        isa_ok( $rsobj31, "DBIx::Class::ResultSet" );
+        isa_ok( $rsobj32, "DBIx::Class::ResultSet" );
+        isa_ok( $rsobj33, "DBIx::Class::ResultSet" );
+        isa_ok( $rsobj34, "DBIx::Class::ResultSet" );
 
         # basic rel test
         my $obj4 = $rsobj4->find(123);
         isa_ok( $obj4->fkid_singular, $class3);
 
+        ok($class4->column_info('fkid')->{is_foreign_key}, 'Foreign key detected');
+
         my $obj3 = $rsobj3->find(1);
         my $rs_rel4 = $obj3->search_related('loader_test4zes');
         isa_ok( $rs_rel4->first, $class4);
 
         # find on multi-col pk
         my $obj5 = $rsobj5->find({id1 => 1, id2 => 1});
-        is( $obj5->id2, 1 );
+        is( $obj5->id2, 1, "Find on multi-col PK" );
 
         # mulit-col fk def
         my $obj6 = $rsobj6->find(1);
         isa_ok( $obj6->loader_test2, $class2);
         isa_ok( $obj6->loader_test5, $class5);
 
+        ok($class6->column_info('loader_test2')->{is_foreign_key}, 'Foreign key detected');
+        ok($class6->column_info('id')->{is_foreign_key}, 'Foreign key detected');
+        ok($class6->column_info('id2')->{is_foreign_key}, 'Foreign key detected');
+
         # fk that references a non-pk key (UNIQUE)
         my $obj8 = $rsobj8->find(1);
         isa_ok( $obj8->loader_test7, $class7);
 
+        ok($class8->column_info('loader_test7')->{is_foreign_key}, 'Foreign key detected');
+
         # test double-fk 17 ->-> 16
         my $obj17 = $rsobj17->find(33);
 
         my $rs_rel16_one = $obj17->loader16_one;
         isa_ok($rs_rel16_one, $class16);
-        is($rs_rel16_one->dat, 'y16');
+        is($rs_rel16_one->dat, 'y16', "Multiple FKs to same table");
+
+        ok($class17->column_info('loader16_one')->{is_foreign_key}, 'Foreign key detected');
 
         my $rs_rel16_two = $obj17->loader16_two;
         isa_ok($rs_rel16_two, $class16);
-        is($rs_rel16_two->dat, 'z16');
+        is($rs_rel16_two->dat, 'z16', "Multiple FKs to same table");
+
+        ok($class17->column_info('loader16_two')->{is_foreign_key}, 'Foreign key detected');
 
         my $obj16 = $rsobj16->find(2);
         my $rs_rel17 = $obj16->search_related('loader_test17_loader16_ones');
         isa_ok($rs_rel17->first, $class17);
-        is($rs_rel17->first->id, 3);
+        is($rs_rel17->first->id, 3, "search_related with multiple FKs from same table");
         
         # XXX test m:m 18 <- 20 -> 19
+        ok($class20->column_info('parent')->{is_foreign_key}, 'Foreign key detected');
+        ok($class20->column_info('child')->{is_foreign_key}, 'Foreign key detected');
         
         # XXX test double-fk m:m 21 <- 22 -> 21
+        ok($class22->column_info('parent')->{is_foreign_key}, 'Foreign key detected');
+        ok($class22->column_info('child')->{is_foreign_key}, 'Foreign key detected');
 
         # test double multi-col fk 26 -> 25
         my $obj26 = $rsobj26->find(33);
 
         my $rs_rel25_one = $obj26->loader_test25_id_rel1;
         isa_ok($rs_rel25_one, $class25);
-        is($rs_rel25_one->dat, 'x25');
+        is($rs_rel25_one->dat, 'x25', "Multiple multi-col FKs to same table");
+
+        ok($class26->column_info('id')->{is_foreign_key}, 'Foreign key detected');
+        ok($class26->column_info('rel1')->{is_foreign_key}, 'Foreign key detected');
+        ok($class26->column_info('rel2')->{is_foreign_key}, 'Foreign key detected');
 
         my $rs_rel25_two = $obj26->loader_test25_id_rel2;
         isa_ok($rs_rel25_two, $class25);
-        is($rs_rel25_two->dat, 'y25');
+        is($rs_rel25_two->dat, 'y25', "Multiple multi-col FKs to same table");
 
         my $obj25 = $rsobj25->find(3,42);
         my $rs_rel26 = $obj25->search_related('loader_test26_id_rel1s');
         isa_ok($rs_rel26->first, $class26);
-        is($rs_rel26->first->id, 3);
+        is($rs_rel26->first->id, 3, "search_related with multiple multi-col FKs from same table");
+
+        # test one-to-one rels
+        my $obj27 = $rsobj27->find(1);
+        my $obj28 = $obj27->loader_test28;
+        isa_ok($obj28, $class28);
+        is($obj28->get_column('id'), 1, "One-to-one relationship with PRIMARY FK");
+
+        ok($class28->column_info('id')->{is_foreign_key}, 'Foreign key detected');
+
+        my $obj29 = $obj27->loader_test29;
+        isa_ok($obj29, $class29);
+        is($obj29->id, 1, "One-to-one relationship with UNIQUE FK");
+
+        ok($class29->column_info('fk')->{is_foreign_key}, 'Foreign key detected');
+
+        $obj27 = $rsobj27->find(2);
+        is($obj27->loader_test28, undef, "Undef for missing one-to-one row");
+        is($obj27->loader_test29, undef, "Undef for missing one-to-one row");
+
+        # test outer join for nullable referring columns:
+        SKIP: {
+          skip "unreliable column info from db driver",11 unless 
+            ($class32->column_info('rel2')->{is_nullable});
+
+          ok($class32->column_info('rel1')->{is_foreign_key}, 'Foreign key detected');
+          ok($class32->column_info('rel2')->{is_foreign_key}, 'Foreign key detected');
+          
+          my $obj32 = $rsobj32->find(1,{prefetch=>[qw/rel1 rel2/]});
+          my $obj34 = $rsobj34->find(
+            1,{prefetch=>[qw/loader_test33_id_rel1 loader_test33_id_rel2/]}
+          );
+          my $skip_outerjoin;
+          isa_ok($obj32,$class32) or $skip_outerjoin = 1;
+          isa_ok($obj34,$class34) or $skip_outerjoin = 1;
+
+          ok($class34->column_info('id')->{is_foreign_key}, 'Foreign key detected');
+          ok($class34->column_info('rel1')->{is_foreign_key}, 'Foreign key detected');
+          ok($class34->column_info('rel2')->{is_foreign_key}, 'Foreign key detected');
+
+          SKIP: {
+            skip "Pre-requisite test failed", 4 if $skip_outerjoin;
+            my $rs_rel31_one = $obj32->rel1;
+            my $rs_rel31_two = $obj32->rel2;
+            isa_ok($rs_rel31_one, $class31);
+            is($rs_rel31_two, undef);
+
+            my $rs_rel33_one = $obj34->loader_test33_id_rel1;
+            my $rs_rel33_two = $obj34->loader_test33_id_rel2;
+
+            isa_ok($rs_rel33_one,$class33);
+            is($rs_rel33_two, undef);
+
+          }
+        }
 
         # from Chisel's tests...
         SKIP: {
             if($self->{vendor} =~ /sqlite/i) {
-                skip 'SQLite cannot do the advanced tests', 8;
+                skip 'SQLite cannot do the advanced tests', 10;
             }
 
             my $moniker10 = $monikers->{loader_test10};
@@ -401,39 +519,40 @@ sub run_tests {
             isa_ok( $rsobj10, "DBIx::Class::ResultSet" ); 
             isa_ok( $rsobj11, "DBIx::Class::ResultSet" );
 
+            ok($class10->column_info('loader_test11')->{is_foreign_key}, 'Foreign key detected');
+            ok($class11->column_info('loader_test10')->{is_foreign_key}, 'Foreign key detected');
+
             my $obj10 = $rsobj10->create({ subject => 'xyzzy' });
 
             $obj10->update();
-            ok( defined $obj10, '$obj10 is defined' );
+            ok( defined $obj10, 'Create row' );
 
             my $obj11 = $rsobj11->create({ loader_test10 => $obj10->id() });
             $obj11->update();
-            ok( defined $obj11, '$obj11 is defined' );
+            ok( defined $obj11, 'Create related row' );
 
             eval {
                 my $obj10_2 = $obj11->loader_test10;
                 $obj10_2->loader_test11( $obj11->id11() );
                 $obj10_2->update();
             };
-            is($@, '', 'No errors after eval{}');
+            ok(!$@, "Setting up circular relationship");
 
             SKIP: {
-                skip 'Previous eval block failed', 3
-                    unless ($@ eq '');
+                skip 'Previous eval block failed', 3 if $@;
         
                 my $results = $rsobj10->search({ subject => 'xyzzy' });
-                is( $results->count(), 1,
-                    'One $rsobj10 returned from search' );
+                is( $results->count(), 1, 'No duplicate row created' );
 
                 my $obj10_3 = $results->first();
                 isa_ok( $obj10_3, $class10 );
                 is( $obj10_3->loader_test11()->id(), $obj11->id(),
-                    'found same $rsobj11 object we expected' );
+                    'Circular rel leads back to same row' );
             }
         }
 
         SKIP: {
-            skip 'This vendor cannot do inline relationship definitions', 5
+            skip 'This vendor cannot do inline relationship definitions', 8
                 if $self->{no_inline_rels};
 
             my $moniker12 = $monikers->{loader_test12};
@@ -447,6 +566,10 @@ sub run_tests {
             isa_ok( $rsobj12, "DBIx::Class::ResultSet" ); 
             isa_ok( $rsobj13, "DBIx::Class::ResultSet" );
 
+            ok($class13->column_info('id')->{is_foreign_key}, 'Foreign key detected');
+            ok($class13->column_info('loader_test12')->{is_foreign_key}, 'Foreign key detected');
+            ok($class13->column_info('dat')->{is_foreign_key}, 'Foreign key detected');
+
             my $obj13 = $rsobj13->find(1);
             isa_ok( $obj13->id, $class12 );
             isa_ok( $obj13->loader_test12, $class12);
@@ -454,7 +577,7 @@ sub run_tests {
         }
 
         SKIP: {
-            skip 'This vendor cannot do out-of-line implicit rel defs', 3
+            skip 'This vendor cannot do out-of-line implicit rel defs', 4
                 if $self->{no_implicit_rels};
             my $moniker14 = $monikers->{loader_test14};
             my $class14   = $classes->{loader_test14};
@@ -467,6 +590,8 @@ sub run_tests {
             isa_ok( $rsobj14, "DBIx::Class::ResultSet" ); 
             isa_ok( $rsobj15, "DBIx::Class::ResultSet" );
 
+            ok($class15->column_info('loader_test14')->{is_foreign_key}, 'Foreign key detected');
+
             my $obj15 = $rsobj15->find(1);
             isa_ok( $obj15->loader_test14, $class14 );
         }
@@ -474,7 +599,7 @@ sub run_tests {
 
     # rescan test
     SKIP: {
-        skip $self->{skip_rels}, 4 if $self->{skip_rels};
+        skip $self->{skip_rels}, 5 if $self->{skip_rels};
 
         my @statements_rescan = (
             qq{
@@ -493,14 +618,18 @@ sub run_tests {
         $dbh->disconnect;
 
         my @new = $conn->rescan;
-        is(scalar(@new), 1);
-        is($new[0], 'LoaderTest30');
+        is_deeply(\@new, [ qw/LoaderTest30/ ], "Rescan");
 
         my $rsobj30   = $conn->resultset('LoaderTest30');
         isa_ok($rsobj30, 'DBIx::Class::ResultSet');
         my $obj30 = $rsobj30->find(123);
         isa_ok( $obj30->loader_test2, $class2);
+
+        ok($rsobj30->result_source->column_info('loader_test2')->{is_foreign_key},
+           'Foreign key detected');
     }
+
+    $self->{extra}->{run}->($conn, $monikers, $classes) if $self->{extra}->{run};
 }
 
 sub dbconnect {
@@ -761,6 +890,72 @@ sub create {
 
         q{ INSERT INTO loader_test26 (id,rel1,rel2) VALUES (33,5,7) },
         q{ INSERT INTO loader_test26 (id,rel1,rel2) VALUES (3,42,42) },
+
+        qq{
+            CREATE TABLE loader_test27 (
+                id INTEGER NOT NULL PRIMARY KEY
+            ) $self->{innodb}
+        },
+
+        q{ INSERT INTO loader_test27 (id) VALUES (1) },
+        q{ INSERT INTO loader_test27 (id) VALUES (2) },
+
+        qq{
+            CREATE TABLE loader_test28 (
+                id INTEGER NOT NULL PRIMARY KEY,
+                FOREIGN KEY (id) REFERENCES loader_test27 (id)
+            ) $self->{innodb}
+        },
+
+        q{ INSERT INTO loader_test28 (id) VALUES (1) },
+
+        qq{
+            CREATE TABLE loader_test29 (
+                id INTEGER NOT NULL PRIMARY KEY,
+                fk INTEGER NOT NULL UNIQUE,
+                FOREIGN KEY (fk) REFERENCES loader_test27 (id)
+            ) $self->{innodb}
+        },
+
+        q{ INSERT INTO loader_test29 (id,fk) VALUES (1,1) },
+
+        qq{
+          CREATE TABLE loader_test31 (
+            id INTEGER NOT NULL PRIMARY KEY
+          ) $self->{innodb}
+        },
+        q{ INSERT INTO loader_test31 (id) VALUES (1) },
+
+        qq{
+          CREATE TABLE loader_test32 (
+            id INTEGER NOT NULL PRIMARY KEY,
+            rel1 INTEGER NOT NULL,
+            rel2 INTEGER,
+            FOREIGN KEY (rel1) REFERENCES loader_test31(id),
+            FOREIGN KEY (rel2) REFERENCES loader_test31(id)
+          ) $self->{innodb}
+        },
+        q{ INSERT INTO loader_test32 (id,rel1) VALUES (1,1) },
+
+        qq{
+          CREATE TABLE loader_test33 (
+            id1 INTEGER NOT NULL,
+            id2 INTEGER NOT NULL,
+            PRIMARY KEY (id1,id2)
+          ) $self->{innodb}
+        },
+        q{ INSERT INTO loader_test33 (id1,id2) VALUES (1,2) },
+
+        qq{
+          CREATE TABLE loader_test34 (
+            id INTEGER NOT NULL PRIMARY KEY,
+            rel1 INTEGER NOT NULL,
+            rel2 INTEGER,
+            FOREIGN KEY (id,rel1) REFERENCES loader_test33(id1,id2),
+            FOREIGN KEY (id,rel2) REFERENCES loader_test33(id1,id2)
+          ) $self->{innodb}
+        },
+        q{ INSERT INTO loader_test34 (id,rel1) VALUES (1,2) },
     );
 
     my @statements_advanced = (
@@ -859,6 +1054,8 @@ sub create {
             $dbh->do($_) for (@statements_implicit_rels);
         }
     }
+
+    $dbh->do($_) for @{ $self->{extra}->{create} || [] };
     $dbh->disconnect();
 }
 
@@ -894,6 +1091,13 @@ sub drop_tables {
         loader_test21
         loader_test26
         loader_test25
+        loader_test28
+        loader_test29
+        loader_test27
+        loader_test32
+        loader_test31
+        loader_test34
+        loader_test33
     /;
 
     my @tables_advanced = qw/
@@ -926,6 +1130,8 @@ sub drop_tables {
 
     my $dbh = $self->dbconnect(0);
 
+    $dbh->do("DROP TABLE $_") for @{ $self->{extra}->{drop} || [] };
+
     my $drop_auto_inc = $self->{auto_inc_drop_cb} || sub {};
 
     unless($self->{skip_rels}) {