-
Notifications
You must be signed in to change notification settings - Fork 91
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
Postgres not ready #54
Comments
Yep, I had the same issue with both Mysql and Postgres. The problem that is log entry "database system is ready to accept connections" (for mysql case) doesn't actually mean that you can connect straight away. So I've got an extended version: trait HasMysqlDatabase extends DockerTestKit with DockerMysqlService with BeforeAndAfterEach {
self: Suite =>
var mysqldb: Database = _
def bootstrapSchema: Option[DefaultMySQLDriver.DDL] = None
private val mysqlInitPc = PatienceConfig(Span(5, Seconds), Span(10, Millis))
private def initDb(port: Int) = {
val dbConfig =
s"""
|main = {
| connectionPool = "HikariCP"
| dataSourceClass = "com.mysql.jdbc.jdbc2.optional.MysqlDataSource"
| properties = {
| url = "jdbc:mysql://${dockerExecutor.host}:$port/$MysqlDatabase",
| user = "$MysqlUser"
| password = "$MysqlPassword"
| }
| numThreads = 2
|}
""".stripMargin
val db = Database.forConfig("main", ConfigFactory.parseString(dbConfig))
RetryUtils.looped({
db.run(sql"select 1".as[Int])
}, 5, FiniteDuration(500, TimeUnit.MILLISECONDS)).map(_ => db)
}
override def beforeAll(): Unit = {
super.beforeAll()
val port = mysqlContainer.getPorts().futureValue(mysqlInitPc).apply(MysqlAdvertisedPort)
mysqldb = initDb(port).futureValue(mysqlInitPc)
}
override protected def beforeEach(): Unit = {
super.beforeEach()
bootstrapSchema.foreach { schema =>
mysqldb.run(schema.create).futureValue(mysqlInitPc)
}
}
override protected def afterEach(): Unit = {
try {
bootstrapSchema.foreach { schema =>
mysqldb.run(schema.drop).futureValue(mysqlInitPc)
}
} finally {
super.afterEach()
}
}
override def afterAll(): Unit = {
Try(mysqldb.close())
super.afterAll()
}
} |
I don't want to make the sample project to depend on Slick, but if somebody want to implement this with plain jdbc and send a PR, they are more than welcome |
I created even simpler PostgresReadyChecker with pure java/scala libraries (only org.postgresql.Driver needs to be on path)
And then it can be used as loped checker
Usually after 6-7 seconds it is started. Code is here |
Cool, thanks. Could you please adding ability to pass port (but keep default). container.getPorts().map(x => x.values.head) Make a PR to |
I needed to expose port because otherwise java couldn't access postgres other than in another container with link... |
Hi, I'm using spotify docker client, official postgres image and docker checker from your example. Without that sleep slick is unable to connect to postgres.
Full code is here
https://github.com/fokot/docker-scala/blob/master/src/test/scala/fokot/docker/scala/service/CatRepositorySlickTest.scala
The text was updated successfully, but these errors were encountered: