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)
}
}
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With