From 678d0c911c78ef4358404dee6965a37348184e91 Mon Sep 17 00:00:00 2001 From: weijiuqiao <59040746+weijiuqiao@users.noreply.github.com> Date: Sat, 12 Nov 2022 07:00:38 +0800 Subject: [PATCH] Vocabulary builder: safer db migration (#9702) Refactor db migration to allow restarting from an intermediate stage. --- plugins/vocabbuilder.koplugin/db.lua | 40 ++++++++++++++++------------ 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/plugins/vocabbuilder.koplugin/db.lua b/plugins/vocabbuilder.koplugin/db.lua index 13288db2f..354db8f72 100644 --- a/plugins/vocabbuilder.koplugin/db.lua +++ b/plugins/vocabbuilder.koplugin/db.lua @@ -65,28 +65,34 @@ function VocabularyBuilder:createDB() -- Update version db_conn:exec(string.format("PRAGMA user_version=%d;", DB_SCHEMA_VERSION)) elseif db_version < DB_SCHEMA_VERSION then + local logger = require("logger") + local ok, re + local log = function(msg) + logger.warn("[vocab builder db migration]", msg) + end if db_version < 20220608 then - db_conn:exec([[ ALTER TABLE vocabulary ADD prev_context TEXT; - ALTER TABLE vocabulary ADD next_context TEXT; - ALTER TABLE vocabulary ADD title_id INTEGER; - - INSERT INTO title (name) - SELECT DISTINCT book_title FROM vocabulary; - - UPDATE vocabulary SET title_id = ( - SELECT id FROM title WHERE name = book_title - ); - - ALTER TABLE vocabulary DROP book_title;]]) + ok, re = pcall(db_conn.exec, db_conn, "ALTER TABLE vocabulary ADD prev_context TEXT;") + if not ok then log(re) end + ok, re = pcall(db_conn.exec, db_conn, "ALTER TABLE vocabulary ADD next_context TEXT;") + if not ok then log(re) end + ok, re = pcall(db_conn.exec, db_conn, "ALTER TABLE vocabulary ADD title_id INTEGER;") + if not ok then log(re) end + ok, re = pcall(db_conn.exec, db_conn, "INSERT OR IGNORE INTO title (name) SELECT DISTINCT book_title FROM vocabulary;") + if not ok then log(re) end + ok, re = pcall(db_conn.exec, db_conn, "UPDATE vocabulary SET title_id = (SELECT id FROM title WHERE name = book_title);") + if not ok then log(re) end + ok, re = pcall(db_conn.exec, db_conn, "ALTER TABLE vocabulary DROP book_title;") + if not ok then log(re) end end if db_version < 20220730 then - if tonumber(db_conn:rowexec("SELECT COUNT(*) FROM pragma_table_info('title') WHERE name='filter'")) == 0 then - db_conn:exec("ALTER TABLE title ADD filter INTEGER NOT NULL DEFAULT 1;") - end + ok, re = pcall(db_conn.exec, db_conn, "ALTER TABLE title ADD filter INTEGER NOT NULL DEFAULT 1;") + if not ok then log(re) end end if db_version < 20221002 then - db_conn:exec([[ ALTER TABLE vocabulary ADD streak_count INTEGER NULL DEFAULT 0; - UPDATE vocabulary SET streak_count = review_count; ]]) + ok, re = pcall(db_conn.exec, db_conn, [[ + ALTER TABLE vocabulary ADD streak_count INTEGER NULL DEFAULT 0; + UPDATE vocabulary SET streak_count = review_count; ]]) + if not ok then log(re) end end db_conn:exec("CREATE INDEX IF NOT EXISTS title_id_index ON vocabulary(title_id);")