zonkyio / embedded-postgres

Java embedded PostgreSQL component for testing
Apache License 2.0
344 stars 43 forks source link

Any way to specify postgres version using sbt? #41

Open hedefalk opened 4 years ago

hedefalk commented 4 years ago

I'm using embedded-postgres in a Scala project and building with sbt. I now need features from postgres 12 but can't seem to understand how to use a Maven BOM from sbt. Maybe its not even possible:

https://github.com/sbt/sbt/issues/4531 https://stackoverflow.com/questions/42032303/how-do-i-use-a-maven-bom-bill-of-materials-to-manage-my-dependencies-in-sbt

Is there any other way I can specify the postgres version to use, maybe programmatically?

Thanks for this project!

tomix26 commented 3 years ago

Hi, it should be possible to specify it with the dependencyOverrides setting, see the example below. Note that I didn't test it because I don't use sbt. But according to sbt reference manual, it should work. The only thing I'm not sure about is the asterisk instead of the name of the dependency. Maybe you will have to use an explicit name of the dependency.

dependencyOverrides += "io.zonky.test.postgres" % "*" % "12.4.0"
mblund commented 3 years ago

We couldn't get the dependencyOverrides to work but instead we pointed out the exact jar file for the binary.

  val embeddedPostgres = ("io.zonky.test" % "embedded-postgres" % "1.2.8")
    .exclude("io.zonky.test", "postgres")

  val postgresV = "12.1.0"
  val osVersion        =
    System.getProperty("os.name").toLowerCase match {
      case osName if osName.contains("mac")   =>
        "embedded-postgres-binaries-darwin-amd64"
      case osName if osName.contains("win")   =>
        "embedded-postgres-binaries-windows-amd64"
      case osName if osName.contains("linux") =>
        "embedded-postgres-binaries-linux-amd64"
      case osName                             => throw new RuntimeException(s"Unknown operating system $osName")
    }
  val postgresBinaries = "io.zonky.test.postgres" % osVersion % postgresV

and then we used this these values in our project definition to get the right depenencies.

lazy val persistence = (project in file("libs/persistence"))
  .configs(IntegrationTest)
  .settings(
    name := "persistence",
    common,
    Defaults.itSettings,
    libraryDependencies ++= Seq(
          embeddedPostgres % "it",
          postgresBinaries % "it",
          flyway           % "it"
        ),

This got our integration test to run fine with postgres 12.1.