1b72059f25805a11b6cd0dca3309112962226dfd
[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(73,
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: 1
50         name:
51           name: name
52           data_type: varchar
53           size:
54             - 32
55           order: 2
56         swedish_name:
57           name: swedish_name
58           data_type: varchar
59           size: 32
60           extra:
61             mysql_charset: swe7
62           order: 3
63         description:
64           name: description
65           data_type: text
66           extra:
67             mysql_charset: utf8
68             mysql_collate: utf8_general_ci
69           order: 4
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: 1
86           is_foreign_key: 1
87         foo:
88           name: foo
89           data_type: int
90           order: 2
91           is_not_null: 1
92         foo2:
93           name: foo2
94           data_type: int
95           order: 3
96           is_not_null: 1
97         bar_set:
98           name: bar_set
99           data_type: set
100           order: 4
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: 3
134       fields:
135         id:
136           name: id
137           data_type: int
138           is_primary_key: 0
139           order: 1
140           is_foreign_key: 1
141         foo:
142           name: foo
143           data_type: int
144           order: 2
145           is_not_null: 1
146         foo2:
147           name: foo2
148           data_type: int
149           order: 3
150           is_not_null: 1
151         bar_set:
152           name: bar_set
153           data_type: set
154           order: 4
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     $sqlt->quote_identifiers(0);
258
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->quote_identifiers(1);
270     $sqlt->add_drop_table(1);
271
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
277     eq_or_diff $out, join(";\n\n", @stmts) . ";\n\n", "Output looks right with DROP TABLEs";
278     is_deeply \@out, \@stmts,          "Array output looks right with DROP TABLEs";
279 }
280
281 ###############################################################################
282 # New alter/add subs
283
284 {
285 my $table = SQL::Translator::Schema::Table->new( name => 'mytable');
286
287 my $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
297 my $field1_sql = SQL::Translator::Producer::MySQL::create_field($field1);
298
299 is($field1_sql, 'myfield VARCHAR(10)', 'Create field works');
300
301 my $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
311 my $alter_field = SQL::Translator::Producer::MySQL::alter_field($field1,
312                                                                 $field2);
313 is($alter_field, 'ALTER TABLE mytable CHANGE COLUMN myfield myfield VARCHAR(25) NOT NULL', 'Alter field works');
314
315 my $add_field = SQL::Translator::Producer::MySQL::add_field($field1);
316
317 is($add_field, 'ALTER TABLE mytable ADD COLUMN myfield VARCHAR(10)', 'Add field works');
318
319 my $drop_field = SQL::Translator::Producer::MySQL::drop_field($field2);
320 is($drop_field, 'ALTER TABLE mytable DROP COLUMN myfield', 'Drop field works');
321
322 my $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
329 my $field3_sql = SQL::Translator::Producer::MySQL::create_field($field3, { mysql_version => 4.1 });
330 is($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 });
332 is($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,);
334 is($field3_sql, "myfield enum('0','1') NOT NULL", 'When no version specified, use enum for boolean type');
335
336 my $number_sizes = {
337     '3, 2' => 'double',
338     12 => 'bigint',
339     1 => 'tinyint',
340     4 => 'int',
341 };
342 for my $size (keys %$number_sizes) {
343     my $expected = $number_sizes->{$size};
344     my $number_field = SQL::Translator::Schema::Field->new(
345         name => "numberfield_$expected",
346         table => $table,
347         data_type => 'number',
348         size => $size,
349         is_nullable => 1,
350         is_foreign_key => 0,
351         is_unique => 0
352     );
353
354     is(
355         SQL::Translator::Producer::MySQL::create_field($number_field),
356         "numberfield_$expected $expected($size)",
357         "Use $expected for NUMBER types of size $size"
358     );
359 }
360
361 my $varchars;
362 for my $size (qw/255 256 65535 65536/) {
363     $varchars->{$size} = SQL::Translator::Schema::Field->new(
364         name => "vch_$size",
365         table => $table,
366         data_type => 'varchar',
367         size => $size,
368         is_nullable => 1,
369     );
370 }
371
372
373 is (
374     SQL::Translator::Producer::MySQL::create_field($varchars->{255}, { mysql_version => 5.000003 }),
375     'vch_255 varchar(255)',
376     'VARCHAR(255) is not substituted with TEXT for Mysql >= 5.0.3'
377 );
378 is (
379     SQL::Translator::Producer::MySQL::create_field($varchars->{255}, { mysql_version => 5.0 }),
380     'vch_255 varchar(255)',
381     'VARCHAR(255) is not substituted with TEXT for Mysql < 5.0.3'
382 );
383 is (
384     SQL::Translator::Producer::MySQL::create_field($varchars->{255}),
385     'vch_255 varchar(255)',
386     'VARCHAR(255) is not substituted with TEXT when no version specified',
387 );
388
389
390 is (
391     SQL::Translator::Producer::MySQL::create_field($varchars->{256}, { mysql_version => 5.000003 }),
392     'vch_256 varchar(256)',
393     'VARCHAR(256) is not substituted with TEXT for Mysql >= 5.0.3'
394 );
395 is (
396     SQL::Translator::Producer::MySQL::create_field($varchars->{256}, { mysql_version => 5.0 }),
397     'vch_256 text',
398     'VARCHAR(256) is substituted with TEXT for Mysql < 5.0.3'
399 );
400 is (
401     SQL::Translator::Producer::MySQL::create_field($varchars->{256}),
402     'vch_256 text',
403     'VARCHAR(256) is substituted with TEXT when no version specified',
404 );
405
406
407 is (
408     SQL::Translator::Producer::MySQL::create_field($varchars->{65535}, { mysql_version => 5.000003 }),
409     'vch_65535 varchar(65535)',
410     'VARCHAR(65535) is not substituted with TEXT for Mysql >= 5.0.3'
411 );
412 is (
413     SQL::Translator::Producer::MySQL::create_field($varchars->{65535}, { mysql_version => 5.0 }),
414     'vch_65535 text',
415     'VARCHAR(65535) is substituted with TEXT for Mysql < 5.0.3'
416 );
417 is (
418     SQL::Translator::Producer::MySQL::create_field($varchars->{65535}),
419     'vch_65535 text',
420     'VARCHAR(65535) is substituted with TEXT when no version specified',
421 );
422
423
424 is (
425     SQL::Translator::Producer::MySQL::create_field($varchars->{65536}, { mysql_version => 5.000003 }),
426     'vch_65536 text',
427     'VARCHAR(65536) is substituted with TEXT for Mysql >= 5.0.3'
428 );
429 is (
430     SQL::Translator::Producer::MySQL::create_field($varchars->{65536}, { mysql_version => 5.0 }),
431     'vch_65536 text',
432     'VARCHAR(65536) is substituted with TEXT for Mysql < 5.0.3'
433 );
434 is (
435     SQL::Translator::Producer::MySQL::create_field($varchars->{65536}),
436     'vch_65536 text',
437     'VARCHAR(65536) is substituted with TEXT when no version specified',
438 );
439
440
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
453   my $view_sql_replace = <<'EOV';
454 CREATE OR REPLACE
455    ALGORITHM = MERGE
456    DEFINER = CURRENT_USER
457    SQL SECURITY DEFINER
458   VIEW view_foo ( id, name ) AS
459     SELECT id, name FROM thing
460 EOV
461
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);
470   my $view_sql_noreplace = <<'EOV';
471 CREATE
472   VIEW view_foo ( id, name ) AS
473     SELECT id, name FROM thing
474 EOV
475
476   is($view1_sql2, $view_sql_noreplace, 'correct "CREATE VIEW" SQL');
477
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   }
500 }
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 }
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
565
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 });
577 is($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 });
579 is($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);
581 is($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};
591         my $number_field = SQL::Translator::Schema::Field->new(
592             name => "numberfield_$expected",
593             table => $table,
594             data_type => 'number',
595             size => $size,
596             is_nullable => 1,
597             is_foreign_key => 0,
598             is_unique => 0
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/) {
610         $varchars->{$size} = SQL::Translator::Schema::Field->new(
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 }),
622         '`vch_255` varchar(255)',
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 }),
627         '`vch_255` varchar(255)',
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),
632         '`vch_255` varchar(255)',
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 }),
639         '`vch_256` varchar(256)',
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 }),
644         '`vch_256` text',
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),
649         '`vch_256` text',
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 }),
656         '`vch_65535` varchar(65535)',
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 }),
661         '`vch_65535` text',
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),
666         '`vch_65535` text',
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 }),
673         '`vch_65536` text',
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 }),
678         '`vch_65536` text',
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),
683         '`vch_65536` text',
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';
700 CREATE 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`
706 EOV
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';
717 CREATE
718   VIEW `view_foo` ( `id`, `name` ) AS
719     SELECT `id`, `name` FROM `my`.`thing`
720 EOV
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 }
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 }