Release commit for 1.62
[dbsrgits/SQL-Translator.git] / t / 64xml-to-mysql.t
CommitLineData
227d4a05 1#!/usr/bin/perl
2use strict;
3
4use FindBin qw/$Bin/;
5use Test::More;
6use Test::SQL::Translator;
7use Test::Exception;
8use Test::Differences;
9use Data::Dumper;
10use SQL::Translator;
11use SQL::Translator::Schema::Constants;
12
13
14BEGIN {
15 maybe_plan(2, 'SQL::Translator::Parser::XML::SQLFairy',
16 'SQL::Translator::Producer::MySQL');
17}
18
19my $xmlfile = "$Bin/data/xml/schema.xml";
20
21my $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
31die "Can't find test schema $xmlfile" unless -e $xmlfile;
32
33my @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',
281c5d17 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,
227d4a05 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,
281c5d17 58 `num` numeric(10, 2) NULL,
227d4a05 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
81my $sql = $sqlt->translate(
82 from => 'XML-SQLFairy',
83 to => 'MySQL',
84 filename => $xmlfile,
85) or die $sqlt->error;
86
87eq_or_diff($sql, join("", map { "$_;\n\n" } @want));
88
89my @sql = $sqlt->translate(
90 from => 'XML-SQLFairy',
91 to => 'MySQL',
92 filename => $xmlfile,
93) or die $sqlt->error;
94
95is_deeply(\@sql, \@want);