I have this on my app/scripts
folder (I created this folder inside app/
). I'm not sure how I can properly set the classpath here, thus I didn't even run this to know if it will actually connect to the database. How can I run this in a clean way from the command line?
package scripts
import scala.collection.TraversableOnce
import scala.collection.generic.SeqForwarder
import scala.io.Source
import scala.slick.jdbc.{StaticQuery => Q}
import scala.slick.session.Session
import scala.slick.session.Database
import play.api.db.DB
import tables.Campeonatos
import tables.Jogos
import org.postgresql.Driver
import play.api.test._
import play.api.test.Helpers._
// ...
class InsertJogosCSV extends App {
val dao = new DAO()
val application = FakeApplication()
def insertJogos(csv: CSV)(implicit s: Session) = {
val times = dao.getTimeIdByNameMap
var count = 0
csv foreach { case cols =>
count += 1
dao.insertJogo(cols, times)
}
count
}
val csvFilePath: String = args(0)
val csv = new CSV(csvFilePath)
csv.printLines
running(application) {
val realDatabase = Database.forDataSource(DB.getDataSource()(application))
implicit val s = realDatabase.createSession
insertJogos(csv)
}
}
You could achieve this by using the
play test:console
command at the root of your app. First you could probably move the code into a main method rather than extendingApp
:then run the
play test:console
command and do the followingThe
play test:console
by default adds everything from the app folder to your class path as well as theFakeApplication
context that you need for your script. Hope that helps.Similar question: https://stackoverflow.com/a/11297578/2556428
I am using another approach , for similar task.
Create empty subproject in main playframework project.
In build.sbt it looks like
and sjutil/build.sbt like normal sbt project with extra deps if needed, for me it was akka-remote
You can place some App direct in sjutil/ folder
sjutil/actorsstarter.scala:
after that you can run this script with:
and make everything you can do with normal project: stage, dist and etc.
I've made a blog post explaining my final solution. Should work as an answer to the question.
http://blog.felipe.rs/2014/05/15/run-maintenance-scripts-in-the-context-of-a-running-play-framework-application/