File: 64xml-to-mysql.t

package info (click to toggle)
libsql-translator-perl 0.11011-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 15,380 kB
  • sloc: perl: 251,748; sql: 3,805; xml: 233; makefile: 7
file content (95 lines) | stat: -rw-r--r-- 2,603 bytes parent folder | download
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
#!/usr/bin/perl
use strict;

use FindBin qw/$Bin/;
use Test::More;
use Test::SQL::Translator;
use Test::Exception;
use Test::Differences;
use Data::Dumper;
use SQL::Translator;
use SQL::Translator::Schema::Constants;


BEGIN {
    maybe_plan(2, 'SQL::Translator::Parser::XML::SQLFairy',
              'SQL::Translator::Producer::MySQL');
}

my $xmlfile = "$Bin/data/xml/schema.xml";

my $sqlt;
$sqlt = SQL::Translator->new(
    no_comments => 1,
    show_warnings  => 0,
    add_drop_table => 1,
    producer_args => {
        mysql_version => 5.005,
    },
);

die "Can't find test schema $xmlfile" unless -e $xmlfile;

my @want = (
    q[SET foreign_key_checks=0],

    q[DROP TABLE IF EXISTS `Basic`],
    q[CREATE TABLE `Basic` (
  `id` integer(10) zerofill NOT NULL auto_increment,
  `title` varchar(100) NOT NULL DEFAULT 'hello',
  `description` text DEFAULT '',
  `email` varchar(500),
  `explicitnulldef` varchar(255),
  `explicitemptystring` varchar(255) DEFAULT '',
  `emptytagdef` varchar(255) DEFAULT '' comment 'Hello emptytagdef',
  `another_id` integer(10) DEFAULT 2,
  `timest` timestamp,
  INDEX `titleindex` (`title`),
  INDEX (`another_id`),
  PRIMARY KEY (`id`),
  UNIQUE `emailuniqueindex` (`email`),
  UNIQUE `very_long_index_name_on_title_field_which_should_be_tru_14b59999` (`title`),
  CONSTRAINT `Basic_fk` FOREIGN KEY (`another_id`) REFERENCES `Another` (`id`)
) ENGINE=InnoDB],

    q[DROP TABLE IF EXISTS `Another`],
    q[CREATE TABLE `Another` (
  `id` integer(10) NOT NULL auto_increment,
  `num` numeric(10, 2),
  PRIMARY KEY (`id`)
) ENGINE=InnoDB],
    q[CREATE OR REPLACE
  VIEW `email_list` ( `email` ) AS
    SELECT email FROM Basic WHERE (email IS NOT NULL)
],

    q[DROP TRIGGER IF EXISTS `foo_trigger`],
    q[CREATE TRIGGER `foo_trigger` after insert ON `Basic`
  FOR EACH ROW BEGIN update modified=timestamp(); END],

    q[DROP TRIGGER IF EXISTS `bar_trigger_insert`],
    q[CREATE TRIGGER `bar_trigger_insert` before insert ON `Basic`
  FOR EACH ROW BEGIN update modified2=timestamp(); END],

    q[DROP TRIGGER IF EXISTS `bar_trigger_update`],
    q[CREATE TRIGGER `bar_trigger_update` before update ON `Basic`
  FOR EACH ROW BEGIN update modified2=timestamp(); END],

    q[SET foreign_key_checks=1],
);

my $sql = $sqlt->translate(
    from     => 'XML-SQLFairy',
    to       => 'MySQL',
    filename => $xmlfile,
) or die $sqlt->error;

eq_or_diff($sql, join("", map { "$_;\n\n" } @want));

my @sql = $sqlt->translate(
    from     => 'XML-SQLFairy',
    to       => 'MySQL',
    filename => $xmlfile,
) or die $sqlt->error;

is_deeply(\@sql, \@want);