jfischoff / tmp-postgres

Create temporary postgres instances
BSD 3-Clause "New" or "Revised" License
53 stars 18 forks source link

withConfig fails when passed defaultOptions #260

Open kozross opened 4 years ago

kozross commented 4 years ago

Given the following source code:

module Main (main) where

import           Control.Exception.Safe             (bracket)
import           Control.Monad                      (void)
import           Database.Postgres.Temp             (Config (connectionOptions),
                                                     defaultConfig,
                                                     toConnectionString,
                                                     withConfig)
import           Database.PostgreSQL.Simple         (Connection, close,
                                                     connectPostgreSQL,
                                                     execute_)
import           Database.PostgreSQL.Simple.Options (defaultOptions)

main :: IO ()
main = do
  putStrLn "Initializing temporary database"
  res <- withConfig (defaultConfig { connectionOptions = defaultOptions }) $ \db -> do
            bracket (connectPostgreSQL . toConnectionString $ db)
                    close
                    setUpDb
  case res of
    Left e   -> print ("Init failed: " <> show e)
    Right () -> putStrLn "Done"

setUpDb :: Connection -> IO ()
setUpDb conn =
  void . execute_ conn $ "create table test_table (name text primary key, number_of_pets int not null);"

I get a ConnectionTimeout when I try to run. This does not occur when I use with. Am I doing something wrong?

jfischoff commented 4 years ago

Try using the verboseConfig and see what output you get.

kozross commented 4 years ago

@jfischoff Key issue seems to be this:

2020-06-18 22:53:47 GMT [32146]: LOG:  listening on IPv4 address "127.0.0.1", port 59961

It appears that any suggestions about what port should be used are not taken from connectionOptions. Where would I set these?

jfischoff commented 4 years ago

Oh I see now. Yeah you have to set the port to.

withConfig (defaultConfig { connectionOptions = defaultOptions , port = pure $ Just 5432})
jfischoff commented 4 years ago

Is there a reason you a specific connectionOptions? I just wonder if you need to at all.

kozross commented 4 years ago

It's required for testing a database interface library at work. I have to specify a port to connect on, so a random one makes this difficult.

jfischoff commented 4 years ago

ah gotcha so you can just set the port in that case.

withConfig (defaultConfig { port = pure $ Just 5432})
jfischoff commented 4 years ago

The whole situation kinda sucks. There are million other connection options and I need a way to set all of the but port which is special because it is usually generated. I don't know. I might have just made a mistake in the design and maybe port should be set through the connection options and not be it's own thing.