sqlt-diff option to quote names
Jaime Soriano Pastor [Wed, 21 Dec 2011 15:08:10 +0000 (16:08 +0100)]
script/sqlt-diff
t/70sqlt-diff_script.t [new file with mode: 0644]
t/70sqlt-diff_script_old.t [moved from t/30sqlt-diff.t with 100% similarity]

index f2ee1fb..03da501 100755 (executable)
@@ -53,6 +53,8 @@ Options:
   --ignore-proc-sql    Ignore procedure SQL differences
   --no-batch-alters    Do not clump multile alters to the same table into a
                        single ALTER TABLE statement where possible.
+  --quote=<character>  Use <character> to quote all table and field
+                       names in statements
 
 =head1 DESCRIPTION
 
@@ -115,7 +117,8 @@ $VERSION = '1.59';
 
 my ( @input, $list, $help, $debug, $trace, $caseopt, $ignore_index_names,
     $ignore_constraint_names, $output_db, $mysql_parser_version,
-    $ignore_view_sql, $ignore_proc_sql, $no_batch_alters );
+    $ignore_view_sql, $ignore_proc_sql, $no_batch_alters, $quote
+);
 for my $arg ( @ARGV ) {
     if ( $arg =~ m/^-?-l(ist)?$/ ) {
         $list = 1;
@@ -150,18 +153,21 @@ for my $arg ( @ARGV ) {
     elsif ( $arg =~ m/^--ignore-proc-sql$/ ) {
         $ignore_proc_sql = 1;
     }
+    elsif ( $arg =~ m/^--quote=(.)$/ ) {
+        $quote = $1;
+    }
     elsif ( $arg =~ m/^([^=]+)=(.+)$/ ) {
         push @input, { file => $1, parser => $2 };
     }
     elsif ( $arg =~ m/^--no-batch-alters$/ ) {
-      $no_batch_alters = 1;
+        $no_batch_alters = 1;
     }
     else {
         pod2usage( msg => "Unknown argument '$arg'" );
     }
 }
 
-print STDERR <<'EOM';
+print STDERR <<'EOM' unless $ENV{SQLT_NEWDIFF_NOWARN};
 This code is experimental, currently the new code only supports MySQL or
 SQLite diffing. To add support for other databases, please patch the relevant
 SQL::Translator::Producer:: module.  If you need compatibility with the old
@@ -214,7 +220,12 @@ my $result = SQL::Translator::Diff::schema_diff($source_schema, $source_db,
                                                   output_db               => $output_db,
                                                   no_batch_alters         => $no_batch_alters,
                                                   debug                   => $debug,
-                                                  trace                   => $trace });
+                                                  trace                   => $trace,
+                                                  producer_args => {
+                                                      quote_table_names   => $quote,
+                                                      quote_field_names   => $quote,
+                                                  },
+                                                  });
 if($result)
 {
     print $result;
diff --git a/t/70sqlt-diff_script.t b/t/70sqlt-diff_script.t
new file mode 100644 (file)
index 0000000..138d8bc
--- /dev/null
@@ -0,0 +1,72 @@
+#!/usr/bin/perl
+# vim: set ft=perl:
+
+use strict;
+
+use File::Spec::Functions qw(catfile updir tmpdir);
+use FindBin qw($Bin);
+use Test::More;
+use Test::SQL::Translator qw(maybe_plan);
+
+my @script = qw(script sqlt-diff);
+my @create1 = qw(data sqlite create.sql);
+my @create2 = qw(data sqlite create2.sql);
+
+my $sqlt_diff = catfile($Bin, updir, @script);
+my $create1 = catfile($Bin, @create1);
+my $create2 = catfile($Bin, @create2);
+
+BEGIN {
+    maybe_plan(16,
+        'SQL::Translator::Parser::MySQL',
+        );
+}
+
+$ENV{SQLT_NEWDIFF_NOWARN} = 1;
+
+my @mysql_create1 = qw(data mysql create.sql);
+my @mysql_create2 = qw(data mysql create2.sql);
+
+my $mysql_create1 = (-d "t")
+    ? catfile($Bin, @mysql_create1)
+    : catfile($Bin, "t", @mysql_create1);
+
+my $mysql_create2 = (-d "t")
+    ? catfile($Bin, @mysql_create2)
+    : catfile($Bin, "t", @mysql_create2);
+
+# Test for differences
+my @cmd = ($^X, $sqlt_diff, "$mysql_create1=MySQL", "$mysql_create2=MySQL");
+my $out = `@cmd`;
+
+like($out, qr/CHANGE COLUMN person_id/, "Detected altered 'person_id' field");
+like($out, qr/CHANGE COLUMN iq/, "Detected altered 'iq' field");
+like($out, qr/CHANGE COLUMN name/, "Detected altered 'name' field");
+like($out, qr/CHANGE COLUMN age/, "Detected altered 'age' field");
+like($out, qr/ADD COLUMN is_rock_star/,
+    "Detected missing rock star field");
+like($out, qr/ADD UNIQUE UC_person_id/,
+    "Detected missing unique constraint");
+like($out, qr/ADD UNIQUE INDEX unique_name/,
+    "Detected unique index with different name");
+like($out, qr/DROP FOREIGN KEY FK5302D47D93FE702E/,
+    "Detected drop foreign key");
+like($out, qr/ADD CONSTRAINT FK5302D47D93FE702E_diff/,
+    "Detected add constraint");
+unlike($out, qr/ADD PRIMARY KEY/, "Primary key looks different when it shouldn't");
+
+# Test for quoted output
+@cmd = ($^X, $sqlt_diff, '--quote=\`', "$mysql_create1=MySQL", "$mysql_create2=MySQL");
+$out = `@cmd`;
+
+like($out, qr/ALTER TABLE `person`/, "Quoted table name");
+like($out, qr/CHANGE COLUMN `person_id`/, "Quoted 'person_id' field");
+like($out, qr/CHANGE COLUMN `iq`/, "Quoted 'iq' field");
+like($out, qr/CHANGE COLUMN `name`/, "Quoted 'name' field");
+like($out, qr/CHANGE COLUMN `age`/, "Quoted 'age' field");
+
+# Test for sameness
+@cmd = ($^X, $sqlt_diff, "$mysql_create1=MySQL", "$mysql_create1=MySQL");
+$out = `@cmd`;
+
+like($out, qr/No differences found/, "Properly detected no differences");
similarity index 100%
rename from t/30sqlt-diff.t
rename to t/70sqlt-diff_script_old.t