Tab/WS crusade
[dbsrgits/SQL-Translator.git] / t / mysql-sqlite-translate.t
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4 use Test::More;
5 use_ok( "SQL::Translator" );
6 use_ok( "SQL::Translator::Parser::MySQL" );
7 use_ok( "SQL::Translator::Producer::SQLite" );
8
9 # This test reproduces a bug in SQL::Translator::Producer::SQLite.
10 #
11 # When tables are created their names are not added to %global_names, and
12 # may be duplicated.
13 #
14 # SQL::Translator::Producer::SQLite version 1.59.
15 # compliments of SymKat <symkat@symkat.com>
16
17
18
19 my $output = SQL::Translator
20     ->new( data => do { local $/; <DATA> })
21     ->translate( from => 'MySQL', to => 'SQLite' );
22
23 sub find_table_names {
24     my ( $content ) = @_;
25     my @tables;
26
27     for my $line ( split /\n/, $content ) {
28         if ($content =~ /CREATE (?:INDEX|UNIQUE|TABLE| ){0,6} ([^\s]+)/gc) {
29             push @tables, $1;
30         }
31     }
32     return @tables;
33 }
34
35 sub has_dupes {
36     my ( @list ) = @_;
37     my %hist;
38
39     for my $elem ( @list ) {
40         return 0 if exists $hist{$elem};
41         $hist{$elem} = 1;
42     }
43     return 1;
44 }
45
46 ok ( has_dupes( find_table_names( $output ) ) );
47
48 done_testing;
49
50 __DATA__
51 CREATE TABLE `ip_address` (
52   `id` int(11) NOT NULL auto_increment,
53   `ip_address` varchar(255) NOT NULL,
54   `machine_id` int(11) default NULL,
55   `primary_machine_id` int(11) default NULL,
56   `secondary_machine_id` int(11) default NULL,
57   `tertiary_machine_id` int(11) default NULL,
58   `protocol` enum('ipv4','ipv6') NOT NULL default 'ipv4',
59   `shared` tinyint(1) NOT NULL default '1',
60   PRIMARY KEY  (`id`),
61   UNIQUE KEY `ip_address` (`ip_address`),
62   KEY `machine_id` (`machine_id`),
63   KEY `primary_machine_id` (`primary_machine_id`),
64   KEY `secondary_machine_id` (`secondary_machine_id`),
65   KEY `tertiary_machine_id` (`tertiary_machine_id`),
66   CONSTRAINT `ip_address_ibfk_1` FOREIGN KEY (`machine_id`) REFERENCES `machine` (`id`),
67   CONSTRAINT `ip_address_ibfk_2` FOREIGN KEY (`primary_machine_id`) REFERENCES `machine` (`id`),
68   CONSTRAINT `ip_address_ibfk_3` FOREIGN KEY (`secondary_machine_id`) REFERENCES `machine` (`id`),
69   CONSTRAINT `ip_address_ibfk_4` FOREIGN KEY (`tertiary_machine_id`) REFERENCES `machine` (`id`)
70 );
71