From: Chris Hilton Date: Thu, 1 Mar 2007 22:16:00 +0000 (+0000) Subject: Added options to sqlt-diff to ignore index and/or constraint name differences X-Git-Tag: v0.11008~382 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=d990d84b92b2744c1546c964462230f6950dff77;p=dbsrgits%2FSQL-Translator.git Added options to sqlt-diff to ignore index and/or constraint name differences --- diff --git a/bin/sqlt-diff b/bin/sqlt-diff index 2466aae..3d2e2be 100755 --- a/bin/sqlt-diff +++ b/bin/sqlt-diff @@ -2,7 +2,7 @@ # vim: set ft=perl: # ------------------------------------------------------------------- -# $Id: sqlt-diff,v 1.13 2006-05-20 09:02:47 schiffbruechige Exp $ +# $Id: sqlt-diff,v 1.14 2007-03-01 22:16:00 duality72 Exp $ # ------------------------------------------------------------------- # Copyright (C) 2002-4 The SQLFairy Authors # @@ -44,6 +44,8 @@ Options: -d|--debug Show debugging info -t|--trace Turn on tracing for Parse::RecDescent -c|--case-insensitive Compare tables/columns case-insenstiviely + --ignore-index-names Ignore index name differences + --ignore-constraint-names Ignore constraint name differences =head1 DESCRIPTION @@ -100,9 +102,9 @@ use SQL::Translator::Diff; use SQL::Translator::Schema::Constants; use vars qw( $VERSION ); -$VERSION = sprintf "%d.%02d", q$Revision: 1.13 $ =~ /(\d+)\.(\d+)/; +$VERSION = sprintf "%d.%02d", q$Revision: 1.14 $ =~ /(\d+)\.(\d+)/; -my ( @input, $list, $help, $debug, $trace, $caseopt ); +my ( @input, $list, $help, $debug, $trace, $caseopt , $ignore_index_names, $ignore_constraint_names ); for my $arg ( @ARGV ) { if ( $arg =~ m/^-?-l(ist)?$/ ) { $list = 1; @@ -116,9 +118,15 @@ for my $arg ( @ARGV ) { elsif ( $arg =~ m/^-?-t(race)?$/ ) { $trace = 1; } - elsif ( $arg =~ m/^-?-c(ase-insenstive)?$/ ) { + elsif ( $arg =~ m/^-?-c(ase-insensitive)?$/ ) { $caseopt = 1; } + elsif ( $arg =~ m/^--ignore-index-names$/ ) { + $ignore_index_names = 1; + } + elsif ( $arg =~ m/^--ignore-constraint-names?$/ ) { + $ignore_constraint_names = 1; + } elsif ( $arg =~ m/^([^=]+)=(.+)$/ ) { push @input, { file => $1, parser => $2 }; } @@ -164,9 +172,11 @@ my ( $source_schema, $source_db, $target_schema, $target_db ) = map { my $result = SQL::Translator::Diff::schema_diff($source_schema, $source_db, $target_schema, $target_db, - { caseopt => $caseopt, - debug => $debug, - trace => $trace }); + { caseopt => $caseopt, + ignore_index_names => $ignore_index_names, + ignore_constraint_names => $ignore_constraint_names, + debug => $debug, + trace => $trace }); if($result) { print $result; diff --git a/lib/SQL/Translator/Diff.pm b/lib/SQL/Translator/Diff.pm index 3f421e8..8ac37c9 100644 --- a/lib/SQL/Translator/Diff.pm +++ b/lib/SQL/Translator/Diff.pm @@ -12,6 +12,8 @@ sub schema_diff my $caseopt = $options->{caseopt} || 0; my $debug = $options->{debug} || 0; my $trace = $options->{trace} || 0; + my $ignore_index_names = $options->{ignore_index_names} || 0; + my $ignore_constraint_names = $options->{ignore_constraint_names} || 0; my $case_insensitive = $source_db =~ /SQLServer/ || $caseopt; @@ -214,7 +216,7 @@ END INDEX: for my $i_tar ( $tar_table->get_indices ) { for my $i_src ( $src_table->get_indices ) { - if ( $i_tar->equals($i_src, $case_insensitive) ) { + if ( $i_tar->equals($i_src, $case_insensitive, $ignore_index_names) ) { $checked_indices{$i_src} = 1; next INDEX; } @@ -231,7 +233,7 @@ END for my $i_src ( $src_table->get_indices ) { next if $checked_indices{$i_src}; for my $i_tar ( $tar_table->get_indices ) { - next INDEX2 if $i_src->equals($i_tar, $case_insensitive); + next INDEX2 if $i_src->equals($i_tar, $case_insensitive, $ignore_index_names); } $source_db =~ /SQLServer/ ? push @diffs_index_drops, "DROP INDEX $tar_table_name.".$i_src->name.";" @@ -242,9 +244,9 @@ END CONSTRAINT: for my $c_tar ( $tar_table->get_constraints ) { next if $target_db =~ /Oracle/ && - $c_tar->type eq UNIQUE && $c_tar->name =~ /^SYS_/i; + $c_tar->type eq UNIQUE && $c_tar->name =~ /^SYS_/i; # Ignore Oracle SYS_ constraints hack for my $c_src ( $src_table->get_constraints ) { - if ( $c_tar->equals($c_src, $case_insensitive) ) { + if ( $c_tar->equals($c_src, $case_insensitive, $ignore_constraint_names) ) { $checked_constraints{$c_src} = 1; next CONSTRAINT; } @@ -255,10 +257,10 @@ END CONSTRAINT2: for my $c_src ( $src_table->get_constraints ) { next if $source_db =~ /Oracle/ && - $c_src->type eq UNIQUE && $c_src->name =~ /^SYS_/i; + $c_src->type eq UNIQUE && $c_src->name =~ /^SYS_/i; # Ignore Oracle SYS_ constraints hack next if $checked_constraints{$c_src}; for my $c_tar ( $tar_table->get_constraints ) { - next CONSTRAINT2 if $c_src->equals($c_tar, $case_insensitive); + next CONSTRAINT2 if $c_src->equals($c_tar, $case_insensitive, $ignore_constraint_names); } if ( $c_src->type eq UNIQUE ) { push @diffs_constraint_drops, "ALTER TABLE $tar_table_name DROP INDEX ". diff --git a/lib/SQL/Translator/Schema/Constraint.pm b/lib/SQL/Translator/Schema/Constraint.pm index e8bef98..002580b 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.20 2007-02-27 20:45:30 duality72 Exp $ +# $Id: Constraint.pm,v 1.21 2007-03-01 22:15:59 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.20 $ =~ /(\d+)\.(\d+)/; +$VERSION = sprintf "%d.%02d", q$Revision: 1.21 $ =~ /(\d+)\.(\d+)/; my %VALID_CONSTRAINT_TYPE = ( PRIMARY_KEY, 1, @@ -539,10 +539,13 @@ Determines if this constraint is the same as another my $self = shift; my $other = shift; my $case_insensitive = shift; + my $ignore_constraint_names = shift; return 0 unless $self->SUPER::equals($other); return 0 unless $self->type eq $other->type; - return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name; + unless ($ignore_constraint_names) { + return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name; + } return 0 unless $self->deferrable eq $other->deferrable; #return 0 unless $self->is_valid eq $other->is_valid; return 0 unless $case_insensitive ? uc($self->table->name) eq uc($other->table->name) diff --git a/lib/SQL/Translator/Schema/Index.pm b/lib/SQL/Translator/Schema/Index.pm index 05e67cb..db628e2 100644 --- a/lib/SQL/Translator/Schema/Index.pm +++ b/lib/SQL/Translator/Schema/Index.pm @@ -1,7 +1,7 @@ package SQL::Translator::Schema::Index; # ---------------------------------------------------------------------- -# $Id: Index.pm,v 1.14 2005-08-10 16:46:55 duality72 Exp $ +# $Id: Index.pm,v 1.15 2007-03-01 22:16:00 duality72 Exp $ # ---------------------------------------------------------------------- # Copyright (C) 2002-4 SQLFairy Authors # @@ -53,7 +53,7 @@ use base 'SQL::Translator::Schema::Object'; use vars qw($VERSION $TABLE_COUNT $VIEW_COUNT); -$VERSION = sprintf "%d.%02d", q$Revision: 1.14 $ =~ /(\d+)\.(\d+)/; +$VERSION = sprintf "%d.%02d", q$Revision: 1.15 $ =~ /(\d+)\.(\d+)/; my %VALID_INDEX_TYPE = ( UNIQUE, 1, @@ -249,9 +249,12 @@ Determines if this index is the same as another my $self = shift; my $other = shift; my $case_insensitive = shift; + my $ignore_index_names = shift; return 0 unless $self->SUPER::equals($other); -# return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name; + unless ($ignore_index_names) { + return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name; + } return 0 unless $self->is_valid eq $other->is_valid; return 0 unless $self->type eq $other->type; my $selfFields = join(":", $self->fields); diff --git a/t/30sqlt-diff.t b/t/30sqlt-diff.t index 80ced29..177b7d2 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(20, + maybe_plan(23, 'SQL::Translator::Parser::SQLite', 'SQL::Translator::Parser::MySQL', 'SQL::Translator::Parser::Oracle', @@ -58,6 +58,7 @@ my $mysql_create2 = (-d "t") ? catfile($Bin, @mysql_create2) : catfile($Bin, "t", @mysql_create2); +# Test for differences @cmd = ($sqlt_diff, "$mysql_create1=MySQL", "$mysql_create2=MySQL"); $out = `@cmd`; @@ -70,14 +71,27 @@ like($out, qr/ALTER TABLE person ADD is_rock_star/, "Detected missing rock star field"); like($out, qr/ALTER TABLE person ADD UNIQUE UC_person_id/, "Detected missing unique constraint"); +like($out, qr/CREATE UNIQUE INDEX unique_name/, + "Detected unique index with different name"); like($out, qr/ALTER TABLE person ENGINE=InnoDB;/, "Detected altered table option"); -like($out, qr/ALTER TABLE employee DROP FOREIGN KEY/, +like($out, qr/ALTER TABLE employee DROP FOREIGN KEY FK5302D47D93FE702E/, "Detected drop foreign key"); -like($out, qr/ALTER TABLE employee ADD CONSTRAINT/, +like($out, qr/ALTER TABLE employee ADD CONSTRAINT FK5302D47D93FE702E_diff/, "Detected add constraint"); unlike($out, qr/ALTER TABLE employee ADD PRIMARY KEY/, "Primary key looks different when it shouldn't"); - + +# Test ignore parameters +@cmd = ($sqlt_diff, "--ignore-index-names", "--ignore-constraint-names", + "$mysql_create1=MySQL", "$mysql_create2=MySQL"); +$out = `@cmd`; + +unlike($out, qr/CREATE UNIQUE INDEX unique_name/, + "Detected unique index with different name"); +unlike($out, qr/ALTER TABLE employee ADD CONSTRAINT FK5302D47D93FE702E_diff/, + "Detected add constraint"); + +# Test for sameness @cmd = ($sqlt_diff, "$mysql_create1=MySQL", "$mysql_create1=MySQL"); $out = `@cmd`; diff --git a/t/data/mysql/create2.sql b/t/data/mysql/create2.sql index 441c2ef..2f210aa 100644 --- a/t/data/mysql/create2.sql +++ b/t/data/mysql/create2.sql @@ -9,7 +9,7 @@ create table person ( UNIQUE KEY UC_person_id (person_id) ) ENGINE=InnoDB; -create unique index u_name on person (name); +create unique index unique_name on person (name); create table employee ( position varchar(50),