From 1bdfb0ae1b80d3a57a0853b2aa6b84b3cffed602 Mon Sep 17 00:00:00 2001
From: Hugo Trentesaux <hugo@trentesaux.fr>
Date: Wed, 11 Sep 2024 19:24:31 +0200
Subject: [PATCH] only update profile is new timestamp is higher

prevents older data to modify newer data
---
 src/indexer/database.ts | 23 ++++++++++++-----------
 1 file changed, 12 insertions(+), 11 deletions(-)

diff --git a/src/indexer/database.ts b/src/indexer/database.ts
index f2dfd73..468a0f9 100644
--- a/src/indexer/database.ts
+++ b/src/indexer/database.ts
@@ -71,23 +71,24 @@ export async function setLatestIndexedCID(cid: CID) {
 }
 
 // cesium plus profile query and param builder
-// completely overwrites previous data
+// completely overwrites previous data if new timestamp is higher
 const cesiumPlusProfile: QueryBuilder = {
   query: `INSERT INTO
     profiles(index_request_cid, time, pubkey, data_cid, title, description, avatar, geoloc, city, socials)
     VALUES ($1, $2, $3, $4, $5, $6, $7, point($8, $9), $10, $11)
     ON CONFLICT (pubkey)
     DO UPDATE SET
-        index_request_cid = EXCLUDED.index_request_cid,
-        time = EXCLUDED.time,
-        pubkey = EXCLUDED.pubkey, 
-        data_cid = EXCLUDED.data_cid, 
-        title = EXCLUDED.title,
-        description = EXCLUDED.description,
-        avatar = EXCLUDED.avatar,
-        geoloc = EXCLUDED.geoloc,
-        city = EXCLUDED.city,
-        socials = EXCLUDED.socials;
+      index_request_cid = EXCLUDED.index_request_cid,
+      time = EXCLUDED.time,
+      pubkey = EXCLUDED.pubkey, 
+      data_cid = EXCLUDED.data_cid, 
+      title = EXCLUDED.title,
+      description = EXCLUDED.description,
+      avatar = EXCLUDED.avatar,
+      geoloc = EXCLUDED.geoloc,
+      city = EXCLUDED.city,
+      socials = EXCLUDED.socials;
+    WHERE EXCLUDED.time > profiles.time;
         `,
   dataGetter: defaultDataGetter,
   dataTransform: defaultDataTransform,
-- 
GitLab