graphql-engine/server/src-exec/Main.hs
Phil Freeman 94102c0460
Add downgrade command (close #1156) (#3760)
* Add downgrade command

* Add docs per @lexi-lambda's suggestions

* make tests pass

* Update hdb_version once, from Haskell

* more work based on feedback

* Improve the usage message

* Small docs changes

* Test downgrades exist for each tag

* Update downgrading.rst

* Use git-log to find tags which are ancestors of the current commit

Co-authored-by: Vamshi Surabhi <0x777@users.noreply.github.com>
2020-02-07 16:33:12 +05:30

65 lines
2.3 KiB
Haskell

{-# LANGUAGE RecordWildCards #-}
module Main where
import Data.Text.Conversions (convertText)
import Hasura.App
import Hasura.Logging (Hasura)
import Hasura.Prelude
import Hasura.RQL.DDL.Metadata (fetchMetadata)
import Hasura.RQL.DDL.Schema
import Hasura.RQL.Types
import Hasura.Server.Init
import Hasura.Server.Migrate (downgradeCatalog, dropCatalog)
import Hasura.Server.Version
import qualified Data.ByteString.Lazy as BL
import qualified Data.ByteString.Lazy.Char8 as BLC
import qualified Database.PG.Query as Q
main :: IO ()
main = parseArgs >>= unAppM . runApp
runApp :: HGEOptions Hasura -> AppM ()
runApp (HGEOptionsG rci hgeCmd) =
withVersion $$(getVersionFromEnvironment) case hgeCmd of
HCServe serveOptions -> do
(initCtx, initTime) <- initialiseCtx hgeCmd rci
runHGEServer serveOptions initCtx initTime
HCExport -> do
(initCtx, _) <- initialiseCtx hgeCmd rci
res <- runTx' initCtx fetchMetadata
either printErrJExit printJSON res
HCClean -> do
(initCtx, _) <- initialiseCtx hgeCmd rci
res <- runTx' initCtx dropCatalog
either printErrJExit (const cleanSuccess) res
HCExecute -> do
(InitCtx{..}, _) <- initialiseCtx hgeCmd rci
queryBs <- liftIO BL.getContents
let sqlGenCtx = SQLGenCtx False
res <- runAsAdmin _icPgPool sqlGenCtx _icHttpManager do
schemaCache <- buildRebuildableSchemaCache
execQuery queryBs
& runHasSystemDefinedT (SystemDefined False)
& runCacheRWT schemaCache
& fmap (\(res, _, _) -> res)
either printErrJExit (liftIO . BLC.putStrLn) res
HCDowngrade opts -> do
(InitCtx{..}, initTime) <- initialiseCtx hgeCmd rci
let sqlGenCtx = SQLGenCtx False
res <- downgradeCatalog opts initTime
& runAsAdmin _icPgPool sqlGenCtx _icHttpManager
either printErrJExit (liftIO . print) res
HCVersion -> liftIO $ putStrLn $ "Hasura GraphQL Engine: " ++ convertText currentVersion
where
runTx' initCtx tx =
liftIO $ runExceptT $ Q.runTx (_icPgPool initCtx) (Q.Serializable, Nothing) tx
cleanSuccess = liftIO $ putStrLn "successfully cleaned graphql-engine related data"