Merge 'trunk' into 'roundtrip'
Peter Rabbitson [Sun, 3 May 2009 02:25:45 +0000 (02:25 +0000)]
r1520@Thesaurus (orig r1519):  ribasushi | 2009-05-03 03:20:27 +0200
Teah xml parser about database_events
r1521@Thesaurus (orig r1520):  ribasushi | 2009-05-03 03:27:29 +0200
Extra data and first test for xml database_event support
r1522@Thesaurus (orig r1521):  ribasushi | 2009-05-03 03:45:10 +0200
Improve xml database_event deprecation warning
Only issue warning if show_warnings was set on translator
Fix tests to suppress warn noise
r1523@Thesaurus (orig r1522):  ribasushi | 2009-05-03 04:18:28 +0200
Teach sqlite how to deal with multi-event triggers
r1524@Thesaurus (orig r1523):  ribasushi | 2009-05-03 04:19:09 +0200
Adjust xml-db2 tests
r1525@Thesaurus (orig r1524):  ribasushi | 2009-05-03 04:22:55 +0200
Add Carp::Clan to dependencies

lib/SQL/Translator/Producer/MySQL.pm
t/16xml-parser.t
t/18ttschema-producer.t
t/27sqlite-roundtrip.t [deleted file]
t/46xml-to-pg.t
t/51-xml-to-oracle.t
t/60roundtrip.t [new file with mode: 0644]
t/61translator_agnostic.t [new file with mode: 0644]
t/data/xml/schema.xml

index 40c59f2..1f517cd 100644 (file)
@@ -612,9 +612,15 @@ sub create_index
     my $qf = $options->{quote_field_names} || '';
 
     return join( ' ', 
-                 lc $index->type eq 'normal' ? 'INDEX' : $index->type . ' INDEX',
-                 truncate_id_uniquely( $index->name, $options->{max_id_length} || $DEFAULT_MAX_ID_LENGTH ),
-                 '(' . $qf . join( "$qf, $qf", $index->fields ) . $qf . ')'
+                  lc $index->type eq 'normal' 
+                    ? 'INDEX' 
+                    : $index->type . ' INDEX'
+                  ,
+                  $index->name 
+                    ? (truncate_id_uniquely( $index->name, $options->{max_id_length} || $DEFAULT_MAX_ID_LENGTH ) )
+                    : ()
+                  ,
+                  '(' . $qf . join( "$qf, $qf", $index->fields ) . $qf . ')'
                  );
 
 }
index 485d3e6..c962846 100644 (file)
@@ -99,7 +99,7 @@ schema_ok( $scma, {
                 {
                     name => "email",
                     data_type => "varchar",
-                    size => 255,
+                    size => 500,
                     is_unique => 1,
                     default_value => undef,
                     is_nullable => 1,
index 8485785..7b5d5b9 100644 (file)
@@ -146,7 +146,7 @@ Fields
     
     email
         data_type:             varchar
-        size:                  255
+        size:                  500
         is_nullable:           1
         default_value:         
         is_primary_key:        0
diff --git a/t/27sqlite-roundtrip.t b/t/27sqlite-roundtrip.t
deleted file mode 100644 (file)
index 9e871d0..0000000
+++ /dev/null
@@ -1,71 +0,0 @@
-#!/usr/bin/perl
-
-use warnings;
-use strict;
-use Test::More;
-use Test::SQL::Translator qw(maybe_plan);
-use FindBin qw/$Bin/;
-
-use SQL::Translator;
-use SQL::Translator::Schema::Constants;
-
-BEGIN {
-    maybe_plan(7,
-        'SQL::Translator::Parser::SQLite',
-        'SQL::Translator::Producer::SQLite',
-    );
-}
-
-my $file = "$Bin/data/sqlite/create.sql";
-
-{
-    #local $/;
-    #open my $fh, "<$file" or die "Can't read file '$file': $!\n";
-    #my $data = <$fh>;
-
-    my $t = SQL::Translator->new;
-
-    my $schema1 = $t->translate (
-        parser => 'SQLite',
-        file => $file,
-        debug => 1
-    ) or die $t->error;
-    isa_ok ($schema1, 'SQL::Translator::Schema', 'First parser pass produced a schema');
-
-
-    my $data2 = $t->translate (
-        data => $schema1,
-        producer => 'SQLite',
-    ) or die $t->error;
-    like ($data2, qr/BEGIN.+COMMIT/is, 'Received some meaningful output from the producer');
-
-    # get a new translator
-    $t = SQL::Translator->new;
-
-    my $schema2 = $t->translate (
-        parser => 'SQLite',
-        data => \$data2,
-    ) or die $t->error;
-    isa_ok ($schema2, 'SQL::Translator::Schema', 'Second parser pass produced a schema');
-
-    my @t1 = $schema1->get_tables;
-    my @t2 = $schema2->get_tables;
-
-    my @v1 = $schema1->get_views;
-    my @v2 = $schema2->get_views;
-
-    my @g1 = $schema1->get_triggers;
-    my @g2 = $schema2->get_triggers;
-
-    is (@t2, @t1, 'Equal amount of tables');
-
-    is_deeply (
-        [ map { $_->name } (@t1) ],
-        [ map { $_->name } (@t2) ],
-        'Table names match',
-    );
-
-    is (@v2, @v1, 'Equal amount of views');
-
-    is (@g2, @g1, 'Equal amount of triggers');
-}
index c38d917..87469dc 100644 (file)
@@ -39,7 +39,7 @@ CREATE TABLE "Basic" (
   "id" serial NOT NULL,
   "title" character varying(100) DEFAULT 'hello' NOT NULL,
   "description" text DEFAULT '',
-  "email" character varying(255),
+  "email" character varying(500),
   "explicitnulldef" character varying,
   "explicitemptystring" character varying DEFAULT '',
   -- Hello emptytagdef
index 7f802a6..5d01ae1 100644 (file)
@@ -45,7 +45,7 @@ my $want = [
   id number(10) NOT NULL,
   title varchar2(100) DEFAULT \'hello\' NOT NULL,
   description clob DEFAULT \'\',
-  email varchar2(255),
+  email varchar2(500),
   explicitnulldef varchar2,
   explicitemptystring varchar2 DEFAULT \'\',
   emptytagdef varchar2 DEFAULT \'\',
@@ -107,7 +107,7 @@ CREATE TABLE Basic (
   id number(10) NOT NULL,
   title varchar2(100) DEFAULT 'hello' NOT NULL,
   description clob DEFAULT '',
-  email varchar2(255),
+  email varchar2(500),
   explicitnulldef varchar2,
   explicitemptystring varchar2 DEFAULT '',
   emptytagdef varchar2 DEFAULT '',
diff --git a/t/60roundtrip.t b/t/60roundtrip.t
new file mode 100644 (file)
index 0000000..4cfc53a
--- /dev/null
@@ -0,0 +1,218 @@
+#!/usr/bin/perl
+
+use warnings;
+use strict;
+use Test::More qw/no_plan/;
+use Test::Exception;
+use FindBin qw/$Bin/;
+
+use SQL::Translator;
+
+### Set $ENV{SQLTTEST_RT_DEBUG} = 1 for more output
+
+# What tests to run - parser/producer name, and optional args
+my $plan = [
+  {
+    engine => 'SQLite',
+    producer_args => {},
+    parser_args => {},
+  },
+  {
+    engine => 'MySQL',
+    producer_args => {},
+    parser_args => {},
+  },
+  {
+    engine => 'MySQL',
+    name => 'MySQL 5.0',
+    producer_args => { mysql_version => 5 },
+    parser_args => { mysql_parser_version => 5 },
+  },
+  {
+    engine => 'MySQL',
+    name => 'MySQL 5.1',
+    producer_args => { mysql_version => '5.1' },
+    parser_args => { mysql_parser_version => '5.1' },
+  },
+  {
+    engine => 'PostgreSQL',
+    producer_args => {},
+    parser_args => {},
+  },
+  {
+    engine => 'Oracle',
+    producer_args => {},
+    parser_args => {},
+  },
+  {
+    engine => 'SQLServer',
+    producer_args => {},
+    parser_args => {},
+  },
+  {
+    engine => 'Sybase',
+    producer_args => {},
+    parser_args => {},
+  },
+  {
+    engine => 'DB2',
+    producer_args => {},
+    parser_args => {},
+  },
+
+# There is no Access producer
+#  {
+#    engine => 'Access',
+#    producer_args => {},
+#    parser_args => {},
+#  },
+];
+
+
+# This data file has the right mix of table/view/procedure/trigger
+# definitions, and lists enough quirks to trip up most combos
+# I am not sure if augmenting it will break other tests - experiment
+my $base_file = "$Bin/data/xml/schema.xml";
+
+my $base_t = SQL::Translator->new;
+$base_t->$_ (1) for qw/add_drop_table no_comments/;
+
+my $base_schema = $base_t->translate (
+  parser => 'XML',
+  file => $base_file,
+) or die $base_t->error;
+
+
+for my $args (@$plan) {
+
+  $args->{name} ||= $args->{engine};
+
+  lives_ok (
+    sub { check_roundtrip ($args, $base_schema) },
+    "Round trip for $args->{name} did not throw an exception",
+  );
+}
+
+
+sub check_roundtrip {
+  my ($args, $base_schema) = @_;
+  my $base_t = $base_schema->translator;
+
+# create some sql from the submitted schema
+  my $base_sql = $base_t->translate (
+    data => $base_schema,
+    producer => $args->{engine},
+    producer_args => $args->{producer_args},
+  );
+
+  like (
+    $base_sql,
+    qr/^\s*CREATE TABLE/m,  #assume there is at least one create table statement
+    "Received some meaningful output from the first $args->{name} production",
+  ) or do {
+    diag ( _gen_diag ($base_t->error) );
+    return;
+  };
+
+# parse the sql back
+  my $parser_t = SQL::Translator->new;
+  $parser_t->$_ (1) for qw/add_drop_table no_comments/;
+  my $mid_schema = $parser_t->translate (
+    data => $base_sql,
+    parser => $args->{engine},
+    parser_args => $args->{parser_args},
+  );
+
+  isa_ok ($mid_schema, 'SQL::Translator::Schema', "First $args->{name} parser pass produced a schema:")
+    or do {
+      diag (_gen_diag ( $parser_t->error, $base_sql ) );
+      return;
+    };
+
+# schemas should be comparable at least as far as table/field numbers go
+  is_deeply (
+    _get_table_info ($mid_schema->get_tables),
+    _get_table_info ($base_schema->get_tables),
+    "Schema tables generally match afer $args->{name} parser trip",
+  ) or return;
+
+# and produce sql once again
+
+# Producing a schema with a Translator different from the one the schema was generated
+# from does not work. This is arguably a bug, 61translator_agnostic.t works with that
+#  my $producer_t = SQL::Translator->new;
+#  $producer_t->$_ (1) for qw/add_drop_table no_comments/;
+
+#  my $rt_sql = $producer_t->translate (
+#    data => $mid_schema,
+#    producer => $args->{engine},
+#    producer_args => $args->{producer_args},
+#  );
+
+  my $rt_sql = $parser_t->translate (
+    data => $mid_schema,
+    producer => $args->{engine},
+    producer_args => $args->{producer_args},
+  );
+
+  like (
+    $rt_sql,
+    qr/^\s*CREATE TABLE/m,  #assume there is at least one create table statement
+    "Received some meaningful output from the second $args->{name} production",
+  ) or do {
+    diag ( _gen_diag ( $parser_t->error ) );
+    return;
+  };
+
+# the two sql strings should be identical
+  my $msg = "$args->{name} SQL roundtrip successful - SQL statements match";
+  $ENV{SQLTTEST_RT_DEBUG}
+    ? is_deeply (
+      [ split /\n/, $rt_sql ],
+      [ split /\n/, $base_sql ],
+      $msg,
+    )
+    : ok ($rt_sql eq $base_sql, $msg)
+  ;
+}
+
+sub _get_table_info {
+  my @tables = @_;
+
+  my @info;
+
+  for my $t (@tables) {
+    push @info, {
+      name => $t->name,
+      fields => [
+        map { $_->name } ($t->get_fields),
+      ],
+    };
+  }
+
+  return \@info;
+}
+
+# takes an error string and an optional SQL block
+# returns the string conctenated with a line-numbered block for
+# easier reading
+sub _gen_diag {
+  my ($err, $sql) = @_;
+
+  return 'Unknown error' unless $err;
+
+
+  if ($sql and $ENV{SQLTTEST_RT_DEBUG}) {
+    my @sql_lines;
+    for (split /\n/, $sql) {
+      push @sql_lines, sprintf ('%03d: %s',
+        scalar @sql_lines + 1,
+        $_,
+      );
+    }
+
+    return "$err\n\n" . join ("\n", @sql_lines);
+  }
+
+  return $err;
+}
diff --git a/t/61translator_agnostic.t b/t/61translator_agnostic.t
new file mode 100644 (file)
index 0000000..f35abca
--- /dev/null
@@ -0,0 +1,42 @@
+#!/usr/bin/perl
+
+use warnings;
+use strict;
+use Test::More qw/no_plan/;
+use Test::Exception;
+use FindBin qw/$Bin/;
+
+use SQL::Translator;
+
+# Producing a schema with a Translator different from the one the schema was
+# generated should just work. After all the $schema object is just data.
+
+
+my $base_file = "$Bin/data/xml/schema.xml";
+my $base_t = SQL::Translator->new;
+$base_t->$_ (1) for qw/add_drop_table no_comments/;
+
+# create a base schema attached to $base_t
+my $base_schema = $base_t->translate (
+  parser => 'XML',
+  file => $base_file,
+) or die $base_t->error;
+
+# now create a new translator and try to feed it the same schema
+my $new_t = SQL::Translator->new;
+$new_t->$_ (1) for qw/add_drop_table no_comments/;
+
+my $sql = $new_t->translate (
+  data => $base_schema,
+  producer => 'SQLite'
+);
+
+TODO: {
+  local $TODO = 'This will probably not work before the rewrite';
+
+  like (
+    $sql,
+    qr/^\s*CREATE TABLE/m,  #assume there is at least one create table statement 
+    "Received some meaningful output from the producer",
+  );
+}
index 02e2877..8601891 100644 (file)
@@ -24,7 +24,7 @@ Created on Fri Aug 15 15:08:18 2003
           <field
               name="description"
               size="0" data_type="text" order="3" default_value="" />
-          <field name="email" size="255" data_type="varchar" order="4">
+          <field name="email" size="500" data_type="varchar" order="4">
               <extra foo="bar" hello="world" bar="baz" />
           </field>
           <field name="explicitnulldef" size="0" data_type="varchar" order="5" />