Files
simplex-chat/src/Simplex/Chat/Options/Postgres.hs
T
Evgeny f0467aee00 directory service: fix queries (#6539)
* fix directory service queries

* fix

* reduce postgres pool size to 1

* stabilize postgres client tests, remove slow handshake tests

* update simplexmq

* fix test

* test delay
2026-01-04 19:04:32 +00:00

96 lines
2.5 KiB
Haskell

{-# LANGUAGE ApplicativeDo #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE NamedFieldPuns #-}
module Simplex.Chat.Options.Postgres where
import qualified Data.ByteString.Char8 as B
import Foreign.C.String
import Options.Applicative
import Numeric.Natural (Natural)
import Simplex.Messaging.Agent.Store.Interface (DBOpts (..))
data ChatDbOpts = ChatDbOpts
{ dbConnstr :: String,
dbSchemaPrefix :: String,
dbPoolSize :: Natural,
dbCreateSchema :: Bool
}
chatDbOptsP :: FilePath -> String -> Parser ChatDbOpts
chatDbOptsP _appDir defaultDbName = do
dbConnstr <-
strOption
( long "database"
<> short 'd'
<> metavar "DB_CONN"
<> help "Database connection string"
<> value ("postgresql://simplex@/" <> defaultDbName)
<> showDefault
)
dbSchemaPrefix <-
strOption
( long "schema-prefix"
<> metavar "DB_SCHEMA_PREFIX"
<> help "Database schema prefix"
<> value "simplex_v1"
<> showDefault
)
dbPoolSize <-
option
auto
( long "pool-size"
<> metavar "DB_POOL_SIZE"
<> help "Database connection pool size"
<> value 1
<> showDefault
)
dbCreateSchema <-
switch
( long "create-schema"
<> help "Create database schema when it does not exist"
)
pure ChatDbOpts {dbConnstr, dbSchemaPrefix, dbPoolSize, dbCreateSchema}
migrationBackupPathP :: Parser (Maybe FilePath)
migrationBackupPathP = pure Nothing
dbString :: ChatDbOpts -> String
dbString ChatDbOpts {dbConnstr} = dbConnstr
toDBOpts :: ChatDbOpts -> String -> Bool -> [()] -> DBOpts
toDBOpts ChatDbOpts {dbConnstr, dbSchemaPrefix, dbPoolSize, dbCreateSchema} dbSuffix _keepKey _dbFunctions =
DBOpts
{ connstr = B.pack dbConnstr,
schema = B.pack $ if null dbSchemaPrefix then "simplex_v1" <> dbSuffix else dbSchemaPrefix <> dbSuffix,
poolSize = dbPoolSize,
createSchema = dbCreateSchema
}
chatSuffix :: String
chatSuffix = "_chat_schema"
agentSuffix :: String
agentSuffix = "_agent_schema"
chatDBFunctions :: [()]
chatDBFunctions = []
mobileDbOpts :: CString -> CString -> IO ChatDbOpts
mobileDbOpts schemaPrefix connstr = do
dbSchemaPrefix <- peekCString schemaPrefix
dbConnstr <- peekCString connstr
pure $
ChatDbOpts
{ dbConnstr,
dbSchemaPrefix,
dbPoolSize = 1,
dbCreateSchema = True
}
removeDbKey :: ChatDbOpts -> ChatDbOpts
removeDbKey = id
errorDbStr :: DBOpts -> String
errorDbStr DBOpts {schema} = B.unpack schema