graphql-engine/server/src-lib/Hasura/Server/API/PGDump.hs
Samir Talwar 342391f39d Upgrade Ormolu to v0.5.
This upgrades the version of Ormolu required by the HGE repository to v0.5.0.1, and reformats all code accordingly.

Ormolu v0.5 reformats code that uses infix operators. This is mostly useful, adding newlines and indentation to make it clear which operators are applied first, but in some cases, it's unpleasant. To make this easier on the eyes, I had to do the following:

* Add a few fixity declarations (search for `infix`)
* Add parentheses to make precedence clear, allowing Ormolu to keep everything on one line
* Rename `relevantEq` to `(==~)` in #6651 and set it to `infix 4`
* Add a few _.ormolu_ files (thanks to @hallettj for helping me get started), mostly for Autodocodec operators that don't have explicit fixity declarations

In general, I think these changes are quite reasonable. They mostly affect indentation.

PR-URL: https://github.com/hasura/graphql-engine-mono/pull/6675
GitOrigin-RevId: cd47d87f1d089fb0bc9dcbbe7798dbceedcd7d83
2022-11-02 20:55:13 +00:00

100 lines
3.2 KiB
Haskell

-- | API related to Postgres' pg dump
module Hasura.Server.API.PGDump
( PGDumpReqBody (..),
execPGDump,
)
where
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 PG
import Hasura.Base.Error qualified as RTE
import Hasura.Prelude
import Hasura.RQL.Types.Common
import System.Exit
import System.Process
import Text.Regex.TDFA qualified as TDFA
data PGDumpReqBody = PGDumpReqBody
{ prbSource :: !SourceName,
prbOpts :: ![String],
prbCleanOutput :: !Bool
}
deriving (Show, Eq)
instance FromJSON PGDumpReqBody where
parseJSON = withObject "Object" $ \o ->
PGDumpReqBody
<$> o .:? "source" .!= defaultSource
<*> o .: "opts"
<*> o .:? "clean_output" .!= False
execPGDump ::
(MonadError RTE.QErr m, MonadIO m) =>
PGDumpReqBody ->
PG.ConnInfo ->
m BL.ByteString
execPGDump b ci = do
eOutput <- liftIO $ try execProcess
output <- onLeft eOutput throwException
onLeft output $ \err ->
RTE.throw500 $ "error while executing pg_dump: " <> err
where
throwException :: (MonadError RTE.QErr m) => IOException -> m a
throwException _ = RTE.throw500 "internal exception while executing pg_dump"
execProcess = do
(exitCode, stdOut, stdErr) <- readProcessWithExitCode "pg_dump" opts ""
return $ case exitCode of
ExitSuccess -> Right $ unUTF8 $ convertText (clean stdOut)
ExitFailure _ -> Left $ toText stdErr
connString = T.unpack $ bsToTxt $ PG.pgConnString $ PG.ciDetails ci
opts = connString : "--encoding=utf8" : prbOpts b
clean str
| prbCleanOutput b =
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
|| (eventTriggerRegex `TDFA.match` line)
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 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';"
]
eventTriggerRegex =
let regexStr :: String =
-- pg functions created by hasura for event triggers used "notify_hasura"
-- These changes are also documented on the method pgIdenTrigger
"^CREATE TRIGGER \"?notify_hasura_.+\"? AFTER [[:alnum:]]+ "
<> "ON .+ FOR EACH ROW EXECUTE (FUNCTION|PROCEDURE) "
<> "\"?hdb_catalog\"?\\.\"?notify_hasura_.+\"?\\(\\);$"
in TDFA.makeRegex regexStr :: TDFA.Regex