Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run a comand line scala script that uses Play Framework app database?

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)
  }
}
like image 740
philix Avatar asked Sep 19 '25 21:09

philix


1 Answers

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/

like image 76
philix Avatar answered Sep 21 '25 11:09

philix