Update some triggers and fix inconsistent database entries that could cause sync errors

This commit is contained in:
Dan Stillman 2009-07-17 07:07:27 +00:00
parent 4dde67acd1
commit 0def6c109f
3 changed files with 65 additions and 17 deletions

View File

@ -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);

View File

@ -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;

View File

@ -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()