Commit cfbd39f8 authored by Alexandre Delanoë's avatar Alexandre Delanoë

[MIGRATION] upgrade script (WIP)

parent e724b810
...@@ -15,27 +15,24 @@ Import a corpus binary. ...@@ -15,27 +15,24 @@ Import a corpus binary.
module Main where module Main where
import Data.Text (Text)
import Data.Either (Either(..)) import Data.Either (Either(..))
import Gargantext.API.Dev (withDevEnv, runCmdDev) import Gargantext.API.Dev (withDevEnv, runCmdDev)
import Gargantext.API.Prelude (GargError)
import Gargantext.API.Node () -- instances only import Gargantext.API.Node () -- instances only
import Gargantext.API.Prelude (GargError)
import Gargantext.Core.Types.Individu (User(..), arbitraryNewUsers, NewUser(..), arbitraryUsername, GargPassword(..)) import Gargantext.Core.Types.Individu (User(..), arbitraryNewUsers, NewUser(..), arbitraryUsername, GargPassword(..))
import Gargantext.Database.Action.Flow (getOrMkRoot, getOrMk_RootWithCorpus) import Gargantext.Database.Action.Flow (getOrMkRoot, getOrMk_RootWithCorpus)
import Gargantext.Database.Query.Table.Node (getOrMkList)
import Gargantext.Database.Query.Table.User (insertNewUsers, )
import Gargantext.Database.Admin.Config (userMaster, corpusMasterName) import Gargantext.Database.Admin.Config (userMaster, corpusMasterName)
import Gargantext.Database.Admin.Types.Node
import Gargantext.Database.Admin.Trigger.Init (initFirstTriggers, initLastTriggers) import Gargantext.Database.Admin.Trigger.Init (initFirstTriggers, initLastTriggers)
import Gargantext.Database.Admin.Types.Hyperdata (HyperdataCorpus) import Gargantext.Database.Admin.Types.Hyperdata (HyperdataCorpus)
import Gargantext.Database.Admin.Types.Node
import Gargantext.Database.Prelude (Cmd, ) import Gargantext.Database.Prelude (Cmd, )
import Gargantext.Database.Query.Table.Node (getOrMkList)
import Gargantext.Database.Query.Table.User (insertNewUsers, )
import Gargantext.Prelude import Gargantext.Prelude
import System.Environment (getArgs) import Gargantext.Prelude.Config (GargConfig(..), readConfig)
import Prelude (getLine) import Prelude (getLine)
import System.Environment (getArgs)
-- TODO put this in gargantext.ini
secret :: Text
secret = "Database secret to change"
main :: IO () main :: IO ()
main = do main = do
...@@ -51,6 +48,8 @@ main = do ...@@ -51,6 +48,8 @@ main = do
putStrLn "Enter master user (gargantua) _email_ :" putStrLn "Enter master user (gargantua) _email_ :"
email <- getLine email <- getLine
cfg <- readConfig iniPath
let secret = _gc_secretkey cfg
let createUsers :: Cmd GargError Int64 let createUsers :: Cmd GargError Int64
createUsers = insertNewUsers (NewUser "gargantua" (cs email) (GargPassword $ cs password) createUsers = insertNewUsers (NewUser "gargantua" (cs email) (GargPassword $ cs password)
......
...@@ -12,44 +12,85 @@ Import a corpus binary. ...@@ -12,44 +12,85 @@ Import a corpus binary.
-} -}
{-# LANGUAGE Strict #-} {-# LANGUAGE Strict #-}
{-# LANGUAGE QuasiQuotes #-}
module Main where module Main where
import Data.Text (Text)
import Gargantext.API.Admin.EnvTypes (DevEnv) import Gargantext.API.Admin.EnvTypes (DevEnv)
import Gargantext.API.Dev (withDevEnv, runCmdDev) import Gargantext.API.Dev (withDevEnv, runCmdDev)
import Gargantext.API.Prelude (GargError) import Gargantext.API.Prelude (GargError)
import Gargantext.API.Node () -- instances only import Gargantext.API.Node () -- instances only
import Gargantext.API.Ngrams.Tools (getRepo) -- import Gargantext.API.Ngrams.Tools (getRepo)
import Gargantext.Database.Query.Table.Node (getOrMkList)
import Data.Either (Either(..))
import Gargantext.Database.Admin.Types.Hyperdata (HyperdataCorpus)
import Gargantext.Database.Prelude (Cmd'', ) import Gargantext.Database.Prelude (Cmd'', )
import Gargantext.Database.Action.Flow (getOrMkRoot, getOrMk_RootWithCorpus)
import Gargantext.Core.NodeStory import Gargantext.Core.NodeStory
import Gargantext.Core.Types.Individu (User(..), arbitraryNewUsers, NewUser(..), arbitraryUsername, GargPassword(..))
import Gargantext.Prelude import Gargantext.Prelude
import Gargantext.Prelude.Config (GargConfig(..), readConfig) import Gargantext.Prelude.Config (GargConfig(..), readConfig)
import System.Environment (getArgs) import System.Environment (getArgs)
import Prelude (getLine) import Prelude (getLine)
import GHC.IO.Exception (IOException) import GHC.IO.Exception (IOException)
import Database.PostgreSQL.Simple.SqlQQ (sql)
import Gargantext.Database.Prelude (Cmd, execPGSQuery)
import Gargantext.Database.Admin.Config (userMaster, corpusMasterName)
import Gargantext.Database.Admin.Trigger.Init
import Gargantext.Prelude
import Gargantext.Prelude.Config (GargConfig(..), readConfig)
main :: IO () main :: IO ()
main = do main = do
putStrLn "Manual method:" putStrLn "Manual method:"
putStrLn "Upgrade your GarganText instance with the script:" putStrLn "Upgrade your GarganText instance with the script:"
putStrLn "Then press enter key to launch upgrade." putStrLn "./bin/psql gargantext.ini < devops/postgres/upgrade/0.0.5/schema.sql"
putStrLn "Then press enter key when you are done."
_ok <- getLine _ok <- getLine
[iniPath] <- getArgs [iniPath] <- getArgs
cfg <- readConfig iniPath cfg <- readConfig iniPath
let secret = _gc_secretkey cfg
let
sqlUpdateTriggerHash :: Cmd'' DevEnv IOException Int64
sqlUpdateTriggerHash = do
execPGSQuery query ()
where
query = [sql|
UPDATE nodes SET typename = typename;
|]
let
sqlNodes2Context :: Cmd'' DevEnv IOException Int64
sqlNodes2Context = do
execPGSQuery query ()
where
query = [sql|
UPDATE nodes SET typename = typename;
|]
let let
-- upgrade :: Cmd'' DevEnv GargError () contextsTriggers :: Cmd GargError ()
upgrade :: Cmd'' DevEnv IOException () contextsTriggers = do
upgrade = do (masterUserId, _masterRootId, masterCorpusId)
let repo_filepath = _gc_repofilepath cfg <- getOrMk_RootWithCorpus (UserName userMaster)
repo <- getRepo (Left corpusMasterName)
_ <- liftBase $ repoMigration repo_filepath repo (Nothing :: Maybe HyperdataCorpus)
masterListId <- getOrMkList masterCorpusId masterUserId
_triggers <- initLastTriggers masterListId
pure () pure ()
withDevEnv iniPath $ \env -> do withDevEnv iniPath $ \env -> do
_ <- runCmdDev env upgrade _ <- runCmdDev env (initFirstTriggers secret :: Cmd GargError [Int64])
_ <- runCmdDev env (contextsTriggers :: Cmd GargError ())
_ <- runCmdDev env sqlUpdateTriggerHash
putStrLn "Uprade done with success" putStrLn "Uprade done with success"
pure () pure ()
...@@ -13,7 +13,9 @@ USER="gargantua" ...@@ -13,7 +13,9 @@ USER="gargantua"
psql -c "DROP DATABASE IF EXISTS \"${DB}\"" psql -c "DROP DATABASE IF EXISTS \"${DB}\""
createdb "${DB}" createdb "${DB}"
psql "${DB}" < schema.sql #psql "${DB}" < schema.sql
../../bin/psql ../../gargantext.ini < gargandb.dump
psql -c "ALTER DATABASE \"${DB}\" OWNER to \"${USER}\"" psql -c "ALTER DATABASE \"${DB}\" OWNER to \"${USER}\""
......
...@@ -154,8 +154,6 @@ CREATE TABLE public.context_node_ngrams ( ...@@ -154,8 +154,6 @@ CREATE TABLE public.context_node_ngrams (
weight double precision, weight double precision,
PRIMARY KEY (context_id, node_id, ngrams_id, ngrams_type) PRIMARY KEY (context_id, node_id, ngrams_id, ngrams_type)
); );
ALTER TABLE public.context_node_ngrams OWNER TO gargantua; ALTER TABLE public.context_node_ngrams OWNER TO gargantua;
CREATE TABLE public.context_node_ngrams2 ( CREATE TABLE public.context_node_ngrams2 (
......
...@@ -73,5 +73,10 @@ CREATE INDEX ON public.context_node_ngrams2 USING btree (context_id); ...@@ -73,5 +73,10 @@ CREATE INDEX ON public.context_node_ngrams2 USING btree (context_id);
CREATE INDEX ON public.context_node_ngrams2 USING btree (nodengrams_id); CREATE INDEX ON public.context_node_ngrams2 USING btree (nodengrams_id);
CREATE INDEX ON public.context_node_ngrams2 USING btree (context_id, nodengrams_id); CREATE INDEX ON public.context_node_ngrams2 USING btree (context_id, nodengrams_id);
DROP TABLE if EXISTS public.node_nodengrams_nodengrams;
DROP TRIGGER if EXISTS trigger_count_delete2 ON nodes_nodes;
DROP TRIGGER if EXISTS trigger_count_update_add ON nodes_nodes;
DROP TRIGGER if EXISTS trigger_delete_count ON nodes_nodes;
DROP TRIGGER if EXISTS trigger_insert_count ON nodes_nodes;
DELETE TABLE public.node_nodengrams_nodengrams
...@@ -379,19 +379,20 @@ executables: ...@@ -379,19 +379,20 @@ executables:
- gargantext-prelude - gargantext-prelude
- base - base
# gargantext-upgrade: gargantext-upgrade:
# main: Main.hs main: Main.hs
# source-dirs: bin/gargantext-upgrade source-dirs: bin/gargantext-upgrade
# ghc-options: ghc-options:
# - -threaded - -threaded
# - -rtsopts - -rtsopts
# - -with-rtsopts=-N - -with-rtsopts=-N
# - -O2 - -O2
# - -Wmissing-signatures - -Wmissing-signatures
# dependencies: dependencies:
# - gargantext - gargantext
# - gargantext-prelude - gargantext-prelude
# - base - base
- postgresql-simple
gargantext-admin: gargantext-admin:
main: Main.hs main: Main.hs
......
...@@ -52,7 +52,7 @@ triggerCountInsert = execPGSQuery query (toDBid NodeDocument, toDBid NodeList) ...@@ -52,7 +52,7 @@ triggerCountInsert = execPGSQuery query (toDBid NodeDocument, toDBid NodeList)
END END
$$ LANGUAGE plpgsql; $$ LANGUAGE plpgsql;
-- DROP trigger trigger_count_insert on context_node_ngrams; DROP trigger IF EXISTS trigger_count_insert on context_node_ngrams;
CREATE TRIGGER trigger_count_insert AFTER INSERT on context_node_ngrams CREATE TRIGGER trigger_count_insert AFTER INSERT on context_node_ngrams
REFERENCING NEW TABLE AS NEW REFERENCING NEW TABLE AS NEW
...@@ -95,7 +95,7 @@ triggerCountInsert2 = execPGSQuery query ( toDBid NodeCorpus ...@@ -95,7 +95,7 @@ triggerCountInsert2 = execPGSQuery query ( toDBid NodeCorpus
END END
$$ LANGUAGE plpgsql; $$ LANGUAGE plpgsql;
-- DROP trigger trigger_count_insert2 on context_node_ngrams2; DROP trigger IF EXISTS trigger_count_insert2 on context_node_ngrams2;
CREATE TRIGGER trigger_count_insert2 AFTER INSERT on context_node_ngrams2 CREATE TRIGGER trigger_count_insert2 AFTER INSERT on context_node_ngrams2
REFERENCING NEW TABLE AS NEW REFERENCING NEW TABLE AS NEW
......
...@@ -13,7 +13,7 @@ Triggers on Nodes table. ...@@ -13,7 +13,7 @@ Triggers on Nodes table.
{-# LANGUAGE QuasiQuotes #-} {-# LANGUAGE QuasiQuotes #-}
module Gargantext.Database.Admin.Trigger.Nodes module Gargantext.Database.Admin.Trigger.Contexts
where where
import Data.Text (Text) import Data.Text (Text)
...@@ -33,7 +33,6 @@ triggerSearchUpdate = execPGSQuery query ( toDBid NodeDocument ...@@ -33,7 +33,6 @@ triggerSearchUpdate = execPGSQuery query ( toDBid NodeDocument
where where
query :: DPS.Query query :: DPS.Query
query = [sql| query = [sql|
-- DROP TRIGGER search_update_trigger on contexts;
CREATE OR REPLACE FUNCTION public.search_update() CREATE OR REPLACE FUNCTION public.search_update()
RETURNS trigger AS $$ RETURNS trigger AS $$
begin begin
...@@ -57,6 +56,7 @@ triggerSearchUpdate = execPGSQuery query ( toDBid NodeDocument ...@@ -57,6 +56,7 @@ triggerSearchUpdate = execPGSQuery query ( toDBid NodeDocument
ALTER FUNCTION public.search_update() OWNER TO gargantua; ALTER FUNCTION public.search_update() OWNER TO gargantua;
DROP TRIGGER IF EXISTS search_update_trigger on contexts;
CREATE TRIGGER search_update_trigger CREATE TRIGGER search_update_trigger
BEFORE INSERT OR UPDATE BEFORE INSERT OR UPDATE
ON contexts FOR EACH ROW ON contexts FOR EACH ROW
...@@ -109,13 +109,13 @@ triggerUpdateHash secret = execPGSQuery query ( toDBid NodeDocument ...@@ -109,13 +109,13 @@ triggerUpdateHash secret = execPGSQuery query ( toDBid NodeDocument
END END
$$ LANGUAGE plpgsql; $$ LANGUAGE plpgsql;
DROP TRIGGER IF EXISTS nodes_hash_insert ON nodes;
DROP TRIGGER IF EXISTS nodes_hash_update ON nodes;
CREATE TRIGGER nodes_hash_insert BEFORE INSERT ON nodes FOR EACH ROW EXECUTE PROCEDURE hash_insert_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(); CREATE TRIGGER nodes_hash_update BEFORE UPDATE ON nodes FOR EACH ROW EXECUTE PROCEDURE hash_update_nodes();
DROP TRIGGER IF EXISTS contexts_hash_insert ON contexts;
DROP TRIGGER IF EXISTS contexts_hash_update ON contexts;
CREATE TRIGGER contexts_hash_insert BEFORE INSERT ON contexts FOR EACH ROW EXECUTE PROCEDURE hash_insert_nodes(); CREATE TRIGGER contexts_hash_insert BEFORE INSERT ON contexts FOR EACH ROW EXECUTE PROCEDURE hash_insert_nodes();
CREATE TRIGGER contexts_hash_update BEFORE UPDATE ON nodes FOR EACH ROW EXECUTE PROCEDURE hash_update_nodes(); CREATE TRIGGER contexts_hash_update BEFORE UPDATE ON contexts FOR EACH ROW EXECUTE PROCEDURE hash_update_nodes();
|] |]
...@@ -18,7 +18,7 @@ module Gargantext.Database.Admin.Trigger.Init ...@@ -18,7 +18,7 @@ module Gargantext.Database.Admin.Trigger.Init
import Data.Text (Text) import Data.Text (Text)
import Gargantext.Database.Admin.Trigger.ContextNodeNgrams (triggerCountInsert, triggerCountInsert2) import Gargantext.Database.Admin.Trigger.ContextNodeNgrams (triggerCountInsert, triggerCountInsert2)
import Gargantext.Database.Admin.Trigger.Nodes (triggerSearchUpdate, triggerUpdateHash) import Gargantext.Database.Admin.Trigger.Contexts (triggerSearchUpdate, triggerUpdateHash)
import Gargantext.Database.Admin.Trigger.NodesContexts ({-triggerDeleteCount,-} triggerInsertCount, triggerUpdateAdd, triggerUpdateDel, MasterListId) -- , triggerCoocInsert) import Gargantext.Database.Admin.Trigger.NodesContexts ({-triggerDeleteCount,-} triggerInsertCount, triggerUpdateAdd, triggerUpdateDel, MasterListId) -- , triggerCoocInsert)
import Gargantext.Database.Prelude (Cmd) import Gargantext.Database.Prelude (Cmd)
import Gargantext.Prelude import Gargantext.Prelude
......
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