diff --git a/atom b/atom index 6481b3a..fc14e46 100755 --- a/atom +++ b/atom @@ -267,6 +267,7 @@ openDatabase() { exec 5>&3 exec 3> >(tee -a $tempdir/debug.log >&5) fi + echo 'PRAGMA foreign_keys = ON;' >&3 } closeDatabase() { diff --git a/share/schema.sql b/share/schema.sql index d4f66e0..ac40b2a 100644 --- a/share/schema.sql +++ b/share/schema.sql @@ -8,6 +8,7 @@ CREATE TABLE source_files ( last_change INTEGER NOT NULL DEFAULT (strftime('%s','now')), last_seen INTEGER NOT NULL DEFAULT (strftime('%s','now')), FOREIGN KEY (mime_type) REFERENCES mime_types(id) + ON DELETE SET NULL ); CREATE TABLE destinations ( id INTEGER PRIMARY KEY, @@ -19,8 +20,10 @@ CREATE TABLE destination_files ( last_change INTEGER NOT NULL DEFAULT 0, source_file_id INTEGER, destination_id INTEGER, - FOREIGN KEY (source_file_id) REFERENCES source_files(id), + FOREIGN KEY (source_file_id) REFERENCES source_files(id) + ON DELETE SET NULL, FOREIGN KEY (destination_id) REFERENCES destinations(id) + ON DELETE CASCADE ); CREATE TABLE mime_types ( id INTEGER PRIMARY KEY, @@ -31,8 +34,9 @@ CREATE TABLE mime_actions ( mime_type INTEGER, destination_id INTEGER, action INTEGER DEFAULT 1, - FOREIGN KEY (mime_type) REFERENCES mime_types(id) + FOREIGN KEY (mime_type) REFERENCES mime_types(id), FOREIGN KEY (destination_id) REFERENCES destinations(id) + ON DELETE CASCADE ); CREATE VIEW mime_type_actions AS @@ -66,9 +70,5 @@ BEGIN INNER JOIN destinations WHERE source_files.id=new.id; END; -CREATE TRIGGER delete_destinations_files AFTER DELETE ON source_files -BEGIN - UPDATE destination_files SET source_file_id=NULL WHERE source_file_id=old.id; -END; COMMIT;