From a243b95f563f06f8e0ee1e4c51528da8fea42caa Mon Sep 17 00:00:00 2001
From: Przemek Kaminski <pk@intrepidus.pl>
Date: Thu, 8 Sep 2022 20:07:09 +0200
Subject: [PATCH] [nodeStory] add version to node_story_archive_history

This is so that we correctly order _a_history later.
---
 devops/postgres/schema.sql          |  1 +
 devops/postgres/upgrade/0.0.6.2.sql |  2 ++
 gargantext.cabal                    |  7 ++++---
 src/Gargantext/Core/NodeStory.hs    | 19 ++++++++++---------
 4 files changed, 17 insertions(+), 12 deletions(-)
 create mode 100644 devops/postgres/upgrade/0.0.6.2.sql

diff --git a/devops/postgres/schema.sql b/devops/postgres/schema.sql
index 460499fe..01e8736d 100644
--- a/devops/postgres/schema.sql
+++ b/devops/postgres/schema.sql
@@ -244,6 +244,7 @@ create table public.node_story_archive_history (
   ngrams_type_id INTEGER NOT NULL,
   ngrams_id INTEGER NOT NULL,
   patch jsonb DEFAULT '{}'::jsonb NOT NULL,
+  version INTEGER NOT NULL DEFAULT 0,
   PRIMARY KEY (id),
   FOREIGN KEY (node_id) REFERENCES public.nodes(id) ON DELETE CASCADE,
   FOREIGN KEY (ngrams_id) REFERENCES public.ngrams(id) ON DELETE CASCADE
diff --git a/devops/postgres/upgrade/0.0.6.2.sql b/devops/postgres/upgrade/0.0.6.2.sql
new file mode 100644
index 00000000..ec691e87
--- /dev/null
+++ b/devops/postgres/upgrade/0.0.6.2.sql
@@ -0,0 +1,2 @@
+ALTER TABLE node_story_archive_history
+  ADD COLUMN version INTEGER NOT NULL DEFAULT 0;
diff --git a/gargantext.cabal b/gargantext.cabal
index 43715e87..355dd6b6 100644
--- a/gargantext.cabal
+++ b/gargantext.cabal
@@ -5,7 +5,7 @@ cabal-version: 1.12
 -- see: https://github.com/sol/hpack
 
 name:           gargantext
-version:        0.0.6
+version:        0.0.6.1
 synopsis:       Search, map, share
 description:    Please see README.md
 category:       Data
@@ -51,6 +51,7 @@ library
       Gargantext.Core.Types.Individu
       Gargantext.Core.Types.Main
       Gargantext.Core.Utils.Prefix
+      Gargantext.Utils.SpacyNLP
       Gargantext.Database.Action.Flow
       Gargantext.Database.Action.Flow.Types
       Gargantext.Database.Action.User.New
@@ -76,6 +77,7 @@ library
       Gargantext.Core.Text.Metrics.TFICF
       Gargantext.Core.Text.Metrics.CharByChar
       Gargantext.Core.Text.Metrics.Count
+      Gargantext.Core.Text.Prepare
       Gargantext.Core.Text.Search
       Gargantext.Core.Text.Terms
       Gargantext.Core.Text.Terms.Mono
@@ -316,7 +318,6 @@ library
       Gargantext.Database.Types
       Gargantext.Utils.Aeson
       Gargantext.Utils.JohnSnowNLP
-      Gargantext.Utils.SpacyNLP
       Gargantext.Utils.Servant
       Gargantext.Utils.UTCTime
       Paths_gargantext
@@ -491,9 +492,9 @@ library
     , transformers-base
     , tuple
     , unordered-containers
+    , uri-encode
     , utf8-string
     , uuid
-    , uri-encode
     , validity
     , vector
     , wai
diff --git a/src/Gargantext/Core/NodeStory.hs b/src/Gargantext/Core/NodeStory.hs
index 0191e6a3..e3e89a7d 100644
--- a/src/Gargantext/Core/NodeStory.hs
+++ b/src/Gargantext/Core/NodeStory.hs
@@ -346,7 +346,7 @@ getNodesIdWithType c nt = do
 -- nodeStorySelect :: Select NodeStoryRead
 -- nodeStorySelect = selectTable nodeStoryTable
 
--- TODO Check ordering, "first patch in the _a_history list is the most recent"
+-- NOTE "first patch in the _a_history list is the most recent"
 getNodeArchiveHistory :: PGS.Connection -> NodeId -> IO [NgramsStatePatch']
 getNodeArchiveHistory c nodeId = do
   as <- runPGSQuery c query (PGS.Only nodeId) :: IO [(TableNgrams.NgramsType, NgramsTerm, NgramsPatch)]
@@ -356,15 +356,16 @@ getNodeArchiveHistory c nodeId = do
     query = [sql| SELECT ngrams_type_id, terms, patch
                     FROM node_story_archive_history
                     JOIN ngrams ON ngrams.id = ngrams_id
-                    WHERE node_id = ? |]
+                    WHERE node_id = ?
+                    ORDER BY version DESC |]
 
 ngramsIdQuery :: PGS.Query
 ngramsIdQuery = [sql| SELECT id FROM ngrams WHERE terms = ? |]
 
 
-insertNodeArchiveHistory :: PGS.Connection -> NodeId -> [NgramsStatePatch'] -> IO ()
-insertNodeArchiveHistory _ _ [] = pure ()
-insertNodeArchiveHistory c nodeId (h:hs) = do
+insertNodeArchiveHistory :: PGS.Connection -> NodeId -> Version -> [NgramsStatePatch'] -> IO ()
+insertNodeArchiveHistory _ _ _ [] = pure ()
+insertNodeArchiveHistory c nodeId version (h:hs) = do
   let tuples = mconcat $ (\(nType, (NgramsTablePatch patch)) ->
                            (\(term, p) ->
                               (nodeId, nType, term, p)) <$> PM.toList patch) <$> PM.toList h :: [(NodeId, TableNgrams.NgramsType, NgramsTerm, NgramsPatch)]
@@ -372,13 +373,13 @@ insertNodeArchiveHistory c nodeId (h:hs) = do
                       ngrams <- runPGSQuery c ngramsIdQuery (PGS.Only term)
                       pure $ (\(PGS.Only termId) -> (nId, nType, termId, term, patch)) <$> (headMay ngrams)
                       ) tuples :: IO [Maybe (NodeId, TableNgrams.NgramsType, Int, NgramsTerm, NgramsPatch)]
-  _ <- runPGSExecuteMany c query $ ((\(nId, nType, termId, _term, patch) -> (nId, nType, termId, patch)) <$> (catMaybes tuplesM))
-  _ <- insertNodeArchiveHistory c nodeId hs
+  _ <- runPGSExecuteMany c query $ ((\(nId, nType, termId, _term, patch) -> (nId, nType, termId, patch, version)) <$> (catMaybes tuplesM))
+  _ <- insertNodeArchiveHistory c nodeId version hs
   pure ()
   where
 
     query :: PGS.Query
-    query = [sql| INSERT INTO node_story_archive_history(node_id, ngrams_type_id, ngrams_id, patch) VALUES (?, ?, ?, ?) |]
+    query = [sql| INSERT INTO node_story_archive_history(node_id, ngrams_type_id, ngrams_id, patch, version) VALUES (?, ?, ?, ?, ?) |]
 
 getNodeStory :: PGS.Connection -> NodeId -> IO NodeListStory
 getNodeStory c nId@(NodeId nodeId) = do
@@ -540,7 +541,7 @@ upsertNodeStories c nodeId@(NodeId nId) newArchive = do
     -- whether it's insert or update, we can insert node archive history already
     -- NOTE: It is assumed that the most recent change is the first in the
     -- list, so we save these in reverse order
-    insertNodeArchiveHistory c nodeId $ reverse $ newArchive ^. a_history
+    insertNodeArchiveHistory c nodeId (newArchive ^. a_version) $ reverse $ newArchive ^. a_history
 
     (NodeStory m) <- getNodeStory c nodeId
     case Map.lookup nodeId m of
-- 
2.21.0