Clean up option parsing and fix identifier quoting in Producer::MySQL
[dbsrgits/SQL-Translator.git] / t / 30sqlt-new-diff-mysql.t
index 5a9997e..2df13b5 100644 (file)
@@ -13,11 +13,11 @@ use Test::SQL::Translator qw(maybe_plan);
 use SQL::Translator::Schema::Constants;
 use Storable 'dclone';
 
-plan tests => 8;
+plan tests => 9;
 
 use_ok('SQL::Translator::Diff') or die "Cannot continue\n";
 
-my $tr            = SQL::Translator->new;
+my $tr = SQL::Translator->new;
 
 my ( $source_schema, $target_schema, $parsed_sql_schema ) = map {
     my $t = SQL::Translator->new;
@@ -25,7 +25,7 @@ my ( $source_schema, $target_schema, $parsed_sql_schema ) = map {
       or die $tr->error;
     my $out = $t->translate( catfile($Bin, qw/data diff/, $_ ) )
       or die $tr->error;
-    
+
     my $schema = $t->schema;
     unless ( $schema->name ) {
         $schema->name( $_ );
@@ -34,7 +34,19 @@ my ( $source_schema, $target_schema, $parsed_sql_schema ) = map {
 } (qw( create1.yml create2.yml ));
 
 # Test for differences
-my $out = SQL::Translator::Diff::schema_diff( $source_schema, 'MySQL', $target_schema, 'MySQL', { no_batch_alters => 1, producer_options => { quote_table_names => 0 } } );
+my @out = SQL::Translator::Diff::schema_diff(
+    $source_schema, 'MySQL',
+    $target_schema, 'MySQL',
+    {
+        no_batch_alters  => 1,
+        producer_args => { quote_identifiers => 0 }
+    }
+);
+
+ok( @out, 'Got a list' );
+
+my $out = join('', @out);
+
 eq_or_diff($out, <<'## END OF DIFF', "Diff as expected");
 -- Convert schema 'create1.yml' to 'create2.yml':;
 
@@ -43,7 +55,7 @@ BEGIN;
 SET foreign_key_checks=0;
 
 CREATE TABLE added (
-  id integer(11)
+  id integer(11) NULL
 );
 
 SET foreign_key_checks=1;
@@ -58,19 +70,19 @@ ALTER TABLE person DROP INDEX u_name;
 
 ALTER TABLE employee DROP COLUMN job_title;
 
-ALTER TABLE new_name ADD COLUMN new_field integer;
+ALTER TABLE new_name ADD COLUMN new_field integer NULL;
 
-ALTER TABLE person ADD COLUMN is_rock_star tinyint(4) DEFAULT '1';
+ALTER TABLE person ADD COLUMN is_rock_star tinyint(4) NULL DEFAULT 1;
 
 ALTER TABLE person CHANGE COLUMN person_id person_id integer(11) NOT NULL auto_increment;
 
 ALTER TABLE person CHANGE COLUMN name name varchar(20) NOT NULL;
 
-ALTER TABLE person CHANGE COLUMN age age integer(11) DEFAULT '18';
+ALTER TABLE person CHANGE COLUMN age age integer(11) NULL DEFAULT 18;
 
-ALTER TABLE person CHANGE COLUMN iq iq integer(11) DEFAULT '0';
+ALTER TABLE person CHANGE COLUMN iq iq integer(11) NULL DEFAULT 0;
 
-ALTER TABLE person CHANGE COLUMN description physical_description text;
+ALTER TABLE person CHANGE COLUMN description physical_description text NULL;
 
 ALTER TABLE person ADD UNIQUE INDEX unique_name (name);
 
@@ -94,7 +106,7 @@ COMMIT;
 $out = SQL::Translator::Diff::schema_diff($source_schema, 'MySQL', $target_schema, 'MySQL',
     { ignore_index_names => 1,
       ignore_constraint_names => 1,
-      producer_options => { quote_table_names => 0 },
+      producer_args => { quote_identifiers => 0 },
     });
 
 eq_or_diff($out, <<'## END OF DIFF', "Diff as expected");
@@ -105,7 +117,7 @@ BEGIN;
 SET foreign_key_checks=0;
 
 CREATE TABLE added (
-  id integer(11)
+  id integer(11) NULL
 );
 
 SET foreign_key_checks=1;
@@ -113,15 +125,15 @@ SET foreign_key_checks=1;
 ALTER TABLE employee DROP COLUMN job_title;
 
 ALTER TABLE old_name RENAME TO new_name,
-                     ADD COLUMN new_field integer;
+                     ADD COLUMN new_field integer NULL;
 
 ALTER TABLE person DROP INDEX UC_age_name,
-                   ADD COLUMN is_rock_star tinyint(4) DEFAULT '1',
+                   ADD COLUMN is_rock_star tinyint(4) NULL DEFAULT 1,
                    CHANGE COLUMN person_id person_id integer(11) NOT NULL auto_increment,
                    CHANGE COLUMN name name varchar(20) NOT NULL,
-                   CHANGE COLUMN age age integer(11) DEFAULT '18',
-                   CHANGE COLUMN iq iq integer(11) DEFAULT '0',
-                   CHANGE COLUMN description physical_description text,
+                   CHANGE COLUMN age age integer(11) NULL DEFAULT 18,
+                   CHANGE COLUMN iq iq integer(11) NULL DEFAULT 0,
+                   CHANGE COLUMN description physical_description text NULL,
                    ADD UNIQUE UC_person_id (person_id),
                    ADD UNIQUE UC_age_name (age, name),
                    ENGINE=InnoDB;
@@ -155,18 +167,18 @@ eq_or_diff($out, <<'## END OF DIFF', "No differences found");
 
   # Lets remove the renamed table so we dont have to change the SQL or other tests
   $target_schema->drop_table('new_name');
-  
+
   my $schema = $t->schema;
   unless ( $schema->name ) {
       $schema->name( 'create.sql' );
   }
 
-  # Now lets change the type of one of the 'integer' columns so that it 
+  # Now lets change the type of one of the 'integer' columns so that it
   # matches what the mysql parser sees for '<col> interger'.
   my $field = $target_schema->get_table('employee')->get_field('employee_id');
   $field->data_type('integer');
   $field->size(0);
-  $out = SQL::Translator::Diff::schema_diff($schema, 'MySQL', $target_schema, 'MySQL', { producer_options => { quote_table_names => 0 } } );
+  $out = SQL::Translator::Diff::schema_diff($schema, 'MySQL', $target_schema, 'MySQL', { producer_args => { quote_identifiers => 0 } } );
   eq_or_diff($out, <<'## END OF DIFF', "No differences found");
 -- Convert schema 'create.sql' to 'create2.yml':;
 
@@ -175,7 +187,7 @@ BEGIN;
 SET foreign_key_checks=0;
 
 CREATE TABLE added (
-  id integer(11)
+  id integer(11) NULL
 );
 
 SET foreign_key_checks=1;
@@ -186,12 +198,13 @@ ALTER TABLE employee DROP FOREIGN KEY FK5302D47D93FE702E,
 
 ALTER TABLE person DROP INDEX UC_age_name,
                    DROP INDEX u_name,
-                   ADD COLUMN is_rock_star tinyint(4) DEFAULT '1',
+                   ADD COLUMN is_rock_star tinyint(4) NULL DEFAULT 1,
+                   ADD COLUMN value double(8, 2) NULL DEFAULT 0.00,
                    CHANGE COLUMN person_id person_id integer(11) NOT NULL auto_increment,
                    CHANGE COLUMN name name varchar(20) NOT NULL,
-                   CHANGE COLUMN age age integer(11) DEFAULT '18',
-                   CHANGE COLUMN iq iq integer(11) DEFAULT '0',
-                   CHANGE COLUMN description physical_description text,
+                   CHANGE COLUMN age age integer(11) NULL DEFAULT 18,
+                   CHANGE COLUMN iq iq integer(11) NULL DEFAULT 0,
+                   CHANGE COLUMN description physical_description text NULL,
                    ADD UNIQUE INDEX unique_name (name),
                    ADD UNIQUE UC_person_id (person_id),
                    ADD UNIQUE UC_age_name (age, name),
@@ -245,7 +258,7 @@ BEGIN;
 
 ALTER TABLE employee DROP FOREIGN KEY FK5302D47D93FE702E_diff;
 
-ALTER TABLE employee ADD COLUMN new integer,
+ALTER TABLE employee ADD COLUMN new integer NULL,
                      ADD CONSTRAINT FK5302D47D93FE702E_diff FOREIGN KEY (employee_id) REFERENCES person (person_id) ON DELETE CASCADE,
                      ADD CONSTRAINT new_constraint FOREIGN KEY (employee_id) REFERENCES patty (fake);
 
@@ -256,7 +269,7 @@ COMMIT;
 }
 
 {
-  # Test other things about renaming tables to - namely that renames 
+  # Test other things about renaming tables to - namely that renames
   # constraints are still formated right.
 
   my $s1 = SQL::Translator::Schema->new;
@@ -303,8 +316,8 @@ COMMIT;
 ## END OF DIFF
 
   # Test quoting works too.
-  $out = SQL::Translator::Diff::schema_diff($s1, 'MySQL', $s2, 'MySQL', 
-    { producer_options => { quote_table_names => '`' } }
+  $out = SQL::Translator::Diff::schema_diff($s1, 'MySQL', $s2, 'MySQL',
+    { producer_args => { quote_identifiers => 1 } }
   );
   eq_or_diff($out, <<'## END OF DIFF', "Quoting can be turned on");
 -- Convert schema 'Schema 3' to 'Schema 4':;
@@ -312,8 +325,8 @@ COMMIT;
 BEGIN;
 
 ALTER TABLE `employee` RENAME TO `fnord`,
-                       DROP FOREIGN KEY bar_fk,
-                       ADD CONSTRAINT foo_fk FOREIGN KEY (employee_id) REFERENCES `foo` (id);
+                       DROP FOREIGN KEY `bar_fk`,
+                       ADD CONSTRAINT `foo_fk` FOREIGN KEY (`employee_id`) REFERENCES `foo` (`id`);
 
 
 COMMIT;