mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-19 21:41:44 +03:00
12335bce91
Dupe of https://github.com/hasura/graphql-engine-mono/pull/2872 with branch renamed so it doesn't break a tool. prev pr: https://github.com/hasura/graphql-engine-mono/pull/2921 PR-URL: https://github.com/hasura/graphql-engine-mono/pull/2922 Co-authored-by: Abby Sassel <3883855+sassela@users.noreply.github.com> GitOrigin-RevId: 24af6a814dd106864a2fd3cb985edfe9a8ef5d61
79 lines
2.1 KiB
Haskell
79 lines
2.1 KiB
Haskell
{-# OPTIONS -Wno-redundant-constraints #-}
|
|
|
|
-- | PostgreSQL helpers.
|
|
module Harness.Postgres
|
|
( livenessCheck,
|
|
run_,
|
|
runPersistent,
|
|
)
|
|
where
|
|
|
|
import Control.Concurrent
|
|
import Control.Exception
|
|
import Control.Monad.IO.Unlift
|
|
import Control.Monad.Logger
|
|
import Control.Monad.Reader
|
|
import Data.ByteString.Char8 qualified as S8
|
|
import Data.String
|
|
import Database.Persist.Postgresql qualified as Postgresql
|
|
import Database.Persist.Sql
|
|
import Database.PostgreSQL.LibPQ qualified as PQ
|
|
import Database.PostgreSQL.Simple qualified as Postgres
|
|
import GHC.Stack
|
|
import Harness.Constants as Constants
|
|
import System.Process.Typed
|
|
import Prelude
|
|
|
|
-- | Check the postgres server is live and ready to accept connections.
|
|
livenessCheck :: HasCallStack => IO ()
|
|
livenessCheck = loop Constants.postgresLivenessCheckAttempts
|
|
where
|
|
loop 0 = error ("Liveness check failed for PostgreSQL.")
|
|
loop attempts =
|
|
catch
|
|
( bracket
|
|
(PQ.connectdb (fromString Constants.postgresqlConnectionString))
|
|
PQ.finish
|
|
(const (pure ()))
|
|
)
|
|
( \(_failure :: ExitCodeException) -> do
|
|
threadDelay
|
|
Constants.postgresLivenessCheckIntervalMicroseconds
|
|
loop (attempts - 1)
|
|
)
|
|
|
|
-- | Run a plain SQL query. On error, print something useful for
|
|
-- debugging.
|
|
run_ :: HasCallStack => String -> IO ()
|
|
run_ q =
|
|
catch
|
|
( bracket
|
|
( Postgres.connectPostgreSQL
|
|
(fromString Constants.postgresqlConnectionString)
|
|
)
|
|
Postgres.close
|
|
(\conn -> void (Postgres.execute_ conn (fromString q)))
|
|
)
|
|
( \(e :: Postgres.SqlError) ->
|
|
error
|
|
( unlines
|
|
[ "PostgreSQL query error:",
|
|
S8.unpack (Postgres.sqlErrorMsg e),
|
|
"SQL was:",
|
|
q
|
|
]
|
|
)
|
|
)
|
|
|
|
-- | Run persistent for postgres.
|
|
runPersistent ::
|
|
(MonadUnliftIO m, HasCallStack) =>
|
|
ReaderT SqlBackend (NoLoggingT m) a ->
|
|
m a
|
|
runPersistent actions =
|
|
runNoLoggingT
|
|
( Postgresql.withPostgresqlConn
|
|
(fromString Constants.postgresqlConnectionString)
|
|
(runReaderT actions)
|
|
)
|