From: Chris Hilton Date: Tue, 27 Feb 2007 20:45:30 +0000 (+0000) Subject: Changed Constraint.equals() to ignore ordering of fields and reference fields X-Git-Tag: v0.11008~383 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=6a0f3000439a978666ecb692f0faaccff3a78c13;p=dbsrgits%2FSQL-Translator.git Changed Constraint.equals() to ignore ordering of fields and reference fields --- diff --git a/lib/SQL/Translator/Schema/Constraint.pm b/lib/SQL/Translator/Schema/Constraint.pm index a8133b3..e8bef98 100644 --- a/lib/SQL/Translator/Schema/Constraint.pm +++ b/lib/SQL/Translator/Schema/Constraint.pm @@ -1,7 +1,7 @@ package SQL::Translator::Schema::Constraint; # ---------------------------------------------------------------------- -# $Id: Constraint.pm,v 1.19 2005-08-10 16:42:47 duality72 Exp $ +# $Id: Constraint.pm,v 1.20 2007-02-27 20:45:30 duality72 Exp $ # ---------------------------------------------------------------------- # Copyright (C) 2002-4 SQLFairy Authors # @@ -51,7 +51,7 @@ use base 'SQL::Translator::Schema::Object'; use vars qw($VERSION $TABLE_COUNT $VIEW_COUNT); -$VERSION = sprintf "%d.%02d", q$Revision: 1.19 $ =~ /(\d+)\.(\d+)/; +$VERSION = sprintf "%d.%02d", q$Revision: 1.20 $ =~ /(\d+)\.(\d+)/; my %VALID_CONSTRAINT_TYPE = ( PRIMARY_KEY, 1, @@ -548,13 +548,36 @@ Determines if this constraint is the same as another return 0 unless $case_insensitive ? uc($self->table->name) eq uc($other->table->name) : $self->table->name eq $other->table->name; return 0 unless $self->expression eq $other->expression; - my $selfFields = join(":", $self->fields); - my $otherFields = join(":", $other->fields); - return 0 unless $case_insensitive ? uc($selfFields) eq uc($otherFields) : $selfFields eq $otherFields; + + # Check fields, regardless of order + my %otherFields = (); # create a hash of the other fields + foreach my $otherField ($other->fields) { + $otherField = uc($otherField) if $case_insensitive; + $otherFields{$otherField} = 1; + } + foreach my $selfField ($self->fields) { # check for self fields in hash + $selfField = uc($selfField) if $case_insensitive; + return 0 unless $otherFields{$selfField}; + delete $otherFields{$selfField}; + } + # Check all other fields were accounted for + return 0 unless keys %otherFields == 0; + + # Check reference fields, regardless of order + my %otherRefFields = (); # create a hash of the other reference fields + foreach my $otherRefField ($other->reference_fields) { + $otherRefField = uc($otherRefField) if $case_insensitive; + $otherRefFields{$otherRefField} = 1; + } + foreach my $selfRefField ($self->reference_fields) { # check for self reference fields in hash + $selfRefField = uc($selfRefField) if $case_insensitive; + return 0 unless $otherRefFields{$selfRefField}; + delete $otherRefFields{$selfRefField}; + } + # Check all other reference fields were accounted for + return 0 unless keys %otherRefFields == 0; + return 0 unless $case_insensitive ? uc($self->reference_table) eq uc($other->reference_table) : $self->reference_table eq $other->reference_table; - my $selfRefFields = join(":", $self->reference_fields); - my $otherRefFields = join(":", $other->reference_fields); - return 0 unless $case_insensitive ? uc($selfRefFields) eq uc($otherRefFields) : $selfRefFields eq $otherRefFields; return 0 unless $self->match_type eq $other->match_type; return 0 unless $self->on_delete eq $other->on_delete; return 0 unless $self->on_update eq $other->on_update; diff --git a/t/30sqlt-diff.t b/t/30sqlt-diff.t index a9fdea6..80ced29 100644 --- a/t/30sqlt-diff.t +++ b/t/30sqlt-diff.t @@ -25,7 +25,7 @@ my $create2 = (-d "t") : catfile($Bin, "t", @create2); BEGIN { - maybe_plan(19, + maybe_plan(20, 'SQL::Translator::Parser::SQLite', 'SQL::Translator::Parser::MySQL', 'SQL::Translator::Parser::Oracle', @@ -76,6 +76,7 @@ like($out, qr/ALTER TABLE employee DROP FOREIGN KEY/, "Detected drop foreign key"); like($out, qr/ALTER TABLE employee ADD CONSTRAINT/, "Detected add constraint"); +unlike($out, qr/ALTER TABLE employee ADD PRIMARY KEY/, "Primary key looks different when it shouldn't"); @cmd = ($sqlt_diff, "$mysql_create1=MySQL", "$mysql_create1=MySQL"); $out = `@cmd`; diff --git a/t/data/mysql/create.sql b/t/data/mysql/create.sql index 9802baa..3e7d603 100644 --- a/t/data/mysql/create.sql +++ b/t/data/mysql/create.sql @@ -12,6 +12,7 @@ create unique index u_name on person (name); create table employee ( position varchar(50), employee_id integer, - CONSTRAINT FK5302D47D93FE702E FOREIGN KEY (employee_id) REFERENCES person (person_id) + CONSTRAINT FK5302D47D93FE702E FOREIGN KEY (employee_id) REFERENCES person (person_id), + PRIMARY KEY (position, employee_id) ) ENGINE=InnoDB; diff --git a/t/data/mysql/create2.sql b/t/data/mysql/create2.sql index 57a02d9..441c2ef 100644 --- a/t/data/mysql/create2.sql +++ b/t/data/mysql/create2.sql @@ -14,5 +14,6 @@ create unique index u_name on person (name); create table employee ( position varchar(50), employee_id INTEGER, - CONSTRAINT FK5302D47D93FE702E_diff FOREIGN KEY (employee_id) REFERENCES person (person_id) + CONSTRAINT FK5302D47D93FE702E_diff FOREIGN KEY (employee_id) REFERENCES person (person_id), + PRIMARY KEY (employee_id, position) ) ENGINE=InnoDB;