Is it possible to run evolutions programmatically?

830 views
Skip to first unread message

Farid Zakaria

unread,
Jul 5, 2014, 11:46:46 AM7/5/14
to play-fr...@googlegroups.com
I have removed the dependency on a current Play application for most of my application and I'd like to be able to test the database layer.
I was wondering if there was a way (without starting an application) to programmatically in Scala run the set of evolutions on a given DataSource?


Farid Zakaria

unread,
Jul 5, 2014, 11:55:00 AM7/5/14
to play-fr...@googlegroups.com

Fernando Correia

unread,
Jul 5, 2014, 4:10:04 PM7/5/14
to play-fr...@googlegroups.com
I recommend using Flyway instead of the native evolutions. I find it way more powerful and flexible, and it supports working with valid SQL files.

Alberto Souza

unread,
Jul 6, 2014, 5:45:03 PM7/6/14
to play-fr...@googlegroups.com
Hi, here a example:

OfflineEvolutions.applyScript(new File("."),this.getClass().getClassLoader(), "default")

Assuming that you are using "default" as the key in your application.conf :). Hope it helps!

Alberto Souza

www.caelum.com.br
www.leanpub.com/playframeworknapratica
www.alots.wordpress.com/

Farid Zakaria

unread,
Jul 6, 2014, 5:53:02 PM7/6/14
to play-fr...@googlegroups.com
Yes, it looked like I had to implement it differently.
I try to create a different database for each test class (since they execute in parallel) and OfflineEvolutions does not let you override the configuration to perhaps specify a unique database name (might send PR later).

For now, I just read the SQL myself and strip out whats necessary (additional semi-colons) and execute it myself.
Will post code if anyone interested

Farid Zakaria



--
You received this message because you are subscribed to a topic in the Google Groups "play-framework" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/play-framework/NTp3WtoQG2E/unsubscribe.
To unsubscribe from this group and all its topics, send an email to play-framewor...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Alberto Souza

unread,
Jul 6, 2014, 9:46:34 PM7/6/14
to play-fr...@googlegroups.com
If you want, you can pass a key which is different from "default". But I think you already know about that. 
Message has been deleted

Farid Zakaria

unread,
Jul 11, 2014, 6:16:36 PM7/11/14
to play-fr...@googlegroups.com
Checkout: http://blog.fzakaria.com/2014/07/unit-test-in-play-framework-with-slick/
I wrote a blog post on how we solved running Evolution scripts (i think pretty neatly) for Unit Tests

Farid Zakaria



On Fri, Jul 11, 2014 at 7:52 AM, keepscoding <keeps...@gmail.com> wrote:
how to execute OfflineEvolutions? 

keepscoding

unread,
Jul 11, 2014, 9:05:20 PM7/11/14
to play-fr...@googlegroups.com
thanks, fren...

James Ward

unread,
Dec 22, 2016, 7:09:55 PM12/22/16
to Play Framework
Here is the Play 2.5 version:

import java.io.File

import play.api.db.DBApi
import play.api.db.evolutions.OfflineEvolutions
import play.api.inject.guice.GuiceApplicationBuilder

object ApplyEvolutions extends App {
 val app
= new GuiceApplicationBuilder().build()

 val dbApi
= app.injector.instanceOf[DBApi]

 
OfflineEvolutions.applyScript(new File("."), this.getClass.getClassLoader, dbApi, "default")

 app
.stop()
}

And if you want to be able to run this on Heroku, have a Procfile like:
web: target/universal/stage/bin/foo -Dhttp.port=$PORT -Dplay.modules.disabled.0=play.api.db.DBModule -Dplay.modules.disabled.1=play.api.db.evolutions.EvolutionsModule
applyevolutions: target/universal/stage/bin/foo -main ApplyEvolutions

Then run:
heroku run applyevolutions


Hope that helps someone.

-James
Reply all
Reply to author
Forward
0 new messages