55{-# LANGUAGE ScopedTypeVariables #-}
66
77module Simplex.Messaging.Agent.Store.Postgres
8- ( createDBStore ,
8+ ( DBOpts (.. ),
9+ createDBStore ,
910 closeDBStore ,
1011 reopenDBStore ,
1112 execSQL ,
@@ -14,47 +15,49 @@ where
1415
1516import Control.Exception (throwIO )
1617import Control.Monad (unless , void )
18+ import Data.ByteString (ByteString )
1719import Data.Functor (($>) )
1820import Data.String (fromString )
1921import Data.Text (Text )
20- import Database.PostgreSQL.Simple (ConnectInfo ( .. ), Only (.. ))
22+ import Database.PostgreSQL.Simple (Only (.. ))
2123import qualified Database.PostgreSQL.Simple as PSQL
2224import Database.PostgreSQL.Simple.SqlQQ (sql )
2325import Simplex.Messaging.Agent.Store.Migrations (migrateSchema )
2426import Simplex.Messaging.Agent.Store.Postgres.Common
2527import qualified Simplex.Messaging.Agent.Store.Postgres.DB as DB
26- import Simplex.Messaging.Agent.Store.Postgres.Util (createDBAndUserIfNotExists )
2728import Simplex.Messaging.Agent.Store.Shared (Migration (.. ), MigrationConfirmation (.. ), MigrationError (.. ))
2829import Simplex.Messaging.Util (ifM )
2930import UnliftIO.Exception (bracketOnError , onException )
3031import UnliftIO.MVar
3132import UnliftIO.STM
3233
33- -- | Create a new Postgres DBStore with the given connection info, schema name and migrations.
34- -- This function creates the user and/or database passed in connectInfo if they do not exist
35- -- (expects the default 'postgres' user and 'postgres' db to exist).
34+ data DBOpts = DBOpts
35+ { connstr :: ByteString ,
36+ schema :: String
37+ }
38+
39+ -- | Create a new Postgres DBStore with the given connection string, schema name and migrations.
3640-- If passed schema does not exist in connectInfo database, it will be created.
3741-- Applies necessary migrations to schema.
3842-- TODO [postgres] authentication / user password, db encryption (?)
39- createDBStore :: ConnectInfo -> String -> [Migration ] -> MigrationConfirmation -> IO (Either MigrationError DBStore )
40- createDBStore connectInfo schema migrations confirmMigrations = do
41- createDBAndUserIfNotExists connectInfo
42- st <- connectPostgresStore connectInfo schema
43+ createDBStore :: DBOpts -> [Migration ] -> MigrationConfirmation -> IO (Either MigrationError DBStore )
44+ createDBStore DBOpts {connstr, schema} migrations confirmMigrations = do
45+ st <- connectPostgresStore connstr schema
4346 r <- migrateSchema st migrations confirmMigrations True `onException` closeDBStore st
4447 case r of
4548 Right () -> pure $ Right st
4649 Left e -> closeDBStore st $> Left e
4750
48- connectPostgresStore :: ConnectInfo -> String -> IO DBStore
49- connectPostgresStore dbConnectInfo dbSchema = do
50- (dbConn, dbNew) <- connectDB dbConnectInfo dbSchema -- TODO [postgres] analogue for dbBusyLoop?
51+ connectPostgresStore :: ByteString -> String -> IO DBStore
52+ connectPostgresStore dbConnstr dbSchema = do
53+ (dbConn, dbNew) <- connectDB dbConnstr dbSchema -- TODO [postgres] analogue for dbBusyLoop?
5154 dbConnection <- newMVar dbConn
5255 dbClosed <- newTVarIO False
53- pure DBStore {dbConnectInfo , dbSchema, dbConnection, dbNew, dbClosed}
56+ pure DBStore {dbConnstr , dbSchema, dbConnection, dbNew, dbClosed}
5457
55- connectDB :: ConnectInfo -> String -> IO (DB. Connection , Bool )
56- connectDB dbConnectInfo schema = do
57- db <- PSQL. connect dbConnectInfo
58+ connectDB :: ByteString -> String -> IO (DB. Connection , Bool )
59+ connectDB connstr schema = do
60+ db <- PSQL. connectPostgreSQL connstr
5861 schemaExists <- prepare db `onException` PSQL. close db
5962 let dbNew = not schemaExists
6063 pure (db, dbNew)
@@ -84,12 +87,12 @@ closeDBStore st@DBStore {dbClosed} =
8487 atomically $ writeTVar dbClosed True
8588
8689openPostgresStore_ :: DBStore -> IO ()
87- openPostgresStore_ DBStore {dbConnectInfo , dbSchema, dbConnection, dbClosed} =
90+ openPostgresStore_ DBStore {dbConnstr , dbSchema, dbConnection, dbClosed} =
8891 bracketOnError
8992 (takeMVar dbConnection)
9093 (tryPutMVar dbConnection)
9194 $ \ _dbConn -> do
92- (dbConn, _dbNew) <- connectDB dbConnectInfo dbSchema
95+ (dbConn, _dbNew) <- connectDB dbConnstr dbSchema
9396 atomically $ writeTVar dbClosed False
9497 putMVar dbConnection dbConn
9598
0 commit comments