2 Module : Gargantext.Database.Admin.Trigger.Nodes
3 Description : Triggers configuration
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
10 Triggers on Nodes table.
14 {-# LANGUAGE QuasiQuotes #-}
16 module Gargantext.Database.Admin.Trigger.Nodes
19 import Data.Text (Text)
20 import Database.PostgreSQL.Simple.SqlQQ (sql)
21 import Gargantext.Core (HasDBid(..))
22 import Gargantext.Database.Admin.Types.Node -- (ListId, CorpusId, NodeId)
23 import Gargantext.Database.Prelude (Cmd, execPGSQuery)
24 import Gargantext.Prelude
25 import qualified Database.PostgreSQL.Simple as DPS
28 triggerSearchUpdate :: HasDBid NodeType => Cmd err Int64
29 triggerSearchUpdate = execPGSQuery query ( toDBid NodeDocument
36 -- DROP TRIGGER search_update_trigger on nodes;
37 CREATE OR REPLACE FUNCTION public.search_update()
40 IF new.typename = ? AND new.hyperdata @> '{"language_iso2":"EN"}' THEN
41 new.search := to_tsvector( 'english' , (new.hyperdata ->> 'title') || ' ' || (new.hyperdata ->> 'abstract'));
43 ELSIF new.typename = ? AND new.hyperdata @> '{"language_iso2":"FR"}' THEN
44 new.search := to_tsvector( 'french' , (new.hyperdata ->> 'title') || ' ' || (new.hyperdata ->> 'abstract'));
46 ELSIF new.typename = ? THEN
47 new.search := to_tsvector( 'french' , (new.hyperdata ->> 'prenom')
48 || ' ' || (new.hyperdata ->> 'nom')
49 || ' ' || (new.hyperdata ->> 'fonction')
52 new.search := to_tsvector( 'english' , (new.hyperdata ->> 'title') || ' ' || (new.hyperdata ->> 'abstract'));
58 ALTER FUNCTION public.search_update() OWNER TO gargantua;
60 CREATE TRIGGER search_update_trigger
61 BEFORE INSERT OR UPDATE
63 EXECUTE PROCEDURE search_update();
65 -- Initialize index with already existing data
66 UPDATE nodes SET hyperdata = hyperdata;
72 triggerUpdateHash :: HasDBid NodeType => Secret -> Cmd err Int64
73 triggerUpdateHash secret = execPGSQuery query ( toDBid NodeDocument
86 CREATE OR REPLACE FUNCTION hash_insert_nodes()
91 IF NEW.typename = ? OR NEW.typename = ?
92 THEN NEW.hash_id = digest(CONCAT(?, NEW.typename, NEW.name, NEW.parent_id, NEW.hyperdata), 'sha256');
93 ELSE NEW.hash_id = digest(CONCAT(?, NEW.typename, NEW.name, NEW.id, NEW.hyperdata), 'sha256');
101 CREATE OR REPLACE FUNCTION hash_update_nodes()
102 RETURNS trigger AS $$
104 IF NEW.typename = ? OR NEW.typename = ?
105 THEN NEW.hash_id = digest(CONCAT(?, NEW.typename, NEW.name, NEW.parent_id, NEW.hyperdata), 'sha256');
106 ELSE NEW.hash_id = digest(CONCAT(?, NEW.typename, NEW.name, NEW.id, NEW.hyperdata), 'sha256');
113 CREATE TRIGGER nodes_hash_insert BEFORE INSERT ON nodes FOR EACH ROW EXECUTE PROCEDURE hash_insert_nodes();
114 CREATE TRIGGER nodes_hash_update BEFORE UPDATE ON nodes FOR EACH ROW EXECUTE PROCEDURE hash_update_nodes();