Why is play framework not running my evolutions?

1.8k Views Asked by At

I recently started a new project, based from the scala-play-react-seed.

I have a little experience with Play and have other projects that use play-slick and slick-evolutions - everything works fine and the evolutions are recognised and applied at startup.

In the new project, this isn't happening. My connection to the database is all OK so that's not the issue.

I don't get any errors or warnings about the evolutions, as far as I can see.

I have tried explicitly turning them on in application.conf.

This is my build.sbt:

// core
libraryDependencies ++= Seq(
  evolutions,
  ehcache,
  ws,
  specs2 % Test,
  guice)

libraryDependencies += "org.scalatestplus.play" %% "scalatestplus-play" % "4.0.2" % Test

// database
libraryDependencies += "com.typesafe.play" %% "play-slick" % "4.0.2"
libraryDependencies += "com.typesafe.play" %% "play-slick-evolutions" % "4.0.2"
libraryDependencies += "org.postgresql" % "postgresql" % "9.4-1201-jdbc41"

I have a suspicion that the hook into the React UI is blocking the backend from picking up those files somehow, but have no idea where to start looking. Any help greatly appreciated.

Edit:

I'm pretty sure my application.conf is set up correctly but here it is:

slick.dbs.default.profile = "slick.jdbc.PostgresProfile$"
slick.dbs.default.db.driver = "org.postgresql.Driver"
slick.dbs.default.db.url = "jdbc:postgresql://localhost:5432/standups?user=postgres&password=password"

As I said before Im pretty sure this is set up correctly because I can talk to the database. It's just the evolutions that aren't being picked up.

This is my 1.sql, which resides in conf/evolutions/default:

-- !Ups

create table person
(
    id       serial       not null primary key,
    name     varchar(255) not null,
    age      smallint     not null
);

-- !Downs

drop table if exists person cascade;
3

There are 3 best solutions below

4
Richard Dallaway On BEST ANSWER

It's unclear to me why your evolutions aren't running.

I've tried to emulate your set up in this commit: https://github.com/d6y/scala-play-react-seed/commit/408853bda6f26a7a4fdc49487c2bb00d243ac0dc

...where I had to modify the FrontendRunHook to actually start the front-end and the server (via https://github.com/yohangz/scala-play-react-seed/pull/30).

Other than that, sbt run started the application and applied the evolution (verified by looking in the database).

I added play.evolutions.db.default.autoApply = true, but without that you would have been told the database needs a migration.

I'm also using JDK 8 with this, to avoid a warning relating to Guice (WARNING: An illegal reflective access operation has occurred). However, you didn't mention you saw that, so that's also probably unrelated.

4
vkt On

you need to enable the play evolutions config parameter

https://www.playframework.com/documentation/2.8.x/Evolutions

play.evolutions.enabled=true


For example, to enable autoApply for all evolutions, you might set play.evolutions.autoApply=true in application.conf or in a system property. To disable autocommit for a datasource named default, you set play.evolutions.db.default.autocommit=false
2
Richard Dallaway On

You'll also need to call applicationEvolutions at some point when your project starts up.

Typically that would be done in an instance of ApplicationLoader.

This means:

  1. adding a class for your application "components", which includes extending DBComponents with EvolutionsComponents and accessing applicationEvolutions.
  2. writing a class that extends ApplicationLoader which retuns your components in the load method of ApplicationLoader.
  3. adding the fully qualified class to your application.conf as play.application.loader=your.package.name.here.MyAppLoader

You can see the outline of the ApplicationLoader and components at: https://www.playframework.com/documentation/2.8.x/ScalaCompileTimeDependencyInjection#Application-entry-point

The specifics for mixing in the database code to call applicationEvolutions is at: https://www.playframework.com/documentation/2.8.x/Evolutions#Enable-evolutions