From ddc7be75c7651fb1181225f281e20e4f19a8b11d Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Tue, 15 Jun 2021 01:33:28 -0400 Subject: [PATCH] Use `PRAGMA legacy_alter_table=ON` for existing schema updates This won't make a difference until we update Firefox and get a new SQLite version, but at that point the existing schema update steps that recreate tables by renaming the old table would result in broken foreign keys. This patch will make sure that newer SQLite versions will use the legacy behavior for the existing steps, and going forward schema update steps that want to recreate tables will need to create a new table, migrate the data, delete the old table, and rename the new one into place. --- chrome/content/zotero/xpcom/schema.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/chrome/content/zotero/xpcom/schema.js b/chrome/content/zotero/xpcom/schema.js index 29f947f70c..f8dd3df05d 100644 --- a/chrome/content/zotero/xpcom/schema.js +++ b/chrome/content/zotero/xpcom/schema.js @@ -2590,6 +2590,12 @@ Zotero.Schema = new function(){ Zotero.DB.requireTransaction(); + // Use old rename/FK behavior from SQLite <3.25 + // https://stackoverflow.com/a/57275538 + if (fromVersion <= 113) { + yield Zotero.DB.queryAsync("PRAGMA legacy_alter_table=ON"); + } + // Step through version changes until we reach the current version // // Each block performs the changes necessary to move from the @@ -3303,6 +3309,14 @@ Zotero.Schema = new function(){ } } + else if (i == 118) { + // Switch to new rename/FK behavior. All further table rebuilds must create a new + // table with a temporary name, do an INSERT...SELECT (with default/missing values + // as appropriate), delete the old table, and rename the new one back to the + // original name. https://stackoverflow.com/a/57275538 + yield Zotero.DB.queryAsync("PRAGMA legacy_alter_table=OFF"); + } + // If breaking compatibility or doing anything dangerous, clear minorUpdateFrom }