Skip to content

Instantly share code, notes, and snippets.

@hmemcpy
Created July 8, 2020 10:49
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hmemcpy/b7a4298988bef0a92aa12660e66cadea to your computer and use it in GitHub Desktop.
Save hmemcpy/b7a4298988bef0a92aa12660e66cadea to your computer and use it in GitHub Desktop.
ZIO layer for Postgres test container
import com.dimafeng.testcontainers.PostgreSQLContainer
import zio.blocking.{effectBlocking, Blocking}
import zio.{Has, ZLayer, ZManaged}
object TestContainer {
type Postgres = Has[PostgreSQLContainer]
def postgres(version: Option[String] = None): ZLayer[Blocking, Throwable, Postgres] =
ZManaged.make {
effectBlocking {
val container = new PostgreSQLContainer(
dockerImageNameOverride = version
.map(version => s"postgres:$version")
.orElse(Some("postgres"))
)
container.start()
container
}
}(container => effectBlocking(container.stop()).orDie).toLayer
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment