File: 38-mysql-producer.t

package info (click to toggle)
libsql-translator-perl 0.11024-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 4,572 kB
  • sloc: perl: 67,471; sql: 3,809; xml: 258; makefile: 2
file content (845 lines) | stat: -rw-r--r-- 29,560 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
#!/usr/bin/perl -w
# vim:filetype=perl

#
# Note that the bulk of the testing for the mysql producer is in
# 08postgres-to-mysql.t. This test is for additional stuff that can't be tested
# using an Oracle schema as source e.g. extra attributes.
#

use strict;
use Test::More;
use Test::Exception;
use Test::SQL::Translator qw(maybe_plan);

use Data::Dumper;
use FindBin qw/$Bin/;

# Testing 1,2,3,4...
#=============================================================================

BEGIN {
    maybe_plan(75,
        'YAML',
        'SQL::Translator::Producer::MySQL',
        'Test::Differences',
    )
}
use Test::Differences;
use SQL::Translator;

# Main test.
{
my $yaml_in = <<EOSCHEMA;
---
schema:
  tables:
    thing:
      name: thing
      extra:
        mysql_charset: latin1
        mysql_collate: latin1_danish_ci
      order: 1
      fields:
        id:
          name: id
          data_type: unsigned int
          is_primary_key: 1
          is_auto_increment: 1
          order: 1
        name:
          name: name
          data_type: varchar
          size:
            - 32
          order: 2
        swedish_name:
          name: swedish_name
          data_type: varchar
          size: 32
          extra:
            mysql_charset: swe7
          order: 3
        description:
          name: description
          data_type: text
          extra:
            mysql_charset: utf8
            mysql_collate: utf8_general_ci
          order: 4
        timestamp:
          data_type: timestamp
          default_value: !!perl/ref
            =: CURRENT_TIMESTAMP
          extra:
            on update: !!perl/ref
              =: CURRENT_TIMESTAMP
          is_nullable: 1
          is_primary_key: 0
          is_unique: 0
          name: timestamp
          order: 5
          size:
            - 0
      constraints:
        - type: UNIQUE
          fields:
            - name
          name: idx_unique_name

    thing2:
      name: some.thing2
      extra:
      order: 2
      fields:
        id:
          name: id
          data_type: int
          is_primary_key: 0
          order: 1
          is_foreign_key: 1
        foo:
          name: foo
          data_type: int
          order: 2
          is_not_null: 1
        foo2:
          name: foo2
          data_type: int
          order: 3
          is_not_null: 1
        bar_set:
          name: bar_set
          data_type: set
          order: 4
          is_not_null: 1
          extra:
            list:
              - foo
              - bar
              - ba'z
      indices:
        - type: NORMAL
          fields:
            - id
          name: index_1
        - type: NORMAL
          fields:
            - id
          name: really_long_name_bigger_than_64_chars_aaaaaaaaaaaaaaaaaaaaaaaaaaa
      constraints:
        - type: PRIMARY_KEY
          fields:
            - id
            - foo
        - reference_table: thing
          type: FOREIGN_KEY
          fields: foo
          name: fk_thing
        - reference_table: thing
          type: FOREIGN_KEY
          fields: foo2
          name: fk_thing

    thing3:
      name: some.thing3
      extra:
      order: 3
      fields:
        id:
          name: id
          data_type: int
          is_primary_key: 0
          order: 1
          is_foreign_key: 1
        foo:
          name: foo
          data_type: int
          order: 2
          is_not_null: 1
        foo2:
          name: foo2
          data_type: int
          order: 3
          is_not_null: 1
        bar_set:
          name: bar_set
          data_type: set
          order: 4
          is_not_null: 1
          extra:
            list:
              - foo
              - bar
              - ba'z
      indices:
        - type: NORMAL
          fields:
            - id
          name: index_1
        - type: NORMAL
          fields:
            - id
          name: really_long_name_bigger_than_64_chars_aaaaaaaaaaaaaaaaaaaaaaaaaaa
      constraints:
        - type: PRIMARY_KEY
          fields:
            - id
            - foo
        - reference_table: some.thing2
          type: FOREIGN_KEY
          fields: foo
          name: fk_thing
        - reference_table: some.thing2
          type: FOREIGN_KEY
          fields: foo2
          name: fk_thing
EOSCHEMA

my @stmts = (
"SET foreign_key_checks=0",

"DROP TABLE IF EXISTS `thing`",
"CREATE TABLE `thing` (
  `id` unsigned int NOT NULL auto_increment,
  `name` varchar(32) NULL,
  `swedish_name` varchar(32) character set swe7 NULL,
  `description` text character set utf8 collate utf8_general_ci NULL,
  `timestamp` timestamp on update CURRENT_TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE `idx_unique_name` (`name`)
) ENGINE=InnoDB DEFAULT CHARACTER SET latin1 COLLATE latin1_danish_ci",

"DROP TABLE IF EXISTS `some`.`thing2`",
"CREATE TABLE `some`.`thing2` (
  `id` integer NOT NULL,
  `foo` integer NOT NULL,
  `foo2` integer NULL,
  `bar_set` set('foo', 'bar', 'ba''z') NULL,
  INDEX `index_1` (`id`),
  INDEX `really_long_name_bigger_than_64_chars_aaaaaaaaaaaaaaaaa_aed44c47` (`id`),
  INDEX (`foo`),
  INDEX (`foo2`),
  PRIMARY KEY (`id`, `foo`),
  CONSTRAINT `fk_thing` FOREIGN KEY (`foo`) REFERENCES `thing` (`id`),
  CONSTRAINT `fk_thing_1` FOREIGN KEY (`foo2`) REFERENCES `thing` (`id`)
) ENGINE=InnoDB",

"DROP TABLE IF EXISTS `some`.`thing3`",
"CREATE TABLE `some`.`thing3` (
  `id` integer NOT NULL,
  `foo` integer NOT NULL,
  `foo2` integer NULL,
  `bar_set` set('foo', 'bar', 'ba''z') NULL,
  INDEX `index_1` (`id`),
  INDEX `really_long_name_bigger_than_64_chars_aaaaaaaaaaaaaaaaa_aed44c47` (`id`),
  INDEX (`foo`),
  INDEX (`foo2`),
  PRIMARY KEY (`id`, `foo`),
  CONSTRAINT `fk_thing_2` FOREIGN KEY (`foo`) REFERENCES `some`.`thing2` (`id`, `foo`),
  CONSTRAINT `fk_thing_3` FOREIGN KEY (`foo2`) REFERENCES `some`.`thing2` (`id`, `foo`)
) ENGINE=InnoDB",

"SET foreign_key_checks=1",

);

my @stmts_no_drop = grep {$_ !~ /^DROP TABLE/} @stmts;

my $mysql_out = join(";\n\n", @stmts_no_drop) . ";\n\n";


    my $sqlt;
    $sqlt = SQL::Translator->new(
        show_warnings  => 1,
        no_comments    => 1,
#        debug          => 1,
        from           => "YAML",
        to             => "MySQL",
        quote_table_names => 1,
        quote_field_names => 1
    );

    my $out = $sqlt->translate(\$yaml_in)
    or die "Translate error:".$sqlt->error;
    ok $out ne "",                    "Produced something!";
    eq_or_diff $out, $mysql_out,      "Scalar output looks right with quoting";

    my @out = $sqlt->translate(\$yaml_in)
      or die "Translat eerror:".$sqlt->error;
    is_deeply \@out, \@stmts_no_drop, "Array output looks right with quoting";

    $sqlt->quote_identifiers(0);

    $out = $sqlt->translate(\$yaml_in)
      or die "Translate error:".$sqlt->error;

    @out = $sqlt->translate(\$yaml_in)
      or die "Translate error:".$sqlt->error;
    $mysql_out =~ s/`//g;
    my @unquoted_stmts = map { s/`//g; $_} @stmts_no_drop;
    eq_or_diff $out, $mysql_out,       "Output looks right without quoting";
    is_deeply \@out, \@unquoted_stmts, "Array output looks right without quoting";

    $sqlt->quote_identifiers(1);
    $sqlt->add_drop_table(1);

    @out = $sqlt->translate(\$yaml_in)
      or die "Translat eerror:".$sqlt->error;
    $out = $sqlt->translate(\$yaml_in)
      or die "Translat eerror:".$sqlt->error;

    eq_or_diff $out, join(";\n\n", @stmts) . ";\n\n", "Output looks right with DROP TABLEs";
    is_deeply \@out, \@stmts,          "Array output looks right with DROP TABLEs";
}

###############################################################################
# New alter/add subs

{
my $table = SQL::Translator::Schema::Table->new( name => 'mytable');

my $field1 = SQL::Translator::Schema::Field->new( name => 'myfield',
                                                  table => $table,
                                                  data_type => 'VARCHAR',
                                                  size => 10,
                                                  default_value => undef,
                                                  is_auto_increment => 0,
                                                  is_nullable => 1,
                                                  is_foreign_key => 0,
                                                  is_unique => 0 );

my $field1_sql = SQL::Translator::Producer::MySQL::create_field($field1);

is($field1_sql, 'myfield VARCHAR(10) NULL', 'Create field works');

my $field2 = SQL::Translator::Schema::Field->new( name      => 'myfield',
                                                  table => $table,
                                                  data_type => 'VARCHAR',
                                                  size      => 25,
                                                  default_value => undef,
                                                  is_auto_increment => 0,
                                                  is_nullable => 0,
                                                  is_foreign_key => 0,
                                                  is_unique => 0 );

my $alter_field = SQL::Translator::Producer::MySQL::alter_field($field1,
                                                                $field2);
is($alter_field, 'ALTER TABLE mytable CHANGE COLUMN myfield myfield VARCHAR(25) NOT NULL', 'Alter field works');

my $add_field = SQL::Translator::Producer::MySQL::add_field($field1);

is($add_field, 'ALTER TABLE mytable ADD COLUMN myfield VARCHAR(10) NULL', 'Add field works');

my $drop_field = SQL::Translator::Producer::MySQL::drop_field($field2);
is($drop_field, 'ALTER TABLE mytable DROP COLUMN myfield', 'Drop field works');

my $field3 = SQL::Translator::Schema::Field->new( name      => 'myfield',
                                                  table => $table,
                                                  data_type => 'boolean',
                                                  is_nullable => 0,
                                                  is_foreign_key => 0,
                                                  is_unique => 0 );

my $field3_sql = SQL::Translator::Producer::MySQL::create_field($field3, { mysql_version => 4.1 });
is($field3_sql, 'myfield boolean NOT NULL', 'For Mysql >= 4, use boolean type');
$field3_sql = SQL::Translator::Producer::MySQL::create_field($field3, { mysql_version => 3.22 });
is($field3_sql, "myfield enum('0','1') NOT NULL", 'For Mysql < 4, use enum for boolean type');
$field3_sql = SQL::Translator::Producer::MySQL::create_field($field3,);
is($field3_sql, "myfield enum('0','1') NOT NULL", 'When no version specified, use enum for boolean type');

my $number_sizes = {
    '3, 2' => 'double',
    12 => 'bigint',
    1 => 'tinyint',
    4 => 'int',
};
for my $size (keys %$number_sizes) {
    my $expected = $number_sizes->{$size};
    my $number_field = SQL::Translator::Schema::Field->new(
        name => "numberfield_$expected",
        table => $table,
        data_type => 'number',
        size => $size,
        is_nullable => 1,
        is_foreign_key => 0,
        is_unique => 0
    );

    is(
        SQL::Translator::Producer::MySQL::create_field($number_field),
        "numberfield_$expected $expected($size) NULL",
        "Use $expected for NUMBER types of size $size"
    );
}

my $varchars;
for my $size (qw/255 256 65535 65536/) {
    $varchars->{$size} = SQL::Translator::Schema::Field->new(
        name => "vch_$size",
        table => $table,
        data_type => 'varchar',
        size => $size,
        is_nullable => 1,
    );
}


is (
    SQL::Translator::Producer::MySQL::create_field($varchars->{255}, { mysql_version => 5.000003 }),
    'vch_255 varchar(255) NULL',
    'VARCHAR(255) is not substituted with TEXT for Mysql >= 5.0.3'
);
is (
    SQL::Translator::Producer::MySQL::create_field($varchars->{255}, { mysql_version => 5.0 }),
    'vch_255 varchar(255) NULL',
    'VARCHAR(255) is not substituted with TEXT for Mysql < 5.0.3'
);
is (
    SQL::Translator::Producer::MySQL::create_field($varchars->{255}),
    'vch_255 varchar(255) NULL',
    'VARCHAR(255) is not substituted with TEXT when no version specified',
);


is (
    SQL::Translator::Producer::MySQL::create_field($varchars->{256}, { mysql_version => 5.000003 }),
    'vch_256 varchar(256) NULL',
    'VARCHAR(256) is not substituted with TEXT for Mysql >= 5.0.3'
);
is (
    SQL::Translator::Producer::MySQL::create_field($varchars->{256}, { mysql_version => 5.0 }),
    'vch_256 text NULL',
    'VARCHAR(256) is substituted with TEXT for Mysql < 5.0.3'
);
is (
    SQL::Translator::Producer::MySQL::create_field($varchars->{256}),
    'vch_256 text NULL',
    'VARCHAR(256) is substituted with TEXT when no version specified',
);


is (
    SQL::Translator::Producer::MySQL::create_field($varchars->{65535}, { mysql_version => 5.000003 }),
    'vch_65535 varchar(65535) NULL',
    'VARCHAR(65535) is not substituted with TEXT for Mysql >= 5.0.3'
);
is (
    SQL::Translator::Producer::MySQL::create_field($varchars->{65535}, { mysql_version => 5.0 }),
    'vch_65535 text NULL',
    'VARCHAR(65535) is substituted with TEXT for Mysql < 5.0.3'
);
is (
    SQL::Translator::Producer::MySQL::create_field($varchars->{65535}),
    'vch_65535 text NULL',
    'VARCHAR(65535) is substituted with TEXT when no version specified',
);


is (
    SQL::Translator::Producer::MySQL::create_field($varchars->{65536}, { mysql_version => 5.000003 }),
    'vch_65536 text NULL',
    'VARCHAR(65536) is substituted with TEXT for Mysql >= 5.0.3'
);
is (
    SQL::Translator::Producer::MySQL::create_field($varchars->{65536}, { mysql_version => 5.0 }),
    'vch_65536 text NULL',
    'VARCHAR(65536) is substituted with TEXT for Mysql < 5.0.3'
);
is (
    SQL::Translator::Producer::MySQL::create_field($varchars->{65536}),
    'vch_65536 text NULL',
    'VARCHAR(65536) is substituted with TEXT when no version specified',
);


{
  my $view1 = SQL::Translator::Schema::View->new( name => 'view_foo',
                                                  fields => [qw/id name/],
                                                  sql => 'SELECT id, name FROM thing',
                                                  extra => {
                                                    mysql_definer => 'CURRENT_USER',
                                                    mysql_algorithm => 'MERGE',
                                                    mysql_security => 'DEFINER',
                                                  });
  my $create_opts = { add_replace_view => 1, no_comments => 1 };
  my $view1_sql1 = SQL::Translator::Producer::MySQL::create_view($view1, $create_opts);

  my $view_sql_replace = <<'EOV';
CREATE OR REPLACE
   ALGORITHM = MERGE
   DEFINER = CURRENT_USER
   SQL SECURITY DEFINER
  VIEW view_foo ( id, name ) AS
    SELECT id, name FROM thing
EOV

  is($view1_sql1, $view_sql_replace, 'correct "CREATE OR REPLACE VIEW" SQL');


  my $view2 = SQL::Translator::Schema::View->new( name => 'view_foo',
                                                  fields => [qw/id name/],
                                                  sql => 'SELECT id, name FROM thing',);
  my $create2_opts = { add_replace_view => 0, no_comments => 1 };
  my $view1_sql2 = SQL::Translator::Producer::MySQL::create_view($view2, $create2_opts);
  my $view_sql_noreplace = <<'EOV';
CREATE
  VIEW view_foo ( id, name ) AS
    SELECT id, name FROM thing
EOV

  is($view1_sql2, $view_sql_noreplace, 'correct "CREATE VIEW" SQL');

  {
    my %extra = $view1->extra;
    is_deeply \%extra,
      {
        'mysql_algorithm' => 'MERGE',
        'mysql_definer'   => 'CURRENT_USER',
        'mysql_security'  => 'DEFINER'
      },
      'Extra attributes';
  }

  $view1->remove_extra(qw/mysql_definer mysql_security/);
  {
    my %extra = $view1->extra;
    is_deeply \%extra, { 'mysql_algorithm' => 'MERGE', }, 'Extra attributes after first reset_extra call';
  }

  $view1->remove_extra();
  {
    my %extra = $view1->extra;
    is_deeply \%extra, {}, 'Extra attributes completely removed';
  }
}

{

    # certain types do not support a size, see also:
    # http://dev.mysql.com/doc/refman/5.1/de/create-table.html
    for my $type (qw/date time timestamp datetime year/) {
        my $field = SQL::Translator::Schema::Field->new(
            name              => "my$type",
            table             => $table,
            data_type         => $type,
            size              => 10,
            default_value     => undef,
            is_auto_increment => 0,
            is_nullable       => 1,
            is_foreign_key    => 0,
            is_unique         => 0
        );
        my $sql = SQL::Translator::Producer::MySQL::create_field($field);
        is($sql, "my$type $type NULL", "Skip length param for type $type");
    }
}

} #non quoted test

{
    #Quoted test
    my $table = SQL::Translator::Schema::Table->new( name => 'mydb.mytable');

    my $field1 = SQL::Translator::Schema::Field->new( name => 'myfield',
                                                  table => $table,
                                                  data_type => 'VARCHAR',
                                                  size => 10,
                                                  default_value => undef,
                                                  is_auto_increment => 0,
                                                  is_nullable => 1,
                                                  is_foreign_key => 0,
                                                  is_unique => 0 );


    my $field2 = SQL::Translator::Schema::Field->new( name      => 'myfield',
                                                  table => $table,
                                                  data_type => 'VARCHAR',
                                                  size      => 25,
                                                  default_value => undef,
                                                  is_auto_increment => 0,
                                                  is_nullable => 0,
                                                  is_foreign_key => 0,
                                                  is_unique => 0 );

    my $field3 = SQL::Translator::Schema::Field->new( name      => 'myfield',
                                                  table => $table,
                                                  data_type => 'boolean',
                                                  is_nullable => 0,
                                                  is_foreign_key => 0,
                                                  is_unique => 0 );


    my $qt = '`';
    my $qf = '`';
    my $options = {
        quote_table_names => $qt,
        quote_field_names => $qf,
    };


    my $alter_field = SQL::Translator::Producer::MySQL::alter_field($field1, $field2, $options);
    is($alter_field, 'ALTER TABLE `mydb`.`mytable` CHANGE COLUMN `myfield` `myfield` VARCHAR(25) NOT NULL', 'Alter field works');

    my $add_field = SQL::Translator::Producer::MySQL::add_field($field1, $options);

    is($add_field, 'ALTER TABLE `mydb`.`mytable` ADD COLUMN `myfield` VARCHAR(10) NULL', 'Add field works');

    my $drop_field = SQL::Translator::Producer::MySQL::drop_field($field2, $options);
    is($drop_field, 'ALTER TABLE `mydb`.`mytable` DROP COLUMN `myfield`', 'Drop field works');

    my $field3_sql = SQL::Translator::Producer::MySQL::create_field($field3, { mysql_version => 4.1, %$options });
is($field3_sql, '`myfield` boolean NOT NULL', 'For Mysql >= 4, use boolean type');
$field3_sql = SQL::Translator::Producer::MySQL::create_field($field3, { mysql_version => 3.22, %$options });
is($field3_sql, "`myfield` enum('0','1') NOT NULL", 'For Mysql < 4, use enum for boolean type');
$field3_sql = SQL::Translator::Producer::MySQL::create_field($field3,$options);
is($field3_sql, "`myfield` enum('0','1') NOT NULL", 'When no version specified, use enum for boolean type');

    my $number_sizes = {
        '3, 2' => 'double',
        12 => 'bigint',
        1 => 'tinyint',
        4 => 'int',
    };
    for my $size (keys %$number_sizes) {
        my $expected = $number_sizes->{$size};
        my $number_field = SQL::Translator::Schema::Field->new(
            name => "numberfield_$expected",
            table => $table,
            data_type => 'number',
            size => $size,
            is_nullable => 1,
            is_foreign_key => 0,
            is_unique => 0
        );

        is(
            SQL::Translator::Producer::MySQL::create_field($number_field, $options),
            "`numberfield_$expected` $expected($size) NULL",
            "Use $expected for NUMBER types of size $size"
        );
    }

    my $varchars;
    for my $size (qw/255 256 65535 65536/) {
        $varchars->{$size} = SQL::Translator::Schema::Field->new(
            name => "vch_$size",
            table => $table,
            data_type => 'varchar',
            size => $size,
            is_nullable => 1,
        );
    }


    is (
        SQL::Translator::Producer::MySQL::create_field($varchars->{255}, { mysql_version => 5.000003, %$options }),
        '`vch_255` varchar(255) NULL',
        'VARCHAR(255) is not substituted with TEXT for Mysql >= 5.0.3'
    );
    is (
        SQL::Translator::Producer::MySQL::create_field($varchars->{255}, { mysql_version => 5.0, %$options }),
        '`vch_255` varchar(255) NULL',
        'VARCHAR(255) is not substituted with TEXT for Mysql < 5.0.3'
    );
    is (
        SQL::Translator::Producer::MySQL::create_field($varchars->{255}, $options),
        '`vch_255` varchar(255) NULL',
        'VARCHAR(255) is not substituted with TEXT when no version specified',
    );


    is (
        SQL::Translator::Producer::MySQL::create_field($varchars->{256}, { mysql_version => 5.000003, %$options }),
        '`vch_256` varchar(256) NULL',
        'VARCHAR(256) is not substituted with TEXT for Mysql >= 5.0.3'
    );
    is (
        SQL::Translator::Producer::MySQL::create_field($varchars->{256}, { mysql_version => 5.0, %$options }),
        '`vch_256` text NULL',
        'VARCHAR(256) is substituted with TEXT for Mysql < 5.0.3'
    );
    is (
        SQL::Translator::Producer::MySQL::create_field($varchars->{256}, $options),
        '`vch_256` text NULL',
        'VARCHAR(256) is substituted with TEXT when no version specified',
    );


    is (
        SQL::Translator::Producer::MySQL::create_field($varchars->{65535}, { mysql_version => 5.000003, %$options }),
        '`vch_65535` varchar(65535) NULL',
        'VARCHAR(65535) is not substituted with TEXT for Mysql >= 5.0.3'
    );
    is (
        SQL::Translator::Producer::MySQL::create_field($varchars->{65535}, { mysql_version => 5.0, %$options }),
        '`vch_65535` text NULL',
        'VARCHAR(65535) is substituted with TEXT for Mysql < 5.0.3'
    );
    is (
        SQL::Translator::Producer::MySQL::create_field($varchars->{65535}, $options),
        '`vch_65535` text NULL',
        'VARCHAR(65535) is substituted with TEXT when no version specified',
    );


    is (
        SQL::Translator::Producer::MySQL::create_field($varchars->{65536}, { mysql_version => 5.000003, %$options }),
        '`vch_65536` text NULL',
        'VARCHAR(65536) is substituted with TEXT for Mysql >= 5.0.3'
    );
    is (
        SQL::Translator::Producer::MySQL::create_field($varchars->{65536}, { mysql_version => 5.0, %$options }),
        '`vch_65536` text NULL',
        'VARCHAR(65536) is substituted with TEXT for Mysql < 5.0.3'
    );
    is (
        SQL::Translator::Producer::MySQL::create_field($varchars->{65536}, $options),
        '`vch_65536` text NULL',
        'VARCHAR(65536) is substituted with TEXT when no version specified',
    );

    {
      my $view1 = SQL::Translator::Schema::View->new( name => 'view_foo',
                                                      fields => [qw/id name/],
                                                      sql => 'SELECT `id`, `name` FROM `my`.`thing`',
                                                      extra => {
                                                        mysql_definer => 'CURRENT_USER',
                                                        mysql_algorithm => 'MERGE',
                                                        mysql_security => 'DEFINER',
                                                      });
      my $create_opts = { add_replace_view => 1, no_comments => 1, %$options };
      my $view1_sql1 = SQL::Translator::Producer::MySQL::create_view($view1, $create_opts);

      my $view_sql_replace = <<'EOV';
CREATE OR REPLACE
   ALGORITHM = MERGE
   DEFINER = CURRENT_USER
   SQL SECURITY DEFINER
  VIEW `view_foo` ( `id`, `name` ) AS
    SELECT `id`, `name` FROM `my`.`thing`
EOV

      is($view1_sql1, $view_sql_replace, 'correct "CREATE OR REPLACE VIEW" SQL');


      my $view2 = SQL::Translator::Schema::View->new( name => 'view_foo',
                                                      fields => [qw/id name/],
                                                      sql => 'SELECT `id`, `name` FROM `my`.`thing`',);
      my $create2_opts = { add_replace_view => 0, no_comments => 1, %$options };
      my $view1_sql2 = SQL::Translator::Producer::MySQL::create_view($view2, $create2_opts);
      my $view_sql_noreplace = <<'EOV';
CREATE
  VIEW `view_foo` ( `id`, `name` ) AS
    SELECT `id`, `name` FROM `my`.`thing`
EOV

      is($view1_sql2, $view_sql_noreplace, 'correct "CREATE VIEW" SQL');

      {
        my %extra = $view1->extra;
        is_deeply \%extra,
          {
            'mysql_algorithm' => 'MERGE',
            'mysql_definer'   => 'CURRENT_USER',
            'mysql_security'  => 'DEFINER'
          },
          'Extra attributes';
      }

      $view1->remove_extra(qw/mysql_definer mysql_security/);
      {
        my %extra = $view1->extra;
        is_deeply \%extra, { 'mysql_algorithm' => 'MERGE', }, 'Extra attributes after first reset_extra call';
      }

      $view1->remove_extra();
      {
        my %extra = $view1->extra;
        is_deeply \%extra, {}, 'Extra attributes completely removed';
      }
    }

    {

        # certain types do not support a size, see also:
        # http://dev.mysql.com/doc/refman/5.1/de/create-table.html
        for my $type (qw/date time timestamp datetime year/) {
            my $field = SQL::Translator::Schema::Field->new(
                name              => "my$type",
                table             => $table,
                data_type         => $type,
                size              => 10,
                default_value     => undef,
                is_auto_increment => 0,
                is_nullable       => 1,
                is_foreign_key    => 0,
                is_unique         => 0
            );
            my $sql = SQL::Translator::Producer::MySQL::create_field($field, $options);
            is($sql, "`my$type` $type NULL", "Skip length param for type $type");
        }
    }
}

{ # test for rt62250
    my $table = SQL::Translator::Schema::Table->new(name => 'table');
    $table->add_field(
        SQL::Translator::Schema::Field->new( name => 'mypk',
                                             table => $table,
                                             data_type => 'INT',
                                             size => 10,
                                             default_value => undef,
                                             is_auto_increment => 1,
                                             is_nullable => 0,
                                             is_foreign_key => 0,
                                             is_unique => 1 ));

    my $constraint = $table->add_constraint(fields => ['mypk'], type => 'PRIMARY_KEY');
    my $options = {quote_table_names => '`'};
    is(SQL::Translator::Producer::MySQL::alter_drop_constraint($constraint,$options),
       'ALTER TABLE `table` DROP PRIMARY KEY','valid drop primary key');
}

{
    my $schema = SQL::Translator::Schema->new();
    my $table = $schema->add_table( name => 'foo', fields => ['bar'] );

    {
        my $trigger = $schema->add_trigger(
            name                => 'mytrigger',
            perform_action_when => 'before',
            database_events     => 'update',
            on_table            => 'foo',
            fields              => ['bar'],
            action              => 'BEGIN baz(); END'
        );
        my ($def) = SQL::Translator::Producer::MySQL::create_trigger($trigger);
        my $expected
          = "--\n"
          . "-- Trigger mytrigger\n"
          . "--\n"
          . "CREATE TRIGGER mytrigger before update ON foo\n"
          . "  FOR EACH ROW BEGIN baz(); END";
        is($def, $expected, 'trigger created');
    }

    {
        my $trigger = $schema->add_trigger(
            name                => 'mytrigger2',
            perform_action_when => 'after',
            database_events     => ['insert'],
            on_table            => 'foo',
            fields              => ['bar'],
            action              => 'baz()'
        );
        my ($def) = SQL::Translator::Producer::MySQL::create_trigger($trigger);
        my $expected
          = "--\n"
          . "-- Trigger mytrigger2\n"
          . "--\n"
          . "CREATE TRIGGER mytrigger2 after insert ON foo\n"
          . "  FOR EACH ROW BEGIN baz(); END";
        is($def, $expected, 'trigger created');
    }
}