File: mysql-sqlite-translate.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 (71 lines) | stat: -rw-r--r-- 2,099 bytes parent folder | download | duplicates (6)
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
#!/usr/bin/perl
use warnings;
use strict;
use Test::More;
use_ok( "SQL::Translator" );
use_ok( "SQL::Translator::Parser::MySQL" );
use_ok( "SQL::Translator::Producer::SQLite" );

# This test reproduces a bug in SQL::Translator::Producer::SQLite.
#
# When tables are created their names are not added to %global_names, and
# may be duplicated.
#
# SQL::Translator::Producer::SQLite version 1.59.
# compliments of SymKat <symkat@symkat.com>



my $output = SQL::Translator
    ->new( data => do { local $/; <DATA> })
    ->translate( from => 'MySQL', to => 'SQLite' );

sub find_table_names {
    my ( $content ) = @_;
    my @tables;

    for my $line ( split /\n/, $content ) {
        if ($content =~ /CREATE (?:INDEX|UNIQUE|TABLE| ){0,6} ([^\s]+)/gc) {
            push @tables, $1;
        }
    }
    return @tables;
}

sub has_dupes {
    my ( @list ) = @_;
    my %hist;

    for my $elem ( @list ) {
        return 0 if exists $hist{$elem};
        $hist{$elem} = 1;
    }
    return 1;
}

ok ( has_dupes( find_table_names( $output ) ) );

done_testing;

__DATA__
CREATE TABLE `ip_address` (
  `id` int(11) NOT NULL auto_increment,
  `ip_address` varchar(255) NOT NULL,
  `machine_id` int(11) default NULL,
  `primary_machine_id` int(11) default NULL,
  `secondary_machine_id` int(11) default NULL,
  `tertiary_machine_id` int(11) default NULL,
  `protocol` enum('ipv4','ipv6') NOT NULL default 'ipv4',
  `shared` tinyint(1) NOT NULL default '1',
  PRIMARY KEY  (`id`),
  UNIQUE KEY `ip_address` (`ip_address`),
  KEY `machine_id` (`machine_id`),
  KEY `primary_machine_id` (`primary_machine_id`),
  KEY `secondary_machine_id` (`secondary_machine_id`),
  KEY `tertiary_machine_id` (`tertiary_machine_id`),
  CONSTRAINT `ip_address_ibfk_1` FOREIGN KEY (`machine_id`) REFERENCES `machine` (`id`),
  CONSTRAINT `ip_address_ibfk_2` FOREIGN KEY (`primary_machine_id`) REFERENCES `machine` (`id`),
  CONSTRAINT `ip_address_ibfk_3` FOREIGN KEY (`secondary_machine_id`) REFERENCES `machine` (`id`),
  CONSTRAINT `ip_address_ibfk_4` FOREIGN KEY (`tertiary_machine_id`) REFERENCES `machine` (`id`)
);