test + fix for Oracle multi-column constraint generation
Alexander Hartmaier [Sat, 13 Nov 2010 13:56:25 +0000 (13:56 +0000)]
lib/SQL/Translator/Producer/Oracle.pm
t/55-oracle-producer.t [new file with mode: 0644]

index f8cbe5f..5db9d7d 100644 (file)
@@ -327,7 +327,8 @@ sub create_table {
         for my $c ( $table->get_constraints ) {
             my $name    = $c->name || '';
             my @fields  = map { quote($_,$qf) } $c->fields;
-            my @rfields = quote($c->reference_fields,$qf);
+            my @rfields = map { quote($_,$qf) } $c->reference_fields;
+
             next if !@fields && $c->type ne CHECK_C;
 
             if ( $c->type eq PRIMARY_KEY ) {
diff --git a/t/55-oracle-producer.t b/t/55-oracle-producer.t
new file mode 100644 (file)
index 0000000..8f90c31
--- /dev/null
@@ -0,0 +1,83 @@
+#!/usr/bin/perl
+# vim: set ft=perl:
+
+use strict;
+use warnings;
+use Test::More;
+
+use SQL::Translator::Schema::Constants;
+use SQL::Translator::Schema::Table;
+use SQL::Translator::Schema::Field;
+use SQL::Translator::Schema::Constraint;
+use SQL::Translator::Producer::Oracle;
+
+{
+    my $table1 = SQL::Translator::Schema::Table->new( name => 'table1' );
+
+    my $table1_field1 = $table1->add_field(
+        name              => 'fk_col1',
+        data_type         => 'NUMBER',
+        size              => 6,
+        default_value     => undef,
+        is_auto_increment => 0,
+        is_nullable       => 0,
+        is_foreign_key    => 1,
+        is_unique         => 0
+    );
+
+    my $table1_field2 = $table1->add_field(
+        name              => 'fk_col2',
+        data_type         => 'VARCHAR',
+        size              => 64,
+        default_value     => undef,
+        is_auto_increment => 0,
+        is_nullable       => 0,
+        is_foreign_key    => 1,
+        is_unique         => 0
+    );
+
+    my $table2 = SQL::Translator::Schema::Table->new( name => 'table2' );
+
+    my $table2_field1 = $table2->add_field(
+        name              => 'fk_col1',
+        data_type         => 'NUMBER',
+        size              => 6,
+        default_value     => undef,
+        is_auto_increment => 0,
+        is_nullable       => 0,
+        is_foreign_key    => 0,
+        is_unique         => 0
+    );
+
+    my $table2_field2 = $table2->add_field(
+        name              => 'fk_col2',
+        data_type         => 'VARCHAR',
+        size              => 64,
+        default_value     => undef,
+        is_auto_increment => 0,
+        is_nullable       => 0,
+        is_foreign_key    => 0,
+        is_unique         => 0
+    );
+
+    my $constraint1 = $table1->add_constraint(
+        name             => 'foo',
+        fields           => [qw/ fk_col1 fk_col2 /],
+        reference_fields => [qw/ fk_col1 fk_col2 /],
+        reference_table  => 'table2',
+        type             => FOREIGN_KEY,
+    );
+
+    my ($table1_def, $fk1_def, $trigger1_def,
+        $index1_def, $constraint1_def
+    ) = SQL::Translator::Producer::Oracle::create_table($table1);
+
+    is_deeply(
+        $fk1_def,
+        [   'ALTER TABLE table1 ADD CONSTRAINT table1_fk_col1_fk_col2_fk FOREIGN KEY (fk_col1, fk_col2) REFERENCES table2 (fk_col1, fk_col2)'
+        ],
+        'correct "CREATE CONSTRAINT" SQL'
+    );
+}
+
+done_testing();