SkyWalker
SkyWalker

Reputation: 14307

Play 2.5.x: How to change the default evolutions directory location?

Is there a way to change the default evolutions directory location? I need this because I have the following apparently rare use-case:

Is there a way to using application.conf or some sbt setting alla e.g.

javaOptions in Test += "-Dconfig.file=conf/application.test.conf"

be able to change the default directory of evolutions for test?

My OP would be solved if only I could say e.g. (theoretical doesn't work!)

javaOptions in Test += "-Devolutions.prefix=conf/testdatabase/"

and then have:

conf/
    evolutions/default/1.sql 
                       2.sql
    testdatabase/evolutions/default/1.sql -> ../../../evolutions/default/1.sql

I will be happy with any other solution for this problem that I am not aware of ... as long as it works :)

Upvotes: 3

Views: 854

Answers (1)

curious
curious

Reputation: 2928

in your test configuration file turn off the default application of evolutions by adding this line.

# Evolutions should not be applied by default
play.evolutions.db.default.enabled = false

Define a helper method in your test file which will apply Evolutions when called.

import play.api.db.Database
import play.api.db.evolutions.{DatabaseEvolutions, EvolutionsReader, ThisClassLoaderEvolutionsReader}

def applyEvolutions(database: Database,
                      evolutionsToSkip: Seq[Int],
                      evolutionsReader: EvolutionsReader = ThisClassLoaderEvolutionsReader,
                      autocommit: Boolean = true,
                      schema: String = ""): Unit = {
    val dbEvolutions = new DatabaseEvolutions(database, schema)
    val evolutions = dbEvolutions.scripts(evolutionsReader).filterNot(s => evolutionsToSkip.contains(s.evolution.revision))
    dbEvolutions.evolve(evolutions, autocommit)
  }

Then in your test override beforeAll and afterAll methods:

override def beforeAll(): Unit = {
    applyEvolutions(database, Seq(2))
  }

  override def afterAll(): Unit = {
    Evolutions.cleanupEvolutions(database)
  }

You can access database as follows:

import play.api.db.{ DBApi, Database }
val databaseAPI = app.injector.instanceOf[DBApi]
val database = databaseAPI.database("default")

Upvotes: 4

Related Questions