question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

sbt tasks do not use scope specific settings

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:16 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
dhoepelmancommented, Apr 7, 2016

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!
1reaction
mikebridgecommented, Feb 8, 2016

@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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

sbt Reference Manual — Scopes
Build-level settings are often used as a fallback when a project doesn't define a project-specific setting. We will discuss more on build-level settings...
Read more >
How to make a sbt task use a specific configuration scope?
I have a task lazy val task = TaskKey[Unit] that takes a lazy val setting = SettingKey[String] as input. I also have three...
Read more >
[sbt] How to make a sbt task use a specific configuration scope?
However, this does not seem to work. > > How can I force task to use the settings from a specific config scope?...
Read more >
sbt Beyond the Defaults: Part III - Scopes - Alejandro Marín
sbt has three scope types (called axes) in where each key can be defined with its own value: Project Axis. Configuration Axis. Task...
Read more >
Making sense of SBT - Beyond the lines
Scopes. So far we've seen that a setting or task key is always linked to a single value or task body. In fact...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found