Skip to content

Commit

Permalink
Respect play.evolutions[.db.default].path config for dynamic evolutions
Browse files Browse the repository at this point in the history
  • Loading branch information
mkurz committed Aug 21, 2023
1 parent 19b8bc9 commit c95e7ad
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
4 changes: 3 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
)
)

Expand Down
9 changes: 8 additions & 1 deletion play-ebean/src/main/java/play/db/ebean/EBeanComponents.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,25 @@

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;

/** Classes for Java compile time dependency injection. */
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();
}
}
21 changes: 18 additions & 3 deletions play-ebean/src/main/java/play/db/ebean/EbeanDynamicEvolutions.java
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand All @@ -28,13 +30,19 @@ public class EbeanDynamicEvolutions extends DynamicEvolutions {
private final EbeanConfig config;
private final Environment environment;

private final EvolutionsConfig evolutionsConfig;

private final Map<String, Database> 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(
() -> {
Expand Down Expand Up @@ -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()) {
Expand All @@ -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));
Expand Down

0 comments on commit c95e7ad

Please sign in to comment.