From 0def6c109fae69a8bbbd96647ce1c46f205a6db1 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Fri, 17 Jul 2009 07:07:27 +0000 Subject: [PATCH] Update some triggers and fix inconsistent database entries that could cause sync errors --- chrome/content/zotero/xpcom/schema.js | 10 ++++ triggers.sql | 70 +++++++++++++++++++++------ userdata.sql | 2 +- 3 files changed, 65 insertions(+), 17 deletions(-) diff --git a/chrome/content/zotero/xpcom/schema.js b/chrome/content/zotero/xpcom/schema.js index 310df851c..87855e29e 100644 --- a/chrome/content/zotero/xpcom/schema.js +++ b/chrome/content/zotero/xpcom/schema.js @@ -2362,6 +2362,16 @@ Zotero.Schema = new function(){ } Zotero.DB.query("UPDATE itemCreators SET creatorID=? WHERE creatorID NOT IN (SELECT creatorID FROM creators)", creatorID); } + + if (i==60) { + Zotero.DB.query("DROP TRIGGER IF EXISTS fki_itemAttachments_libraryID"); + Zotero.DB.query("DROP TRIGGER IF EXISTS fku_itemAttachments_libraryID"); + Zotero.DB.query("DROP TRIGGER IF EXISTS fki_itemNotes_libraryID"); + Zotero.DB.query("DROP TRIGGER IF EXISTS fku_itemNotes_libraryID"); + Zotero.DB.query("DELETE FROM collectionItems WHERE itemID IN (SELECT itemID FROM items NATURAL JOIN itemAttachments WHERE sourceItemID IS NOT NULL UNION SELECT itemID FROM items NATURAL JOIN itemNotes WHERE sourceItemID IS NOT NULL)"); + Zotero.DB.query("UPDATE itemAttachments SET sourceItemID=NULL WHERE sourceItemID=itemID"); + Zotero.DB.query("UPDATE itemNotes SET sourceItemID=NULL WHERE sourceItemID=itemID"); + } } _updateDBVersion('userdata', toVersion); diff --git a/triggers.sql b/triggers.sql index 5e8e22e71..135f7cd46 100644 --- a/triggers.sql +++ b/triggers.sql @@ -1,4 +1,4 @@ --- 11 +-- 12 -- Triggers to validate date field DROP TRIGGER IF EXISTS insert_date_field; @@ -579,13 +579,12 @@ CREATE TRIGGER fku_items_itemID_itemAttachments_itemID UPDATE itemAttachments SET itemID=NEW.itemID WHERE itemID=OLD.itemID; END; - --- itemAttachments libraryID -DROP TRIGGER IF EXISTS fki_itemAttachments_libraryID; -CREATE TRIGGER fki_itemAttachments_libraryID +-- itemAttachments +DROP TRIGGER IF EXISTS fki_itemAttachments; +CREATE TRIGGER fki_itemAttachments BEFORE INSERT ON itemAttachments FOR EACH ROW BEGIN - SELECT RAISE(ABORT, 'insert on table "itemAttachments" violates foreign key constraint "fki_itemAttachments_libraryID"') + SELECT RAISE(ABORT, 'insert on table "itemAttachments" violates foreign key constraint "fki_itemAttachments"') WHERE NEW.sourceItemID IS NOT NULL AND ( ( @@ -599,13 +598,25 @@ CREATE TRIGGER fki_itemAttachments_libraryID ) OR (SELECT libraryID FROM items WHERE itemID = NEW.itemID) != (SELECT libraryID FROM items WHERE itemID = NEW.sourceItemID) ); + + -- Make sure this is an attachment item + SELECT RAISE(ABORT, 'item is not an attachment') WHERE + (SELECT itemTypeID FROM items WHERE itemID = NEW.itemID) != 14; + + -- Make sure parent is a regular item + SELECT RAISE(ABORT, 'parent is not a regular item') WHERE + NEW.sourceItemID IS NOT NULL AND (SELECT itemTypeID FROM items WHERE itemID = NEW.sourceItemID) IN (1,14); + + -- If child, make sure attachment is not in a collection + SELECT RAISE(ABORT, 'collection item must be top level') WHERE + NEW.sourceItemID IS NOT NULL AND (SELECT COUNT(*) FROM collectionItems WHERE itemID=NEW.itemID)>0; END; -DROP TRIGGER IF EXISTS fku_itemAttachments_libraryID; -CREATE TRIGGER fku_itemAttachments_libraryID +DROP TRIGGER IF EXISTS fku_itemAttachments; +CREATE TRIGGER fku_itemAttachments BEFORE UPDATE ON itemAttachments FOR EACH ROW BEGIN - SELECT RAISE(ABORT, 'update on table "itemAttachments" violates foreign key constraint "fku_itemAttachments_libraryID"') + SELECT RAISE(ABORT, 'update on table "itemAttachments" violates foreign key constraint "fku_itemAttachments"') WHERE NEW.sourceItemID IS NOT NULL AND ( ( @@ -619,9 +630,16 @@ CREATE TRIGGER fku_itemAttachments_libraryID ) OR (SELECT libraryID FROM items WHERE itemID = NEW.itemID) != (SELECT libraryID FROM items WHERE itemID = NEW.sourceItemID) ); + + -- Make sure parent is a regular item + SELECT RAISE(ABORT, 'parent is not a regular item') WHERE + NEW.sourceItemID IS NOT NULL AND (SELECT itemTypeID FROM items WHERE itemID = NEW.sourceItemID) IN (1,14); + + -- If child, make sure attachment is not in a collection + SELECT RAISE(ABORT, 'collection item must be top level') WHERE + NEW.sourceItemID IS NOT NULL AND (SELECT COUNT(*) FROM collectionItems WHERE itemID=NEW.itemID)>0; END; - -- itemAttachments/sourceItemID DROP TRIGGER IF EXISTS fki_itemAttachments_sourceItemID_items_itemID; CREATE TRIGGER fki_itemAttachments_sourceItemID_items_itemID @@ -919,9 +937,9 @@ CREATE TRIGGER fku_items_itemID_itemNotes_itemID END; --- itemNotes libraryID -DROP TRIGGER IF EXISTS fki_itemNotes_libraryID; -CREATE TRIGGER fki_itemNotes_libraryID +-- itemNotes +DROP TRIGGER IF EXISTS fki_itemNotes; +CREATE TRIGGER fki_itemNotes BEFORE INSERT ON itemNotes FOR EACH ROW BEGIN SELECT RAISE(ABORT, 'insert on table "itemNotes" violates foreign key constraint "fki_itemNotes_libraryID"') @@ -938,13 +956,25 @@ CREATE TRIGGER fki_itemNotes_libraryID ) OR (SELECT libraryID FROM items WHERE itemID = NEW.itemID) != (SELECT libraryID FROM items WHERE itemID = NEW.sourceItemID) ); + + -- Make sure this is a note or attachment item + SELECT RAISE(ABORT, 'item is not a note or attachment') WHERE + (SELECT itemTypeID FROM items WHERE itemID = NEW.itemID) NOT IN (1,14); + + -- Make sure parent is a regular item + SELECT RAISE(ABORT, 'parent is not a regular item') WHERE + NEW.sourceItemID IS NOT NULL AND (SELECT itemTypeID FROM items WHERE itemID = NEW.sourceItemID) IN (1,14); + + -- If child, make sure note is not in a collection + SELECT RAISE(ABORT, 'collection item must be top level') WHERE + NEW.sourceItemID IS NOT NULL AND (SELECT COUNT(*) FROM collectionItems WHERE itemID=NEW.itemID)>0; END; -DROP TRIGGER IF EXISTS fku_itemNotes_libraryID; -CREATE TRIGGER fku_itemNotes_libraryID +DROP TRIGGER IF EXISTS fku_itemNotes; +CREATE TRIGGER fku_itemNotes BEFORE UPDATE ON itemNotes FOR EACH ROW BEGIN - SELECT RAISE(ABORT, 'update on table "itemNotes" violates foreign key constraint "fku_itemNotes_libraryID"') + SELECT RAISE(ABORT, 'update on table "itemNotes" violates foreign key constraint "fku_itemNotes"') WHERE NEW.sourceItemID IS NOT NULL AND ( ( @@ -958,6 +988,14 @@ CREATE TRIGGER fku_itemNotes_libraryID ) OR (SELECT libraryID FROM items WHERE itemID = NEW.itemID) != (SELECT libraryID FROM items WHERE itemID = NEW.sourceItemID) ); + + -- Make sure parent is a regular item + SELECT RAISE(ABORT, 'parent is not a regular item') WHERE + NEW.sourceItemID IS NOT NULL AND (SELECT itemTypeID FROM items WHERE itemID = NEW.sourceItemID) IN (1,14); + + -- If child, make sure note is not in a collection + SELECT RAISE(ABORT, 'collection item must be top level') WHERE + NEW.sourceItemID IS NOT NULL AND (SELECT COUNT(*) FROM collectionItems WHERE itemID=NEW.itemID)>0; END; diff --git a/userdata.sql b/userdata.sql index abc84a745..e624cff6c 100644 --- a/userdata.sql +++ b/userdata.sql @@ -1,4 +1,4 @@ --- 59 +-- 60 -- This file creates tables containing user-specific data for new users -- -- any changes made here must be mirrored in transition steps in schema.js::_migrateSchema()