How to make Play framework application run SQL script before executing controllers CRUD API method?

500 Views Asked by At

I am creating a new play framework application and using slick to connect to h2 database. But when I am running the application and accessing the data it says table is not found. I expect that it should run the 1.sql file available in conf/evolutions/default folder.

Basically play.evolutions is not working.

following is my application.conf file

slick.dbs.default.driver = "slick.jdbc.MySQLProfile$"
slick.dbs.default.db.driver = "com.mysql.cj.jdbc.Driver"
slick.dbs.default.db.url = "jdbc:mysql://localhost:3306/playscalaslickexample"
slick.dbs.default.db.user = ""
slick.dbs.default.db.password = ""

# Evolutions
# ~~~~~
# You can disable evolutions if needed
play.evolutions.enabled=true
play.evolutions.autoApply=true

# You can disable evolutions for a specific datasource if necessary
play.evolutions.db.default.enabled = true

And I have 1.sql file in evolutions.default folder

I also tried to change the folder path like evolutions.playscalaslickexample. Still I don't see any change.

1

There are 1 best solutions below

3
Teena Vashist On

Add evolutions and jdbc into your dependencies list. Also the 1.sql script should be located in the conf/evolutions/{database name} or conf/evolutions/default directory of your application. Evolutions are automatically activated. If your database schema is not up to date, you need to start the application and try localhost:9000 on the browser an error page will come where you synchronize your database schema by running the appropriate SQL script.

You can also refer documentation https://www.playframework.com/documentation/2.7.x/Evolutions

Please follow the below steps, this might help you

  1. Keep 1.sql inside evolutions->default
  2. Add below lines in application.conf

    play.evolutions.enabled = false

  3. Create test.conf inside conf, if you are using mysql copy below lines in test.conf

    slick.dbs.default.driver = "slick.driver.MySQLDriver$" slick.dbs.default.db.url = "jdbc:h2:mem:test;MODE=MySQL;DATABASE_TO_UPPER=false;DB_CLOSE_DELAY=-1" slick.dbs.default.db.user = "" slick.dbs.default.db.password = ""

  4. Add below line in your build.sbt,

    javaOptions in Test += "-Dconfig.file=conf/test.conf"

    libraryDependencies ++= Seq(evolutions)