From c95e7ad37fc1e035e898e9547d5260e03f27cc29 Mon Sep 17 00:00:00 2001 From: Matthias Kurz Date: Tue, 15 Aug 2023 00:32:49 +0200 Subject: [PATCH] Respect play.evolutions[.db.default].path config for dynamic evolutions --- build.sbt | 4 +++- .../java/play/db/ebean/EBeanComponents.java | 9 +++++++- .../play/db/ebean/EbeanDynamicEvolutions.java | 21 ++++++++++++++++--- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/build.sbt b/build.sbt index cea08783..a630c199 100644 --- a/build.sbt +++ b/build.sbt @@ -29,7 +29,9 @@ lazy val mimaSettings = Seq( }, mimaBinaryIssueFilters ++= Seq( // https://github.com/playframework/play-ebean/pull/281 - Removed io.ebean.EbeanServer in Ebean 13.6.0 - ProblemFilters.exclude[IncompatibleMethTypeProblem]("play.db.ebean.EbeanDynamicEvolutions.generateEvolutionScript") + ProblemFilters.exclude[IncompatibleMethTypeProblem]("play.db.ebean.EbeanDynamicEvolutions.generateEvolutionScript"), + // https://github.com/playframework/play-ebean/pull/387 - Respect `play.evolutions[.db.default].path` config for dynamic evolutions + ProblemFilters.exclude[DirectMissingMethodProblem]("play.db.ebean.EbeanDynamicEvolutions.this"), ) ) diff --git a/play-ebean/src/main/java/play/db/ebean/EBeanComponents.java b/play-ebean/src/main/java/play/db/ebean/EBeanComponents.java index 637afeeb..da61057c 100644 --- a/play-ebean/src/main/java/play/db/ebean/EBeanComponents.java +++ b/play-ebean/src/main/java/play/db/ebean/EBeanComponents.java @@ -4,7 +4,9 @@ package play.db.ebean; +import play.api.db.evolutions.DefaultEvolutionsConfigParser; import play.api.db.evolutions.DynamicEvolutions; +import play.api.db.evolutions.EvolutionsConfig; import play.components.ConfigurationComponents; import play.db.DBComponents; @@ -12,10 +14,15 @@ public interface EBeanComponents extends ConfigurationComponents, DBComponents { default DynamicEvolutions dynamicEvolutions() { - return new EbeanDynamicEvolutions(ebeanConfig(), environment(), applicationLifecycle()); + return new EbeanDynamicEvolutions( + ebeanConfig(), environment(), applicationLifecycle(), evolutionsConfig()); } default EbeanConfig ebeanConfig() { return new DefaultEbeanConfig.EbeanConfigParser(config(), environment(), dbApi()).get(); } + + default EvolutionsConfig evolutionsConfig() { + return new DefaultEvolutionsConfigParser(configuration()).parse(); + } } diff --git a/play-ebean/src/main/java/play/db/ebean/EbeanDynamicEvolutions.java b/play-ebean/src/main/java/play/db/ebean/EbeanDynamicEvolutions.java index dfe6ff1b..d0e34c6c 100644 --- a/play-ebean/src/main/java/play/db/ebean/EbeanDynamicEvolutions.java +++ b/play-ebean/src/main/java/play/db/ebean/EbeanDynamicEvolutions.java @@ -19,6 +19,8 @@ import javax.inject.Singleton; import play.Environment; import play.api.db.evolutions.DynamicEvolutions; +import play.api.db.evolutions.Evolutions$; +import play.api.db.evolutions.EvolutionsConfig; import play.inject.ApplicationLifecycle; /** A Play module that automatically manages Ebean configuration. */ @@ -28,13 +30,19 @@ public class EbeanDynamicEvolutions extends DynamicEvolutions { private final EbeanConfig config; private final Environment environment; + private final EvolutionsConfig evolutionsConfig; + private final Map databases = new HashMap<>(); @Inject public EbeanDynamicEvolutions( - EbeanConfig config, Environment environment, ApplicationLifecycle lifecycle) { + EbeanConfig config, + Environment environment, + ApplicationLifecycle lifecycle, + EvolutionsConfig evolutionsConfig) { this.config = config; this.environment = environment; + this.evolutionsConfig = evolutionsConfig; start(); lifecycle.addStopHook( () -> { @@ -64,7 +72,10 @@ public void create() { if (evolutionScript == null) { return; } - File evolutions = environment.getFile("conf/evolutions/" + key + "/1.sql"); + File evolutions = + environment.getFile( + Evolutions$.MODULE$.fileName( + key, 1, evolutionsConfig.forDatasource(key).path())); try { String content = ""; if (evolutions.exists()) { @@ -74,7 +85,11 @@ public void create() { if (content.isEmpty() || content.startsWith("# --- Created by Ebean DDL") || content.startsWith("-- Created by Ebean DDL")) { - environment.getFile("conf/evolutions/" + key).mkdirs(); + environment + .getFile( + Evolutions$.MODULE$.directoryName( + key, evolutionsConfig.forDatasource(key).path())) + .mkdirs(); if (!content.equals(evolutionScript)) { Files.write( evolutions.toPath(), evolutionScript.getBytes(StandardCharsets.UTF_8));