Fix for mysql producer drop primary key, refs #62250
[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
49 order: 0
1ded8513 50 name:
51 name: name
52 data_type: varchar
53 size:
54 - 32
55 order: 1
56 swedish_name:
57 name: swedish_name
58 data_type: varchar
59 size: 32
60 extra:
61 mysql_charset: swe7
62 order: 2
63 description:
64 name: description
65 data_type: text
66 extra:
67 mysql_charset: utf8
68 mysql_collate: utf8_general_ci
69 order: 3
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
85 order: 0
86 is_foreign_key: 1
87 foo:
88 name: foo
89 data_type: int
90 order: 1
91 is_not_null: 1
fb149f81 92 foo2:
93 name: foo2
94 data_type: int
95 order: 2
96 is_not_null: 1
7c1aae02 97 bar_set:
98 name: bar_set
99 data_type: set
100 order: 3
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:
133 order: 2
134 fields:
135 id:
136 name: id
137 data_type: int
138 is_primary_key: 0
139 order: 0
140 is_foreign_key: 1
141 foo:
142 name: foo
143 data_type: int
144 order: 1
145 is_not_null: 1
146 foo2:
147 name: foo2
148 data_type: int
149 order: 2
150 is_not_null: 1
151 bar_set:
152 name: bar_set
153 data_type: set
154 order: 3
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
fe0f47d0 257
f6af58ae 258 @{$sqlt}{qw/quote_table_names quote_field_names/} = (0,0);
fe0f47d0 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
269 @{$sqlt}{qw/add_drop_table quote_field_names quote_table_names/} = (1,1,1);
270 @out = $sqlt->translate(\$yaml_in)
271 or die "Translat eerror:".$sqlt->error;
272 $out = $sqlt->translate(\$yaml_in)
273 or die "Translat eerror:".$sqlt->error;
274
24d9fe69 275 eq_or_diff $out, join(";\n\n", @stmts) . ";\n\n", "Output looks right with DROP TABLEs";
f6af58ae 276 is_deeply \@out, \@stmts, "Array output looks right with DROP TABLEs";
1ded8513 277}
8db4bd9d 278
279###############################################################################
280# New alter/add subs
281
ab15e426 282{
8db4bd9d 283my $table = SQL::Translator::Schema::Table->new( name => 'mytable');
284
285my $field1 = SQL::Translator::Schema::Field->new( name => 'myfield',
286 table => $table,
287 data_type => 'VARCHAR',
288 size => 10,
289 default_value => undef,
290 is_auto_increment => 0,
291 is_nullable => 1,
292 is_foreign_key => 0,
293 is_unique => 0 );
294
295my $field1_sql = SQL::Translator::Producer::MySQL::create_field($field1);
296
297is($field1_sql, 'myfield VARCHAR(10)', 'Create field works');
298
299my $field2 = SQL::Translator::Schema::Field->new( name => 'myfield',
300 table => $table,
301 data_type => 'VARCHAR',
302 size => 25,
303 default_value => undef,
304 is_auto_increment => 0,
305 is_nullable => 0,
306 is_foreign_key => 0,
307 is_unique => 0 );
308
309my $alter_field = SQL::Translator::Producer::MySQL::alter_field($field1,
310 $field2);
311is($alter_field, 'ALTER TABLE mytable CHANGE COLUMN myfield myfield VARCHAR(25) NOT NULL', 'Alter field works');
312
313my $add_field = SQL::Translator::Producer::MySQL::add_field($field1);
314
315is($add_field, 'ALTER TABLE mytable ADD COLUMN myfield VARCHAR(10)', 'Add field works');
316
317my $drop_field = SQL::Translator::Producer::MySQL::drop_field($field2);
318is($drop_field, 'ALTER TABLE mytable DROP COLUMN myfield', 'Drop field works');
ca1f9923 319
320my $field3 = SQL::Translator::Schema::Field->new( name => 'myfield',
321 table => $table,
322 data_type => 'boolean',
323 is_nullable => 0,
324 is_foreign_key => 0,
325 is_unique => 0 );
326
327my $field3_sql = SQL::Translator::Producer::MySQL::create_field($field3, { mysql_version => 4.1 });
328is($field3_sql, 'myfield boolean NOT NULL', 'For Mysql >= 4, use boolean type');
329$field3_sql = SQL::Translator::Producer::MySQL::create_field($field3, { mysql_version => 3.22 });
330is($field3_sql, "myfield enum('0','1') NOT NULL", 'For Mysql < 4, use enum for boolean type');
331$field3_sql = SQL::Translator::Producer::MySQL::create_field($field3,);
332is($field3_sql, "myfield enum('0','1') NOT NULL", 'When no version specified, use enum for boolean type');
d28afa66 333
5d666b31 334my $number_sizes = {
335 '3, 2' => 'double',
336 12 => 'bigint',
337 1 => 'tinyint',
338 4 => 'int',
339};
340for my $size (keys %$number_sizes) {
341 my $expected = $number_sizes->{$size};
aee4b66e 342 my $number_field = SQL::Translator::Schema::Field->new(
75b8fbe8 343 name => "numberfield_$expected",
5d666b31 344 table => $table,
345 data_type => 'number',
346 size => $size,
347 is_nullable => 1,
348 is_foreign_key => 0,
aee4b66e 349 is_unique => 0
5d666b31 350 );
351
352 is(
353 SQL::Translator::Producer::MySQL::create_field($number_field),
75b8fbe8 354 "numberfield_$expected $expected($size)",
5d666b31 355 "Use $expected for NUMBER types of size $size"
356 );
357}
358
359my $varchars;
360for my $size (qw/255 256 65535 65536/) {
aee4b66e 361 $varchars->{$size} = SQL::Translator::Schema::Field->new(
5d666b31 362 name => "vch_$size",
363 table => $table,
364 data_type => 'varchar',
365 size => $size,
366 is_nullable => 1,
367 );
368}
369
370
371is (
372 SQL::Translator::Producer::MySQL::create_field($varchars->{255}, { mysql_version => 5.000003 }),
aee4b66e 373 'vch_255 varchar(255)',
5d666b31 374 'VARCHAR(255) is not substituted with TEXT for Mysql >= 5.0.3'
375);
376is (
377 SQL::Translator::Producer::MySQL::create_field($varchars->{255}, { mysql_version => 5.0 }),
aee4b66e 378 'vch_255 varchar(255)',
5d666b31 379 'VARCHAR(255) is not substituted with TEXT for Mysql < 5.0.3'
380);
381is (
382 SQL::Translator::Producer::MySQL::create_field($varchars->{255}),
aee4b66e 383 'vch_255 varchar(255)',
5d666b31 384 'VARCHAR(255) is not substituted with TEXT when no version specified',
385);
386
387
388is (
389 SQL::Translator::Producer::MySQL::create_field($varchars->{256}, { mysql_version => 5.000003 }),
aee4b66e 390 'vch_256 varchar(256)',
5d666b31 391 'VARCHAR(256) is not substituted with TEXT for Mysql >= 5.0.3'
392);
393is (
394 SQL::Translator::Producer::MySQL::create_field($varchars->{256}, { mysql_version => 5.0 }),
aee4b66e 395 'vch_256 text',
5d666b31 396 'VARCHAR(256) is substituted with TEXT for Mysql < 5.0.3'
397);
398is (
399 SQL::Translator::Producer::MySQL::create_field($varchars->{256}),
aee4b66e 400 'vch_256 text',
5d666b31 401 'VARCHAR(256) is substituted with TEXT when no version specified',
402);
403
404
405is (
406 SQL::Translator::Producer::MySQL::create_field($varchars->{65535}, { mysql_version => 5.000003 }),
aee4b66e 407 'vch_65535 varchar(65535)',
5d666b31 408 'VARCHAR(65535) is not substituted with TEXT for Mysql >= 5.0.3'
409);
410is (
411 SQL::Translator::Producer::MySQL::create_field($varchars->{65535}, { mysql_version => 5.0 }),
aee4b66e 412 'vch_65535 text',
5d666b31 413 'VARCHAR(65535) is substituted with TEXT for Mysql < 5.0.3'
414);
415is (
416 SQL::Translator::Producer::MySQL::create_field($varchars->{65535}),
aee4b66e 417 'vch_65535 text',
5d666b31 418 'VARCHAR(65535) is substituted with TEXT when no version specified',
419);
420
421
422is (
423 SQL::Translator::Producer::MySQL::create_field($varchars->{65536}, { mysql_version => 5.000003 }),
aee4b66e 424 'vch_65536 text',
5d666b31 425 'VARCHAR(65536) is substituted with TEXT for Mysql >= 5.0.3'
426);
427is (
428 SQL::Translator::Producer::MySQL::create_field($varchars->{65536}, { mysql_version => 5.0 }),
aee4b66e 429 'vch_65536 text',
5d666b31 430 'VARCHAR(65536) is substituted with TEXT for Mysql < 5.0.3'
431);
432is (
433 SQL::Translator::Producer::MySQL::create_field($varchars->{65536}),
aee4b66e 434 'vch_65536 text',
5d666b31 435 'VARCHAR(65536) is substituted with TEXT when no version specified',
436);
437
438
d28afa66 439{
440 my $view1 = SQL::Translator::Schema::View->new( name => 'view_foo',
441 fields => [qw/id name/],
442 sql => 'SELECT id, name FROM thing',
443 extra => {
444 mysql_definer => 'CURRENT_USER',
445 mysql_algorithm => 'MERGE',
446 mysql_security => 'DEFINER',
447 });
448 my $create_opts = { add_replace_view => 1, no_comments => 1 };
449 my $view1_sql1 = SQL::Translator::Producer::MySQL::create_view($view1, $create_opts);
450
1c8ec56e 451 my $view_sql_replace = <<'EOV';
452CREATE OR REPLACE
d28afa66 453 ALGORITHM = MERGE
454 DEFINER = CURRENT_USER
455 SQL SECURITY DEFINER
1c8ec56e 456 VIEW view_foo ( id, name ) AS
d28afa66 457 SELECT id, name FROM thing
1c8ec56e 458EOV
459
d28afa66 460 is($view1_sql1, $view_sql_replace, 'correct "CREATE OR REPLACE VIEW" SQL');
461
462
463 my $view2 = SQL::Translator::Schema::View->new( name => 'view_foo',
464 fields => [qw/id name/],
465 sql => 'SELECT id, name FROM thing',);
466 my $create2_opts = { add_replace_view => 0, no_comments => 1 };
467 my $view1_sql2 = SQL::Translator::Producer::MySQL::create_view($view2, $create2_opts);
1c8ec56e 468 my $view_sql_noreplace = <<'EOV';
469CREATE
470 VIEW view_foo ( id, name ) AS
d28afa66 471 SELECT id, name FROM thing
1c8ec56e 472EOV
473
d28afa66 474 is($view1_sql2, $view_sql_noreplace, 'correct "CREATE VIEW" SQL');
1c8ec56e 475
12018c09 476 {
477 my %extra = $view1->extra;
478 is_deeply \%extra,
479 {
480 'mysql_algorithm' => 'MERGE',
481 'mysql_definer' => 'CURRENT_USER',
482 'mysql_security' => 'DEFINER'
483 },
484 'Extra attributes';
485 }
486
487 $view1->remove_extra(qw/mysql_definer mysql_security/);
488 {
489 my %extra = $view1->extra;
490 is_deeply \%extra, { 'mysql_algorithm' => 'MERGE', }, 'Extra attributes after first reset_extra call';
491 }
492
493 $view1->remove_extra();
494 {
495 my %extra = $view1->extra;
496 is_deeply \%extra, {}, 'Extra attributes completely removed';
497 }
d28afa66 498}
de176728 499
500{
501
502 # certain types do not support a size, see also:
503 # http://dev.mysql.com/doc/refman/5.1/de/create-table.html
504 for my $type (qw/date time timestamp datetime year/) {
505 my $field = SQL::Translator::Schema::Field->new(
506 name => "my$type",
507 table => $table,
508 data_type => $type,
509 size => 10,
510 default_value => undef,
511 is_auto_increment => 0,
512 is_nullable => 1,
513 is_foreign_key => 0,
514 is_unique => 0
515 );
516 my $sql = SQL::Translator::Producer::MySQL::create_field($field);
517 is($sql, "my$type $type", "Skip length param for type $type");
518 }
519}
ab15e426 520
521} #non quoted test
522
523{
524 #Quoted test
525 my $table = SQL::Translator::Schema::Table->new( name => 'mydb.mytable');
526
527 my $field1 = SQL::Translator::Schema::Field->new( name => 'myfield',
528 table => $table,
529 data_type => 'VARCHAR',
530 size => 10,
531 default_value => undef,
532 is_auto_increment => 0,
533 is_nullable => 1,
534 is_foreign_key => 0,
535 is_unique => 0 );
536
537
538 my $field2 = SQL::Translator::Schema::Field->new( name => 'myfield',
539 table => $table,
540 data_type => 'VARCHAR',
541 size => 25,
542 default_value => undef,
543 is_auto_increment => 0,
544 is_nullable => 0,
545 is_foreign_key => 0,
546 is_unique => 0 );
547
548 my $field3 = SQL::Translator::Schema::Field->new( name => 'myfield',
549 table => $table,
550 data_type => 'boolean',
551 is_nullable => 0,
552 is_foreign_key => 0,
553 is_unique => 0 );
554
555
556 my $qt = '`';
557 my $qf = '`';
558 my $options = {
559 quote_table_names => $qt,
560 quote_field_names => $qf,
561 };
562
aee4b66e 563
ab15e426 564 my $alter_field = SQL::Translator::Producer::MySQL::alter_field($field1, $field2, $options);
565 is($alter_field, 'ALTER TABLE `mydb`.`mytable` CHANGE COLUMN `myfield` `myfield` VARCHAR(25) NOT NULL', 'Alter field works');
566
567 my $add_field = SQL::Translator::Producer::MySQL::add_field($field1, $options);
568
569 is($add_field, 'ALTER TABLE `mydb`.`mytable` ADD COLUMN `myfield` VARCHAR(10)', 'Add field works');
570
571 my $drop_field = SQL::Translator::Producer::MySQL::drop_field($field2, $options);
572 is($drop_field, 'ALTER TABLE `mydb`.`mytable` DROP COLUMN `myfield`', 'Drop field works');
573
574 my $field3_sql = SQL::Translator::Producer::MySQL::create_field($field3, { mysql_version => 4.1, %$options });
575is($field3_sql, '`myfield` boolean NOT NULL', 'For Mysql >= 4, use boolean type');
576$field3_sql = SQL::Translator::Producer::MySQL::create_field($field3, { mysql_version => 3.22, %$options });
577is($field3_sql, "`myfield` enum('0','1') NOT NULL", 'For Mysql < 4, use enum for boolean type');
578$field3_sql = SQL::Translator::Producer::MySQL::create_field($field3,$options);
579is($field3_sql, "`myfield` enum('0','1') NOT NULL", 'When no version specified, use enum for boolean type');
580
581 my $number_sizes = {
582 '3, 2' => 'double',
583 12 => 'bigint',
584 1 => 'tinyint',
585 4 => 'int',
586 };
587 for my $size (keys %$number_sizes) {
588 my $expected = $number_sizes->{$size};
aee4b66e 589 my $number_field = SQL::Translator::Schema::Field->new(
ab15e426 590 name => "numberfield_$expected",
591 table => $table,
592 data_type => 'number',
593 size => $size,
594 is_nullable => 1,
595 is_foreign_key => 0,
aee4b66e 596 is_unique => 0
ab15e426 597 );
598
599 is(
600 SQL::Translator::Producer::MySQL::create_field($number_field, $options),
601 "`numberfield_$expected` $expected($size)",
602 "Use $expected for NUMBER types of size $size"
603 );
604 }
605
606 my $varchars;
607 for my $size (qw/255 256 65535 65536/) {
aee4b66e 608 $varchars->{$size} = SQL::Translator::Schema::Field->new(
ab15e426 609 name => "vch_$size",
610 table => $table,
611 data_type => 'varchar',
612 size => $size,
613 is_nullable => 1,
614 );
615 }
616
617
618 is (
619 SQL::Translator::Producer::MySQL::create_field($varchars->{255}, { mysql_version => 5.000003, %$options }),
aee4b66e 620 '`vch_255` varchar(255)',
ab15e426 621 'VARCHAR(255) is not substituted with TEXT for Mysql >= 5.0.3'
622 );
623 is (
624 SQL::Translator::Producer::MySQL::create_field($varchars->{255}, { mysql_version => 5.0, %$options }),
aee4b66e 625 '`vch_255` varchar(255)',
ab15e426 626 'VARCHAR(255) is not substituted with TEXT for Mysql < 5.0.3'
627 );
628 is (
629 SQL::Translator::Producer::MySQL::create_field($varchars->{255}, $options),
aee4b66e 630 '`vch_255` varchar(255)',
ab15e426 631 'VARCHAR(255) is not substituted with TEXT when no version specified',
632 );
633
634
635 is (
636 SQL::Translator::Producer::MySQL::create_field($varchars->{256}, { mysql_version => 5.000003, %$options }),
aee4b66e 637 '`vch_256` varchar(256)',
ab15e426 638 'VARCHAR(256) is not substituted with TEXT for Mysql >= 5.0.3'
639 );
640 is (
641 SQL::Translator::Producer::MySQL::create_field($varchars->{256}, { mysql_version => 5.0, %$options }),
aee4b66e 642 '`vch_256` text',
ab15e426 643 'VARCHAR(256) is substituted with TEXT for Mysql < 5.0.3'
644 );
645 is (
646 SQL::Translator::Producer::MySQL::create_field($varchars->{256}, $options),
aee4b66e 647 '`vch_256` text',
ab15e426 648 'VARCHAR(256) is substituted with TEXT when no version specified',
649 );
650
651
652 is (
653 SQL::Translator::Producer::MySQL::create_field($varchars->{65535}, { mysql_version => 5.000003, %$options }),
aee4b66e 654 '`vch_65535` varchar(65535)',
ab15e426 655 'VARCHAR(65535) is not substituted with TEXT for Mysql >= 5.0.3'
656 );
657 is (
658 SQL::Translator::Producer::MySQL::create_field($varchars->{65535}, { mysql_version => 5.0, %$options }),
aee4b66e 659 '`vch_65535` text',
ab15e426 660 'VARCHAR(65535) is substituted with TEXT for Mysql < 5.0.3'
661 );
662 is (
663 SQL::Translator::Producer::MySQL::create_field($varchars->{65535}, $options),
aee4b66e 664 '`vch_65535` text',
ab15e426 665 'VARCHAR(65535) is substituted with TEXT when no version specified',
666 );
667
668
669 is (
670 SQL::Translator::Producer::MySQL::create_field($varchars->{65536}, { mysql_version => 5.000003, %$options }),
aee4b66e 671 '`vch_65536` text',
ab15e426 672 'VARCHAR(65536) is substituted with TEXT for Mysql >= 5.0.3'
673 );
674 is (
675 SQL::Translator::Producer::MySQL::create_field($varchars->{65536}, { mysql_version => 5.0, %$options }),
aee4b66e 676 '`vch_65536` text',
ab15e426 677 'VARCHAR(65536) is substituted with TEXT for Mysql < 5.0.3'
678 );
679 is (
680 SQL::Translator::Producer::MySQL::create_field($varchars->{65536}, $options),
aee4b66e 681 '`vch_65536` text',
ab15e426 682 'VARCHAR(65536) is substituted with TEXT when no version specified',
683 );
684
685 {
686 my $view1 = SQL::Translator::Schema::View->new( name => 'view_foo',
687 fields => [qw/id name/],
688 sql => 'SELECT `id`, `name` FROM `my`.`thing`',
689 extra => {
690 mysql_definer => 'CURRENT_USER',
691 mysql_algorithm => 'MERGE',
692 mysql_security => 'DEFINER',
693 });
694 my $create_opts = { add_replace_view => 1, no_comments => 1, %$options };
695 my $view1_sql1 = SQL::Translator::Producer::MySQL::create_view($view1, $create_opts);
696
697 my $view_sql_replace = <<'EOV';
698CREATE OR REPLACE
699 ALGORITHM = MERGE
700 DEFINER = CURRENT_USER
701 SQL SECURITY DEFINER
702 VIEW `view_foo` ( `id`, `name` ) AS
703 SELECT `id`, `name` FROM `my`.`thing`
704EOV
705
706 is($view1_sql1, $view_sql_replace, 'correct "CREATE OR REPLACE VIEW" SQL');
707
708
709 my $view2 = SQL::Translator::Schema::View->new( name => 'view_foo',
710 fields => [qw/id name/],
711 sql => 'SELECT `id`, `name` FROM `my`.`thing`',);
712 my $create2_opts = { add_replace_view => 0, no_comments => 1, %$options };
713 my $view1_sql2 = SQL::Translator::Producer::MySQL::create_view($view2, $create2_opts);
714 my $view_sql_noreplace = <<'EOV';
715CREATE
716 VIEW `view_foo` ( `id`, `name` ) AS
717 SELECT `id`, `name` FROM `my`.`thing`
718EOV
719
720 is($view1_sql2, $view_sql_noreplace, 'correct "CREATE VIEW" SQL');
721
722 {
723 my %extra = $view1->extra;
724 is_deeply \%extra,
725 {
726 'mysql_algorithm' => 'MERGE',
727 'mysql_definer' => 'CURRENT_USER',
728 'mysql_security' => 'DEFINER'
729 },
730 'Extra attributes';
731 }
732
733 $view1->remove_extra(qw/mysql_definer mysql_security/);
734 {
735 my %extra = $view1->extra;
736 is_deeply \%extra, { 'mysql_algorithm' => 'MERGE', }, 'Extra attributes after first reset_extra call';
737 }
738
739 $view1->remove_extra();
740 {
741 my %extra = $view1->extra;
742 is_deeply \%extra, {}, 'Extra attributes completely removed';
743 }
744 }
745
746 {
747
748 # certain types do not support a size, see also:
749 # http://dev.mysql.com/doc/refman/5.1/de/create-table.html
750 for my $type (qw/date time timestamp datetime year/) {
751 my $field = SQL::Translator::Schema::Field->new(
752 name => "my$type",
753 table => $table,
754 data_type => $type,
755 size => 10,
756 default_value => undef,
757 is_auto_increment => 0,
758 is_nullable => 1,
759 is_foreign_key => 0,
760 is_unique => 0
761 );
762 my $sql = SQL::Translator::Producer::MySQL::create_field($field, $options);
763 is($sql, "`my$type` $type", "Skip length param for type $type");
764 }
765 }
766}
b62fa492 767
768{ # test for rt62250
769 my $table = SQL::Translator::Schema::Table->new(name => 'table');
770 $table->add_field(
771 SQL::Translator::Schema::Field->new( name => 'mypk',
772 table => $table,
773 data_type => 'INT',
774 size => 10,
775 default_value => undef,
776 is_auto_increment => 1,
777 is_nullable => 0,
778 is_foreign_key => 0,
779 is_unique => 1 ));
780
781 my $constraint = $table->add_constraint(fields => ['mypk'], type => 'PRIMARY_KEY');
782 my $options = {quote_table_names => '`'};
783 is(SQL::Translator::Producer::MySQL::alter_drop_constraint($constraint,$options),
784 'ALTER TABLE `table` DROP PRIMARY KEY','valid drop primary key');
785}