2020-04-24 10:55:51 +03:00
|
|
|
-- | API related to Postgres' pg dump
|
|
|
|
module Hasura.Server.API.PGDump
|
2021-09-24 01:56:37 +03:00
|
|
|
( PGDumpReqBody (..),
|
|
|
|
execPGDump,
|
|
|
|
)
|
|
|
|
where
|
2019-04-30 11:34:08 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
import Control.Exception (IOException, try)
|
|
|
|
import Data.Aeson
|
|
|
|
import Data.ByteString.Lazy qualified as BL
|
|
|
|
import Data.Char (isSpace)
|
|
|
|
import Data.List qualified as L
|
|
|
|
import Data.Text qualified as T
|
|
|
|
import Data.Text.Conversions
|
|
|
|
import Database.PG.Query qualified as Q
|
|
|
|
import Hasura.Base.Error qualified as RTE
|
|
|
|
import Hasura.Prelude
|
|
|
|
import Hasura.RQL.Types (SourceName, defaultSource)
|
|
|
|
import System.Exit
|
|
|
|
import System.Process
|
|
|
|
import Text.Regex.TDFA qualified as TDFA
|
2020-12-28 15:56:00 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
data PGDumpReqBody = PGDumpReqBody
|
|
|
|
{ prbSource :: !SourceName,
|
|
|
|
prbOpts :: ![String],
|
|
|
|
prbCleanOutput :: !Bool
|
|
|
|
}
|
|
|
|
deriving (Show, Eq)
|
2019-04-30 11:34:08 +03:00
|
|
|
|
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
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
execPGDump ::
|
|
|
|
(MonadError RTE.QErr m, MonadIO m) =>
|
|
|
|
PGDumpReqBody ->
|
|
|
|
Q.ConnInfo ->
|
|
|
|
m BL.ByteString
|
2019-04-30 11:34:08 +03:00
|
|
|
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
|
2021-09-24 01:56:37 +03:00
|
|
|
ExitSuccess -> Right $ unUTF8 $ convertText (clean stdOut)
|
2020-04-03 03:00:13 +03:00
|
|
|
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 =
|
2021-09-24 01:56:37 +03:00
|
|
|
unlines $ filter (not . shouldDropLine) (lines str)
|
2019-07-03 11:37:13 +03:00
|
|
|
| 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 =
|
2021-09-24 01:56:37 +03:00
|
|
|
[ "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 xmloption = content;",
|
|
|
|
"SET client_min_messages = warning;",
|
|
|
|
"SET row_security = off;",
|
|
|
|
"SET default_tablespace = '';",
|
|
|
|
"SET default_with_oids = false;",
|
|
|
|
"SET default_table_access_method = heap;",
|
|
|
|
"CREATE SCHEMA public;",
|
|
|
|
"COMMENT ON SCHEMA public IS 'standard public schema';"
|
2019-07-03 11:37:13 +03:00
|
|
|
]
|
|
|
|
|
2020-10-06 18:22:09 +03:00
|
|
|
eventTriggerRegex =
|
2019-07-03 11:37:13 +03:00
|
|
|
let regexStr :: String =
|
2021-09-24 01:56:37 +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_.+\"?\\(\\);$"
|
2021-09-24 01:56:37 +03:00
|
|
|
in TDFA.makeRegex regexStr :: TDFA.Regex
|