Vocabulary builder: safer db migration (#9702)

Refactor db migration to allow restarting from
an intermediate stage.
reviewable/pr9776/r1
weijiuqiao 2 years ago committed by GitHub
parent d6b67f42d8
commit 678d0c911c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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);")

Loading…
Cancel
Save