From 45685f8a7cd5b294cf7941d904337ebdc87eda35 Mon Sep 17 00:00:00 2001 From: Vincent Riquer Date: Tue, 30 Apr 2013 00:52:18 +0200 Subject: [PATCH] Force update of destinations after a tag update Also force an update of the DB schema on each run. --- lib/database/openDatabase | 2 +- share/schema.sql | 34 +++++++++++++++++++++------------- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/lib/database/openDatabase b/lib/database/openDatabase index 74f4745..df05d84 100644 --- a/lib/database/openDatabase +++ b/lib/database/openDatabase @@ -12,7 +12,6 @@ openDatabase() { then mkdir -p "${database%/*}" fi - sqlite3 "$database" < $schema fi sqlite3 -bail "$database" \ < "$tempdir/sqlite.in" \ @@ -24,5 +23,6 @@ openDatabase() { exec 5>&3 exec 3> >(tee -a $tempdir/debug.log >&5) fi + cat $schema >&3 echo 'PRAGMA foreign_keys = ON;' >&3 } diff --git a/share/schema.sql b/share/schema.sql index 9e82275..2a915e4 100644 --- a/share/schema.sql +++ b/share/schema.sql @@ -1,5 +1,5 @@ BEGIN TRANSACTION; -CREATE TABLE source_files ( +CREATE TABLE IF NOT EXISTS source_files ( id INTEGER PRIMARY KEY, filename TEXT UNIQUE NOT NULL, size INTEGER NOT NULL, @@ -10,11 +10,11 @@ CREATE TABLE source_files ( FOREIGN KEY (mime_type) REFERENCES mime_types(id) ON DELETE SET NULL ); -CREATE TABLE destinations ( +CREATE TABLE IF NOT EXISTS destinations ( id INTEGER PRIMARY KEY, name TEXT UNIQUE NOT NULL ); -CREATE TABLE destination_files ( +CREATE TABLE IF NOT EXISTS destination_files ( id INTEGER PRIMARY KEY, filename TEXT, old_filename TEXT, @@ -27,11 +27,11 @@ CREATE TABLE destination_files ( FOREIGN KEY (destination_id) REFERENCES destinations(id) ON DELETE CASCADE ); -CREATE TABLE mime_types ( +CREATE TABLE IF NOT EXISTS mime_types ( id INTEGER PRIMARY KEY, mime_text TEXT UNIQUE NOT NULL ); -CREATE TABLE mime_actions ( +CREATE TABLE IF NOT EXISTS mime_actions ( id INTEGER PRIMARY KEY, mime_type INTEGER, destination_id INTEGER, @@ -40,7 +40,7 @@ CREATE TABLE mime_actions ( FOREIGN KEY (destination_id) REFERENCES destinations(id) ON DELETE CASCADE ); -CREATE TABLE tags ( +CREATE TABLE IF NOT EXISTS tags ( source_file INTEGER PRIMARY KEY, genre TEXT, albumartist TEXT, @@ -61,13 +61,14 @@ CREATE TABLE tags ( ON DELETE CASCADE ); -CREATE VIEW mime_type_actions AS +CREATE VIEW IF NOT EXISTS mime_type_actions AS SELECT mime_types.id,mime_types.mime_text, mime_actions.destination_id,mime_actions.action FROM mime_types INNER JOIN mime_actions ON mime_actions.mime_type = mime_types.id; -CREATE TRIGGER update_mime_actions INSTEAD OF UPDATE OF action ON mime_type_actions +CREATE TRIGGER IF NOT EXISTS update_mime_actions + INSTEAD OF UPDATE OF action ON mime_type_actions BEGIN UPDATE mime_actions SET action=new.action @@ -75,7 +76,8 @@ BEGIN AND destination_id=old.destination_id; END; -CREATE TRIGGER create_dest_files_and_mime_actions AFTER INSERT ON destinations +CREATE TRIGGER IF NOT EXISTS create_dest_files_and_mime_actions + AFTER INSERT ON destinations BEGIN INSERT INTO mime_actions (mime_type,destination_id) @@ -87,7 +89,7 @@ BEGIN FROM source_files; END; -CREATE TRIGGER create_mime_actions AFTER INSERT ON mime_types +CREATE TRIGGER IF NOT EXISTS create_mime_actions AFTER INSERT ON mime_types BEGIN INSERT INTO mime_actions (mime_type,destination_id) SELECT mime_types.id,destinations.id @@ -95,18 +97,24 @@ BEGIN WHERE mime_types.id=new.id; END; -CREATE INDEX sourcefiles_by_name ON source_files (filename,id); +CREATE INDEX IF NOT EXISTS sourcefiles_by_name ON source_files (filename,id); -CREATE TRIGGER create_destinations AFTER INSERT ON source_files +CREATE TRIGGER IF NOT EXISTS create_destinations AFTER INSERT ON source_files BEGIN INSERT INTO destination_files (source_file_id,destination_id) SELECT source_files.id,destinations.id FROM source_files INNER JOIN destinations WHERE source_files.id=new.id; END; -CREATE TRIGGER create_tags AFTER INSERT ON source_files +CREATE TRIGGER IF NOT EXISTS create_tags AFTER INSERT ON source_files BEGIN INSERT INTO tags (source_file,last_change) VALUES (new.id,0); END; +CREATE TRIGGER IF NOT EXISTS force_destination_update_on_tag_update + AFTER UPDATE ON tags +BEGIN + UPDATE destination_files SET last_change=0 + WHERE source_file_id=old.source_file; +END; COMMIT;