Commit 705f1e2d authored by Alexandre Delanoë's avatar Alexandre Delanoë

[DB] triggers ok

parent db0a62b6
Pipeline #1040 failed with stage
......@@ -15,6 +15,7 @@ Import a corpus binary.
module Main where
import Data.Text (Text)
import Data.Either (Either(..))
import Data.Maybe (Maybe(..))
import Gargantext.API.Admin.Settings (withDevEnv, runCmdDev)
......@@ -26,13 +27,16 @@ import Gargantext.Database.Query.Table.Node (getOrMkList)
import Gargantext.Database.Query.Table.User (insertUsersDemo)
import Gargantext.Database.Admin.Config (userMaster, corpusMasterName)
import Gargantext.Database.Admin.Types.Node
import Gargantext.Database.Admin.Trigger.Init (initTriggers)
import Gargantext.Database.Admin.Trigger.Init (initFirstTriggers, initLastTriggers)
import Gargantext.Database.Admin.Types.Hyperdata (HyperdataCorpus)
import Gargantext.Database.Admin.Types.Node (CorpusId, RootId, ListId)
import Gargantext.Database.Prelude (Cmd, )
import Gargantext.Prelude
import System.Environment (getArgs)
secret :: Text
secret = "Database secret to change"
main :: IO ()
main = do
[iniPath] <- getArgs
......@@ -50,12 +54,13 @@ main = do
initMaster = do
(masterUserId, masterRootId, masterCorpusId) <- getOrMk_RootWithCorpus (UserName userMaster) (Left corpusMasterName) (Nothing :: Maybe HyperdataCorpus)
masterListId <- getOrMkList masterCorpusId masterUserId
_triggers <- initTriggers masterListId
_triggers <- initLastTriggers masterListId
pure (masterUserId, masterRootId, masterCorpusId, masterListId)
withDevEnv iniPath $ \env -> do
_ <- runCmdDev env (initFirstTriggers secret :: Cmd GargError [Int64])
_ <- runCmdDev env createUsers
_ <- runCmdDev env mkRoots
x <- runCmdDev env initMaster
_ <- runCmdDev env mkRoots
putStrLn $ show x
pure ()
......@@ -192,6 +192,3 @@ CREATE OR REPLACE function node_pos(int, int) returns bigint
--drop index node_by_pos;
create index node_by_pos on nodes using btree(node_pos(id,typename));
......@@ -16,17 +16,22 @@ Ngrams by node enable contextual metrics.
module Gargantext.Database.Admin.Trigger.Init
where
import Data.Text (Text)
import Gargantext.Database.Admin.Trigger.NodeNodeNgrams (triggerCountInsert, triggerCountInsert2)
import Gargantext.Database.Admin.Trigger.Nodes (triggerSearchUpdate)
import Gargantext.Database.Admin.Trigger.Nodes (triggerSearchUpdate, triggerUpdateHash)
import Gargantext.Database.Admin.Trigger.NodesNodes (triggerDeleteCount, triggerInsertCount, triggerUpdateAdd, triggerUpdateDel, MasterListId) -- , triggerCoocInsert)
import Gargantext.Database.Prelude (Cmd)
import Gargantext.Prelude
------------------------------------------------------------------------
initTriggers :: MasterListId -> Cmd err [Int64]
initTriggers lId = do
t0 <- triggerSearchUpdate
initFirstTriggers :: Text -> Cmd err [Int64]
initFirstTriggers secret = do
t0 <- triggerUpdateHash secret
pure [t0]
initLastTriggers :: MasterListId -> Cmd err [Int64]
initLastTriggers lId = do
t0 <- triggerSearchUpdate
t1 <- triggerCountInsert
t1' <- triggerCountInsert2
-- t1'' <- triggerCoocInsert lId
......
......@@ -16,6 +16,7 @@ Triggers on Nodes table.
module Gargantext.Database.Admin.Trigger.Nodes
where
import Data.Text (Text)
import Database.PostgreSQL.Simple.SqlQQ (sql)
import qualified Database.PostgreSQL.Simple as DPS
......@@ -67,32 +68,52 @@ triggerSearchUpdate = execPGSQuery query ( nodeTypeId NodeDocument
|]
triggerUpdateHash :: Cmd err Int64
triggerUpdateHash = execPGSQuery query ( nodeTypeId NodeDocument
, nodeTypeId NodeContact
)
type Secret = Text
triggerUpdateHash :: Secret -> Cmd err Int64
triggerUpdateHash secret = execPGSQuery query ( nodeTypeId NodeDocument
, nodeTypeId NodeContact
, secret
, secret
, nodeTypeId NodeDocument
, nodeTypeId NodeContact
, secret
, secret
)
where
query :: DPS.Query
query = [sql|
CREATE OR REPLACE FUNCTION hash_insert_nodes()
RETURNS trigger AS $$
BEGIN
IF NEW.hash_id = ''
THEN
IF NEW.typename = ? OR NEW.typename = ?
THEN NEW.hash_id = digest(CONCAT(?, NEW.typename, NEW.name, NEW.parent_id, NEW.hyperdata), 'sha256');
ELSE NEW.hash_id = digest(CONCAT(?, NEW.typename, NEW.name, NEW.id, NEW.hyperdata), 'sha256');
END IF;
END IF;
RETURN NEW;
END
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION hash_update_nodes()
RETURNS trigger AS $$
BEGIN
IF tg_op = 'INSERT' OR tg_op = 'UPDATE' THEN
IF NEW.hash_id = ''
THEN
IF NEW.typename = ? OR NEW.typename = ?
THEN NEW.hash_id = digest(CONCAT(NEW.parent_id, NEW.hyperdata), 'sha256');
ELSE NEW.hash_id = digest(CONCAT(NEW.id, NEW.hyperdata), 'sha256');
END IF;
END IF;
RETURN NEW;
END IF;
IF NEW.typename = ? OR NEW.typename = ?
THEN NEW.hash_id = digest(CONCAT(?, NEW.typename, NEW.name, NEW.parent_id, NEW.hyperdata), 'sha256');
ELSE NEW.hash_id = digest(CONCAT(?, NEW.typename, NEW.name, NEW.id, NEW.hyperdata), 'sha256');
END IF;
RETURN NEW;
END
$$ LANGUAGE plpgsql;
CREATE TRIGGER some_table_hash_update
BEFORE INSERT OR UPDATE ON nodes FOR EACH ROW EXECUTE PROCEDURE hash_update_nodes();
CREATE TRIGGER nodes_hash_insert BEFORE INSERT ON nodes FOR EACH ROW EXECUTE PROCEDURE hash_insert_nodes();
CREATE TRIGGER nodes_hash_update BEFORE UPDATE ON nodes FOR EACH ROW EXECUTE PROCEDURE hash_update_nodes();
|]
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment