1

I'm using Play framework btw. Slick does not help you "create" tables. Users have to create it by themselves, using tableQuery.ddl.create. When it was still Slick 1.0, I read a post about creating a Global.scalaand inside it looks like this:

import play.api._
import scala.slick.jdbc.meta.MTable
import play.api.Play.current
import play.api.db.slick._

import models.current.dao._
import models.current.dao.profile.simple._


object Global extends GlobalSettings {

  override def onStart(app: Application) {
    DB.withSession {implicit rs =>

      databaseCreate(Articles, Labels, Article_Labels, Users, Messages)

      def databaseCreate(tables: TableQuery[_]*) = {
         for (table <- tables) 
           if (MTable.getTables(table.toString).list.isEmpty) table.ddl.create
      }
    }
  }
}

However, since it is Slick 2.0 and I'm using the Cake pattern, this doesn't work anymore. I used to be able to pull Table class out and there is an attribute called name which I can call, but now with the new and improved Cake pattern, all I'm left with is TableQuery, which literally has nothing. (Cake pattern example is here: https://github.com/slick/slick-examples/blob/master/src/main/scala/com/typesafe/slick/examples/lifted/MultiDBCakeExample.scala)

My DAO class looks like this:

class DAO(override val profile: JdbcProfile) extends ArticleComponent with Article_LabelComponent with LabelComponent with MessageComponent with UserComponent with Profile {
  val Articles = TableQuery[Articles]
  val Article_Labels = TableQuery[Article_Labels]
  val Labels = TableQuery[Labels]
  val Messages = TableQuery[Messages]
  val Users = TableQuery[Users]
}

object current {
  val dao = new DAO(DB(play.api.Play.current).driver)
}

How should I modify my Global.scala to create a table if a table is not present in the Database?

windweller
  • 2,365
  • 5
  • 33
  • 56

1 Answers1

1

take a look : http://blog.knoldus.com/2014/01/20/scala-slick-2-0-for-multi-database/

and you can put directly ddl create statement in Glabal.scala:

object Global extends GlobalSettings 


 override def onStart(app: Application): Unit = {
   Logger.info("Apllication has started")
 try {
    Connection.databaseObject.withSession { implicit session: Session =>
     (Articles.ddl ++ Article_Labels.ddl ++ Labels.ddl ++ Messages.ddl ++       Users.ddl).create
   Logger.info("All tables have been created")

} catch {
  case ex: Exception => Logger.info(ex.getMessage() + ex.printStackTrace())
}

  }
}


define connection object: 
object Connection {
   def databaseObject(): Database = {
 Database.forDataSource(DB.getDataSource())
}
}
Sky
  • 2,509
  • 1
  • 19
  • 28
  • Can you also provide a "import" list? I can't get the so called "Datbase" type – windweller Mar 08 '14 at 05:54
  • take a look: https://github.com/satendrakumar06/playwithslick2withmultipledatabase this is incomplete sample.But may be it helpful. – Sky Mar 08 '14 at 20:49