All Downloads are FREE. Search and download functionalities are using the official Maven repository.

codacy.test.checker.PostgresReadyChecker.scala Maven / Gradle / Ivy

The newest version!
package codacy.test.checker

import java.sql.DriverManager

import scala.concurrent.{ExecutionContext, Future}

import com.whisk.docker.{DockerCommandExecutor, DockerContainerState, DockerReadyChecker}

case class PostgresReadyChecker(
    user: String,
    password: String,
    port: Option[Int],
    host: Option[String],
    andThen: () => Future[Boolean] = () => Future.successful(true)
) extends DockerReadyChecker {

  override def apply(container: DockerContainerState)(implicit docker: DockerCommandExecutor, ec: ExecutionContext) = {
    container
      .getPorts()(docker, ec)
      .map { p =>
        Class.forName("org.postgresql.Driver")
        val url =
          s"jdbc:postgresql://${host.get}:${port.getOrElse(p)}/?loggerLevel=OFF"
        val res = Option(DriverManager.getConnection(url, user, password)).map(_.close).isDefined

        if (res)
          println(s"Postgres started on port: ${port.getOrElse(p)}")

        res
      }(ec)
      .flatMap { res =>
        if (res) andThen()
        else Future(false)(ec)
      }(ec)
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy