5 use Test::More qw/no_plan/;
11 use SQL::Translator::Utils qw/ddl_parser_instance/;
14 ### Set $ENV{SQLTTEST_RT_DEBUG} = 1 for more output
16 # What tests to run - parser/producer name, and optional args
20 req => 'XML::LibXML 1.69',
41 producer_args => { mysql_version => 5 },
42 parser_args => { mysql_parser_version => 5 },
47 producer_args => { mysql_version => '5.1' },
48 parser_args => { mysql_parser_version => '5.1' },
51 engine => 'PostgreSQL',
56 engine => 'SQLServer',
65 todo => 'Needs volunteers',
71 todo => 'Needs volunteers',
73 # too noisy under new P::RD
76 # producer_args => {},
78 # todo => 'Needs volunteers',
81 # There is no Access producer
84 # producer_args => {},
90 # This data file has the right mix of table/view/procedure/trigger
91 # definitions, and lists enough quirks to trip up most combos
92 my $base_file = "$Bin/data/roundtrip_autogen.yaml";
93 open (my $base_fh, '<', $base_file) or die "$base_file: $!";
95 my $base_t = SQL::Translator->new;
96 $base_t->$_ (1) for qw/add_drop_table no_comments/;
98 my $base_schema = $base_t->translate (
100 data => do { local $/; <$base_fh>; },
101 ) or die $base_t->error;
104 #assume there is at least one table
106 XML => qr/<tables>\s*<table/,
107 YAML => qr/\A---\n.+tables\:/s,
108 SQL => qr/^\s*CREATE TABLE/m,
111 for my $args (@$plan) {
113 $args->{name} ||= $args->{engine};
115 my @req = ref $args->{req} ? @{$args->{req}} : $args->{req}||();
119 push @missing, $_ if ($@);
122 skip sprintf ('Need %s for %s roundtrip test',
123 join (', ', @missing),
128 use_ok("SQL::Translator::Producer::$args->{engine}");
129 use_ok("SQL::Translator::Parser::$args->{engine}");
131 ok(ddl_parser_instance($args->{engine}), 'Got proper parser instance')
132 unless $args->{no_grammar};
135 local $TODO = $args->{todo} if $args->{todo};
138 # silence PR::D from spewing on STDERR
139 local $::RD_ERRORS = 0 if $args->{todo};
140 local $::RD_WARN = 0 if $args->{todo};
141 local $::RD_HINT = 0 if $args->{todo};
144 sub { check_roundtrip ($args, $base_schema) },
145 "Round trip for $args->{name} did not throw an exception",
152 sub check_roundtrip {
153 my ($args, $base_schema) = @_;
154 my $base_t = $base_schema->translator;
156 # create some output from the submitted schema
157 my $base_out = $base_t->translate (
158 data => $base_schema,
159 producer => $args->{engine},
160 producer_args => $args->{producer_args},
165 $string_re->{$args->{engine}} || $string_re->{SQL},
166 "Received some meaningful output from the first $args->{name} production",
168 diag ( _gen_diag ($base_t->error) );
173 my $parser_t = SQL::Translator->new;
174 $parser_t->$_ (1) for qw/add_drop_table no_comments/;
175 my $mid_schema = $parser_t->translate (
177 parser => $args->{engine},
178 parser_args => $args->{parser_args},
181 isa_ok ($mid_schema, 'SQL::Translator::Schema', "First $args->{name} parser pass produced a schema:")
183 diag (_gen_diag ( $parser_t->error, $base_out ) );
185 note join ("\n" . ( '=' x 76) . "\n",
187 (join ("\n", map { ++$i . ":\t$_" } split /\n/, $base_out) ),
193 # schemas should be comparable at least as far as table/field numbers go
195 _get_table_info ($mid_schema->get_tables),
196 _get_table_info ($base_schema->get_tables),
197 "Schema tables generally match afer $args->{name} parser trip",
200 # and produce sql once again
202 # Producing a schema with a Translator different from the one the schema was generated
203 # from does not work. This is arguably a bug, 61translator_agnostic.t works with that
204 # my $producer_t = SQL::Translator->new;
205 # $producer_t->$_ (1) for qw/add_drop_table no_comments/;
207 # my $rt_sql = $producer_t->translate (
208 # data => $mid_schema,
209 # producer => $args->{engine},
210 # producer_args => $args->{producer_args},
213 my $rt_out = $parser_t->translate (
215 producer => $args->{engine},
216 producer_args => $args->{producer_args},
221 $string_re->{$args->{engine}} || $string_re->{SQL},
222 "Received some meaningful output from the second $args->{name} production",
224 diag ( _gen_diag ( $parser_t->error ) );
228 # the two sql strings should be identical
229 my $msg = "$args->{name} SQL roundtrip successful - SQL statements match";
230 $ENV{SQLTTEST_RT_DEBUG} #stringify below because IO::Scalar does not behave nice
231 ? eq_or_diff ("$rt_out", "$base_out", $msg)
232 : ok ("$rt_out" eq "$base_out", $msg)
236 sub _get_table_info {
241 for my $t (@tables) {
245 map { $_->name } ($t->get_fields),
253 # takes an error string and an optional output block
254 # returns the string conctenated with a line-numbered block for
257 my ($err, $out) = @_;
259 return 'Unknown error' unless $err;
262 if ($out and $ENV{SQLTTEST_RT_DEBUG}) {
264 for (split /\n/, $out) {
265 push @lines, sprintf ('%03d: %s',
271 return "$err\n\n" . join ("\n", @lines);