From: Devin Austin Date: Tue, 7 Jun 2011 18:07:30 +0000 (-0600) Subject: added kaitlyn's patch for mysql->sqlite translation X-Git-Tag: v0.11011~75 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=56785c0119455c87b2932a71a629113dcf5b8d70;p=dbsrgits%2FSQL-Translator.git added kaitlyn's patch for mysql->sqlite translation --- diff --git a/AUTHORS b/AUTHORS index 51bd194..7a64963 100644 --- a/AUTHORS +++ b/AUTHORS @@ -37,6 +37,7 @@ The following people have contributed to the SQLFairy project: - Sam Angiuoli - Stephen Bennett - Stephen Clouse +- SymKat - Vincent Bachelier - Wallace Reis - Ying Zhang diff --git a/Changes b/Changes index ea7fa94..0b96a40 100644 --- a/Changes +++ b/Changes @@ -4,6 +4,7 @@ * Fixed alter_drop_constraint for foreign keys and applying multiple changes via alter_field to a column in Postgres Producer * added a working mechanism for naming foreign keys in the PostgreSQL producer +* Fix possible name duplication in SQLlite producer # ---------------------------------------------------------- # 0.11010 2011-10-05 diff --git a/lib/SQL/Translator/Producer/SQLite.pm b/lib/SQL/Translator/Producer/SQLite.pm index 1838e5f..3344307 100644 --- a/lib/SQL/Translator/Producer/SQLite.pm +++ b/lib/SQL/Translator/Producer/SQLite.pm @@ -133,6 +133,8 @@ sub create_view { my $add_drop_view = $options->{add_drop_view}; my $view_name = $view->name; + $global_names{$view_name} = 1; + debug("PKG: Looking at view '${view_name}'\n"); # Header. Should this look like what mysqldump produces? @@ -165,6 +167,8 @@ sub create_table my ($table, $options) = @_; my $table_name = $table->name; + $global_names{$table_name} = 1; + my $no_comments = $options->{no_comments}; my $add_drop_table = $options->{add_drop_table}; my $sqlite_version = $options->{sqlite_version} || 0; @@ -376,6 +380,8 @@ sub create_trigger { my @statements; my $trigger_name = $trigger->name; + $global_names{$trigger_name} = 1; + my $events = $trigger->database_events; for my $evt ( @$events ) { diff --git a/t/mysql-sqlite-translate.t b/t/mysql-sqlite-translate.t new file mode 100644 index 0000000..55043a4 --- /dev/null +++ b/t/mysql-sqlite-translate.t @@ -0,0 +1,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 + + + +my $output = SQL::Translator + ->new( data => do { local $/; }) + ->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`) +); +