3
votes

I am using sbt [0.13] to compile a play [2.2] project using scala [2.10.3]. I have .sql files and scala files used for database migrations. The directory structure looks like:

app
|-> db
     |-> migration
           |-> V1__init.scala
           |-> V2__newTable.sql

When I run compile from the play console (REPL), the scala file (V1__init.scala) is compiled to a .class and copied to the classes folder. But the .sql file is not moved.

I tried adding unmanagedResourceDirectories in Compile <++= baseDirectory { dir => Seq(dir/"app/db/migration") ++ Seq(dir/"db/migration") } but it did not copy the files. The whole block looks like:

val main = play.Project(appName, appVersion, appDependencies).settings(
    scalaVersion := "2.10.3",
    scalacOptions ++= Seq("-feature"),   // enable feature warnings
    unmanagedResourceDirectories in Compile <++=  baseDirectory { dir => Seq(dir/"app/db/migration") ++ Seq(dir/"db/migration") }
)

I also tried using copyResources, but couldn't get that to work. Described here: http://www.playframework.com/documentation/2.0/SBTSettings

So does anyone know how I can copy the sql files to the classes folder?

Thanks!

UPDATE

I got IO.copyDirectory(new java.io.File("app/db/migration"), new java.io.File("target/scala-2.10/classes/db/migration"), true) to copy the files, but the destination is hard-coded and will change when I update scala

1
Have you considered using the builtin support for database evolutions?wingedsubmariner
Yes I have, but it looks like evolutions does not support running arbitrary scala code, instead it needs .sql filesJon
You can alternatively put the sql scripts into the conf dir, so it is automatically in the classpath.Schleichardt
@Schleichardt I had thought about this, but I don't think the conf dir is automatically compiled, which is needed so migration scripts can be written in scala or java. Thanks thoughJon

1 Answers

0
votes
  val main = play.Project(appName, appVersion, appDependencies).settings(
    scalaVersion := "2.10.3",
    scalacOptions ++= Seq("-feature"),   // enable feature warnings
    unmanagedResourceDirectories in Compile <+= scalaSource in Compile,
    excludeFilter in unmanagedResources in Compile := "*.scala" || "*.java"
  )

You can check easily the contents in the class folder with:

 sbt clean full-classpath && ls target/scala-2.10/classes/db/migration/