flyway: sbt tasks do not use scope specific settings

Hello,

Firstly, thanks for this excellent library. I am really enjoying the compactness and direct SQL-access.

I am trying to use the sbt plugin with a play application. I would like to use different databases for dev and test. I have overridden the flywayUrl in the Test scope. But the test:flywayMigrate task still uses the flywayUrl setting value and not the test:flywayUrl setting value.

I have a sample app - https://github.com/diwa/flyway_play_test to reproduce the problem

The following is the configuration extract from build.sbt

flywaySettings

flywayUrl := "jdbc:h2:file:target/devbar"

flywayUser := "SA"

flywayLocations := Seq("filesystem:conf/db/migrations/main")

flywayUrl in Test := "jdbc:h2:file:target/testbar"

flywayLocations in Test := Seq("filesystem:conf/db/migrations/main", "filesystem:conf/db/migrations/test")

SBT session

Global settings are recognized

➜  flyway_play_test git:(master) sbt
[info] Loading project definition from /Users/diwa/code/sandbox/flyway_play_test/project
[info] Set current project to flyway_play_test (in build file:/Users/diwa/code/sandbox/flyway_play_test/)

[flyway_play_test] $ flywayUrl
[info] jdbc:h2:file:target/devbar
[flyway_play_test] $ flywayLocations
[info] List(filesystem:conf/db/migrations/main)
[flyway_play_test] $ flywayInfo
[info] Database: jdbc:h2:file:target/devbar (H2 1.3)
[info] +----------------+----------------------------+---------------------+---------+
[info] | Version        | Description                | Installed on        | State   |
[info] +----------------+----------------------------+---------------------+---------+
[info] | 1              | Create person table        |                     | Pending |
[info] +----------------+----------------------------+---------------------+---------+
[success] Total time: 1 s, completed May 25, 2014 10:24:01 PM

Test Scope settings are recognized

[flyway_play_test] $ test:flywayUrl
[info] jdbc:h2:file:target/testbar
[flyway_play_test] $ test:flywayLocations
[info] List(filesystem:conf/db/migrations/main, filesystem:conf/db/migrations/test)

flywayInfo Task does not use test scope settings (see database)

[flyway_play_test] $ test:flywayInfo
[info] Database: jdbc:h2:file:target/devbar (H2 1.3)   
[info] +----------------+----------------------------+---------------------+---------+
[info] | Version        | Description                | Installed on        | State   |
[info] +----------------+----------------------------+---------------------+---------+
[info] | 1              | Create person table        |                     | Pending |
[info] +----------------+----------------------------+---------------------+---------+
[success] Total time: 0 s, completed May 25, 2014 10:24:25 PM

Normal migration works

[flyway_play_test] $ flywayMigrate
[info] Database: jdbc:h2:file:target/devbar (H2 1.3)
[info] Validated 1 migration (execution time 00:00.003s)
[info] Creating Metadata table: "PUBLIC"."schema_version"
[info] Current version of schema "PUBLIC": << Empty Schema >>
[info] Migrating schema "PUBLIC" to version 1
[info] Successfully applied 1 migration to schema "PUBLIC" (execution time 00:00.027s).
[success] Total time: 0 s, completed May 25, 2014 10:25:09 PM

Test Scope migration does not work (nothing found to migrate)

[flyway_play_test] $ test:flywayMigrate
[info] Database: jdbc:h2:file:target/devbar (H2 1.3)
[info] Validated 1 migration (execution time 00:00.003s)
[info] Current version of schema "PUBLIC": 1
[info] Schema "PUBLIC" is up to date. No migration necessary.
[success] Total time: 0 s, completed May 25, 2014 10:25:15 PM

Can you please help with this as it is essential to have different dbs for dev and test ?

@jsuereth - Am I doing something wrong in the sbt settings for scopes here ?

Thanks in advance for all your support.

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Comments: 16 (4 by maintainers)

Commits related to this issue

Most upvoted comments

What is the way in Flyway 4.0 to do this? By default, only default and Test configuration seem to be respected, and @mikebridge 's solution does not work since flywaySettings is no longer defined.

lazy val Production: Configuration = config("prod") extends Compile

flywayUrl in Test := "jdbc:postgresql://localhost:5432/test"
flywayUser in Test := "test"
flywayPassword in Test := ""

flywayUrl in IntegrationTest := "jdbc:postgresql://localhost:5432/it"
flywayUser in IntegrationTest := "it"
flywayPassword in IntegrationTest := ""

flywayUrl in Production := "jdbc:postgresql://localhost:5432/prod"
flywayUser in Production := "production"
flywayPassword in Production := ""

Results in:

$ sbt test:flywayInfo
[info] Flyway 4.0 by Boxfuse
[info] Database: jdbc:postgresql://localhost:5432/test (PostgreSQL 9.5)
[info] +---------+---------------------------------+---------------------+---------+
[info] | Version | Description                     | Installed on        | State   |
[info] +---------+---------------------------------+---------------------+---------+
/* etc */

$sbt it:flywayInfo
[info] Flyway 4.0 by Boxfuse
org.flywaydb.core.api.FlywayException: Unable to connect to the database. Configure the url, user and password!

$sbt prod:flywayInfo
[info] Flyway 4.0 by Boxfuse
org.flywaydb.core.api.FlywayException: Unable to connect to the database. Configure the url, user and password!

@axelfontaine I believe that the current implementation in master is correct. This appears to be the magic sbt incantation to execute tasks in a different Configuration scope:

// Copy the flywaySettings into the IntegrationTest configuration.
 settings(inConfig(IntegrationTest)(flywaySettings): _*).

With that set, I can run either it:flywayMigrate or flywayMigrate and they each find the variables from the correct scope.