Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sbt tasks do not use scope specific settings #763

Closed
diwa-zz opened this issue May 25, 2014 · 16 comments
Closed

sbt tasks do not use scope specific settings #763

diwa-zz opened this issue May 25, 2014 · 16 comments
Labels
Milestone

Comments

@diwa-zz
Copy link

@diwa-zz diwa-zz commented May 25, 2014

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.

@urbas
Copy link

@urbas urbas commented Jul 3, 2014

I haven't tried this, and it's a wild guess, but can you try using lower-case test, e.g.:

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

flywayLocations in test := Seq("filesystem:conf/db/migrations/main", "filesystem:conf/db/migrations/test")
@axelfontaine
Copy link
Contributor

@axelfontaine axelfontaine commented Nov 24, 2014

@cdedie Thoughts?

@axelfontaine axelfontaine added the m: SBT label Nov 26, 2014
@axelfontaine axelfontaine changed the title [flyway-sbt] flyway sbt tasks do not use scope specific settings sbt tasks do not use scope specific settings Nov 26, 2014
@cdedie
Copy link
Contributor

@cdedie cdedie commented Nov 27, 2014

The test scope is currently not supported by the flyway-sbt plugin.
Here is a pull request to tackle this problem: #891

@ghost
Copy link

@ghost ghost commented Nov 28, 2014

+1 for this. Big boon for SBT users

@axelfontaine axelfontaine added this to the Flyway 4.0 milestone Nov 28, 2014
@joshlemer
Copy link

@joshlemer joshlemer commented Sep 3, 2015

+1

axelfontaine added a commit to flyway/flywaydb.org that referenced this issue Oct 1, 2015
@axelfontaine
Copy link
Contributor

@axelfontaine axelfontaine commented Oct 1, 2015

Fixed. Thanks @cdedie !

@mikebridge
Copy link

@mikebridge mikebridge commented Jan 27, 2016

I'm not sure if I am doing this correctly, but can't figure out how to use a different scope. With the IntegrationTest scope defined in Play I tried:

// build.sbt
lazy val api = Project.project.in(file("api")).
  // ...
  settings(flywaySettings: _*).
  settings(
    // ...
    flywayUrl in IntegrationTest := "jdbc:postgresql://localhost:5432/mydb_test"
    flywayUser in IntegrationTest := "user"
    flywayPassword in IntegrationTest := "pw"
  )

Then in sbt:

> api/it:flywayUrl
[info] jdbc:postgresql://localhost:5432/mydb_test
> api/it:flywayUser
[info] user
> api/it:flywayPassword
[info] pw
> api/it:flywayMigrate
// ...
[info] Flyway 3.2.1 by Boxfuse
[trace] Stack trace suppressed: run last api/*:flywayMigrate for the full output.
[error] (api/*:flywayMigrate) org.flywaydb.core.api.FlywayException: Unable to connect to the database. Configure the url, user and password!
[error] Total time: 3 s, completed 27-Jan-2016 1:33:08 PM
@mikebridge
Copy link

@mikebridge mikebridge commented Jan 27, 2016

Sorry, I see #891 isn't in the current release.

@mikebridge
Copy link

@mikebridge mikebridge commented Jan 28, 2016

Is there a snapshot version of flyway available with this fix in it somewhere?

@ghost
Copy link

@ghost ghost commented Jan 28, 2016

+1 for an intermediate release to consume this.

I am the original reporter of the issue 19 months ago.

@mikebridge
Copy link

@mikebridge mikebridge commented Jan 31, 2016

@diwalak I tried to compile the version in master, but it doesn't appear to fix this issue.

$ git clone git@github.com/flyway/flyway
$ cd flyway
$ mvn install -P-CommercialDBTest -P-CommandlinePlatformAssemblies -DskipTests=true
$ cd flyway-sbt
$ sbt publish-local
// plugins.sbt
resolvers += "Local Maven" at Path.userHome.asFile.toURI.toURL + ".m2/repository"
addSbtPlugin("org.flywaydb" % "flyway-sbt" % "0-SNAPSHOT", "0.13", "2.10")
// build.sbt
lazy val api = Project.project.in(file("api")).
  // ...
  enablePlugins(FlywayPlugin).
  configs(IntegrationTest).
  settings(Defaults.itSettings: _*).
  settings(
    // ...
    flywayUrl in IntegrationTest := "jdbc:postgresql://localhost:5432/mydb_test"
    flywayUser in IntegrationTest := "user"
    flywayPassword in IntegrationTest := "pw"
  ),
 (test in IntegrationTest) <<= (test in IntegrationTest) dependsOn flywayMigrate,

Unfortunately, I still get exactly the same error:

> api/it:flywayUrl
[info] jdbc:postgresql://localhost:5432/mydb_test
> api/it:flywayUser
[info] user
> api/it:flywayPassword
[info] pw
> api/it:flywayMigrate
//...
[info] Flyway 0-SNAPSHOT by Boxfuse
[error] (api/*:flywayMigrate) org.flywaydb.core.api.FlywayException: Unable to connect to the database. Configure the url, user and password!
[error] Total time: 26 s, completed 31-Jan-2016 3:10:55 PM

@axelfontaine & @cdedie Unless I'm doing something wrong, should this issue be re-opened? I think the problem is not fixed.

@mikebridge
Copy link

@mikebridge mikebridge commented Feb 1, 2016

I assume this is connected to this but both those are completely unintelligible to me. Whatever's happening, flywayMigrate isn't reading from the right configuration...?

@axelfontaine
Copy link
Contributor

@axelfontaine axelfontaine commented Feb 1, 2016

@cdedie Thoughts?

@mikebridge
Copy link

@mikebridge mikebridge commented Feb 2, 2016

@axelfontaine I can invest some time in trying to figure this out, but it will take me some time to get up-to-speed on how to write sbt plugins---unless @cdedie has a quick fix. The problem seems to be that Test is hardcoded.

@mikebridge
Copy link

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

@dhoepelman
Copy link

@dhoepelman dhoepelman commented 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!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
7 participants
You can’t perform that action at this time.