10 ### Set $ENV{SQLTTEST_RT_DEBUG} = 1 for more output
12 # What tests to run - parser/producer name, and optional args
16 # req => 'XML::LibXML',
34 producer_args => { mysql_version => 5 },
35 parser_args => { mysql_parser_version => 5 },
40 producer_args => { mysql_version => '5.1' },
41 parser_args => { mysql_parser_version => '5.1' },
44 engine => 'PostgreSQL',
49 # engine => 'SQLServer',
50 # producer_args => {},
56 # producer_args => {},
58 # todo => 'Needs volunteers',
62 # producer_args => {},
64 # todo => 'Needs volunteers',
68 # producer_args => {},
70 # todo => 'Needs volunteers',
73 # There is no Access producer
76 # producer_args => {},
82 # This data file has the right mix of table/view/procedure/trigger
83 # definitions, and lists enough quirks to trip up most combos
84 my $base_file = "$Bin/data/roundtrip_autogen.yaml";
85 open (my $base_fh, '<', $base_file) or die "$base_file: $!";
87 my $base_t = SQL::Translator->new;
88 $base_t->$_(1) for qw/add_drop_table no_comments/;
90 my $base_schema = $base_t->translate(
92 data => do { local $/; <$base_fh>; },
95 #assume there is at least one table
97 XML => qr/<tables>\s*<table/,
98 YAML => qr/\A---\n.+tables\:/s,
99 SQL => qr/^\s*CREATE TABLE/m,
102 for my $args (@$plan) {
104 $args->{name} ||= $args->{engine};
106 my @req = ref $args->{req} ? @{$args->{req}} : $args->{req}||();
110 push @missing, $_ if ($@);
113 skip sprintf ('Need %s for %s roundtrip test',
114 join (', ', @missing),
120 local $TODO = $args->{todo} if $args->{todo};
123 sub { check_roundtrip ($args, $base_t) },
124 "Round trip for $args->{name} did not throw an exception",
131 sub check_roundtrip {
132 my ($args, $base_t) = @_;
134 # create some output from the submitted schema
136 my $base_out = $base_t->translate(
137 data => $base_t->schema,
138 producer => $args->{engine},
139 producer_args => $args->{producer_args} || {},
144 $string_re->{$args->{engine}} || $string_re->{SQL},
145 "Received some meaningful output from the first $args->{name} production",
147 diag ( _gen_diag ($@) );
152 my $parser_t = SQL::Translator->new;
153 $parser_t->$_(1) for qw/add_drop_table no_comments/;
154 my $mid_schema = $parser_t->translate (
156 parser => $args->{engine},
157 parser_args => $args->{parser_args} || {},
160 isa_ok ($mid_schema, 'SQL::Translator::Object::Schema', "First $args->{name} parser pass produced a schema:")
162 diag (_gen_diag ( $@, $base_out ) );
166 # schemas should be comparable at least as far as table/field numbers go
168 _get_table_info ($mid_schema->get_tables),
169 _get_table_info ($base_schema->get_tables),
170 "Schema tables generally match afer $args->{name} parser trip",
173 # and produce sql once again
175 # Producing a schema with a Translator different from the one the schema was generated
176 # from does not work. This is arguably a bug, 61translator_agnostic.t works with that
177 # my $producer_t = SQL::Translator->new;
178 # $producer_t->$_ (1) for qw/add_drop_table no_comments/;
180 # my $rt_sql = $producer_t->translate (
181 # data => $mid_schema,
182 # producer => $args->{engine},
183 # producer_args => $args->{producer_args},
186 my $rt_out = $parser_t->translate(
188 producer => $args->{engine},
189 producer_args => $args->{producer_args} || {},
194 $string_re->{$args->{engine}} || $string_re->{SQL},
195 "Received some meaningful output from the second $args->{name} production",
197 diag ( _gen_diag ( $@ ) );
201 # the two sql strings should be identical
202 my $msg = "$args->{name} SQL roundtrip successful - SQL statements match";
203 $ENV{SQLTTEST_RT_DEBUG} #stringify below because IO::Scalar does not behave nice
204 ? eq_or_diff ("$rt_out", "$base_out", $msg)
205 : ok ("$rt_out" eq "$base_out", $msg)
209 sub _get_table_info {
214 for my $t (@tables) {
218 map { $_->name } ($t->get_fields),
226 # takes an error string and an optional output block
227 # returns the string conctenated with a line-numbered block for
230 my ($err, $out) = @_;
232 return 'Unknown error' unless $err;
235 if ($out and $ENV{SQLTTEST_RT_DEBUG}) {
237 for (split /\n/, $out) {
238 push @lines, sprintf ('%03d: %s',
244 return "$err\n\n" . join ("\n", @lines);