From 8b57d0000b8b0dfc0dc994d73723769a1c8e13cf Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Fri, 19 Feb 2021 05:19:51 -0500 Subject: [PATCH] Don't orphan embedded-image attachments in schema integrity check! --- chrome/content/zotero/xpcom/schema.js | 28 +++++++++++++++++++++------ test/tests/schemaTest.js | 6 ++++++ 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/chrome/content/zotero/xpcom/schema.js b/chrome/content/zotero/xpcom/schema.js index ed58f947f9..19374f162a 100644 --- a/chrome/content/zotero/xpcom/schema.js +++ b/chrome/content/zotero/xpcom/schema.js @@ -1799,6 +1799,9 @@ Zotero.Schema = new function(){ var noteID = parseInt(yield Zotero.DB.valueQueryAsync( "SELECT itemTypeID FROM itemTypes WHERE typeName='note'" )); + var annotationID = parseInt(yield Zotero.DB.valueQueryAsync( + "SELECT itemTypeID FROM itemTypes WHERE typeName='annotation'" + )); // The first position is for testing and the second is for repairing. Can be either SQL // statements or promise-returning functions. For statements, the repair entry can be either @@ -1924,16 +1927,29 @@ Zotero.Schema = new function(){ `SELECT COUNT(*) > 0 FROM items WHERE itemTypeID=${attachmentID} AND itemID NOT IN (SELECT itemID FROM itemAttachments)`, `INSERT INTO itemAttachments (itemID, linkMode) SELECT itemID, 0 FROM items WHERE itemTypeID=${attachmentID} AND itemID NOT IN (SELECT itemID FROM itemAttachments)`, ], - // Note/child parents + // Attachments with note parents, unless they're embedded-image attachments [ - `SELECT COUNT(*) > 0 FROM itemAttachments WHERE parentItemID IN (SELECT itemID FROM items WHERE itemTypeID IN (${noteID}, ${attachmentID}))`, - `UPDATE itemAttachments SET parentItemID=NULL WHERE parentItemID IN (SELECT itemID FROM items WHERE itemTypeID IN (${noteID}, ${attachmentID}))`, + `SELECT COUNT(*) > 0 FROM itemAttachments ` + + `WHERE parentItemID IN (SELECT itemID FROM items WHERE itemTypeID=${noteID}) ` + + `AND linkMode != ${Zotero.Attachments.LINK_MODE_EMBEDDED_IMAGE}`, + `UPDATE itemAttachments SET parentItemID=NULL ` + + `WHERE parentItemID IN (SELECT itemID FROM items WHERE itemTypeID=${noteID}) ` + + `AND linkMode != ${Zotero.Attachments.LINK_MODE_EMBEDDED_IMAGE}`, ], + // Attachments with attachment or annotation parents [ - `SELECT COUNT(*) > 0 FROM itemNotes WHERE parentItemID IN (SELECT itemID FROM items WHERE itemTypeID IN (${noteID}, ${attachmentID}))`, - `UPDATE itemNotes SET parentItemID=NULL WHERE parentItemID IN (SELECT itemID FROM items WHERE itemTypeID IN (${noteID}, ${attachmentID}))`, + `SELECT COUNT(*) > 0 FROM itemAttachments ` + + `WHERE parentItemID IN (SELECT itemID FROM items WHERE itemTypeID IN (${attachmentID}, ${annotationID}))`, + `UPDATE itemAttachments SET parentItemID=NULL ` + + `WHERE parentItemID IN (SELECT itemID FROM items WHERE itemTypeID IN (${attachmentID}, ${annotationID}))`, + ], + // Notes with note/attachment/annotation parents + [ + `SELECT COUNT(*) > 0 FROM itemNotes ` + + `WHERE parentItemID IN (SELECT itemID FROM items WHERE itemTypeID IN (${noteID}, ${attachmentID}, ${annotationID}))`, + `UPDATE itemNotes SET parentItemID=NULL ` + + `WHERE parentItemID IN (SELECT itemID FROM items WHERE itemTypeID IN (${noteID}, ${attachmentID}, ${annotationID}))`, ], - // Delete empty creators // This may cause itemCreator gaps, but that's better than empty creators [ diff --git a/test/tests/schemaTest.js b/test/tests/schemaTest.js index a6b3e6f1f6..8e528ced7e 100644 --- a/test/tests/schemaTest.js +++ b/test/tests/schemaTest.js @@ -296,5 +296,11 @@ describe("Zotero.Schema", function() { await assert.isTrue(await Zotero.Schema.integrityCheck(true)); await assert.isTrue(await Zotero.Schema.integrityCheck()); }); + + it("should allow embedded-image attachments under notes", async function () { + var item = await createDataObject('item', { itemType: 'note' }); + await createEmbeddedImage(item); + await assert.isTrue(await Zotero.Schema.integrityCheck()); + }); }) })