-1

Is there a way with Slick that you can run SQL code before the application starts or in the test set up like in Hibernate?

See example https://github.com/geowarin/hibernate-examples/tree/master/standalone-data-jpa/src/main/resources

decapo
  • 789
  • 3
  • 8
  • 25

2 Answers2

0

You can use Evolutions in Play framework. If you are using play-slick, you can add play-slick-evolutions dependency and make sure play.evolutions.enabled is not set to false in application.conf.

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

https://www.playframework.com/documentation/2.4.x/PlaySlick

[UPDATE]

As per your comment, it looks like you want your SQL to be run on startup rather than with migration. If such then you can use JavaGlobal/ScalaGlobal for Play < 2.4. Else you can take look into 2.4 GlobalSettings doc.

Community
  • 1
  • 1
TheKojuEffect
  • 20,103
  • 19
  • 89
  • 125
  • Thanks. Evolutions are enabled. I meant the data loading part. Can I make a script that has Insert Into Table (Blah) Values (Blah, Blah Blah, 3) that Slick will look for and run before the application starts and after migrations. – decapo Jan 09 '16 at 05:22
  • You can try using `GlobalSettings` in Play framework. – TheKojuEffect Jan 09 '16 at 05:41
  • I'm using play 2.4. I'm still confused though. Should I define a module with an eager binding to run the SQL script? How do I do that? Also, how would you do this with just an sbt project that was using slick without Play? – decapo Jan 09 '16 at 05:49
  • If it was simple slick project, refer to hello-slick template. https://github.com/typesafehub/activator-hello-slick/blob/master/src%2Fmain%2Fscala%2FHelloSlick.scala – TheKojuEffect Jan 09 '16 at 05:54
  • Ok. That's adding data with scala code. I was curious about adding data with an sql script specifically without having to write the scala code to do inserts. I have an existing database that I can export with insert statements into my play application but I do not want to use the slick schema generation. Do you know of a way to call an sql script to run within scala? – decapo Jan 09 '16 at 06:00
0

My question is kind of a duplicate question. See here Play Framework DML insert script.

With the play framework you only need to create a sql file in the evolutions directory called 2.sql with the insert statements that you need.

I'm still trying to figure out if you can do something like this without play in a stand alone sbt application.

Community
  • 1
  • 1
decapo
  • 789
  • 3
  • 8
  • 25