use Test::More;
use Test::Differences;
use Test::SQL::Translator qw(maybe_plan);
+use SQL::Translator::Schema::Constants;
+use Storable 'dclone';
-plan tests => 5;
+plan tests => 6;
use_ok('SQL::Translator::Diff') or die "Cannot continue\n";
eq_or_diff($out, <<'## END OF DIFF', "Diff as expected");
-- Convert schema 'create1.yml' to 'create2.yml':
-BEGIN TRANSACTION;
+BEGIN;
SET foreign_key_checks=0;
eq_or_diff($out, <<'## END OF DIFF', "Diff as expected");
-- Convert schema 'create1.yml' to 'create2.yml':
-BEGIN TRANSACTION;
+BEGIN;
SET foreign_key_checks=0;
eq_or_diff($out, <<'## END OF DIFF', "No differences found");
-- Convert schema 'create.sql' to 'create2.yml':
-BEGIN TRANSACTION;
+BEGIN;
SET foreign_key_checks=0;
COMMIT;
## END OF DIFF
}
+
+# Test InnoDB stupidness. Have to drop constraints before re-adding them if
+# they are just alters.
+
+
+{
+ my $s1 = SQL::Translator::Schema->new;
+ my $s2 = SQL::Translator::Schema->new;
+
+ $s1->name('Schema 1');
+ $s2->name('Schema 2');
+
+ my $t1 = $s1->add_table($target_schema->get_table('employee'));
+ my $t2 = $s2->add_table(dclone($target_schema->get_table('employee')));
+
+
+ my ($c) = grep { $_->name eq 'FK5302D47D93FE702E_diff' } $t2->get_constraints;
+ $c->on_delete('CASCADE');
+
+ $t2->add_constraint(
+ name => 'new_constraint',
+ type => 'FOREIGN KEY',
+ fields => ['employee_id'],
+ reference_fields => ['fake'],
+ reference_table => 'patty',
+ );
+
+ $t2->add_field(
+ name => 'new',
+ data_type => 'int'
+ );
+
+ $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':
+
+BEGIN;
+
+ALTER TABLE employee DROP FOREIGN KEY FK5302D47D93FE702E_diff;
+ALTER TABLE employee ADD COLUMN new integer,
+ ADD CONSTRAINT FK5302D47D93FE702E_diff FOREIGN KEY (employee_id) REFERENCES person (person_id) ON DELETE CASCADE,
+ ADD CONSTRAINT new_constraint FOREIGN KEY (employee_id) REFERENCES patty (fake);
+
+COMMIT;
+## END OF DIFF
+}