Skip to content

Commit

Permalink
Add basic access to the PostgREST version through SQL
Browse files Browse the repository at this point in the history
  • Loading branch information
laurenceisla committed Apr 6, 2023
1 parent 16f2849 commit 666da0f
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/PostgREST/AppState.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@ import Data.IORef (IORef, atomicWriteIORef, newIORef,
import Data.Time (ZonedTime, defaultTimeLocale, formatTime,
getZonedTime)
import Data.Time.Clock (UTCTime, getCurrentTime)
import Network.URI (parseURI, uriQuery)

import PostgREST.Config (AppConfig (..))
import PostgREST.Config.PgVersion (PgVersion (..), minimumPgVersion)
import PostgREST.SchemaCache (SchemaCache)
import PostgREST.Version (prettyVersion)

import Protolude

Expand Down Expand Up @@ -111,10 +113,17 @@ destroy = destroyPool

initPool :: AppConfig -> IO SQL.Pool
initPool AppConfig{..} =
SQL.acquire configDbPoolSize timeoutMicroseconds $ toUtf8 configDbUri
SQL.acquire configDbPoolSize timeoutMicroseconds configDbUriAndAppName
where
timeoutMicroseconds = (* oneSecond) <$> configDbPoolAcquisitionTimeout
oneSecond = 1000000
configDbUriAndAppName = toUtf8 configDbUri <>
case uriQuery <$> parseURI (toS configDbUri) of
Nothing -> " " <> appNameParam
Just "" -> "?" <> appNameParam
Just "?" -> appNameParam
_ -> "&" <> appNameParam
appNameParam = "fallback_application_name='PostgREST " <> prettyVersion <> "'"

-- | Run an action with a database connection.
usePool :: AppState -> SQL.Session a -> IO (Either SQL.UsageError a)
Expand Down

0 comments on commit 666da0f

Please sign in to comment.