Merge 'trunk' into 'versioned_enhancements'
[dbsrgits/DBIx-Class-Historic.git] / lib / SQL / Translator / Parser / DBIx / Class.pm
index 1f09482..4f48015 100644 (file)
@@ -5,6 +5,8 @@ package # hide from PAUSE
 
 # Some mistakes the fault of Matt S Trout
 
+# Others the fault of Ash Berlin
+
 use strict;
 use warnings;
 use vars qw($DEBUG $VERSION @EXPORT_OK);
@@ -65,8 +67,6 @@ sub parse {
 
     foreach my $moniker (@monikers)
     {
-        #eval "use $tableclass";
-        #print("Can't load $tableclass"), next if($@);
         my $source = $dbixschema->source($moniker);
 
         next if $seen_tables{$source->name}++;
@@ -78,7 +78,7 @@ sub parse {
         my $colcount = 0;
         foreach my $col ($source->columns)
         {
-            # assuming column_info in dbix is the same as DBI (?)
+            # assuming column_info in dbic is the same as DBI (?)
             # data_type is a number, column_type is text?
             my %colinfo = (
               name => $col,
@@ -156,19 +156,37 @@ sub parse {
                    )
                 {
                     $created_FK_rels{$rel_table}->{$key_test} = 1;
-                    $table->add_constraint(
-                                type             => 'foreign_key',
-                                name             => "fk_$keys[0]",
-                                fields           => \@keys,
-                                reference_fields => \@refkeys,
-                                reference_table  => $rel_table,
-                                on_delete        => $on_delete,
-                                on_update        => $on_update
-                    );
+                    if (scalar(@keys)) {
+                        $table->add_constraint(
+                                    type             => 'foreign_key',
+                                    name             => "fk_$keys[0]",
+                                    fields           => \@keys,
+                                    reference_fields => \@refkeys,
+                                    reference_table  => $rel_table,
+                                    on_delete        => $on_delete,
+                                    on_update        => $on_update
+                        );
+                    
+                        my $index = $table->add_index(
+                                    name   => $table->name . "_fk_$keys[0]",
+                                    fields => \@keys,
+                                    type   => 'NORMAL',
+                        );
+                    }
+
                 }
             }
         }
+
+        if ($source->result_class->can('sqlt_deploy_hook')) {
+          $source->result_class->sqlt_deploy_hook($table);
+        }
     }
+
+    if ($dbixschema->can('sqlt_deploy_hook')) {
+      $dbixschema->sqlt_deploy_hook($schema);
+    }
+
     return 1;
 }