Honor supplied field order when adding fields to a table object
[dbsrgits/SQL-Translator.git] / t / 38-mysql-producer.t
CommitLineData
819fe9ef 1#!/usr/bin/perl -w
1ded8513 2# vim:filetype=perl
3
4#
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.
8#
9
10use strict;
11use Test::More;
12use Test::Exception;
13use Test::SQL::Translator qw(maybe_plan);
14
15use Data::Dumper;
16use FindBin qw/$Bin/;
17
18# Testing 1,2,3,4...
19#=============================================================================
20
21BEGIN {
b62fa492 22 maybe_plan(73,
1ded8513 23 'YAML',
24 'SQL::Translator::Producer::MySQL',
25 'Test::Differences',
26 )
27}
28use Test::Differences;
29use SQL::Translator;
30
31# Main test.
32{
33my $yaml_in = <<EOSCHEMA;
34---
35schema:
36 tables:
37 thing:
38 name: thing
39 extra:
aee4b66e 40 mysql_charset: latin1
41 mysql_collate: latin1_danish_ci
1ded8513 42 order: 1
43 fields:
8c4efd11 44 id:
45 name: id
46 data_type: unsigned int
47 is_primary_key: 1
48 is_auto_increment: 1
807290c3 49 order: 1
1ded8513 50 name:
51 name: name
52 data_type: varchar
53 size:
54 - 32
807290c3 55 order: 2
1ded8513 56 swedish_name:
57 name: swedish_name
58 data_type: varchar
59 size: 32
60 extra:
61 mysql_charset: swe7
807290c3 62 order: 3
1ded8513 63 description:
64 name: description
65 data_type: text
66 extra:
67 mysql_charset: utf8
68 mysql_collate: utf8_general_ci
807290c3 69 order: 4
fe0f47d0 70 constraints:
71 - type: UNIQUE
72 fields:
73 - name
74 name: idx_unique_name
4d438549 75
8c4efd11 76 thing2:
8d693a85 77 name: some.thing2
8c4efd11 78 extra:
79 order: 2
80 fields:
81 id:
82 name: id
83 data_type: int
84 is_primary_key: 0
807290c3 85 order: 1
8c4efd11 86 is_foreign_key: 1
87 foo:
88 name: foo
89 data_type: int
807290c3 90 order: 2
8c4efd11 91 is_not_null: 1
fb149f81 92 foo2:
93 name: foo2
94 data_type: int
807290c3 95 order: 3
fb149f81 96 is_not_null: 1
7c1aae02 97 bar_set:
98 name: bar_set
99 data_type: set
807290c3 100 order: 4
7c1aae02 101 is_not_null: 1
102 extra:
103 list:
104 - foo
105 - bar
106 - baz
4d438549 107 indices:
108 - type: NORMAL
7c1aae02 109 fields:
4d438549 110 - id
111 name: index_1
112 - type: NORMAL
7c1aae02 113 fields:
4d438549 114 - id
f5405d47 115 name: really_long_name_bigger_than_64_chars_aaaaaaaaaaaaaaaaaaaaaaaaaaa
8c4efd11 116 constraints:
117 - type: PRIMARY_KEY
118 fields:
119 - id
120 - foo
121 - reference_table: thing
122 type: FOREIGN_KEY
123 fields: foo
124 name: fk_thing
fb149f81 125 - reference_table: thing
126 type: FOREIGN_KEY
127 fields: foo2
128 name: fk_thing
8c4efd11 129
2c098ea5 130 thing3:
131 name: some.thing3
132 extra:
6a12468d 133 order: 3
2c098ea5 134 fields:
135 id:
136 name: id
137 data_type: int
138 is_primary_key: 0
807290c3 139 order: 1
2c098ea5 140 is_foreign_key: 1
141 foo:
142 name: foo
143 data_type: int
807290c3 144 order: 2
2c098ea5 145 is_not_null: 1
146 foo2:
147 name: foo2
148 data_type: int
807290c3 149 order: 3
2c098ea5 150 is_not_null: 1
151 bar_set:
152 name: bar_set
153 data_type: set
807290c3 154 order: 4
2c098ea5 155 is_not_null: 1
156 extra:
157 list:
158 - foo
159 - bar
160 - baz
161 indices:
162 - type: NORMAL
163 fields:
164 - id
165 name: index_1
166 - type: NORMAL
167 fields:
168 - id
169 name: really_long_name_bigger_than_64_chars_aaaaaaaaaaaaaaaaaaaaaaaaaaa
170 constraints:
171 - type: PRIMARY_KEY
172 fields:
173 - id
174 - foo
175 - reference_table: some.thing2
176 type: FOREIGN_KEY
177 fields: foo
178 name: fk_thing
179 - reference_table: some.thing2
180 type: FOREIGN_KEY
181 fields: foo2
182 name: fk_thing
1ded8513 183EOSCHEMA
184
f6af58ae 185my @stmts = (
24d9fe69 186"SET foreign_key_checks=0",
1ded8513 187
24d9fe69 188"DROP TABLE IF EXISTS `thing`",
f6af58ae 189"CREATE TABLE `thing` (
fe0f47d0 190 `id` unsigned int auto_increment,
191 `name` varchar(32),
f5405d47 192 `swedish_name` varchar(32) character set swe7,
193 `description` text character set utf8 collate utf8_general_ci,
fe0f47d0 194 PRIMARY KEY (`id`),
195 UNIQUE `idx_unique_name` (`name`)
24d9fe69 196) ENGINE=InnoDB DEFAULT CHARACTER SET latin1 COLLATE latin1_danish_ci",
1ded8513 197
8d693a85 198"DROP TABLE IF EXISTS `some`.`thing2`",
199"CREATE TABLE `some`.`thing2` (
fe0f47d0 200 `id` integer,
201 `foo` integer,
fb149f81 202 `foo2` integer,
7c1aae02 203 `bar_set` set('foo', 'bar', 'baz'),
20476859 204 INDEX `index_1` (`id`),
205 INDEX `really_long_name_bigger_than_64_chars_aaaaaaaaaaaaaaaaa_aed44c47` (`id`),
fe0f47d0 206 INDEX (`foo`),
fb149f81 207 INDEX (`foo2`),
fe0f47d0 208 PRIMARY KEY (`id`, `foo`),
da5a1bae 209 CONSTRAINT `fk_thing` FOREIGN KEY (`foo`) REFERENCES `thing` (`id`),
210 CONSTRAINT `fk_thing_1` FOREIGN KEY (`foo2`) REFERENCES `thing` (`id`)
24d9fe69 211) ENGINE=InnoDB",
8c4efd11 212
2c098ea5 213"DROP TABLE IF EXISTS `some`.`thing3`",
214"CREATE TABLE `some`.`thing3` (
215 `id` integer,
216 `foo` integer,
217 `foo2` integer,
218 `bar_set` set('foo', 'bar', 'baz'),
219 INDEX `index_1` (`id`),
220 INDEX `really_long_name_bigger_than_64_chars_aaaaaaaaaaaaaaaaa_aed44c47` (`id`),
221 INDEX (`foo`),
222 INDEX (`foo2`),
223 PRIMARY KEY (`id`, `foo`),
224 CONSTRAINT `fk_thing_2` FOREIGN KEY (`foo`) REFERENCES `some`.`thing2` (`id`, `foo`),
225 CONSTRAINT `fk_thing_3` FOREIGN KEY (`foo2`) REFERENCES `some`.`thing2` (`id`, `foo`)
226) ENGINE=InnoDB",
227
24d9fe69 228"SET foreign_key_checks=1",
f6af58ae 229
230);
231
232my @stmts_no_drop = grep {$_ !~ /^DROP TABLE/} @stmts;
233
24d9fe69 234my $mysql_out = join(";\n\n", @stmts_no_drop) . ";\n\n";
8c4efd11 235
1ded8513 236
237 my $sqlt;
238 $sqlt = SQL::Translator->new(
239 show_warnings => 1,
240 no_comments => 1,
8c4efd11 241# debug => 1,
1ded8513 242 from => "YAML",
243 to => "MySQL",
fe0f47d0 244 quote_table_names => 1,
245 quote_field_names => 1
1ded8513 246 );
247
819fe9ef 248 my $out = $sqlt->translate(\$yaml_in)
249 or die "Translate error:".$sqlt->error;
f6af58ae 250 ok $out ne "", "Produced something!";
251 eq_or_diff $out, $mysql_out, "Scalar output looks right with quoting";
252
253 my @out = $sqlt->translate(\$yaml_in)
254 or die "Translat eerror:".$sqlt->error;
255 is_deeply \@out, \@stmts_no_drop, "Array output looks right with quoting";
fe0f47d0 256
9683e26b 257 $sqlt->quote_identifiers(0);
fe0f47d0 258
259 $out = $sqlt->translate(\$yaml_in)
4d438549 260 or die "Translate error:".$sqlt->error;
f6af58ae 261
262 @out = $sqlt->translate(\$yaml_in)
4d438549 263 or die "Translate error:".$sqlt->error;
fe0f47d0 264 $mysql_out =~ s/`//g;
f6af58ae 265 my @unquoted_stmts = map { s/`//g; $_} @stmts_no_drop;
266 eq_or_diff $out, $mysql_out, "Output looks right without quoting";
267 is_deeply \@out, \@unquoted_stmts, "Array output looks right without quoting";
268
9683e26b 269 $sqlt->quote_identifiers(1);
270 $sqlt->add_drop_table(1);
271
f6af58ae 272 @out = $sqlt->translate(\$yaml_in)
273 or die "Translat eerror:".$sqlt->error;
274 $out = $sqlt->translate(\$yaml_in)
275 or die "Translat eerror:".$sqlt->error;
276
24d9fe69 277 eq_or_diff $out, join(";\n\n", @stmts) . ";\n\n", "Output looks right with DROP TABLEs";
f6af58ae 278 is_deeply \@out, \@stmts, "Array output looks right with DROP TABLEs";
1ded8513 279}
8db4bd9d 280
281###############################################################################
282# New alter/add subs
283
ab15e426 284{
8db4bd9d 285my $table = SQL::Translator::Schema::Table->new( name => 'mytable');
286
287my $field1 = SQL::Translator::Schema::Field->new( name => 'myfield',
288 table => $table,
289 data_type => 'VARCHAR',
290 size => 10,
291 default_value => undef,
292 is_auto_increment => 0,
293 is_nullable => 1,
294 is_foreign_key => 0,
295 is_unique => 0 );
296
297my $field1_sql = SQL::Translator::Producer::MySQL::create_field($field1);
298
299is($field1_sql, 'myfield VARCHAR(10)', 'Create field works');
300
301my $field2 = SQL::Translator::Schema::Field->new( name => 'myfield',
302 table => $table,
303 data_type => 'VARCHAR',
304 size => 25,
305 default_value => undef,
306 is_auto_increment => 0,
307 is_nullable => 0,
308 is_foreign_key => 0,
309 is_unique => 0 );
310
311my $alter_field = SQL::Translator::Producer::MySQL::alter_field($field1,
312 $field2);
313is($alter_field, 'ALTER TABLE mytable CHANGE COLUMN myfield myfield VARCHAR(25) NOT NULL', 'Alter field works');
314
315my $add_field = SQL::Translator::Producer::MySQL::add_field($field1);
316
317is($add_field, 'ALTER TABLE mytable ADD COLUMN myfield VARCHAR(10)', 'Add field works');
318
319my $drop_field = SQL::Translator::Producer::MySQL::drop_field($field2);
320is($drop_field, 'ALTER TABLE mytable DROP COLUMN myfield', 'Drop field works');
ca1f9923 321
322my $field3 = SQL::Translator::Schema::Field->new( name => 'myfield',
323 table => $table,
324 data_type => 'boolean',
325 is_nullable => 0,
326 is_foreign_key => 0,
327 is_unique => 0 );
328
329my $field3_sql = SQL::Translator::Producer::MySQL::create_field($field3, { mysql_version => 4.1 });
330is($field3_sql, 'myfield boolean NOT NULL', 'For Mysql >= 4, use boolean type');
331$field3_sql = SQL::Translator::Producer::MySQL::create_field($field3, { mysql_version => 3.22 });
332is($field3_sql, "myfield enum('0','1') NOT NULL", 'For Mysql < 4, use enum for boolean type');
333$field3_sql = SQL::Translator::Producer::MySQL::create_field($field3,);
334is($field3_sql, "myfield enum('0','1') NOT NULL", 'When no version specified, use enum for boolean type');
d28afa66 335
5d666b31 336my $number_sizes = {
337 '3, 2' => 'double',
338 12 => 'bigint',
339 1 => 'tinyint',
340 4 => 'int',
341};
342for my $size (keys %$number_sizes) {
343 my $expected = $number_sizes->{$size};
aee4b66e 344 my $number_field = SQL::Translator::Schema::Field->new(
75b8fbe8 345 name => "numberfield_$expected",
5d666b31 346 table => $table,
347 data_type => 'number',
348 size => $size,
349 is_nullable => 1,
350 is_foreign_key => 0,
aee4b66e 351 is_unique => 0
5d666b31 352 );
353
354 is(
355 SQL::Translator::Producer::MySQL::create_field($number_field),
75b8fbe8 356 "numberfield_$expected $expected($size)",
5d666b31 357 "Use $expected for NUMBER types of size $size"
358 );
359}
360
361my $varchars;
362for my $size (qw/255 256 65535 65536/) {
aee4b66e 363 $varchars->{$size} = SQL::Translator::Schema::Field->new(
5d666b31 364 name => "vch_$size",
365 table => $table,
366 data_type => 'varchar',
367 size => $size,
368 is_nullable => 1,
369 );
370}
371
372
373is (
374 SQL::Translator::Producer::MySQL::create_field($varchars->{255}, { mysql_version => 5.000003 }),
aee4b66e 375 'vch_255 varchar(255)',
5d666b31 376 'VARCHAR(255) is not substituted with TEXT for Mysql >= 5.0.3'
377);
378is (
379 SQL::Translator::Producer::MySQL::create_field($varchars->{255}, { mysql_version => 5.0 }),
aee4b66e 380 'vch_255 varchar(255)',
5d666b31 381 'VARCHAR(255) is not substituted with TEXT for Mysql < 5.0.3'
382);
383is (
384 SQL::Translator::Producer::MySQL::create_field($varchars->{255}),
aee4b66e 385 'vch_255 varchar(255)',
5d666b31 386 'VARCHAR(255) is not substituted with TEXT when no version specified',
387);
388
389
390is (
391 SQL::Translator::Producer::MySQL::create_field($varchars->{256}, { mysql_version => 5.000003 }),
aee4b66e 392 'vch_256 varchar(256)',
5d666b31 393 'VARCHAR(256) is not substituted with TEXT for Mysql >= 5.0.3'
394);
395is (
396 SQL::Translator::Producer::MySQL::create_field($varchars->{256}, { mysql_version => 5.0 }),
aee4b66e 397 'vch_256 text',
5d666b31 398 'VARCHAR(256) is substituted with TEXT for Mysql < 5.0.3'
399);
400is (
401 SQL::Translator::Producer::MySQL::create_field($varchars->{256}),
aee4b66e 402 'vch_256 text',
5d666b31 403 'VARCHAR(256) is substituted with TEXT when no version specified',
404);
405
406
407is (
408 SQL::Translator::Producer::MySQL::create_field($varchars->{65535}, { mysql_version => 5.000003 }),
aee4b66e 409 'vch_65535 varchar(65535)',
5d666b31 410 'VARCHAR(65535) is not substituted with TEXT for Mysql >= 5.0.3'
411);
412is (
413 SQL::Translator::Producer::MySQL::create_field($varchars->{65535}, { mysql_version => 5.0 }),
aee4b66e 414 'vch_65535 text',
5d666b31 415 'VARCHAR(65535) is substituted with TEXT for Mysql < 5.0.3'
416);
417is (
418 SQL::Translator::Producer::MySQL::create_field($varchars->{65535}),
aee4b66e 419 'vch_65535 text',
5d666b31 420 'VARCHAR(65535) is substituted with TEXT when no version specified',
421);
422
423
424is (
425 SQL::Translator::Producer::MySQL::create_field($varchars->{65536}, { mysql_version => 5.000003 }),
aee4b66e 426 'vch_65536 text',
5d666b31 427 'VARCHAR(65536) is substituted with TEXT for Mysql >= 5.0.3'
428);
429is (
430 SQL::Translator::Producer::MySQL::create_field($varchars->{65536}, { mysql_version => 5.0 }),
aee4b66e 431 'vch_65536 text',
5d666b31 432 'VARCHAR(65536) is substituted with TEXT for Mysql < 5.0.3'
433);
434is (
435 SQL::Translator::Producer::MySQL::create_field($varchars->{65536}),
aee4b66e 436 'vch_65536 text',
5d666b31 437 'VARCHAR(65536) is substituted with TEXT when no version specified',
438);
439
440
d28afa66 441{
442 my $view1 = SQL::Translator::Schema::View->new( name => 'view_foo',
443 fields => [qw/id name/],
444 sql => 'SELECT id, name FROM thing',
445 extra => {
446 mysql_definer => 'CURRENT_USER',
447 mysql_algorithm => 'MERGE',
448 mysql_security => 'DEFINER',
449 });
450 my $create_opts = { add_replace_view => 1, no_comments => 1 };
451 my $view1_sql1 = SQL::Translator::Producer::MySQL::create_view($view1, $create_opts);
452
1c8ec56e 453 my $view_sql_replace = <<'EOV';
454CREATE OR REPLACE
d28afa66 455 ALGORITHM = MERGE
456 DEFINER = CURRENT_USER
457 SQL SECURITY DEFINER
1c8ec56e 458 VIEW view_foo ( id, name ) AS
d28afa66 459 SELECT id, name FROM thing
1c8ec56e 460EOV
461
d28afa66 462 is($view1_sql1, $view_sql_replace, 'correct "CREATE OR REPLACE VIEW" SQL');
463
464
465 my $view2 = SQL::Translator::Schema::View->new( name => 'view_foo',
466 fields => [qw/id name/],
467 sql => 'SELECT id, name FROM thing',);
468 my $create2_opts = { add_replace_view => 0, no_comments => 1 };
469 my $view1_sql2 = SQL::Translator::Producer::MySQL::create_view($view2, $create2_opts);
1c8ec56e 470 my $view_sql_noreplace = <<'EOV';
471CREATE
472 VIEW view_foo ( id, name ) AS
d28afa66 473 SELECT id, name FROM thing
1c8ec56e 474EOV
475
d28afa66 476 is($view1_sql2, $view_sql_noreplace, 'correct "CREATE VIEW" SQL');
1c8ec56e 477
12018c09 478 {
479 my %extra = $view1->extra;
480 is_deeply \%extra,
481 {
482 'mysql_algorithm' => 'MERGE',
483 'mysql_definer' => 'CURRENT_USER',
484 'mysql_security' => 'DEFINER'
485 },
486 'Extra attributes';
487 }
488
489 $view1->remove_extra(qw/mysql_definer mysql_security/);
490 {
491 my %extra = $view1->extra;
492 is_deeply \%extra, { 'mysql_algorithm' => 'MERGE', }, 'Extra attributes after first reset_extra call';
493 }
494
495 $view1->remove_extra();
496 {
497 my %extra = $view1->extra;
498 is_deeply \%extra, {}, 'Extra attributes completely removed';
499 }
d28afa66 500}
de176728 501
502{
503
504 # certain types do not support a size, see also:
505 # http://dev.mysql.com/doc/refman/5.1/de/create-table.html
506 for my $type (qw/date time timestamp datetime year/) {
507 my $field = SQL::Translator::Schema::Field->new(
508 name => "my$type",
509 table => $table,
510 data_type => $type,
511 size => 10,
512 default_value => undef,
513 is_auto_increment => 0,
514 is_nullable => 1,
515 is_foreign_key => 0,
516 is_unique => 0
517 );
518 my $sql = SQL::Translator::Producer::MySQL::create_field($field);
519 is($sql, "my$type $type", "Skip length param for type $type");
520 }
521}
ab15e426 522
523} #non quoted test
524
525{
526 #Quoted test
527 my $table = SQL::Translator::Schema::Table->new( name => 'mydb.mytable');
528
529 my $field1 = SQL::Translator::Schema::Field->new( name => 'myfield',
530 table => $table,
531 data_type => 'VARCHAR',
532 size => 10,
533 default_value => undef,
534 is_auto_increment => 0,
535 is_nullable => 1,
536 is_foreign_key => 0,
537 is_unique => 0 );
538
539
540 my $field2 = SQL::Translator::Schema::Field->new( name => 'myfield',
541 table => $table,
542 data_type => 'VARCHAR',
543 size => 25,
544 default_value => undef,
545 is_auto_increment => 0,
546 is_nullable => 0,
547 is_foreign_key => 0,
548 is_unique => 0 );
549
550 my $field3 = SQL::Translator::Schema::Field->new( name => 'myfield',
551 table => $table,
552 data_type => 'boolean',
553 is_nullable => 0,
554 is_foreign_key => 0,
555 is_unique => 0 );
556
557
558 my $qt = '`';
559 my $qf = '`';
560 my $options = {
561 quote_table_names => $qt,
562 quote_field_names => $qf,
563 };
564
aee4b66e 565
ab15e426 566 my $alter_field = SQL::Translator::Producer::MySQL::alter_field($field1, $field2, $options);
567 is($alter_field, 'ALTER TABLE `mydb`.`mytable` CHANGE COLUMN `myfield` `myfield` VARCHAR(25) NOT NULL', 'Alter field works');
568
569 my $add_field = SQL::Translator::Producer::MySQL::add_field($field1, $options);
570
571 is($add_field, 'ALTER TABLE `mydb`.`mytable` ADD COLUMN `myfield` VARCHAR(10)', 'Add field works');
572
573 my $drop_field = SQL::Translator::Producer::MySQL::drop_field($field2, $options);
574 is($drop_field, 'ALTER TABLE `mydb`.`mytable` DROP COLUMN `myfield`', 'Drop field works');
575
576 my $field3_sql = SQL::Translator::Producer::MySQL::create_field($field3, { mysql_version => 4.1, %$options });
577is($field3_sql, '`myfield` boolean NOT NULL', 'For Mysql >= 4, use boolean type');
578$field3_sql = SQL::Translator::Producer::MySQL::create_field($field3, { mysql_version => 3.22, %$options });
579is($field3_sql, "`myfield` enum('0','1') NOT NULL", 'For Mysql < 4, use enum for boolean type');
580$field3_sql = SQL::Translator::Producer::MySQL::create_field($field3,$options);
581is($field3_sql, "`myfield` enum('0','1') NOT NULL", 'When no version specified, use enum for boolean type');
582
583 my $number_sizes = {
584 '3, 2' => 'double',
585 12 => 'bigint',
586 1 => 'tinyint',
587 4 => 'int',
588 };
589 for my $size (keys %$number_sizes) {
590 my $expected = $number_sizes->{$size};
aee4b66e 591 my $number_field = SQL::Translator::Schema::Field->new(
ab15e426 592 name => "numberfield_$expected",
593 table => $table,
594 data_type => 'number',
595 size => $size,
596 is_nullable => 1,
597 is_foreign_key => 0,
aee4b66e 598 is_unique => 0
ab15e426 599 );
600
601 is(
602 SQL::Translator::Producer::MySQL::create_field($number_field, $options),
603 "`numberfield_$expected` $expected($size)",
604 "Use $expected for NUMBER types of size $size"
605 );
606 }
607
608 my $varchars;
609 for my $size (qw/255 256 65535 65536/) {
aee4b66e 610 $varchars->{$size} = SQL::Translator::Schema::Field->new(
ab15e426 611 name => "vch_$size",
612 table => $table,
613 data_type => 'varchar',
614 size => $size,
615 is_nullable => 1,
616 );
617 }
618
619
620 is (
621 SQL::Translator::Producer::MySQL::create_field($varchars->{255}, { mysql_version => 5.000003, %$options }),
aee4b66e 622 '`vch_255` varchar(255)',
ab15e426 623 'VARCHAR(255) is not substituted with TEXT for Mysql >= 5.0.3'
624 );
625 is (
626 SQL::Translator::Producer::MySQL::create_field($varchars->{255}, { mysql_version => 5.0, %$options }),
aee4b66e 627 '`vch_255` varchar(255)',
ab15e426 628 'VARCHAR(255) is not substituted with TEXT for Mysql < 5.0.3'
629 );
630 is (
631 SQL::Translator::Producer::MySQL::create_field($varchars->{255}, $options),
aee4b66e 632 '`vch_255` varchar(255)',
ab15e426 633 'VARCHAR(255) is not substituted with TEXT when no version specified',
634 );
635
636
637 is (
638 SQL::Translator::Producer::MySQL::create_field($varchars->{256}, { mysql_version => 5.000003, %$options }),
aee4b66e 639 '`vch_256` varchar(256)',
ab15e426 640 'VARCHAR(256) is not substituted with TEXT for Mysql >= 5.0.3'
641 );
642 is (
643 SQL::Translator::Producer::MySQL::create_field($varchars->{256}, { mysql_version => 5.0, %$options }),
aee4b66e 644 '`vch_256` text',
ab15e426 645 'VARCHAR(256) is substituted with TEXT for Mysql < 5.0.3'
646 );
647 is (
648 SQL::Translator::Producer::MySQL::create_field($varchars->{256}, $options),
aee4b66e 649 '`vch_256` text',
ab15e426 650 'VARCHAR(256) is substituted with TEXT when no version specified',
651 );
652
653
654 is (
655 SQL::Translator::Producer::MySQL::create_field($varchars->{65535}, { mysql_version => 5.000003, %$options }),
aee4b66e 656 '`vch_65535` varchar(65535)',
ab15e426 657 'VARCHAR(65535) is not substituted with TEXT for Mysql >= 5.0.3'
658 );
659 is (
660 SQL::Translator::Producer::MySQL::create_field($varchars->{65535}, { mysql_version => 5.0, %$options }),
aee4b66e 661 '`vch_65535` text',
ab15e426 662 'VARCHAR(65535) is substituted with TEXT for Mysql < 5.0.3'
663 );
664 is (
665 SQL::Translator::Producer::MySQL::create_field($varchars->{65535}, $options),
aee4b66e 666 '`vch_65535` text',
ab15e426 667 'VARCHAR(65535) is substituted with TEXT when no version specified',
668 );
669
670
671 is (
672 SQL::Translator::Producer::MySQL::create_field($varchars->{65536}, { mysql_version => 5.000003, %$options }),
aee4b66e 673 '`vch_65536` text',
ab15e426 674 'VARCHAR(65536) is substituted with TEXT for Mysql >= 5.0.3'
675 );
676 is (
677 SQL::Translator::Producer::MySQL::create_field($varchars->{65536}, { mysql_version => 5.0, %$options }),
aee4b66e 678 '`vch_65536` text',
ab15e426 679 'VARCHAR(65536) is substituted with TEXT for Mysql < 5.0.3'
680 );
681 is (
682 SQL::Translator::Producer::MySQL::create_field($varchars->{65536}, $options),
aee4b66e 683 '`vch_65536` text',
ab15e426 684 'VARCHAR(65536) is substituted with TEXT when no version specified',
685 );
686
687 {
688 my $view1 = SQL::Translator::Schema::View->new( name => 'view_foo',
689 fields => [qw/id name/],
690 sql => 'SELECT `id`, `name` FROM `my`.`thing`',
691 extra => {
692 mysql_definer => 'CURRENT_USER',
693 mysql_algorithm => 'MERGE',
694 mysql_security => 'DEFINER',
695 });
696 my $create_opts = { add_replace_view => 1, no_comments => 1, %$options };
697 my $view1_sql1 = SQL::Translator::Producer::MySQL::create_view($view1, $create_opts);
698
699 my $view_sql_replace = <<'EOV';
700CREATE OR REPLACE
701 ALGORITHM = MERGE
702 DEFINER = CURRENT_USER
703 SQL SECURITY DEFINER
704 VIEW `view_foo` ( `id`, `name` ) AS
705 SELECT `id`, `name` FROM `my`.`thing`
706EOV
707
708 is($view1_sql1, $view_sql_replace, 'correct "CREATE OR REPLACE VIEW" SQL');
709
710
711 my $view2 = SQL::Translator::Schema::View->new( name => 'view_foo',
712 fields => [qw/id name/],
713 sql => 'SELECT `id`, `name` FROM `my`.`thing`',);
714 my $create2_opts = { add_replace_view => 0, no_comments => 1, %$options };
715 my $view1_sql2 = SQL::Translator::Producer::MySQL::create_view($view2, $create2_opts);
716 my $view_sql_noreplace = <<'EOV';
717CREATE
718 VIEW `view_foo` ( `id`, `name` ) AS
719 SELECT `id`, `name` FROM `my`.`thing`
720EOV
721
722 is($view1_sql2, $view_sql_noreplace, 'correct "CREATE VIEW" SQL');
723
724 {
725 my %extra = $view1->extra;
726 is_deeply \%extra,
727 {
728 'mysql_algorithm' => 'MERGE',
729 'mysql_definer' => 'CURRENT_USER',
730 'mysql_security' => 'DEFINER'
731 },
732 'Extra attributes';
733 }
734
735 $view1->remove_extra(qw/mysql_definer mysql_security/);
736 {
737 my %extra = $view1->extra;
738 is_deeply \%extra, { 'mysql_algorithm' => 'MERGE', }, 'Extra attributes after first reset_extra call';
739 }
740
741 $view1->remove_extra();
742 {
743 my %extra = $view1->extra;
744 is_deeply \%extra, {}, 'Extra attributes completely removed';
745 }
746 }
747
748 {
749
750 # certain types do not support a size, see also:
751 # http://dev.mysql.com/doc/refman/5.1/de/create-table.html
752 for my $type (qw/date time timestamp datetime year/) {
753 my $field = SQL::Translator::Schema::Field->new(
754 name => "my$type",
755 table => $table,
756 data_type => $type,
757 size => 10,
758 default_value => undef,
759 is_auto_increment => 0,
760 is_nullable => 1,
761 is_foreign_key => 0,
762 is_unique => 0
763 );
764 my $sql = SQL::Translator::Producer::MySQL::create_field($field, $options);
765 is($sql, "`my$type` $type", "Skip length param for type $type");
766 }
767 }
768}
b62fa492 769
770{ # test for rt62250
771 my $table = SQL::Translator::Schema::Table->new(name => 'table');
772 $table->add_field(
773 SQL::Translator::Schema::Field->new( name => 'mypk',
774 table => $table,
775 data_type => 'INT',
776 size => 10,
777 default_value => undef,
778 is_auto_increment => 1,
779 is_nullable => 0,
780 is_foreign_key => 0,
781 is_unique => 1 ));
782
783 my $constraint = $table->add_constraint(fields => ['mypk'], type => 'PRIMARY_KEY');
784 my $options = {quote_table_names => '`'};
785 is(SQL::Translator::Producer::MySQL::alter_drop_constraint($constraint,$options),
786 'ALTER TABLE `table` DROP PRIMARY KEY','valid drop primary key');
787}