Scala Slick, how to create Schema ONLY if it does not exist

Phil picture Phil · Nov 26, 2015 · Viewed 7.5k times · Source

In Scala Slick, a database schema can be created with the following:

val schema = coffees.schema ++ suppliers.schema
db.run(DBIO.seq(
  schema.create
))

From the bottom of this documentation page http://slick.typesafe.com/doc/3.0.0/schemas.html

However, if the database schema already exists then this throws an exception.

Is there a normal way or right way to create the schema IF AND ONLY IF it does not already exist?

Answer

Dmitrii Nikiforov picture Dmitrii Nikiforov · Nov 7, 2019

In Slick 3.3.0 createIfNotExists and dropIfExists schema methods were added. So:

db.run(coffees.schema.createIfNotExists)

Googled this question and tried several solutions from answers until figured it out.