From 9fc87e3b85b5dd7fd997c772e9ae418a666e2937 Mon Sep 17 00:00:00 2001 From: NiLuJe Date: Tue, 20 Oct 2020 01:14:24 +0200 Subject: [PATCH] Address some DB migration concerns (#6804) * Don't overwrite an existing DB backup during DB migrations * Try to be gentler with wonky DBs during migration --- plugins/statistics.koplugin/main.lua | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/plugins/statistics.koplugin/main.lua b/plugins/statistics.koplugin/main.lua index 404ed5899..eba266314 100644 --- a/plugins/statistics.koplugin/main.lua +++ b/plugins/statistics.koplugin/main.lua @@ -307,8 +307,13 @@ Do you want to create an empty database? -- Backup the existing DB first conn:close() local bkp_db_location = db_location .. ".bkp." .. db_version .. "-to-" .. DB_SCHEMA_VERSION - FFIUtil.copyFile(db_location, bkp_db_location) - logger.info("ReaderStatistics: Old DB backed up as", bkp_db_location) + -- Don't overwrite an existing backup + if lfs.attributes(bkp_db_location, "mode") == "file" then + logger.warn("ReaderStatistics: A DB backup from schema", db_version, "to schema", DB_SCHEMA_VERSION, "already exists!") + else + FFIUtil.copyFile(db_location, bkp_db_location) + logger.info("ReaderStatistics: Old DB backed up as", bkp_db_location) + end conn = SQ3.open(db_location) self:upgradeDB(conn) @@ -321,8 +326,13 @@ Do you want to create an empty database? -- We can't know what might happen, so, back the DB up... conn:close() local bkp_db_location = db_location .. ".bkp." .. db_version .. "-to-" .. DB_SCHEMA_VERSION - FFIUtil.copyFile(db_location, bkp_db_location) - logger.info("ReaderStatistics: Old DB backed up as", bkp_db_location) + -- Don't overwrite an existing backup + if lfs.attributes(bkp_db_location, "mode") == "file" then + logger.warn("ReaderStatistics: A DB backup from schema", db_version, "to schema", DB_SCHEMA_VERSION, "already exists!") + else + FFIUtil.copyFile(db_location, bkp_db_location) + logger.info("ReaderStatistics: Old DB backed up as", bkp_db_location) + end conn = SQ3.open(db_location) end @@ -491,7 +501,7 @@ function ReaderStatistics:upgradeDB(conn) -- and not too horribly out of phase with the actual page count at the time the data was originally collected... INSERT INTO page_stat_data SELECT id_book, page, start_time, duration, pages as total_pages FROM page_stat - LEFT JOIN book on book.id = id_book; + JOIN book on book.id = id_book; -- Drop old page_stat table DROP INDEX IF EXISTS page_stat_id_book;