From 2aa11b13ada122cc18ddd802ab90a046e4348290 Mon Sep 17 00:00:00 2001 From: Glen Sawyer Date: Mon, 29 May 2023 04:48:00 -0600 Subject: [PATCH] [plugin] Statistics: sync the `last_open` field (#10498) --- plugins/statistics.koplugin/main.lua | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/plugins/statistics.koplugin/main.lua b/plugins/statistics.koplugin/main.lua index a2dc799fd..c8db6b535 100644 --- a/plugins/statistics.koplugin/main.lua +++ b/plugins/statistics.koplugin/main.lua @@ -3051,6 +3051,16 @@ function ReaderStatistics.onSync(local_path, cached_path, income_path) end sql = sql .. [[ + -- If book was opened more recently on another device, then update local db's `last_open` field + -- NOTE: We could do this as an "upsert" by adding an `ON CONFLICT` clause to the following `INSERT` + -- but using `ON CONFLICT` unnecessarily increments the autoincrement for the table; + -- see https://sqlite.org/forum/info/98d4fb9ced866287 + UPDATE book AS b + SET last_open = i.last_open + FROM income_db.book AS i + WHERE (b.title, b.authors, b.md5) = (i.title, i.authors, i.md5) + AND i.last_open > b.last_open; + -- We merge the local db with income db to form the synced db. -- Do the books INSERT INTO book (