Added support for proper enums under pg (as of 8.3), with pg version check, and defer...
[dbsrgits/SQL-Translator.git] / t / 38-mysql-producer.t
index 74fdaee..3f0a507 100644 (file)
@@ -19,7 +19,7 @@ use FindBin qw/$Bin/;
 #=============================================================================
 
 BEGIN {
-    maybe_plan(6,
+    maybe_plan(11,
         'YAML',
         'SQL::Translator::Producer::MySQL',
         'Test::Differences',
@@ -67,6 +67,12 @@ schema:
             mysql_charset: utf8
             mysql_collate: utf8_general_ci
           order: 3
+      constraints:
+        - type: UNIQUE
+          fields:
+            - name
+          name: idx_unique_name
+
     thing2:
       name: thing2
       extra:
@@ -83,6 +89,20 @@ schema:
           data_type: int
           order: 1
           is_not_null: 1
+        foo2:
+          name: foo2
+          data_type: int
+          order: 2
+          is_not_null: 1
+      indices:
+        - type: NORMAL
+          fields: 
+            - id
+          name: index_1
+        - type: NORMAL
+          fields: 
+            - id
+          name: index_2
       constraints:
         - type: PRIMARY_KEY
           fields:
@@ -92,33 +112,48 @@ schema:
           type: FOREIGN_KEY
           fields: foo
           name: fk_thing
+        - reference_table: thing
+          type: FOREIGN_KEY
+          fields: foo2
+          name: fk_thing
 
 EOSCHEMA
 
-my $mysql_out = <<EOSQL;
-SET foreign_key_checks=0;
+my @stmts = (
+"SET foreign_key_checks=0;\n\n",
+
+"DROP TABLE IF EXISTS `thing`;\n",
+"CREATE TABLE `thing` (
+  `id` unsigned int auto_increment,
+  `name` varchar(32),
+  `swedish_name` varchar(32) CHARACTER SET swe7,
+  `description` text CHARACTER SET utf8 COLLATE utf8_general_ci,
+  PRIMARY KEY (`id`),
+  UNIQUE `idx_unique_name` (`name`)
+) ENGINE=InnoDB DEFAULT CHARACTER SET latin1 COLLATE latin1_danish_ci;\n\n",
+
+"DROP TABLE IF EXISTS `thing2`;\n",
+"CREATE TABLE `thing2` (
+  `id` integer,
+  `foo` integer,
+  `foo2` integer,
+  INDEX index_1 (`id`),
+  INDEX index_2 (`id`),
+  INDEX (`foo`),
+  INDEX (`foo2`),
+  PRIMARY KEY (`id`, `foo`),
+  CONSTRAINT `fk_thing` FOREIGN KEY (`foo`) REFERENCES `thing` (`id`),
+  CONSTRAINT `fk_thing_1` FOREIGN KEY (`foo2`) REFERENCES `thing` (`id`)
+) ENGINE=InnoDB;\n\n",
 
-CREATE TABLE thing (
-  id unsigned int auto_increment,
-  name varchar(32),
-  swedish_name varchar(32) CHARACTER SET swe7,
-  description text CHARACTER SET utf8 COLLATE utf8_general_ci,
-  INDEX (id),
-  PRIMARY KEY (id)
-) Type=InnoDB DEFAULT CHARACTER SET latin1 COLLATE latin1_danish_ci;
+"SET foreign_key_checks=1;\n\n"
 
-CREATE TABLE thing2 (
-  id integer,
-  foo integer,
-  INDEX (id),
-  INDEX (foo),
-  PRIMARY KEY (id, foo),
-  CONSTRAINT thing2_fk_thing FOREIGN KEY (foo) REFERENCES thing (id)
-) Type=InnoDB;
+);
 
-SET foreign_key_checks=1;
+my @stmts_no_drop = grep {$_ !~ /^DROP TABLE/} @stmts;
+
+my $mysql_out = join("", @stmts_no_drop);
 
-EOSQL
 
     my $sqlt;
     $sqlt = SQL::Translator->new(
@@ -127,12 +162,39 @@ EOSQL
 #        debug          => 1,
         from           => "YAML",
         to             => "MySQL",
+        quote_table_names => 1,
+        quote_field_names => 1
     );
 
     my $out = $sqlt->translate(\$yaml_in)
     or die "Translate error:".$sqlt->error;
-    ok $out ne ""                 ,"Produced something!";
-    eq_or_diff $out, $mysql_out   ,"Output looks right";
+    ok $out ne "",                    "Produced something!";
+    eq_or_diff $out, $mysql_out,      "Scalar output looks right with quoting";
+
+    my @out = $sqlt->translate(\$yaml_in)
+      or die "Translat eerror:".$sqlt->error;
+    is_deeply \@out, \@stmts_no_drop, "Array output looks right with quoting";
+
+
+    @{$sqlt}{qw/quote_table_names quote_field_names/} = (0,0);
+    $out = $sqlt->translate(\$yaml_in)
+      or die "Translate error:".$sqlt->error;
+
+    @out = $sqlt->translate(\$yaml_in)
+      or die "Translate error:".$sqlt->error;
+    $mysql_out =~ s/`//g;
+    my @unquoted_stmts = map { s/`//g; $_} @stmts_no_drop;
+    eq_or_diff $out, $mysql_out,       "Output looks right without quoting";
+    is_deeply \@out, \@unquoted_stmts, "Array output looks right without quoting";
+
+    @{$sqlt}{qw/add_drop_table quote_field_names quote_table_names/} = (1,1,1);
+    @out = $sqlt->translate(\$yaml_in)
+      or die "Translat eerror:".$sqlt->error;
+    $out = $sqlt->translate(\$yaml_in)
+      or die "Translat eerror:".$sqlt->error;
+
+    eq_or_diff $out, join("", @stmts), "Output looks right with DROP TABLEs";
+    is_deeply \@out, \@stmts,          "Array output looks right with DROP TABLEs";
 }
 
 ###############################################################################