✨ (weread): 首次加载时把旧内置版 wechat 的数据就地改名为 weread
设置、书库记录、缓存目录和库里的路径、启用状态一并迁移,用户无需重新登录。 Co-authored-by: Copilot <[email protected]>
This commit is contained in:
1 parent
f8ad3f73a6
commit
d8626befbc
3 files changed
+174
-1
No files matched your search
@@ -0,0 +1,114 @@
|
||||
--[[--
|
||||
旧版月读内置微信读书的 id 是 `wechat`,插件版是 `weread`。首次加载时把旧数据就地改名,用户不用重新登录、书架和进度都在:
|
||||
设置、数据库里按源分区的记录、缓存目录及库里记录的路径、源启用状态。
|
||||
|
||||
只在发现旧数据时动手,改完旧设置清空,之后每次启动只是两次空判断。
|
||||
--]]
|
||||
|
||||
local lfs = require("libs/libkoreader-lfs")
|
||||
|
||||
local Legacy = {}
|
||||
|
||||
local OLD, NEW = "wechat", "weread"
|
||||
|
||||
-- 带 source_id 列的表;本表集合与月读主库一致。
|
||||
local TABLES = {
|
||||
"books", "chapters", "notes", "pending_progress", "reading_stats", "xray_entities", "chapter_summaries",
|
||||
"xray_scan", "xray_rare_scan", "review_cache", "xray_removed", "xray_chapter", "toc_snapshots", "toc_entries",
|
||||
"chapter_summary_pending",
|
||||
}
|
||||
|
||||
local done = false
|
||||
|
||||
local function exists(path)
|
||||
return lfs.attributes(path, "mode") ~= nil
|
||||
end
|
||||
|
||||
local function moveSettings()
|
||||
local Settings = require("utils.settings")
|
||||
local old, new = Settings.getSource(OLD), Settings.getSource(NEW)
|
||||
if next(old) == nil then return false end
|
||||
if next(new) == nil then
|
||||
for key, value in pairs(old) do new[key] = value end
|
||||
Settings.saveSource(NEW)
|
||||
end
|
||||
for key in pairs(old) do old[key] = nil end
|
||||
Settings.saveSource(OLD)
|
||||
return true
|
||||
end
|
||||
|
||||
--- 启用状态、当前源跟着改名。
|
||||
local function moveCommon()
|
||||
local Settings = require("utils.settings")
|
||||
local common = Settings.get("source")
|
||||
local changed = false
|
||||
if type(common.enabled_sources) == "table" and common.enabled_sources[OLD] ~= nil then
|
||||
if common.enabled_sources[NEW] == nil then common.enabled_sources[NEW] = common.enabled_sources[OLD] end
|
||||
common.enabled_sources[OLD] = nil
|
||||
changed = true
|
||||
end
|
||||
if common.active_source == OLD then
|
||||
common.active_source = NEW
|
||||
changed = true
|
||||
end
|
||||
if changed then Settings.save("source") end
|
||||
end
|
||||
|
||||
--- 把旧目录并进新目录:新目录没有的整项改名,两边都是目录的往下合并,同名文件留旧的不覆盖。
|
||||
--- 旧目录搬空并删除才返回 true(此时库里的路径才能整体改前缀)。
|
||||
local function mergeDir(old_dir, new_dir)
|
||||
if not exists(new_dir) then return os.rename(old_dir, new_dir) == true end
|
||||
for name in lfs.dir(old_dir) do
|
||||
if name ~= "." and name ~= ".." then
|
||||
local from, to = old_dir .. "/" .. name, new_dir .. "/" .. name
|
||||
if not exists(to) then
|
||||
os.rename(from, to)
|
||||
elseif lfs.attributes(from, "mode") == "directory" and lfs.attributes(to, "mode") == "directory" then
|
||||
mergeDir(from, to)
|
||||
end
|
||||
end
|
||||
end
|
||||
return lfs.rmdir(old_dir) == true
|
||||
end
|
||||
|
||||
---@return boolean moved
|
||||
local function moveCache()
|
||||
local Paths = require("utils.paths")
|
||||
local old_dir = Paths.sourceCacheDir(OLD)
|
||||
if not exists(old_dir) then return false end
|
||||
return mergeDir(old_dir, Paths.sourceCacheDir(NEW))
|
||||
end
|
||||
|
||||
local function moveDatabase(paths_moved)
|
||||
local DB = require("utils.db")
|
||||
DB.transaction(function()
|
||||
for _idx, name in ipairs(TABLES) do
|
||||
-- 同主键的新记录已存在就保留新的,旧的留在原处不再用
|
||||
DB.exec(("UPDATE OR IGNORE %s SET source_id=? WHERE source_id=?;"):format(name), NEW, OLD)
|
||||
end
|
||||
if paths_moved then
|
||||
local old_part, new_part = "/cache/" .. OLD .. "/", "/cache/" .. NEW .. "/"
|
||||
for _idx, name in ipairs({ "books", "chapters", "local_scan" }) do
|
||||
DB.exec(("UPDATE %s SET path=REPLACE(path, ?, ?) WHERE path LIKE ?;"):format(name),
|
||||
old_part, new_part, "%" .. old_part .. "%")
|
||||
end
|
||||
end
|
||||
return true
|
||||
end)
|
||||
end
|
||||
|
||||
--- 就地迁移旧 id 的数据;每个进程只跑一次。
|
||||
function Legacy.run()
|
||||
if done then return end
|
||||
done = true
|
||||
local Settings = require("utils.settings")
|
||||
local has_settings = next(Settings.getSource(OLD)) ~= nil
|
||||
local has_cache = exists(require("utils.paths").sourceCacheDir(OLD))
|
||||
if not has_settings and not has_cache then return end
|
||||
pcall(moveSettings)
|
||||
pcall(moveCommon)
|
||||
local ok, paths_moved = pcall(moveCache)
|
||||
pcall(moveDatabase, ok and paths_moved)
|
||||
end
|
||||
|
||||
return Legacy
|
||||
Reference in new issue
Block a user