From: Fabrice Gabolde Date: Wed, 3 Oct 2012 15:17:06 +0000 (+0200) Subject: Support for SET NULL, SET DEFAULT and NO ACTION in foreign key clauses for SQLite. X-Git-Tag: v0.011017~24 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=565563b9799775b9992c3f7cb85753b1d2b31c39;p=dbsrgits%2FSQL-Translator.git Support for SET NULL, SET DEFAULT and NO ACTION in foreign key clauses for SQLite. According to the documentation this should be all of them (with the existing support of CASCADE and RESTRICT). --- diff --git a/AUTHORS b/AUTHORS index 9f55f2a..9e8db4c 100644 --- a/AUTHORS +++ b/AUTHORS @@ -21,6 +21,7 @@ The following people have contributed to the SQLFairy project: - Darren Chamberlain - Dave Cash - Fabien Wernli +- Fabrice Gabolde - Geoff Cant - Gudmundur A. Thorisson - Guillermo Roditi diff --git a/Changes b/Changes index b9d3707..840828a 100644 --- a/Changes +++ b/Changes @@ -4,6 +4,7 @@ * Fix diff for altering two things per column - add ; at the end * Call all diff methods in list context (it can be merged later) * Fix Pg diff issue with drop constraint on primary keys +* SQLite support for SET NULL, SET DEFAULT and NO ACTION in FK clauses # ---------------------------------------------------------- # 0.11016 2012-10-09 diff --git a/lib/SQL/Translator/Parser/SQLite.pm b/lib/SQL/Translator/Parser/SQLite.pm index 259c0cf..459f203 100644 --- a/lib/SQL/Translator/Parser/SQLite.pm +++ b/lib/SQL/Translator/Parser/SQLite.pm @@ -447,10 +447,10 @@ cascade_def : cascade_update_def cascade_delete_def(?) cascade_delete_def cascade_update_def(?) { $return = { on_delete => $item[1], on_update => $item[2][0] } } -cascade_delete_def : /on\s+delete\s+(\w+)/i +cascade_delete_def : /on\s+delete\s+(set null|set default|cascade|restrict|no action)/i { $return = $1} -cascade_update_def : /on\s+update\s+(\w+)/i +cascade_update_def : /on\s+update\s+(set null|set default|cascade|restrict|no action)/i { $return = $1} table_name : qualified_name diff --git a/t/27sqlite-parser.t b/t/27sqlite-parser.t index ae99f19..5f61ea7 100644 --- a/t/27sqlite-parser.t +++ b/t/27sqlite-parser.t @@ -10,7 +10,7 @@ use SQL::Translator; use SQL::Translator::Schema::Constants; BEGIN { - maybe_plan(21, + maybe_plan(25, 'SQL::Translator::Parser::SQLite'); } SQL::Translator::Parser::SQLite->import('parse'); @@ -74,9 +74,9 @@ $file = "$Bin/data/sqlite/named.sql"; is( $t1->name, 'pet', "'Pet' table" ); my @constraints = $t1->get_constraints; - is( scalar @constraints, 3, '3 constraints on pet' ); + is( scalar @constraints, 5, '5 constraints on pet' ); - my $c1 = pop @constraints; + my $c1 = $constraints[2]; is( $c1->type, 'FOREIGN KEY', 'FK constraint' ); is( $c1->reference_table, 'person', 'References person table' ); is( $c1->name, 'fk_person_id', 'Constraint name fk_person_id' ); @@ -85,4 +85,12 @@ $file = "$Bin/data/sqlite/named.sql"; is( join(',', $c1->reference_fields), 'person_id', 'References person_id field' ); + my $c2 = $constraints[3]; + is( $c2->on_delete, 'SET DEFAULT', 'On delete set default' ); + is( $c2->on_update, 'SET NULL', 'On update set null' ); + + my $c3 = $constraints[4]; + is( $c3->on_update, 'NO ACTION', 'On update no action' ); + is( $c3->on_delete, '', 'On delete not defined' ); + } diff --git a/t/data/sqlite/named.sql b/t/data/sqlite/named.sql index fdc1f98..3654690 100644 --- a/t/data/sqlite/named.sql +++ b/t/data/sqlite/named.sql @@ -2,6 +2,10 @@ create table pet ( "pet_id" int, "person_id" int constraint fk_person_id references person(person_id) on update CASCADE on delete RESTRICT, + "person_id_2" int + constraint fk_person_id_2 references person(person_id) on update SET NULL on delete SET DEFAULT, + "person_id_3" int + constraint fk_person_id_3 references person(person_id) on update NO ACTION, "name" varchar(30), "age" int, constraint age_under_100 check ( age < 100 ),