# 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 {
use SQL::Translator::Schema::Constants;
use Storable 'dclone';
-plan tests => 6;
+plan tests => 7;
use_ok('SQL::Translator::Diff') or die "Cannot continue\n";
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':
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
+}