Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Turned off foreign key checking in MySQL. #135

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/SQL/Translator/Diff.pm
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,10 @@ sub produce_diff_sql {
}

if (@diffs) {
unshift @diffs, "BEGIN";
push @diffs, "\nCOMMIT";
my $begin_cmd = $producer_class->can('begin_commands') ? $producer_class->begin_commands() : "BEGIN";
my $commit_cmd = $producer_class->can('commit_commands') ? $producer_class->commit_commands() : "\nCOMMIT";
unshift @diffs, $begin_cmd;
push @diffs, $commit_cmd;
} else {
@diffs = ("-- No differences found");
}
Expand Down
14 changes: 10 additions & 4 deletions lib/SQL/Translator/Producer/MySQL.pm
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,6 @@ sub produce {
my $create = '';
$create .= header_comment unless ($no_comments);
# \todo Don't set if MySQL 3.x is set on command line
my @create = "SET foreign_key_checks=0";

preprocess_schema($schema);

Expand Down Expand Up @@ -330,9 +329,8 @@ sub produce {


# print "@table_defs\n";
push @table_defs, "SET foreign_key_checks=1";

return wantarray ? ($create ? $create : (), @create, @table_defs) : ($create . join('', map { $_ ? "$_;\n\n" : () } (@create, @table_defs)));
return wantarray ? ($create ? $create : (), @table_defs) : ($create . join('', map { $_ ? "$_;\n\n" : () } @table_defs));
}

sub create_trigger {
Expand Down Expand Up @@ -963,7 +961,7 @@ sub drop_table {
batch_alter_table(
$table, { alter_drop_constraint => [ grep { $_->type eq 'FOREIGN KEY' } $table->get_constraints ] }, $options
),
'DROP TABLE ' . _generator($options)->quote($table),
'DROP TABLE ' . _generator($options)->quote($table)
);
}

Expand Down Expand Up @@ -993,6 +991,14 @@ sub next_unused_name {
return $name;
}

sub begin_commands {
return "BEGIN;\n\nSET foreign_key_checks=0;\n\n";
}

sub commit_commands {
return "\nSET foreign_key_checks=1;\n\nCOMMIT";
}

1;

=pod
Expand Down
24 changes: 18 additions & 6 deletions t/30sqlt-new-diff-mysql.t
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ CREATE TABLE added (
id integer(11) NULL
);

SET foreign_key_checks=1;

ALTER TABLE old_name RENAME TO new_name;

ALTER TABLE employee DROP FOREIGN KEY FK5302D47D93FE702E;
Expand Down Expand Up @@ -99,6 +97,8 @@ ALTER TABLE deleted DROP FOREIGN KEY fk_fake;
DROP TABLE deleted;


SET foreign_key_checks=1;

COMMIT;

## END OF DIFF
Expand All @@ -120,8 +120,6 @@ CREATE TABLE added (
id integer(11) NULL
);

SET foreign_key_checks=1;

ALTER TABLE employee DROP COLUMN job_title;

ALTER TABLE old_name RENAME TO new_name,
Expand All @@ -143,6 +141,8 @@ ALTER TABLE deleted DROP FOREIGN KEY fk_fake;
DROP TABLE deleted;


SET foreign_key_checks=1;

COMMIT;

## END OF DIFF
Expand Down Expand Up @@ -190,8 +190,6 @@ CREATE TABLE added (
id integer(11) NULL
);

SET foreign_key_checks=1;

ALTER TABLE employee DROP FOREIGN KEY FK5302D47D93FE702E,
DROP CONSTRAINT demo_constraint,
DROP COLUMN job_title,
Expand All @@ -214,6 +212,8 @@ ALTER TABLE person DROP CONSTRAINT UC_age_name,
DROP TABLE deleted;


SET foreign_key_checks=1;

COMMIT;

## END OF DIFF
Expand Down Expand Up @@ -257,13 +257,17 @@ COMMIT;

BEGIN;

SET foreign_key_checks=0;

ALTER TABLE employee DROP FOREIGN KEY FK5302D47D93FE702E_diff;

ALTER TABLE employee ADD COLUMN new integer NULL,
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);


SET foreign_key_checks=1;

COMMIT;

## END OF DIFF
Expand Down Expand Up @@ -308,6 +312,8 @@ COMMIT;

BEGIN;

SET foreign_key_checks=0;

ALTER TABLE employee RENAME TO fnord,
DROP FOREIGN KEY bar_fk,
ADD CONSTRAINT foo_fk FOREIGN KEY (employee_id) REFERENCES foo (id);
Expand All @@ -317,6 +323,8 @@ ALTER TABLE deleted DROP FOREIGN KEY fk_fake;
DROP TABLE deleted;


SET foreign_key_checks=1;

COMMIT;

## END OF DIFF
Expand All @@ -330,6 +338,8 @@ COMMIT;

BEGIN;

SET foreign_key_checks=0;

ALTER TABLE `employee` RENAME TO `fnord`,
DROP FOREIGN KEY `bar_fk`,
ADD CONSTRAINT `foo_fk` FOREIGN KEY (`employee_id`) REFERENCES `foo` (`id`);
Expand All @@ -339,6 +349,8 @@ ALTER TABLE `deleted` DROP FOREIGN KEY `fk_fake`;
DROP TABLE `deleted`;


SET foreign_key_checks=1;

COMMIT;

## END OF DIFF
Expand Down
4 changes: 0 additions & 4 deletions t/38-mysql-producer.t
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,6 @@ schema:
EOSCHEMA

my @stmts = (
"SET foreign_key_checks=0",

"DROP TABLE IF EXISTS `thing`",
"CREATE TABLE `thing` (
`id` unsigned int NOT NULL auto_increment,
Expand Down Expand Up @@ -240,8 +238,6 @@ my @stmts = (
CONSTRAINT `fk_thing_3` FOREIGN KEY (`foo2`) REFERENCES `some`.`thing2` (`id`, `foo`)
) ENGINE=InnoDB",

"SET foreign_key_checks=1",

);

my @stmts_no_drop = grep {$_ !~ /^DROP TABLE/} @stmts;
Expand Down
4 changes: 0 additions & 4 deletions t/64xml-to-mysql.t
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ $sqlt = SQL::Translator->new(
die "Can't find test schema $xmlfile" unless -e $xmlfile;

my @want = (
q[SET foreign_key_checks=0],

q[DROP TABLE IF EXISTS `Basic`],
q[CREATE TABLE `Basic` (
`id` integer(10) zerofill NOT NULL auto_increment,
Expand Down Expand Up @@ -74,8 +72,6 @@ my @want = (
q[DROP TRIGGER IF EXISTS `bar_trigger_update`],
q[CREATE TRIGGER `bar_trigger_update` before update ON `Basic`
FOR EACH ROW BEGIN update modified2=timestamp(); END],

q[SET foreign_key_checks=1],
);

my $sql = $sqlt->translate(
Expand Down