play-ebean icon indicating copy to clipboard operation
play-ebean copied to clipboard

Fix hardcoded references to conf directory

Open benmccann opened this issue 11 years ago • 4 comments

EbeanDynamicEvolutions has hardcoded references to the conf directory:

File evolutions = environment.getFile("conf/evolutions/" + key + "/1.sql");
Files.createDirectory(environment.getFile("conf/evolutions/" + key));

benmccann avatar Dec 02 '14 06:12 benmccann

@benmccann @schmitch hey guys, I want to fix this problem but I'm not understanding how to get application conf folder and how to write some tests. Can you help me understand?

I was thinking to use Environment.rootPath() to get application root folder and then concat with /conf. Do you think that is a good approach?

image

felipebonezi avatar May 13 '21 16:05 felipebonezi

I haven't used Play in years. I'm afraid my memory from 2014 is rather fuzzy

benmccann avatar May 13 '21 16:05 benmccann

@felipebonezi Look at this code from Play:

val evolution = {
  // First try a file on the filesystem
  val filename = Evolutions.fileName(db, paddedRevision)
  environment.getExistingFile(filename).map(_.toURI)
}.orElse {
  // If file was not found, try a resource on the classpath
  val resourceName = Evolutions.resourceName(db, paddedRevision)
  environment.resource(resourceName).map(url => url.toURI)
}

Evolutions.fileName(db, revision) also tries to access the evolutions file in the hardcoded conf folder.

  /**
   * Default evolutions directory location.
   */
  def directoryName(db: String): String = s"conf/evolutions/${db}"

  /**
   * Default evolution file location.
   */
  def fileName(db: String, revision: Int): String = s"${directoryName(db)}/${revision}.sql"

  def fileName(db: String, revision: String): String = s"${directoryName(db)}/${revision}.sql"

however if that files does not exist if falls back to Evolutions.resourceName(db, revision) which does not include the hardcoded conf/ prefix (also here the resource gets loaded via environment.resource(...) which loads the resource from the classpath).

I think that is the only way to solve this problem here. There is no helper method which gives you the conf folder. That's because in Play you can disable the PlayLayoutPlugin and then there is no conf folder anymore, but instead the maven style folder structure is used and src/main/resources is used instead.

mkurz avatar May 13 '21 19:05 mkurz

Also, because (depending if PlayLayoutPlugin is enable dor not) the conf folder or src/main/resources gets added to the classpath, so that fallback should be able to read the file, no matter where it is located.

mkurz avatar May 13 '21 19:05 mkurz