From 6844deba609296aab9133cb49bee94ea4145daa0 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Tue, 29 Dec 2015 23:37:45 -0500 Subject: [PATCH] Disable foreign key checks during schema modifications Otherwise renaming a table causes existing foreign keys to be renamed. --- chrome/content/zotero/xpcom/schema.js | 44 +++++++++++++++------------ 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/chrome/content/zotero/xpcom/schema.js b/chrome/content/zotero/xpcom/schema.js index c753298283..6096c3123d 100644 --- a/chrome/content/zotero/xpcom/schema.js +++ b/chrome/content/zotero/xpcom/schema.js @@ -117,26 +117,30 @@ Zotero.Schema = new function(){ yield Zotero.DB.backupDatabase(userdata, true); } - var updated = yield Zotero.DB.executeTransaction(function* (conn) { - yield Zotero.DB.queryAsync("PRAGMA defer_foreign_keys = true"); - - var updated = yield _updateSchema('system'); - - // Update custom tables if they exist so that changes are in - // place before user data migration - if (Zotero.DB.tableExists('customItemTypes')) { - yield _updateCustomTables(updated); - } - updated = yield _migrateUserDataSchema(userdata); - yield _updateSchema('triggers'); - - // Populate combined tables for custom types and fields -- this is likely temporary - // - // We do this again in case custom fields were changed during user data migration - yield _updateCustomTables() - - return updated; - }.bind(this)); + yield Zotero.DB.queryAsync("PRAGMA foreign_keys = false"); + try { + var updated = yield Zotero.DB.executeTransaction(function* (conn) { + var updated = yield _updateSchema('system'); + + // Update custom tables if they exist so that changes are in + // place before user data migration + if (Zotero.DB.tableExists('customItemTypes')) { + yield _updateCustomTables(updated); + } + updated = yield _migrateUserDataSchema(userdata); + yield _updateSchema('triggers'); + + // Populate combined tables for custom types and fields -- this is likely temporary + // + // We do this again in case custom fields were changed during user data migration + yield _updateCustomTables() + + return updated; + }.bind(this)); + } + finally { + yield Zotero.DB.queryAsync("PRAGMA foreign_keys = true"); + } if (updated) { // Upgrade seems to have been a success -- delete any previous backups