· 8 years ago · Jul 31, 2018, 06:54 PM
1#!/usr/bin/perl
2use warnings;
3use strict;
4use Test::More;
5use_ok( "SQL::Translator" );
6use_ok( "SQL::Translator::Parser::MySQL" );
7use_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
16
17
18my $output = SQL::Translator
19 ->new( data => do { local $/; <DATA> })
20 ->translate( from => 'MySQL', to => 'SQLite' );
21
22sub find_table_names {
23 my ( $content ) = @_;
24 my @tables;
25
26 for my $line ( split /\n/, $content ) {
27 if ($content =~ /CREATE (?:INDEX|UNIQUE|TABLE| ){0,6} ([^\s]+)/gc) {
28 push @tables, $1;
29 }
30 }
31 return @tables;
32}
33
34sub has_dupes {
35 my ( @list ) = @_;
36 my %hist;
37
38 for my $elem ( @list ) {
39 return 0 if exists $hist{$elem};
40 $hist{$elem} = 1;
41 }
42 return 1;
43}
44
45ok ( has_dupes( find_table_names( $output ) ) );
46
47done_testing;
48
49__DATA__
50CREATE TABLE `ip_address` (
51 `id` int(11) NOT NULL auto_increment,
52 `ip_address` varchar(255) NOT NULL,
53 `machine_id` int(11) default NULL,
54 `primary_machine_id` int(11) default NULL,
55 `secondary_machine_id` int(11) default NULL,
56 `tertiary_machine_id` int(11) default NULL,
57 `protocol` enum('ipv4','ipv6') NOT NULL default 'ipv4',
58 `shared` tinyint(1) NOT NULL default '1',
59 PRIMARY KEY (`id`),
60 UNIQUE KEY `ip_address` (`ip_address`),
61 KEY `machine_id` (`machine_id`),
62 KEY `primary_machine_id` (`primary_machine_id`),
63 KEY `secondary_machine_id` (`secondary_machine_id`),
64 KEY `tertiary_machine_id` (`tertiary_machine_id`),
65 CONSTRAINT `ip_address_ibfk_1` FOREIGN KEY (`machine_id`) REFERENCES `machine` (`id`),
66 CONSTRAINT `ip_address_ibfk_2` FOREIGN KEY (`primary_machine_id`) REFERENCES `machine` (`id`),
67 CONSTRAINT `ip_address_ibfk_3` FOREIGN KEY (`secondary_machine_id`) REFERENCES `machine` (`id`),
68 CONSTRAINT `ip_address_ibfk_4` FOREIGN KEY (`tertiary_machine_id`) REFERENCES `machine` (`id`)
69);