From: Ash Berlin Date: Mon, 21 Jan 2008 16:22:51 +0000 (+0000) Subject: Better tests (and fix bug) in batch alter with renamed tables + constraints X-Git-Tag: v0.11008~348^2 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=4104f82be9bfe6cc21f16b8f72d9c38eb6f52d69;p=dbsrgits%2FSQL-Translator.git Better tests (and fix bug) in batch alter with renamed tables + constraints --- diff --git a/lib/SQL/Translator/Producer/MySQL.pm b/lib/SQL/Translator/Producer/MySQL.pm index 3f7122d..4bf2b65 100644 --- a/lib/SQL/Translator/Producer/MySQL.pm +++ b/lib/SQL/Translator/Producer/MySQL.pm @@ -718,16 +718,20 @@ sub batch_alter_table { # Now strip off the 'ALTER TABLE xyz' of all but the first one my $qt = $options->{quote_table_name} || ''; - my $table_name = $qt . $renamed_from || $table->name . $qt; + my $table_name = $qt . $table->name . $qt; + + + my $re = $renamed_from + ? qr/^ALTER TABLE (?:\Q$table_name\E|\Q$qt$renamed_from$qt\E) / + : qr/^ALTER TABLE \Q$table_name\E /; my $first = shift @stmts; - my ($alter_table) = $first =~ /^(ALTER TABLE \Q$table_name\E )/; + my ($alter_table) = $first =~ /($re)/; - my $re = qr/^$alter_table/; - $re = qr/^ALTER TABLE \Q$qt@{[$table->name]}$qt\E / if $renamed_from; my $padd = " " x length($alter_table); return $drop_stmt . join( ",\n", $first, map { s/$re//; $padd . $_ } @stmts) . ';'; + } sub drop_table { diff --git a/t/30sqlt-new-diff-mysql.t b/t/30sqlt-new-diff-mysql.t index 867931a..fced6af 100644 --- a/t/30sqlt-new-diff-mysql.t +++ b/t/30sqlt-new-diff-mysql.t @@ -13,7 +13,7 @@ use Test::SQL::Translator qw(maybe_plan); use SQL::Translator::Schema::Constants; use Storable 'dclone'; -plan tests => 6; +plan tests => 7; use_ok('SQL::Translator::Diff') or die "Cannot continue\n"; @@ -214,7 +214,7 @@ COMMIT; data_type => 'int' ); - $out = SQL::Translator::Diff::schema_diff($s1, 'MySQL', $s2, 'MySQL' ); + my $out = SQL::Translator::Diff::schema_diff($s1, 'MySQL', $s2, 'MySQL' ); eq_or_diff($out, <<'## END OF DIFF', "Batch alter of constraints work for InnoDB"); -- Convert schema 'Schema 1' to 'Schema 2': @@ -229,3 +229,49 @@ ALTER TABLE employee ADD COLUMN new integer, COMMIT; ## END OF DIFF } + +{ + # Test other things about renaming tables to - namely that renames + # constraints are still formated right. + + my $s1 = SQL::Translator::Schema->new; + my $s2 = SQL::Translator::Schema->new; + + $s1->name('Schema 3'); + $s2->name('Schema 4'); + + my $t1 = $s1->add_table(dclone($target_schema->get_table('employee'))); + my $t2 = dclone($target_schema->get_table('employee')); + $t2->name('fnord'); + $t2->extra(renamed_from => 'employee'); + $s2->add_table($t2); + + + $t1->add_constraint( + name => 'bar_fk', + type => 'FOREIGN KEY', + fields => ['employee_id'], + reference_fields => ['id'], + reference_table => 'bar', + ); + $t2->add_constraint( + name => 'foo_fk', + type => 'FOREIGN KEY', + fields => ['employee_id'], + reference_fields => ['id'], + reference_table => 'foo', + ); + + my $out = SQL::Translator::Diff::schema_diff($s1, 'MySQL', $s2, 'MySQL' ); + eq_or_diff($out, <<'## END OF DIFF', "Alter/drop constraints works with rename table"); +-- Convert schema 'Schema 3' to 'Schema 4': + +BEGIN; + +ALTER TABLE employee RENAME TO fnord, + DROP FOREIGN KEY bar_fk, + ADD CONSTRAINT foo_fk FOREIGN KEY (employee_id) REFERENCES foo (id); + +COMMIT; +## END OF DIFF +}