core: Mobile.hs postgres interface (#5545)

* core: Mobile.hs postgres interface

* sqlite

* fix

* errors

* postgres

* rename

* rename, refactor

* merge files

* rename

* update simplexmq

---------

Co-authored-by: Evgeny Poberezkin <evgeny@poberezkin.com>
This commit is contained in:
spaced4ndyandEvgeny Poberezkin authored and GitHub committed 2025-01-20 17:41:48 +04:00
1 parent 0e940719c1
commit 20fa30eacc
23 files changed
+177 -182

No files matched your search

+45 -14
View File
@@ -1,37 +1,68 @@
{-# 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 Simplex.Messaging.Agent.Store.Interface (DBOpts (..))
data ChatDbOpts = ChatDbOpts
{ dbName :: String,
dbUser :: String,
{ dbConnstr :: String,
dbSchemaPrefix :: String
}
chatDbOptsP :: FilePath -> String -> Parser ChatDbOpts
chatDbOptsP _appDir defaultDbName = do
dbName <-
dbConnstr <-
strOption
( long "database"
<> short 'd'
<> metavar "DB_NAME"
<> help "Database name"
<> value defaultDbName
<> metavar "DB_CONN"
<> help "Database connection string"
<> value ("postgresql://simplex@/" <> defaultDbName)
<> showDefault
)
dbUser <-
dbSchemaPrefix <-
strOption
( long "database-user"
<> short 'u'
<> metavar "DB_USER"
<> help "Database user"
<> value "simplex"
( long "schema-prefix"
<> metavar "DB_SCHEMA_PREFIX"
<> help "Database schema prefix"
<> value "simplex_v1"
<> showDefault
)
pure ChatDbOpts {dbName, dbUser, dbSchemaPrefix = ""}
pure ChatDbOpts {dbConnstr, dbSchemaPrefix}
dbString :: ChatDbOpts -> String
dbString ChatDbOpts {dbName} = dbName
dbString ChatDbOpts {dbConnstr} = dbConnstr
toDBOpts :: ChatDbOpts -> String -> Bool -> DBOpts
toDBOpts ChatDbOpts {dbConnstr, dbSchemaPrefix} dbSuffix _keepKey =
DBOpts
{ connstr = B.pack dbConnstr,
schema = if null dbSchemaPrefix then "simplex_v1" <> dbSuffix else dbSchemaPrefix <> dbSuffix
}
chatSuffix :: String
chatSuffix = "_chat_schema"
agentSuffix :: String
agentSuffix = "_agent_schema"
mobileDbOpts :: CString -> CString -> IO ChatDbOpts
mobileDbOpts schemaPrefix connstr = do
dbSchemaPrefix <- peekCString schemaPrefix
dbConnstr <- peekCString connstr
pure $
ChatDbOpts
{ dbConnstr,
dbSchemaPrefix
}
removeDbKey :: ChatDbOpts -> ChatDbOpts
removeDbKey = id
errorDbStr :: DBOpts -> String
errorDbStr DBOpts {schema} = schema