From 51bec588f741dfdb08cc32c802caf4ae5b3b0ada Mon Sep 17 00:00:00 2001
From: Michal 'vorner' Vaner <michal.vaner@nic.cz>
Date: Fri, 12 Feb 2016 14:43:49 +0100
Subject: [PATCH] Plugin history: Duplicate the last event on cleanup

That way we are sure we have the current state still in the table. Also,
when deleting several days old events, we still have recent history.
This is to prevent the last event of too long active client from
dropping out.
---
 src/master/dbscripts/purge | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/src/master/dbscripts/purge b/src/master/dbscripts/purge
index 4c81332e..a7ff1bf7 100755
--- a/src/master/dbscripts/purge
+++ b/src/master/dbscripts/purge
@@ -10,6 +10,10 @@ DATE=$(date -d "$CLEAN_DAYS days ago" "+'%Y-%m-%d'")
 
 (
 	echo "BEGIN;"
+	# Duplicate the last plugin history event. This way, even if
+	# the client is connected for a long time, we won't lose all
+	# the events and we shall know what its state is now or a day ago
+	echo "INSERT INTO plugin_history (client, name, timestamp, version, hash, active) SELECT DISTINCT ph.client, ph.name, CURRENT_TIMESTAMP AT TIME ZONE 'UTC', ph.version, ph.hash, ph.active FROM plugin_history AS ph JOIN (SELECT client, name, MAX(timestamp) AS timestamp FROM plugin_history GROUP BY client, name) AS latest ON ph.client = latest.client AND ph.name = latest.name AND ph.timestamp = latest.timestamp;"
 	for TABLE in $TABLES ; do
 		echo "DELETE FROM $TABLE WHERE timestamp < $DATE;"
 	done
-- 
GitLab