I'm using Slick with a Play Framework 2.1 and I have some troubles.
Given the following entity...
package models
import scala.slick.driver.PostgresDriver.simple._
case class Account(id: Option[Long], email: String, password: String)
object Accounts extends Table[Account]("account") {
def id = column[Long]("id", O.PrimaryKey, O.AutoInc)
def email = column[String]("email")
def password = column[String]("password")
def * = id.? ~ email ~ password <> (Account, Account.unapply _)
}
...I have to import a package for a specific database driver, but I want to use H2 for testing and PostgreSQL in production. How should I proceed?
I was able to workaround this by overriding the driver settings in my unit test:
package test
import org.specs2.mutable._
import play.api.test._
import play.api.test.Helpers._
import scala.slick.driver.H2Driver.simple._
import Database.threadLocalSession
import models.{Accounts, Account}
class AccountSpec extends Specification {
"An Account" should {
"be creatable" in {
Database.forURL("jdbc:h2:mem:test1", driver = "org.h2.Driver") withSession {
Accounts.ddl.create
Accounts.insert(Account(None, "user@gmail.com", "Password"))
val account = for (account <- Accounts) yield account
account.first.id.get mustEqual 1
}
}
}
}
I don't like this solution and I'm wondering if there is an elegant way to write DB-agnostic code so there are two different database engines used - one in testing and another in production?
I don't want to use evolution, either, and prefer to let Slick create the database tables for me:
import play.api.Application
import play.api.GlobalSettings
import play.api.Play.current
import play.api.db.DB
import scala.slick.driver.PostgresDriver.simple._
import Database.threadLocalSession
import models.Accounts
object Global extends GlobalSettings {
override def onStart(app: Application) {
lazy val database = Database.forDataSource(DB.getDataSource())
database withSession {
Accounts.ddl.create
}
}
}
The first time I start the application, everything works fine... then, of course, the second time I start the application it crashes because the tables already exist in the PostgreSQL database.
That said, my last two questions are:
- How can I determine whether or not the database tables already exist?
- How can I make the
onStart
method above DB-agnostic so that I can test my application withFakeApplication
?
If, like me, you're not using Play! for the project, a solution is provided by Nishruu here
The play-slick does exactly the same as what is proposed in the other answers, and it seems to be under the umbrella of Play/Typesafe.
You just can import
import play.api.db.slick.Config.driver.simple._
and it will choose the appropriate driver according toconf/application.conf
.It also offers some more things like connection pooling, DDL generation...
You find an example on how to use the cake pattern / dependency injection to decouple the Slick driver from the database access layer here: https://github.com/slick/slick-examples.
How to decouple the Slick driver and test application with FakeApplication
A few days ago I wrote a Slick integration library for play, which moves the driver dependency to the application.conf of the Play project: https://github.com/danieldietrich/slick-integration.
With the help of this library your example would be implemented as follows:
1) Add the dependency to project/Build.scala
Add snapshot repository
Or local repository, if slick-integration is published locally
2) Add the Slick driver to conf/application.conf
3) Implement app/models/Account.scala
In the case of slick-integration, it is assumed that you use primary keys of type Long which are auto incremented. The pk name is 'id'. The Table/Mapper implementation has default methods (delete, findAll, findById, insert, update). Your entities have to implement 'withId' which is needed by the 'insert' method.
4) Implement app/models/DAL.scala
This is the Data Access Layer (DAL) which is used by the controllers to access the database. Transactions are handled by the Table/Mapper implementation within the corresponding Component.
5) Implement test/test/AccountSpec.scala
How to determine whether or not the database tables already exist
I cannot give you a sufficient answer to this question...
... but perhaps this is not really s.th you want to do. What if you add an attribute to an table, say
Account.active
? If you want to safe the data currently stored within your tables, then an alter script would do the job. Currently, such an alter script has to be written by hand. TheDAL.ddl.createStatements
could be used to retrieve the create statements. They should be sorted to be better comparable with previous versions. Then a diff (with previous version) is used to manually create the alter script. Here, evolutions are used to alter the db schema.Here's an example on how to generate (the first) evolution:
I was also trying to address this problem: the ability to switch databases between test and production. The idea of wrapping each table object in a trait was unappealing.
I am not trying to discuss the pros and cons of the cake pattern here, but I found another solution, for those who are interested.
Basically, make an object like this:
Obviously, you can do any decision logic you like here. It does not have to be based on system properties.
Now, instead of:
You can say
UPDATE: A Slick 3.0 Version, courtesy of trent-ahrens: