From 5873e554f19525defb2171627ef97ec9567c2bdb Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Fri, 19 May 2017 07:08:38 -0400 Subject: [PATCH] Allow functions for testing and repair in DB integrity checks This can be used for things that can't be checked or repaired with SQL alone, or that are too difficult to do that way. --- chrome/content/zotero/xpcom/schema.js | 31 ++++++++++++++++++++------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/chrome/content/zotero/xpcom/schema.js b/chrome/content/zotero/xpcom/schema.js index 93943bbe58..01aaf14ae2 100644 --- a/chrome/content/zotero/xpcom/schema.js +++ b/chrome/content/zotero/xpcom/schema.js @@ -1195,8 +1195,11 @@ Zotero.Schema = new function(){ // Non-foreign key checks // - // Repair entry (second position) can be either a string or an array with multiple statements. - var queries = [ + // 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 a + // string or an array with multiple statements. Functions should avoid assuming any global state + // (e.g., loaded data). + var checks = [ // Can't be a FK with itemTypesCombined [ "SELECT COUNT(*) > 1 FROM items WHERE itemTypeID IS NULL", @@ -1246,8 +1249,16 @@ Zotero.Schema = new function(){ ] ]; - for (let sql of queries) { - let errorsFound = yield Zotero.DB.valueQueryAsync(sql[0]); + for (let check of checks) { + let errorsFound = false; + // SQL statement + if (typeof check[0] == 'string') { + errorsFound = yield Zotero.DB.valueQueryAsync(check[0]); + } + // Function + else { + errorsFound = yield check[0](); + } if (!errorsFound) { continue; } @@ -1257,15 +1268,19 @@ Zotero.Schema = new function(){ if (fix) { try { // Single query - if (typeof sql[1] == 'string') { - yield Zotero.DB.queryAsync(sql[1]); + if (typeof check[1] == 'string') { + yield Zotero.DB.queryAsync(check[1]); } // Multiple queries - else { - for (let s of sql[1]) { + else if (Array.isArray(check[1])) { + for (let s of check[1]) { yield Zotero.DB.queryAsync(s); } } + // Function + else { + yield check[1](); + } continue; } catch (e) {