Tab/WS crusade
[dbsrgits/SQL-Translator.git] / t / 38-mysql-producer.t
1 #!/usr/bin/perl -w
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
10 use strict;
11 use Test::More;
12 use Test::Exception;
13 use Test::SQL::Translator qw(maybe_plan);
14
15 use Data::Dumper;
16 use FindBin qw/$Bin/;
17
18 # Testing 1,2,3,4...
19 #=============================================================================
20
21 BEGIN {
22     maybe_plan(72,
23         'YAML',
24         'SQL::Translator::Producer::MySQL',
25         'Test::Differences',
26     )
27 }
28 use Test::Differences;
29 use SQL::Translator;
30
31 # Main test.
32 {
33 my $yaml_in = <<EOSCHEMA;
34 ---
35 schema:
36   tables:
37     thing:
38       name: thing
39       extra:
40         mysql_charset: latin1
41         mysql_collate: latin1_danish_ci
42       order: 1
43       fields:
44         id:
45           name: id
46           data_type: unsigned int
47           is_primary_key: 1
48           is_auto_increment: 1
49           order: 0
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
70       constraints:
71         - type: UNIQUE
72           fields:
73             - name
74           name: idx_unique_name
75
76     thing2:
77       name: some.thing2
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
92         foo2:
93           name: foo2
94           data_type: int
95           order: 2
96           is_not_null: 1
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
107       indices:
108         - type: NORMAL
109           fields:
110             - id
111           name: index_1
112         - type: NORMAL
113           fields:
114             - id
115           name: really_long_name_bigger_than_64_chars_aaaaaaaaaaaaaaaaaaaaaaaaaaa
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
125         - reference_table: thing
126           type: FOREIGN_KEY
127           fields: foo2
128           name: fk_thing
129
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
183 EOSCHEMA
184
185 my @stmts = (
186 "SET foreign_key_checks=0",
187
188 "DROP TABLE IF EXISTS `thing`",
189 "CREATE TABLE `thing` (
190   `id` unsigned int auto_increment,
191   `name` varchar(32),
192   `swedish_name` varchar(32) character set swe7,
193   `description` text character set utf8 collate utf8_general_ci,
194   PRIMARY KEY (`id`),
195   UNIQUE `idx_unique_name` (`name`)
196 ) ENGINE=InnoDB DEFAULT CHARACTER SET latin1 COLLATE latin1_danish_ci",
197
198 "DROP TABLE IF EXISTS `some`.`thing2`",
199 "CREATE TABLE `some`.`thing2` (
200   `id` integer,
201   `foo` integer,
202   `foo2` integer,
203   `bar_set` set('foo', 'bar', 'baz'),
204   INDEX `index_1` (`id`),
205   INDEX `really_long_name_bigger_than_64_chars_aaaaaaaaaaaaaaaaa_aed44c47` (`id`),
206   INDEX (`foo`),
207   INDEX (`foo2`),
208   PRIMARY KEY (`id`, `foo`),
209   CONSTRAINT `fk_thing` FOREIGN KEY (`foo`) REFERENCES `thing` (`id`),
210   CONSTRAINT `fk_thing_1` FOREIGN KEY (`foo2`) REFERENCES `thing` (`id`)
211 ) ENGINE=InnoDB",
212
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
228 "SET foreign_key_checks=1",
229
230 );
231
232 my @stmts_no_drop = grep {$_ !~ /^DROP TABLE/} @stmts;
233
234 my $mysql_out = join(";\n\n", @stmts_no_drop) . ";\n\n";
235
236
237     my $sqlt;
238     $sqlt = SQL::Translator->new(
239         show_warnings  => 1,
240         no_comments    => 1,
241 #        debug          => 1,
242         from           => "YAML",
243         to             => "MySQL",
244         quote_table_names => 1,
245         quote_field_names => 1
246     );
247
248     my $out = $sqlt->translate(\$yaml_in)
249     or die "Translate error:".$sqlt->error;
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";
256
257
258     @{$sqlt}{qw/quote_table_names quote_field_names/} = (0,0);
259     $out = $sqlt->translate(\$yaml_in)
260       or die "Translate error:".$sqlt->error;
261
262     @out = $sqlt->translate(\$yaml_in)
263       or die "Translate error:".$sqlt->error;
264     $mysql_out =~ s/`//g;
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
275     eq_or_diff $out, join(";\n\n", @stmts) . ";\n\n", "Output looks right with DROP TABLEs";
276     is_deeply \@out, \@stmts,          "Array output looks right with DROP TABLEs";
277 }
278
279 ###############################################################################
280 # New alter/add subs
281
282 {
283 my $table = SQL::Translator::Schema::Table->new( name => 'mytable');
284
285 my $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
295 my $field1_sql = SQL::Translator::Producer::MySQL::create_field($field1);
296
297 is($field1_sql, 'myfield VARCHAR(10)', 'Create field works');
298
299 my $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
309 my $alter_field = SQL::Translator::Producer::MySQL::alter_field($field1,
310                                                                 $field2);
311 is($alter_field, 'ALTER TABLE mytable CHANGE COLUMN myfield myfield VARCHAR(25) NOT NULL', 'Alter field works');
312
313 my $add_field = SQL::Translator::Producer::MySQL::add_field($field1);
314
315 is($add_field, 'ALTER TABLE mytable ADD COLUMN myfield VARCHAR(10)', 'Add field works');
316
317 my $drop_field = SQL::Translator::Producer::MySQL::drop_field($field2);
318 is($drop_field, 'ALTER TABLE mytable DROP COLUMN myfield', 'Drop field works');
319
320 my $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
327 my $field3_sql = SQL::Translator::Producer::MySQL::create_field($field3, { mysql_version => 4.1 });
328 is($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 });
330 is($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,);
332 is($field3_sql, "myfield enum('0','1') NOT NULL", 'When no version specified, use enum for boolean type');
333
334 my $number_sizes = {
335     '3, 2' => 'double',
336     12 => 'bigint',
337     1 => 'tinyint',
338     4 => 'int',
339 };
340 for my $size (keys %$number_sizes) {
341     my $expected = $number_sizes->{$size};
342     my $number_field = SQL::Translator::Schema::Field->new(
343         name => "numberfield_$expected",
344         table => $table,
345         data_type => 'number',
346         size => $size,
347         is_nullable => 1,
348         is_foreign_key => 0,
349         is_unique => 0
350     );
351
352     is(
353         SQL::Translator::Producer::MySQL::create_field($number_field),
354         "numberfield_$expected $expected($size)",
355         "Use $expected for NUMBER types of size $size"
356     );
357 }
358
359 my $varchars;
360 for my $size (qw/255 256 65535 65536/) {
361     $varchars->{$size} = SQL::Translator::Schema::Field->new(
362         name => "vch_$size",
363         table => $table,
364         data_type => 'varchar',
365         size => $size,
366         is_nullable => 1,
367     );
368 }
369
370
371 is (
372     SQL::Translator::Producer::MySQL::create_field($varchars->{255}, { mysql_version => 5.000003 }),
373     'vch_255 varchar(255)',
374     'VARCHAR(255) is not substituted with TEXT for Mysql >= 5.0.3'
375 );
376 is (
377     SQL::Translator::Producer::MySQL::create_field($varchars->{255}, { mysql_version => 5.0 }),
378     'vch_255 varchar(255)',
379     'VARCHAR(255) is not substituted with TEXT for Mysql < 5.0.3'
380 );
381 is (
382     SQL::Translator::Producer::MySQL::create_field($varchars->{255}),
383     'vch_255 varchar(255)',
384     'VARCHAR(255) is not substituted with TEXT when no version specified',
385 );
386
387
388 is (
389     SQL::Translator::Producer::MySQL::create_field($varchars->{256}, { mysql_version => 5.000003 }),
390     'vch_256 varchar(256)',
391     'VARCHAR(256) is not substituted with TEXT for Mysql >= 5.0.3'
392 );
393 is (
394     SQL::Translator::Producer::MySQL::create_field($varchars->{256}, { mysql_version => 5.0 }),
395     'vch_256 text',
396     'VARCHAR(256) is substituted with TEXT for Mysql < 5.0.3'
397 );
398 is (
399     SQL::Translator::Producer::MySQL::create_field($varchars->{256}),
400     'vch_256 text',
401     'VARCHAR(256) is substituted with TEXT when no version specified',
402 );
403
404
405 is (
406     SQL::Translator::Producer::MySQL::create_field($varchars->{65535}, { mysql_version => 5.000003 }),
407     'vch_65535 varchar(65535)',
408     'VARCHAR(65535) is not substituted with TEXT for Mysql >= 5.0.3'
409 );
410 is (
411     SQL::Translator::Producer::MySQL::create_field($varchars->{65535}, { mysql_version => 5.0 }),
412     'vch_65535 text',
413     'VARCHAR(65535) is substituted with TEXT for Mysql < 5.0.3'
414 );
415 is (
416     SQL::Translator::Producer::MySQL::create_field($varchars->{65535}),
417     'vch_65535 text',
418     'VARCHAR(65535) is substituted with TEXT when no version specified',
419 );
420
421
422 is (
423     SQL::Translator::Producer::MySQL::create_field($varchars->{65536}, { mysql_version => 5.000003 }),
424     'vch_65536 text',
425     'VARCHAR(65536) is substituted with TEXT for Mysql >= 5.0.3'
426 );
427 is (
428     SQL::Translator::Producer::MySQL::create_field($varchars->{65536}, { mysql_version => 5.0 }),
429     'vch_65536 text',
430     'VARCHAR(65536) is substituted with TEXT for Mysql < 5.0.3'
431 );
432 is (
433     SQL::Translator::Producer::MySQL::create_field($varchars->{65536}),
434     'vch_65536 text',
435     'VARCHAR(65536) is substituted with TEXT when no version specified',
436 );
437
438
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
451   my $view_sql_replace = <<'EOV';
452 CREATE OR REPLACE
453    ALGORITHM = MERGE
454    DEFINER = CURRENT_USER
455    SQL SECURITY DEFINER
456   VIEW view_foo ( id, name ) AS
457     SELECT id, name FROM thing
458 EOV
459
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);
468   my $view_sql_noreplace = <<'EOV';
469 CREATE
470   VIEW view_foo ( id, name ) AS
471     SELECT id, name FROM thing
472 EOV
473
474   is($view1_sql2, $view_sql_noreplace, 'correct "CREATE VIEW" SQL');
475
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   }
498 }
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 }
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
563
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 });
575 is($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 });
577 is($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);
579 is($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};
589         my $number_field = SQL::Translator::Schema::Field->new(
590             name => "numberfield_$expected",
591             table => $table,
592             data_type => 'number',
593             size => $size,
594             is_nullable => 1,
595             is_foreign_key => 0,
596             is_unique => 0
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/) {
608         $varchars->{$size} = SQL::Translator::Schema::Field->new(
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 }),
620         '`vch_255` varchar(255)',
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 }),
625         '`vch_255` varchar(255)',
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),
630         '`vch_255` varchar(255)',
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 }),
637         '`vch_256` varchar(256)',
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 }),
642         '`vch_256` text',
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),
647         '`vch_256` text',
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 }),
654         '`vch_65535` varchar(65535)',
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 }),
659         '`vch_65535` text',
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),
664         '`vch_65535` text',
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 }),
671         '`vch_65536` text',
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 }),
676         '`vch_65536` text',
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),
681         '`vch_65536` text',
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';
698 CREATE 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`
704 EOV
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';
715 CREATE
716   VIEW `view_foo` ( `id`, `name` ) AS
717     SELECT `id`, `name` FROM `my`.`thing`
718 EOV
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 }