5 # Note that the bulk of the testing for the mysql producer is in
6 # 08postgres-to-mysql.t. This test is for additional stuff that can't be tested
7 # using an Oracle schema as source e.g. extra attributes.
13 use Test::SQL::Translator qw(maybe_plan);
19 #=============================================================================
24 'SQL::Translator::Producer::MySQL',
28 use Test::Differences;
33 my $yaml_in = <<EOSCHEMA;
41 mysql_collate: latin1_danish_ci
46 data_type: unsigned int
68 mysql_collate: utf8_general_ci
115 name: really_long_name_bigger_than_64_chars_aaaaaaaaaaaaaaaaaaaaaaaaaaa
121 - reference_table: thing
125 - reference_table: thing
133 "SET foreign_key_checks=0",
135 "DROP TABLE IF EXISTS `thing`",
136 "CREATE TABLE `thing` (
137 `id` unsigned int auto_increment,
139 `swedish_name` varchar(32) character set swe7,
140 `description` text character set utf8 collate utf8_general_ci,
142 UNIQUE `idx_unique_name` (`name`)
143 ) ENGINE=InnoDB DEFAULT CHARACTER SET latin1 COLLATE latin1_danish_ci",
145 "DROP TABLE IF EXISTS `some`.`thing2`",
146 "CREATE TABLE `some`.`thing2` (
150 `bar_set` set('foo', 'bar', 'baz'),
151 INDEX index_1 (`id`),
152 INDEX really_long_name_bigger_than_64_chars_aaaaaaaaaaaaaaaaa_aed44c47 (`id`),
155 PRIMARY KEY (`id`, `foo`),
156 CONSTRAINT `fk_thing` FOREIGN KEY (`foo`) REFERENCES `thing` (`id`),
157 CONSTRAINT `fk_thing_1` FOREIGN KEY (`foo2`) REFERENCES `thing` (`id`)
160 "SET foreign_key_checks=1",
164 my @stmts_no_drop = grep {$_ !~ /^DROP TABLE/} @stmts;
166 my $mysql_out = join(";\n\n", @stmts_no_drop) . ";\n\n";
170 $sqlt = SQL::Translator->new(
176 quote_table_names => 1,
177 quote_field_names => 1
180 my $out = $sqlt->translate(\$yaml_in)
181 or die "Translate error:".$sqlt->error;
182 ok $out ne "", "Produced something!";
183 eq_or_diff $out, $mysql_out, "Scalar output looks right with quoting";
185 my @out = $sqlt->translate(\$yaml_in)
186 or die "Translat eerror:".$sqlt->error;
187 is_deeply \@out, \@stmts_no_drop, "Array output looks right with quoting";
190 @{$sqlt}{qw/quote_table_names quote_field_names/} = (0,0);
191 $out = $sqlt->translate(\$yaml_in)
192 or die "Translate error:".$sqlt->error;
194 @out = $sqlt->translate(\$yaml_in)
195 or die "Translate error:".$sqlt->error;
196 $mysql_out =~ s/`//g;
197 my @unquoted_stmts = map { s/`//g; $_} @stmts_no_drop;
198 eq_or_diff $out, $mysql_out, "Output looks right without quoting";
199 is_deeply \@out, \@unquoted_stmts, "Array output looks right without quoting";
201 @{$sqlt}{qw/add_drop_table quote_field_names quote_table_names/} = (1,1,1);
202 @out = $sqlt->translate(\$yaml_in)
203 or die "Translat eerror:".$sqlt->error;
204 $out = $sqlt->translate(\$yaml_in)
205 or die "Translat eerror:".$sqlt->error;
207 eq_or_diff $out, join(";\n\n", @stmts) . ";\n\n", "Output looks right with DROP TABLEs";
208 is_deeply \@out, \@stmts, "Array output looks right with DROP TABLEs";
211 ###############################################################################
214 my $table = SQL::Translator::Schema::Table->new( name => 'mytable');
216 my $field1 = SQL::Translator::Schema::Field->new( name => 'myfield',
218 data_type => 'VARCHAR',
220 default_value => undef,
221 is_auto_increment => 0,
226 my $field1_sql = SQL::Translator::Producer::MySQL::create_field($field1);
228 is($field1_sql, 'myfield VARCHAR(10)', 'Create field works');
230 my $field2 = SQL::Translator::Schema::Field->new( name => 'myfield',
232 data_type => 'VARCHAR',
234 default_value => undef,
235 is_auto_increment => 0,
240 my $alter_field = SQL::Translator::Producer::MySQL::alter_field($field1,
242 is($alter_field, 'ALTER TABLE mytable CHANGE COLUMN myfield myfield VARCHAR(25) NOT NULL', 'Alter field works');
244 my $add_field = SQL::Translator::Producer::MySQL::add_field($field1);
246 is($add_field, 'ALTER TABLE mytable ADD COLUMN myfield VARCHAR(10)', 'Add field works');
248 my $drop_field = SQL::Translator::Producer::MySQL::drop_field($field2);
249 is($drop_field, 'ALTER TABLE mytable DROP COLUMN myfield', 'Drop field works');
251 my $field3 = SQL::Translator::Schema::Field->new( name => 'myfield',
253 data_type => 'boolean',
258 my $field3_sql = SQL::Translator::Producer::MySQL::create_field($field3, { mysql_version => 4.1 });
259 is($field3_sql, 'myfield boolean NOT NULL', 'For Mysql >= 4, use boolean type');
260 $field3_sql = SQL::Translator::Producer::MySQL::create_field($field3, { mysql_version => 3.22 });
261 is($field3_sql, "myfield enum('0','1') NOT NULL", 'For Mysql < 4, use enum for boolean type');
262 $field3_sql = SQL::Translator::Producer::MySQL::create_field($field3,);
263 is($field3_sql, "myfield enum('0','1') NOT NULL", 'When no version specified, use enum for boolean type');
271 for my $size (keys %$number_sizes) {
272 my $expected = $number_sizes->{$size};
273 my $number_field = SQL::Translator::Schema::Field->new(
274 name => "numberfield_$expected",
276 data_type => 'number',
284 SQL::Translator::Producer::MySQL::create_field($number_field),
285 "numberfield_$expected $expected($size)",
286 "Use $expected for NUMBER types of size $size"
291 for my $size (qw/255 256 65535 65536/) {
292 $varchars->{$size} = SQL::Translator::Schema::Field->new(
295 data_type => 'varchar',
303 SQL::Translator::Producer::MySQL::create_field($varchars->{255}, { mysql_version => 5.000003 }),
304 'vch_255 varchar(255)',
305 'VARCHAR(255) is not substituted with TEXT for Mysql >= 5.0.3'
308 SQL::Translator::Producer::MySQL::create_field($varchars->{255}, { mysql_version => 5.0 }),
309 'vch_255 varchar(255)',
310 'VARCHAR(255) is not substituted with TEXT for Mysql < 5.0.3'
313 SQL::Translator::Producer::MySQL::create_field($varchars->{255}),
314 'vch_255 varchar(255)',
315 'VARCHAR(255) is not substituted with TEXT when no version specified',
320 SQL::Translator::Producer::MySQL::create_field($varchars->{256}, { mysql_version => 5.000003 }),
321 'vch_256 varchar(256)',
322 'VARCHAR(256) is not substituted with TEXT for Mysql >= 5.0.3'
325 SQL::Translator::Producer::MySQL::create_field($varchars->{256}, { mysql_version => 5.0 }),
327 'VARCHAR(256) is substituted with TEXT for Mysql < 5.0.3'
330 SQL::Translator::Producer::MySQL::create_field($varchars->{256}),
332 'VARCHAR(256) is substituted with TEXT when no version specified',
337 SQL::Translator::Producer::MySQL::create_field($varchars->{65535}, { mysql_version => 5.000003 }),
338 'vch_65535 varchar(65535)',
339 'VARCHAR(65535) is not substituted with TEXT for Mysql >= 5.0.3'
342 SQL::Translator::Producer::MySQL::create_field($varchars->{65535}, { mysql_version => 5.0 }),
344 'VARCHAR(65535) is substituted with TEXT for Mysql < 5.0.3'
347 SQL::Translator::Producer::MySQL::create_field($varchars->{65535}),
349 'VARCHAR(65535) is substituted with TEXT when no version specified',
354 SQL::Translator::Producer::MySQL::create_field($varchars->{65536}, { mysql_version => 5.000003 }),
356 'VARCHAR(65536) is substituted with TEXT for Mysql >= 5.0.3'
359 SQL::Translator::Producer::MySQL::create_field($varchars->{65536}, { mysql_version => 5.0 }),
361 'VARCHAR(65536) is substituted with TEXT for Mysql < 5.0.3'
364 SQL::Translator::Producer::MySQL::create_field($varchars->{65536}),
366 'VARCHAR(65536) is substituted with TEXT when no version specified',
371 my $view1 = SQL::Translator::Schema::View->new( name => 'view_foo',
372 fields => [qw/id name/],
373 sql => 'SELECT id, name FROM thing',
375 mysql_definer => 'CURRENT_USER',
376 mysql_algorithm => 'MERGE',
377 mysql_security => 'DEFINER',
379 my $create_opts = { add_replace_view => 1, no_comments => 1 };
380 my $view1_sql1 = SQL::Translator::Producer::MySQL::create_view($view1, $create_opts);
382 my $view_sql_replace = "CREATE OR REPLACE
384 DEFINER = CURRENT_USER
386 VIEW view_foo ( id, name ) AS (
387 SELECT id, name FROM thing
389 is($view1_sql1, $view_sql_replace, 'correct "CREATE OR REPLACE VIEW" SQL');
392 my $view2 = SQL::Translator::Schema::View->new( name => 'view_foo',
393 fields => [qw/id name/],
394 sql => 'SELECT id, name FROM thing',);
395 my $create2_opts = { add_replace_view => 0, no_comments => 1 };
396 my $view1_sql2 = SQL::Translator::Producer::MySQL::create_view($view2, $create2_opts);
397 my $view_sql_noreplace = "CREATE
398 VIEW view_foo ( id, name ) AS (
399 SELECT id, name FROM thing
401 is($view1_sql2, $view_sql_noreplace, 'correct "CREATE VIEW" SQL');
404 my %extra = $view1->extra;
407 'mysql_algorithm' => 'MERGE',
408 'mysql_definer' => 'CURRENT_USER',
409 'mysql_security' => 'DEFINER'
414 $view1->remove_extra(qw/mysql_definer mysql_security/);
416 my %extra = $view1->extra;
417 is_deeply \%extra, { 'mysql_algorithm' => 'MERGE', }, 'Extra attributes after first reset_extra call';
420 $view1->remove_extra();
422 my %extra = $view1->extra;
423 is_deeply \%extra, {}, 'Extra attributes completely removed';