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

Add deferred FKs to SQLite #70

Open
wants to merge 3 commits 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
11 changes: 10 additions & 1 deletion lib/SQL/Translator/Parser/SQLite.pm
Expand Up @@ -425,7 +425,7 @@ table_constraint : PRIMARY_KEY parens_field_list conflict_clause(?)
}
}
|
FOREIGN_KEY parens_field_list REFERENCES ref_def cascade_def(?)
FOREIGN_KEY parens_field_list REFERENCES ref_def cascade_def(?) deferrable(?)
{
$return = {
supertype => 'constraint',
Expand All @@ -435,6 +435,7 @@ table_constraint : PRIMARY_KEY parens_field_list conflict_clause(?)
reference_fields => $item[4]{'reference_fields'},
on_delete => $item[5][0]{'on_delete'},
on_update => $item[5][0]{'on_update'},
deferrable => $item[6],
}
}

Expand All @@ -453,6 +454,14 @@ cascade_delete_def : /on\s+delete\s+(set null|set default|cascade|restrict|no ac
cascade_update_def : /on\s+update\s+(set null|set default|cascade|restrict|no action)/i
{ $return = $1}

not : /not/i

deferrable_initially : /initially (deferred|immediate)/i
deferrable : not(?) /deferrable/i deferrable_initially(?)
{
$return = $item[1] || !$item[3] || $item[3] ne 'initially deferred' ? 0 : 1
}

table_name : qualified_name

qualified_name : NAME
Expand Down
1 change: 1 addition & 0 deletions lib/SQL/Translator/Producer/SQLite.pm
Expand Up @@ -282,6 +282,7 @@ sub create_foreignkey {

$fk_sql .= " ON DELETE " . $c->{on_delete} if $c->{on_delete};
$fk_sql .= " ON UPDATE " . $c->{on_update} if $c->{on_update};
$fk_sql .= " DEFERRABLE INITIALLY DEFERRED" if $c->deferrable;

return $fk_sql;
}
Expand Down
4 changes: 2 additions & 2 deletions t/30sqlt-new-diff-sqlite.t
Expand Up @@ -99,7 +99,7 @@ CREATE TEMPORARY TABLE employee_temp_alter (
position varchar(50) NOT NULL,
employee_id int(11) NOT NULL,
PRIMARY KEY (position, employee_id),
FOREIGN KEY (employee_id) REFERENCES person(person_id)
FOREIGN KEY (employee_id) REFERENCES person(person_id) DEFERRABLE INITIALLY DEFERRED
);

INSERT INTO employee_temp_alter( position, employee_id) SELECT position, employee_id FROM employee;
Expand All @@ -110,7 +110,7 @@ CREATE TABLE employee (
position varchar(50) NOT NULL,
employee_id int(11) NOT NULL,
PRIMARY KEY (position, employee_id),
FOREIGN KEY (employee_id) REFERENCES person(person_id)
FOREIGN KEY (employee_id) REFERENCES person(person_id) DEFERRABLE INITIALLY DEFERRED
);

INSERT INTO employee SELECT position, employee_id FROM employee_temp_alter;
Expand Down
4 changes: 2 additions & 2 deletions t/48xml-to-sqlite.t
Expand Up @@ -50,7 +50,7 @@ CREATE TABLE "Basic" (
"emptytagdef" varchar DEFAULT '',
"another_id" int(10) DEFAULT 2,
"timest" timestamp,
FOREIGN KEY ("another_id") REFERENCES "Another"("id")
FOREIGN KEY ("another_id") REFERENCES "Another"("id") DEFERRABLE INITIALLY DEFERRED
);

CREATE INDEX "titleindex" ON "Basic" ("title");
Expand Down Expand Up @@ -108,7 +108,7 @@ eq_or_diff(\@sql,
"emptytagdef" varchar DEFAULT '',
"another_id" int(10) DEFAULT 2,
"timest" timestamp,
FOREIGN KEY ("another_id") REFERENCES "Another"("id")
FOREIGN KEY ("another_id") REFERENCES "Another"("id") DEFERRABLE INITIALLY DEFERRED
)>,
q<CREATE INDEX "titleindex" ON "Basic" ("title")>,
q<CREATE UNIQUE INDEX "emailuniqueindex" ON "Basic" ("email")>,
Expand Down
2 changes: 1 addition & 1 deletion t/56-sqlite-producer.t
Expand Up @@ -57,7 +57,7 @@ $SQL::Translator::Producer::SQLite::NO_QUOTES = 0;
on_delete => 'RESTRICT',
on_update => 'CASCADE',
);
my $expected = [ 'FOREIGN KEY ("foreign_key") REFERENCES "foo"("id") ON DELETE RESTRICT ON UPDATE CASCADE'];
my $expected = [ 'FOREIGN KEY ("foreign_key") REFERENCES "foo"("id") ON DELETE RESTRICT ON UPDATE CASCADE DEFERRABLE INITIALLY DEFERRED'];
my $result = [SQL::Translator::Producer::SQLite::create_foreignkey($constraint,$create_opts)];
is_deeply($result, $expected, 'correct "FOREIGN KEY"');
}
Expand Down