2019-11-26 15:14:21 +03:00
|
|
|
{-# LANGUAGE RecordWildCards #-}
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2019-11-26 15:14:21 +03:00
|
|
|
module Main where
|
2019-03-12 08:46:27 +03:00
|
|
|
|
2020-01-23 00:55:55 +03:00
|
|
|
import Data.Text.Conversions (convertText)
|
|
|
|
|
2019-11-26 15:14:21 +03:00
|
|
|
import Hasura.App
|
|
|
|
import Hasura.Logging (Hasura)
|
2018-06-27 16:11:32 +03:00
|
|
|
import Hasura.Prelude
|
|
|
|
import Hasura.RQL.DDL.Metadata (fetchMetadata)
|
2019-11-20 21:21:30 +03:00
|
|
|
import Hasura.RQL.DDL.Schema
|
2019-10-21 19:01:05 +03:00
|
|
|
import Hasura.RQL.Types
|
2018-06-27 16:11:32 +03:00
|
|
|
import Hasura.Server.Init
|
2019-11-26 15:14:21 +03:00
|
|
|
import Hasura.Server.Migrate (dropCatalog)
|
|
|
|
import Hasura.Server.Version
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2019-11-26 15:14:21 +03:00
|
|
|
import qualified Data.ByteString.Lazy as BL
|
|
|
|
import qualified Data.ByteString.Lazy.Char8 as BLC
|
|
|
|
import qualified Database.PG.Query as Q
|
2019-03-12 08:46:27 +03:00
|
|
|
|
2018-06-27 16:11:32 +03:00
|
|
|
main :: IO ()
|
2019-11-26 15:14:21 +03:00
|
|
|
main = parseArgs >>= unAppM . runApp
|
2018-07-27 12:34:50 +03:00
|
|
|
|
2019-11-26 15:14:21 +03:00
|
|
|
runApp :: HGEOptions Hasura -> AppM ()
|
|
|
|
runApp (HGEOptionsG rci hgeCmd) =
|
2020-01-23 00:55:55 +03:00
|
|
|
withVersion $$(getVersionFromEnvironment) case hgeCmd of
|
2019-11-26 15:14:21 +03:00
|
|
|
HCServe serveOptions -> do
|
|
|
|
(initCtx, initTime) <- initialiseCtx hgeCmd rci
|
|
|
|
runHGEServer serveOptions initCtx initTime
|
2018-12-19 14:38:33 +03:00
|
|
|
HCExport -> do
|
2019-11-26 15:14:21 +03:00
|
|
|
(initCtx, _) <- initialiseCtx hgeCmd rci
|
|
|
|
res <- runTx' initCtx fetchMetadata
|
2018-12-19 14:38:33 +03:00
|
|
|
either printErrJExit printJSON res
|
2019-01-02 14:24:17 +03:00
|
|
|
|
2018-12-19 14:38:33 +03:00
|
|
|
HCClean -> do
|
2019-11-26 15:14:21 +03:00
|
|
|
(initCtx, _) <- initialiseCtx hgeCmd rci
|
|
|
|
res <- runTx' initCtx dropCatalog
|
2018-12-19 14:38:33 +03:00
|
|
|
either printErrJExit (const cleanSuccess) res
|
2019-01-02 14:24:17 +03:00
|
|
|
|
2018-12-19 14:38:33 +03:00
|
|
|
HCExecute -> do
|
2019-11-26 15:14:21 +03:00
|
|
|
(InitCtx{..}, _) <- initialiseCtx hgeCmd rci
|
|
|
|
queryBs <- liftIO BL.getContents
|
2019-04-17 19:29:39 +03:00
|
|
|
let sqlGenCtx = SQLGenCtx False
|
2019-11-20 21:21:30 +03:00
|
|
|
res <- runAsAdmin _icPgPool sqlGenCtx _icHttpManager do
|
|
|
|
schemaCache <- buildRebuildableSchemaCache
|
|
|
|
execQuery queryBs
|
|
|
|
& runHasSystemDefinedT (SystemDefined False)
|
|
|
|
& runCacheRWT schemaCache
|
|
|
|
& fmap fst
|
2019-11-26 15:14:21 +03:00
|
|
|
either printErrJExit (liftIO . BLC.putStrLn) res
|
2019-01-28 16:55:28 +03:00
|
|
|
|
2020-01-23 00:55:55 +03:00
|
|
|
HCVersion -> liftIO $ putStrLn $ "Hasura GraphQL Engine: " ++ convertText currentVersion
|
2019-11-26 15:14:21 +03:00
|
|
|
where
|
|
|
|
runTx' initCtx tx =
|
|
|
|
liftIO $ runExceptT $ Q.runTx (_icPgPool initCtx) (Q.Serializable, Nothing) tx
|
2019-01-28 16:55:28 +03:00
|
|
|
|
2019-11-26 15:14:21 +03:00
|
|
|
cleanSuccess = liftIO $ putStrLn "successfully cleaned graphql-engine related data"
|