Add support for proper boolean fields in the mysql producer, as of v4.x
Jess Robinson [Wed, 26 Mar 2008 20:55:10 +0000 (20:55 +0000)]
Changes
bin/sqlt
lib/SQL/Translator.pm
lib/SQL/Translator/Producer/MySQL.pm
t/38-mysql-producer.t

diff --git a/Changes b/Changes
index e7d31be..ca1afd0 100644 (file)
--- a/Changes
+++ b/Changes
@@ -1,4 +1,5 @@
 
+* Added support for proper booleans in the mysql producer, when a mysql version of at least 4.x is supplied
 * Added support for proper enums under pg (as of 8.3), with pg version check, and deferrable constraints
 
 # ----------------------------------------------------------
index 2608167..afc05e7 100755 (executable)
--- a/bin/sqlt
+++ b/bin/sqlt
@@ -69,6 +69,10 @@ To translate a schema:
     --mysql-parser-version  Target MySQL parser version for dealing with
                               /*! comments; default = 30000
 
+  MySQL Producer Options:
+
+    --mysql-version  MySQL server version
+
   General Producer Options
 
     --producer-db-user   Database user for producer
@@ -189,6 +193,7 @@ my $producer_dsn;     # db_user "
 my $add_truncate;
 my $mysql_parser_version;  # MySQL parser arg for /*! comments
 my $postgres_version; # PostgreSQL version
+my $mysql_version; # MySQL version
 
 GetOptions(
     'add-drop-table'   => \$add_drop_table,
@@ -232,6 +237,7 @@ GetOptions(
     'version'          => \$show_version,
     'mysql-parser-version=i' => \$mysql_parser_version,
     'postgres-version=f' => \$postgres_version,
+    'mysql-version=f' => \$mysql_version,
 ) or pod2usage(2);
 
 if ($use_same_auth) {
@@ -296,6 +302,7 @@ my $translator           =  SQL::Translator->new(
         indent           => $indent,
         newlines         => $newlines,
         postgres_version => $postgres_version,
+        mysql_version    => $mysql_version,
            package_name     => $package_name,
     },
 );
index 645102c..890b3ca 100644 (file)
@@ -1299,6 +1299,8 @@ The following people have contributed to the SQLFairy project:
 
 =item * Ying Zhang <zyolive@yahoo.com>
 
+=item * Daniel Ruoso <daniel@ruoso.com>
+
 =back
 
 If you would like to contribute to the project, you can send patches
index 1dfede4..a57bd9c 100644 (file)
@@ -240,6 +240,8 @@ sub produce {
     my $add_drop_table = $translator->add_drop_table;
     my $schema         = $translator->schema;
     my $show_warnings  = $translator->show_warnings || 0;
+    my $producer_args  = $translator->producer_args;
+    my $mysql_version  = $producer_args->{mysql_version} || 0;
 
     my ($qt, $qf, $qc) = ('','', '');
     $qt = '`' if $translator->quote_table_names;
@@ -266,7 +268,8 @@ sub produce {
                                          show_warnings     => $show_warnings,
                                          no_comments       => $no_comments,
                                          quote_table_names => $qt,
-                                         quote_field_names => $qf
+                                         quote_field_names => $qf,
+                                         mysql_version     => $mysql_version
                                          });
     }
 
@@ -421,8 +424,13 @@ sub create_field
         @size      = ();
     }
     elsif ( $data_type =~ /boolean/i ) {
-        $data_type = 'enum';
-        $commalist = "'0','1'";
+        my $mysql_version = $options->{mysql_version} || 0;
+        if ($mysql_version >= 4) {
+            $data_type = 'boolean';
+        } else {
+            $data_type = 'enum';
+            $commalist = "'0','1'";
+        }
     }
     elsif ( exists $translate{ lc $data_type } ) {
         $data_type = $translate{ lc $data_type };
index 3f0a507..ce5bbdd 100644 (file)
@@ -19,7 +19,7 @@ use FindBin qw/$Bin/;
 #=============================================================================
 
 BEGIN {
-    maybe_plan(11,
+    maybe_plan(14,
         'YAML',
         'SQL::Translator::Producer::MySQL',
         'Test::Differences',
@@ -236,3 +236,17 @@ is($add_field, 'ALTER TABLE mytable ADD COLUMN myfield VARCHAR(10)', 'Add field
 
 my $drop_field = SQL::Translator::Producer::MySQL::drop_field($field2);
 is($drop_field, 'ALTER TABLE mytable DROP COLUMN myfield', 'Drop field works');
+
+my $field3 = SQL::Translator::Schema::Field->new( name      => 'myfield',
+                                                  table => $table,
+                                                  data_type => 'boolean',
+                                                  is_nullable => 0,
+                                                  is_foreign_key => 0,
+                                                  is_unique => 0 );
+
+my $field3_sql = SQL::Translator::Producer::MySQL::create_field($field3, { mysql_version => 4.1 });
+is($field3_sql, 'myfield boolean NOT NULL', 'For Mysql >= 4, use boolean type');
+$field3_sql = SQL::Translator::Producer::MySQL::create_field($field3, { mysql_version => 3.22 });
+is($field3_sql, "myfield enum('0','1') NOT NULL", 'For Mysql < 4, use enum for boolean type');
+$field3_sql = SQL::Translator::Producer::MySQL::create_field($field3,);
+is($field3_sql, "myfield enum('0','1') NOT NULL", 'When no version specified, use enum for boolean type');