title: How to write database-agnostic Play application and perform first-time database initialization?
body: <p>I'm using <a href="http://slick.typesafe.com/" rel="noreferrer">Slick</a> with a <a href="https://www.playframework.com/" rel="noreferrer">Play Framework</a> 2.1 and I have some troubles.</p>
  
  <p>Given the following entity...</p>
  
  <pre><code>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 &lt;&gt; (Account, Account.unapply _)
  }
  </code></pre>
  
  <p>...I have to import a package for a <em>specific</em> database driver, but I want to use <strong>H2</strong> for <strong>testing</strong> and <strong>PostgreSQL</strong> in <strong>production</strong>. How should I proceed?</p>
  
  <p>I was able to workaround this by <em>overriding</em> the driver settings in my unit test:</p>
  
  <pre><code>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 &lt;- Accounts) yield account
          account.first.id.get mustEqual 1
        }
      }
    }
  }
  </code></pre>
  
  <p>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?</p>
  
  <p>I don't want to use evolution, either, and prefer to let Slick create the database tables for me:</p>
  
  <pre><code>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
      }
    }
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>That said, my last two questions are:</p>
  
  <ol>
  <li>How can I determine whether or not the database tables already exist?</li>
  <li>How can I make the <code>onStart</code> method above DB-agnostic so that I can test my application with <code>FakeApplication</code>?</li>
  </ol>