Release commit for 1.62
[dbsrgits/SQL-Translator.git] / t / 64xml-to-mysql.t
1 #!/usr/bin/perl
2 use strict;
3
4 use FindBin qw/$Bin/;
5 use Test::More;
6 use Test::SQL::Translator;
7 use Test::Exception;
8 use Test::Differences;
9 use Data::Dumper;
10 use SQL::Translator;
11 use SQL::Translator::Schema::Constants;
12
13
14 BEGIN {
15     maybe_plan(2, 'SQL::Translator::Parser::XML::SQLFairy',
16               'SQL::Translator::Producer::MySQL');
17 }
18
19 my $xmlfile = "$Bin/data/xml/schema.xml";
20
21 my $sqlt;
22 $sqlt = SQL::Translator->new(
23     no_comments => 1,
24     show_warnings  => 0,
25     add_drop_table => 1,
26     producer_args => {
27         mysql_version => 5.005,
28     },
29 );
30
31 die "Can't find test schema $xmlfile" unless -e $xmlfile;
32
33 my @want = (
34     q[SET foreign_key_checks=0],
35
36     q[DROP TABLE IF EXISTS `Basic`],
37     q[CREATE TABLE `Basic` (
38   `id` integer(10) zerofill NOT NULL auto_increment,
39   `title` varchar(100) NOT NULL DEFAULT 'hello',
40   `description` text NULL DEFAULT '',
41   `email` varchar(500) NULL,
42   `explicitnulldef` varchar(255) NULL,
43   `explicitemptystring` varchar(255) NULL DEFAULT '',
44   `emptytagdef` varchar(255) NULL DEFAULT '' comment 'Hello emptytagdef',
45   `another_id` integer(10) NULL DEFAULT 2,
46   `timest` timestamp NULL,
47   INDEX `titleindex` (`title`),
48   INDEX (`another_id`),
49   PRIMARY KEY (`id`),
50   UNIQUE `emailuniqueindex` (`email`),
51   UNIQUE `very_long_index_name_on_title_field_which_should_be_tru_14b59999` (`title`),
52   CONSTRAINT `Basic_fk` FOREIGN KEY (`another_id`) REFERENCES `Another` (`id`)
53 ) ENGINE=InnoDB],
54
55     q[DROP TABLE IF EXISTS `Another`],
56     q[CREATE TABLE `Another` (
57   `id` integer(10) NOT NULL auto_increment,
58   `num` numeric(10, 2) NULL,
59   PRIMARY KEY (`id`)
60 ) ENGINE=InnoDB],
61     q[CREATE OR REPLACE
62   VIEW `email_list` ( `email` ) AS
63     SELECT email FROM Basic WHERE (email IS NOT NULL)
64 ],
65
66     q[DROP TRIGGER IF EXISTS `foo_trigger`],
67     q[CREATE TRIGGER `foo_trigger` after insert ON `Basic`
68   FOR EACH ROW BEGIN update modified=timestamp(); END],
69
70     q[DROP TRIGGER IF EXISTS `bar_trigger_insert`],
71     q[CREATE TRIGGER `bar_trigger_insert` before insert ON `Basic`
72   FOR EACH ROW BEGIN update modified2=timestamp(); END],
73
74     q[DROP TRIGGER IF EXISTS `bar_trigger_update`],
75     q[CREATE TRIGGER `bar_trigger_update` before update ON `Basic`
76   FOR EACH ROW BEGIN update modified2=timestamp(); END],
77
78     q[SET foreign_key_checks=1],
79 );
80
81 my $sql = $sqlt->translate(
82     from     => 'XML-SQLFairy',
83     to       => 'MySQL',
84     filename => $xmlfile,
85 ) or die $sqlt->error;
86
87 eq_or_diff($sql, join("", map { "$_;\n\n" } @want));
88
89 my @sql = $sqlt->translate(
90     from     => 'XML-SQLFairy',
91     to       => 'MySQL',
92     filename => $xmlfile,
93 ) or die $sqlt->error;
94
95 is_deeply(\@sql, \@want);