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.
This commit is contained in:
Dan Stillman 2017-05-19 07:08:38 -04:00
parent caf61bec2f
commit 5873e554f1

View File

@ -1195,8 +1195,11 @@ Zotero.Schema = new function(){
// Non-foreign key checks // Non-foreign key checks
// //
// Repair entry (second position) can be either a string or an array with multiple statements. // The first position is for testing and the second is for repairing. Can be either SQL
var queries = [ // 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 // Can't be a FK with itemTypesCombined
[ [
"SELECT COUNT(*) > 1 FROM items WHERE itemTypeID IS NULL", "SELECT COUNT(*) > 1 FROM items WHERE itemTypeID IS NULL",
@ -1246,8 +1249,16 @@ Zotero.Schema = new function(){
] ]
]; ];
for (let sql of queries) { for (let check of checks) {
let errorsFound = yield Zotero.DB.valueQueryAsync(sql[0]); 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) { if (!errorsFound) {
continue; continue;
} }
@ -1257,15 +1268,19 @@ Zotero.Schema = new function(){
if (fix) { if (fix) {
try { try {
// Single query // Single query
if (typeof sql[1] == 'string') { if (typeof check[1] == 'string') {
yield Zotero.DB.queryAsync(sql[1]); yield Zotero.DB.queryAsync(check[1]);
} }
// Multiple queries // Multiple queries
else { else if (Array.isArray(check[1])) {
for (let s of sql[1]) { for (let s of check[1]) {
yield Zotero.DB.queryAsync(s); yield Zotero.DB.queryAsync(s);
} }
} }
// Function
else {
yield check[1]();
}
continue; continue;
} }
catch (e) { catch (e) {