From a814ed3c376dd249cc3d68b6f8a2a34516006b1d Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Thu, 30 Apr 2020 21:14:38 +0200
Subject: [PATCH] Mark end of an update

---
 inc/lektor/database.h |  1 +
 scripts/init.sql      | 12 ++++++++----
 src/database/open.c   |  8 ++++++++
 src/database/update.c |  1 +
 src/net/downloader.c  |  1 +
 5 files changed, 19 insertions(+), 4 deletions(-)

diff --git a/inc/lektor/database.h b/inc/lektor/database.h
index e0975dc7..3c960140 100644
--- a/inc/lektor/database.h
+++ b/inc/lektor/database.h
@@ -24,6 +24,7 @@ struct lkt_queue_state {
 
 long database_get_timestamp(volatile sqlite3 *db);
 void database_stamp(volatile sqlite3 *db);
+void database_updated(volatile sqlite3 *db);
 
 /* Open correctly a database for lektor. */
 bool database_new   (sqlite3 **db);
diff --git a/scripts/init.sql b/scripts/init.sql
index 9646d53c..21bffe9a 100644
--- a/scripts/init.sql
+++ b/scripts/init.sql
@@ -121,12 +121,16 @@ CREATE TABLE IF NOT EXISTS 'stickers.plt'
 
 -- Some useful values:
 -- last_update is the timestamp of the last time the table of kara has been
--- updated.  This is so lektor doesn't have to read all kara in the filesystem,
+-- updated. This is so lektor doesn't have to read all kara in the filesystem,
 -- just the new ones (where their modified timestamp is greater than
--- last_update).  If last_update is NULL then the database is empty.
+-- last_update). If last_update is NULL then the database is empty.
+-- the last_end_update is here to mark a synchronisation point. Multiple
+-- update can be done (updating last_update), but a general update can be
+-- in progress.
 CREATE TABLE IF NOT EXISTS misc
-  ( id          INTEGER PRIMARY KEY DEFAULT 42 CHECK(id = 42)
-  , last_update INTEGER
+  ( id              INTEGER PRIMARY KEY DEFAULT 42 CHECK(id = 42)
+  , last_update     INTEGER
+  , last_end_update INTEGER
   );
 
 INSERT OR REPLACE INTO misc (id) VALUES (42);
diff --git a/src/database/open.c b/src/database/open.c
index 12c09489..61414c90 100644
--- a/src/database/open.c
+++ b/src/database/open.c
@@ -213,3 +213,11 @@ database_stamp(volatile sqlite3 *db)
 error:
     return;
 }
+
+void
+database_updated(volatile sqlite3 *db)
+{
+    SQLITE_EXEC(db, "UPDATE misc SET last_end_update = strftime('%s','now');", error);
+error:
+    return;
+}
diff --git a/src/database/update.c b/src/database/update.c
index 4c677ed5..f4d992e4 100644
--- a/src/database/update.c
+++ b/src/database/update.c
@@ -146,6 +146,7 @@ database_update(sqlite3 *db, const char *kara_dir)
     }
 
     LOG_INFO("Passed directory '%s'", kara_dir);
+    database_updated(db);
     closedir(d);
     return true;
 }
diff --git a/src/net/downloader.c b/src/net/downloader.c
index b4d8d328..5e18a28d 100644
--- a/src/net/downloader.c
+++ b/src/net/downloader.c
@@ -472,6 +472,7 @@ __repo_get_all_id_async(void *arg)
     __handle_got_json(repo->db, repo, json);
     LOG_INFO_SCT("REPO", "%s", "Finished to download and insert kara list");
     json_object_put(json);
+    database_updated(repo->db);
     return NULL;
 }
 
-- 
GitLab