Use precompiled Parse::RecDescent parsers for moar speed
[dbsrgits/SQL-Translator.git] / t / 02mysql-parser.t
index 1ed7001..4dd921c 100644 (file)
@@ -9,19 +9,21 @@ use SQL::Translator;
 use SQL::Translator::Schema::Constants;
 use SQL::Translator::Utils qw//;
 use Test::SQL::Translator qw(maybe_plan);
+use FindBin qw/$Bin/;
 
 BEGIN {
-    maybe_plan(265, "SQL::Translator::Parser::MySQL");
+    maybe_plan(337, "SQL::Translator::Parser::MySQL");
     SQL::Translator::Parser::MySQL->import('parse');
 }
 
 {
     my $tr = SQL::Translator->new;
-    my $data = q|create table sessions (
+    my $data = q|create table "sessions" (
         id char(32) not null default '0' primary key,
         a_session text,
         ssn varchar(12) unique key,
-        age int key
+        age int key,
+        fulltext key `session_fulltext` (a_session)
     );|;
 
     my $val = parse($tr, $data);
@@ -51,7 +53,9 @@ BEGIN {
     is( $f2->is_primary_key, 0, 'Field is not PK' );
 
     my @indices = $table->get_indices;
-    is( scalar @indices, 1, 'Right number of indices (1)' );
+    is( scalar @indices, 2, 'Right number of indices (2)' );
+    my $i = pop @indices;
+    is( $i->type, 'FULLTEXT', 'Found fulltext' );
 
     my @constraints = $table->get_constraints;
     is( scalar @constraints, 2, 'Right number of constraints (2)' );
@@ -67,20 +71,22 @@ BEGIN {
     my $tr = SQL::Translator->new;
     my $data = parse($tr, 
         q[
-            CREATE TABLE check (
+            CREATE TABLE `check` (
               check_id int(7) unsigned zerofill NOT NULL default '0000000' 
                 auto_increment primary key,
               successful date NOT NULL default '0000-00-00',
               unsuccessful date default '0000-00-00',
               i1 int(11) default '0' not null,
               s1 set('a','b','c') default 'b',
-              e1 enum('a','b','c') default 'c',
+              e1 enum('a','b','c') default "c",
               name varchar(30) default NULL,
               foo_type enum('vk','ck') NOT NULL default 'vk',
               date timestamp,
               time_stamp2 timestamp,
+              foo_enabled bit(1) default b'0',
+              bar_enabled bit(1) default b"1",
               KEY (i1),
-              UNIQUE (date, i1),
+              UNIQUE (date, i1) USING BTREE,
               KEY date_idx (date),
               KEY name_idx (name(10))
             ) TYPE=MyISAM PACK_KEYS=1;
@@ -95,7 +101,7 @@ BEGIN {
     is( $table->name, 'check', 'Found "check" table' );
 
     my @fields = $table->get_fields;
-    is( scalar @fields, 10, 'Right number of fields (10)' );
+    is( scalar @fields, 12, 'Right number of fields (12)' );
     my $f1 = shift @fields;
     is( $f1->name, 'check_id', 'First field name is "check_id"' );
     is( $f1->data_type, 'int', 'Type is "int"' );
@@ -186,6 +192,22 @@ BEGIN {
     is( $f10->default_value, undef, 'Default value is undefined' );
     is( $f10->is_primary_key, 0, 'Field is not PK' );
 
+    my $f11 = shift @fields;
+    is( $f11->name, 'foo_enabled', 'Eleventh field name is "foo_enabled"' );
+    is( $f11->data_type, 'bit', 'Type is "bit"' );
+    is( $f11->size, 1, 'Size is "1"' );
+    is( $f11->is_nullable, 1, 'Field can be null' );
+    is( $f11->default_value, '0', 'Default value is 0' );
+    is( $f11->is_primary_key, 0, 'Field is not PK' );
+
+    my $f12 = shift @fields;
+    is( $f12->name, 'bar_enabled', 'Twelveth field name is "bar_enabled"' );
+    is( $f12->data_type, 'bit', 'Type is "bit"' );
+    is( $f12->size, 1, 'Size is "1"' );
+    is( $f12->is_nullable, 1, 'Field can be null' );
+    is( $f12->default_value, '1', 'Default value is 1' );
+    is( $f12->is_primary_key, 0, 'Field is not PK' );
+
     my @indices = $table->get_indices;
     is( scalar @indices, 3, 'Right number of indices (3)' );
 
@@ -231,9 +253,9 @@ BEGIN {
               tax                       decimal(8,2),
               shipping_charge           decimal(8,2),
               price_paid                decimal(8,2),
-              PRIMARY KEY (order_id),
-              KEY (status),
-              KEY (billing_address_id),
+              PRIMARY KEY (order_id) USING BTREE,
+              KEY (status) USING BTREE,
+              KEY USING BTREE (billing_address_id),
               KEY (shipping_address_id),
               KEY (member_id, store_id),
               FOREIGN KEY (status)              REFERENCES order_status(id) MATCH FULL ON DELETE CASCADE ON UPDATE CASCADE,
@@ -507,6 +529,32 @@ BEGIN {
                                `m`.`user_id` AS `user_access` 
                                from (`asset` `a` join `M_ACCESS_CONTROL` `m` on((`a`.`acl_id` = `m`.`acl_id`))) */;
                        DELIMITER ;;
+                       /*!50001 CREATE */
+                       /*! VIEW `vs_asset2` AS 
+                               select `a`.`asset_id` AS `asset_id`,`a`.`fq_name` AS `fq_name`,
+                               `cfgmgmt_mig`.`ap_extract_folder`(`a`.`fq_name`) AS `folder_name`,
+                               `cfgmgmt_mig`.`ap_extract_asset`(`a`.`fq_name`) AS `asset_name`,
+                               `a`.`annotation` AS `annotation`,`a`.`asset_type` AS `asset_type`,
+                               `a`.`foreign_asset_id` AS `foreign_asset_id`,
+                               `a`.`foreign_asset_id2` AS `foreign_asset_id2`,`a`.`dateCreated` AS `date_created`,
+                               `a`.`dateModified` AS `date_modified`,`a`.`container_id` AS `container_id`,
+                               `a`.`creator_id` AS `creator_id`,`a`.`modifier_id` AS `modifier_id`,
+                               `m`.`user_id` AS `user_access` 
+                               from (`asset` `a` join `M_ACCESS_CONTROL` `m` on((`a`.`acl_id` = `m`.`acl_id`))) */;
+                       DELIMITER ;;
+                       /*!50001 CREATE OR REPLACE */
+                       /*! VIEW `vs_asset3` AS 
+                               select `a`.`asset_id` AS `asset_id`,`a`.`fq_name` AS `fq_name`,
+                               `cfgmgmt_mig`.`ap_extract_folder`(`a`.`fq_name`) AS `folder_name`,
+                               `cfgmgmt_mig`.`ap_extract_asset`(`a`.`fq_name`) AS `asset_name`,
+                               `a`.`annotation` AS `annotation`,`a`.`asset_type` AS `asset_type`,
+                               `a`.`foreign_asset_id` AS `foreign_asset_id`,
+                               `a`.`foreign_asset_id2` AS `foreign_asset_id2`,`a`.`dateCreated` AS `date_created`,
+                               `a`.`dateModified` AS `date_modified`,`a`.`container_id` AS `container_id`,
+                               `a`.`creator_id` AS `creator_id`,`a`.`modifier_id` AS `modifier_id`,
+                               `m`.`user_id` AS `user_access` 
+                               from (`asset` `a` join `M_ACCESS_CONTROL` `m` on((`a`.`acl_id` = `m`.`acl_id`))) */;
+                       DELIMITER ;;
                        /*!50003 CREATE*/ /*!50020 DEFINER=`cmdomain`@`localhost`*/ /*!50003 FUNCTION `ap_from_millitime_nullable`( millis_since_1970 BIGINT ) RETURNS timestamp
                        DETERMINISTIC
                                BEGIN
@@ -581,17 +629,24 @@ BEGIN {
     my $t1f2 = shift @fields;
     is( $t1f2->data_type, 'timestamp', 'Field is a timestamp' );
     ok( !$t1f2->is_nullable, 'Field is not nullable' );
-    is( $t1f2->default_value, 'CURRENT_TIMESTAMP', 'Field has right default value' );
+    is_deeply(
+      $t1f2->default_value,
+      \'CURRENT_TIMESTAMP',
+      'Field has right default value' 
+    );
     is( $t1f2->extra('on update'), 'CURRENT_TIMESTAMP', 'Field has right on update qualifier' );
-    
+
     my @views = $schema->get_views;
-    is( scalar @views, 1, 'Right number of views (1)' );
-    my $view1 = shift @views;
+    is( scalar @views, 3, 'Right number of views (3)' );
+
+    my ($view1, $view2, $view3) = @views;
     is( $view1->name, 'vs_asset', 'Found "vs_asset" view' );
-       like($view1->sql, qr/ALGORITHM=UNDEFINED/, "Detected algorithm");
-       like($view1->sql, qr/vs_asset/, "Detected view vs_asset");
-       unlike($view1->sql, qr/cfgmgmt_mig/, "Did not detect cfgmgmt_mig");
-    
+    is( $view2->name, 'vs_asset2', 'Found "vs_asset2" view' );
+    is( $view3->name, 'vs_asset3', 'Found "vs_asset3" view' );
+    like($view1->sql, qr/ALGORITHM=UNDEFINED/, "Detected algorithm");
+    like($view1->sql, qr/vs_asset/, "Detected view vs_asset");
+    unlike($view1->sql, qr/cfgmgmt_mig/, "Did not detect cfgmgmt_mig");
+
     my @procs = $schema->get_procedures;
     is( scalar @procs, 2, 'Right number of procedures (2)' );
     my $proc1 = shift @procs;
@@ -722,3 +777,141 @@ ok ($@, 'Exception thrown on invalid version string');
     is( $c->type, PRIMARY_KEY, 'Constraint is a PK' );
     is( join(',', $c->fields), 'id', 'Constraint is on "id"' );
 }
+
+{
+    my @data = (
+        q|create table quote (
+            id int(11) NOT NULL auto_increment,
+            PRIMARY KEY (id)
+        ) ENGINE="innodb";|,
+        q|create table quote (
+            id int(11) NOT NULL auto_increment,
+            PRIMARY KEY (id)
+        ) ENGINE='innodb';|,
+        q|create table quote (
+            id int(11) NOT NULL auto_increment,
+            PRIMARY KEY (id)
+        ) ENGINE=innodb;|,
+    );
+    for my $data (@data) {
+        my $tr = SQL::Translator->new;
+
+        my $val = parse($tr, $data);
+        my $schema = $tr->schema;
+        is( $schema->is_valid, 1, 'Schema is valid' );
+        my @tables = $schema->get_tables;
+        is( scalar @tables, 1, 'Right number of tables (1)' );
+        my $table  = shift @tables;
+        is( $table->name, 'quote', 'Found "quote" table' );
+
+        my $tableTypeFound = 0;
+        for my $t_option_ref ( $table->options ) {
+        my($key, $value) = %{$t_option_ref};
+        if ( $key eq 'ENGINE' ) {
+            is($value, 'innodb', 'Table has right table engine option' );
+            $tableTypeFound = 1;
+        }
+        }
+
+        fail('Table did not have a type option') unless $tableTypeFound;
+
+        my @fields = $table->get_fields;
+        my $f1 = shift @fields;
+        is( $f1->name, 'id', 'First field name is "id"' );
+        is( $f1->data_type, 'int', 'Type is "int"' );
+        is( $f1->size, 11, 'Size is "11"' );
+        is( $f1->is_nullable, 0, 'Field cannot be null' );
+        is( $f1->is_primary_key, 1, 'Field is PK' );
+    }
+}
+
+{
+    my $tr = SQL::Translator->new;
+    my $data = q|create table "sessions" (
+        id char(32) not null default '0' primary key,
+        ssn varchar(12) NOT NULL default 'test single quotes like in you''re',
+        user varchar(20) NOT NULL default 'test single quotes escaped like you\'re',
+        key using btree (ssn)
+    );|;
+
+    my $val = parse($tr, $data);
+    my $schema = $tr->schema;
+    is( $schema->is_valid, 1, 'Schema is valid' );
+    my @tables = $schema->get_tables;
+    is( scalar @tables, 1, 'Right number of tables (1)' );
+    my $table  = shift @tables;
+    is( $table->name, 'sessions', 'Found "sessions" table' );
+
+    my @fields = $table->get_fields;
+    is( scalar @fields, 3, 'Right number of fields (3)' );
+    my $f1 = shift @fields;
+    my $f2 = shift @fields;
+    my $f3 = shift @fields;
+    is( $f1->name, 'id', 'First field name is "id"' );
+    is( $f1->data_type, 'char', 'Type is "char"' );
+    is( $f1->size, 32, 'Size is "32"' );
+    is( $f1->is_nullable, 0, 'Field cannot be null' );
+    is( $f1->default_value, '0', 'Default value is "0"' );
+    is( $f1->is_primary_key, 1, 'Field is PK' );
+
+    is( $f2->name, 'ssn', 'Second field name is "ssn"' );
+    is( $f2->data_type, 'varchar', 'Type is "varchar"' );
+    is( $f2->size, 12, 'Size is "12"' );
+    is( $f2->is_nullable, 0, 'Field can not be null' );
+    is( $f2->default_value, "test single quotes like in you''re", "Single quote in default value is escaped properly" );
+    is( $f2->is_primary_key, 0, 'Field is not PK' );
+
+    # this is more of a sanity test because the original sqlt regex for default looked for an escaped quote represented as \'
+    # however in mysql 5.x (and probably other previous versions) still actually outputs that as '' 
+    is( $f3->name, 'user', 'Second field name is "user"' );
+    is( $f3->data_type, 'varchar', 'Type is "varchar"' );
+    is( $f3->size, 20, 'Size is "20"' );
+    is( $f3->is_nullable, 0, 'Field can not be null' );
+    is( $f3->default_value, "test single quotes escaped like you\\'re", "Single quote in default value is escaped properly" );
+    is( $f3->is_primary_key, 0, 'Field is not PK' );
+}
+
+{
+    # silence PR::D from spewing on STDERR
+    local $::RD_ERRORS = 0;
+    local $::RD_WARN = 0;
+    local $::RD_HINT = 0;
+    my $tr = SQL::Translator->new;
+    my $data = q|create table "sessions" (
+        id char(32) not null default,
+        ssn varchar(12) NOT NULL default 'test single quotes like in you''re',
+        user varchar(20) NOT NULL default 'test single quotes escaped like you\'re',
+        key using btree (ssn)
+    );|;
+
+    my $val= parse($tr,$data);
+    ok ($tr->error =~ /Parse failed\./, 'Parse failed error without default value');
+}
+
+{
+    # make sure empty string default value still works
+    my $tr = SQL::Translator->new;
+    my $data = q|create table "sessions" (
+        id char(32) not null DEFAULT '',
+        ssn varchar(12) NOT NULL default "",
+        key using btree (ssn)
+    );|;
+    my $val= parse($tr,$data);
+
+    my @fields = $tr->schema->get_table('sessions')->get_fields;
+    is (scalar @fields, 2, 'Both fields parsed correctly');
+    for (@fields) {
+      my $def = $_->default_value;
+      ok( (defined $def and $def eq ''), "Defaults on field $_ correct" );
+    }
+}
+
+{
+    # test rt70437 and rt71468
+    my $file = "$Bin/data/mysql/cashmusic_db.sql";
+    ok (-f $file,"File exists");
+    my $tr = SQL::Translator->new( parser => 'MySQL');
+    ok ($tr->translate($file),'File translated');
+    ok (!$tr->error, 'no error');
+    ok (my $schema = $tr->schema, 'got schema');
+}