Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

withConfig fails when passed defaultOptions #260

Open
kozross opened this issue Jun 18, 2020 · 7 comments
Open

withConfig fails when passed defaultOptions #260

kozross opened this issue Jun 18, 2020 · 7 comments

Comments

@kozross
Copy link

kozross commented Jun 18, 2020

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
Copy link
Owner

Try using the verboseConfig and see what output you get.

@kozross
Copy link
Author

kozross commented Jun 18, 2020

@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
Copy link
Owner

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

withConfig (defaultConfig { connectionOptions = defaultOptions , port = pure $ Just 5432})

@jfischoff
Copy link
Owner

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

@kozross
Copy link
Author

kozross commented Jun 19, 2020

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
Copy link
Owner

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

withConfig (defaultConfig { port = pure $ Just 5432})

@jfischoff
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants