2020-04-24 10:55:51 +03:00
|
|
|
-- | API related to Postgres' pg dump
|
|
|
|
module Hasura.Server.API.PGDump
|
2019-10-21 19:01:05 +03:00
|
|
|
( PGDumpReqBody(..)
|
2019-04-30 11:34:08 +03:00
|
|
|
, execPGDump
|
|
|
|
) where
|
|
|
|
|
2020-04-24 10:55:51 +03:00
|
|
|
import Control.Exception (IOException, try)
|
2020-12-28 15:56:00 +03:00
|
|
|
import Data.Aeson
|
2019-04-30 11:34:08 +03:00
|
|
|
import Data.Aeson.TH
|
2020-11-12 12:25:48 +03:00
|
|
|
import Data.Char (isSpace)
|
2020-04-03 03:00:13 +03:00
|
|
|
import Data.Text.Conversions
|
2019-04-30 11:34:08 +03:00
|
|
|
import Hasura.Prelude
|
2020-12-28 15:56:00 +03:00
|
|
|
import Hasura.RQL.Types (SourceName, defaultSource)
|
2019-04-30 11:34:08 +03:00
|
|
|
import System.Exit
|
|
|
|
import System.Process
|
2020-12-28 15:56:00 +03:00
|
|
|
|
|
|
|
import qualified Data.ByteString.Lazy as BL
|
|
|
|
import qualified Data.List as L
|
|
|
|
import qualified Data.Text as T
|
|
|
|
import qualified Database.PG.Query as Q
|
|
|
|
import qualified Hasura.RQL.Types.Error as RTE
|
2020-04-24 10:55:51 +03:00
|
|
|
import qualified Text.Regex.TDFA as TDFA
|
2019-04-30 11:34:08 +03:00
|
|
|
|
|
|
|
data PGDumpReqBody =
|
|
|
|
PGDumpReqBody
|
2020-12-28 15:56:00 +03:00
|
|
|
{ prbSource :: !SourceName
|
|
|
|
, prbOpts :: ![String]
|
|
|
|
, prbCleanOutput :: !Bool
|
2019-04-30 11:34:08 +03:00
|
|
|
} deriving (Show, Eq)
|
|
|
|
|
2021-01-19 22:14:42 +03:00
|
|
|
$(deriveToJSON hasuraJSON ''PGDumpReqBody)
|
2020-12-28 15:56:00 +03:00
|
|
|
|
|
|
|
instance FromJSON PGDumpReqBody where
|
|
|
|
parseJSON = withObject "Object" $ \o ->
|
|
|
|
PGDumpReqBody
|
|
|
|
<$> o .:? "source" .!= defaultSource
|
|
|
|
<*> o .: "opts"
|
|
|
|
<*> o .:? "clean_output" .!= False
|
2019-04-30 11:34:08 +03:00
|
|
|
|
|
|
|
execPGDump
|
|
|
|
:: (MonadError RTE.QErr m, MonadIO m)
|
|
|
|
=> PGDumpReqBody
|
|
|
|
-> Q.ConnInfo
|
|
|
|
-> m BL.ByteString
|
|
|
|
execPGDump b ci = do
|
2019-07-03 11:37:13 +03:00
|
|
|
eOutput <- liftIO $ try execProcess
|
2020-10-28 19:40:33 +03:00
|
|
|
output <- onLeft eOutput throwException
|
2020-12-28 15:56:00 +03:00
|
|
|
onLeft output $ \err ->
|
|
|
|
RTE.throw500 $ "error while executing pg_dump: " <> err
|
2019-04-30 11:34:08 +03:00
|
|
|
where
|
|
|
|
throwException :: (MonadError RTE.QErr m) => IOException -> m a
|
|
|
|
throwException _ = RTE.throw500 "internal exception while executing pg_dump"
|
|
|
|
|
2019-07-03 11:37:13 +03:00
|
|
|
execProcess = do
|
|
|
|
(exitCode, stdOut, stdErr) <- readProcessWithExitCode "pg_dump" opts ""
|
|
|
|
return $ case exitCode of
|
2020-04-03 03:00:13 +03:00
|
|
|
ExitSuccess -> Right $ unUTF8 $ convertText (clean stdOut)
|
|
|
|
ExitFailure _ -> Left $ toText stdErr
|
2019-07-03 11:37:13 +03:00
|
|
|
|
2019-09-06 01:59:26 +03:00
|
|
|
connString = T.unpack $ bsToTxt $ Q.pgConnString $ Q.ciDetails ci
|
|
|
|
opts = connString : "--encoding=utf8" : prbOpts b
|
2019-07-03 11:37:13 +03:00
|
|
|
|
|
|
|
clean str
|
2020-12-28 15:56:00 +03:00
|
|
|
| prbCleanOutput b =
|
2019-07-03 11:37:13 +03:00
|
|
|
unlines $ filter (not . shouldDropLine) (lines str)
|
|
|
|
| otherwise = str
|
|
|
|
|
|
|
|
shouldDropLine line =
|
|
|
|
-- delete empty lines
|
|
|
|
all isSpace line
|
|
|
|
-- delete comments
|
|
|
|
|| "--" `L.isPrefixOf` line
|
|
|
|
-- delete front matter
|
|
|
|
|| line `elem` preambleLines
|
|
|
|
-- delete notify triggers
|
2020-10-06 18:22:09 +03:00
|
|
|
|| eventTriggerRegex `TDFA.match` line
|
2019-07-03 11:37:13 +03:00
|
|
|
|
|
|
|
preambleLines =
|
|
|
|
[ "SET statement_timeout = 0;"
|
|
|
|
, "SET lock_timeout = 0;"
|
|
|
|
, "SET idle_in_transaction_session_timeout = 0;"
|
|
|
|
, "SET client_encoding = 'UTF8';"
|
|
|
|
, "SET standard_conforming_strings = on;"
|
|
|
|
, "SELECT pg_catalog.set_config('search_path', '', false);"
|
|
|
|
, "SET check_function_bodies = false;"
|
|
|
|
, "SET xmloption = content;"
|
|
|
|
, "SET client_min_messages = warning;"
|
|
|
|
, "SET row_security = off;"
|
|
|
|
, "SET default_tablespace = '';"
|
|
|
|
, "SET default_with_oids = false;"
|
2019-10-26 08:39:57 +03:00
|
|
|
, "SET default_table_access_method = heap;"
|
2019-07-03 11:37:13 +03:00
|
|
|
, "CREATE SCHEMA public;"
|
|
|
|
, "COMMENT ON SCHEMA public IS 'standard public schema';"
|
|
|
|
]
|
|
|
|
|
2020-10-06 18:22:09 +03:00
|
|
|
eventTriggerRegex =
|
2019-07-03 11:37:13 +03:00
|
|
|
let regexStr :: String =
|
2020-10-06 18:22:09 +03:00
|
|
|
-- pg functions created by hasura for event triggers used "notify_hasura"
|
|
|
|
-- These changes are also documented on the method pgIdenTrigger
|
2019-07-03 11:37:13 +03:00
|
|
|
"^CREATE TRIGGER \"?notify_hasura_.+\"? AFTER [[:alnum:]]+ "
|
2019-10-26 08:39:57 +03:00
|
|
|
<> "ON .+ FOR EACH ROW EXECUTE (FUNCTION|PROCEDURE) "
|
2020-11-12 12:25:48 +03:00
|
|
|
<> "\"?hdb_catalog\"?\\.\"?notify_hasura_.+\"?\\(\\);$"
|
2019-07-03 11:37:13 +03:00
|
|
|
in TDFA.makeRegex regexStr :: TDFA.Regex
|