From d810de4f9511f03e98f4e4207ed0be6150402559 Mon Sep 17 00:00:00 2001 From: Vadim Vera Date: Wed, 10 Jan 2024 11:29:11 -0500 Subject: [PATCH] Introduce configuration parameter to control the generation of the evolution SQL files (cherry picked from commit d2910e31fffddc1e53d445f37b4b627c8b27943a) --- .../java/play/db/ebean/DefaultEbeanConfig.java | 15 ++++++++++++--- .../src/main/java/play/db/ebean/EbeanConfig.java | 1 + .../play/db/ebean/EbeanDynamicEvolutions.java | 3 +++ .../java/play/db/ebean/EbeanParsedConfig.java | 14 +++++++++++--- play-ebean/src/main/resources/reference.conf | 3 +++ .../java/play/db/ebean/EbeanParsedConfigTest.java | 7 +++++++ 6 files changed, 37 insertions(+), 6 deletions(-) diff --git a/play-ebean/src/main/java/play/db/ebean/DefaultEbeanConfig.java b/play-ebean/src/main/java/play/db/ebean/DefaultEbeanConfig.java index 8e91d061..d121cfc5 100644 --- a/play-ebean/src/main/java/play/db/ebean/DefaultEbeanConfig.java +++ b/play-ebean/src/main/java/play/db/ebean/DefaultEbeanConfig.java @@ -24,15 +24,24 @@ /** Ebean server configuration. */ @Singleton public class DefaultEbeanConfig implements EbeanConfig { + private final Boolean ddlGenerate; private final String defaultServer; + private final Map serverConfigs; - public DefaultEbeanConfig(String defaultServer, Map serverConfigs) { + public DefaultEbeanConfig( + Boolean ddlGenerate, String defaultServer, Map serverConfigs) { + this.ddlGenerate = ddlGenerate; this.defaultServer = defaultServer; this.serverConfigs = serverConfigs; } + @Override + public Boolean ddlGenerate() { + return ddlGenerate; + } + @Override public String defaultServer() { return defaultServer; @@ -70,7 +79,6 @@ public EbeanConfig get() { * @return a config for Ebean servers. */ public EbeanConfig parse() { - EbeanParsedConfig ebeanConfig = EbeanParsedConfig.parseFromConfig(config); Map serverConfigs = new HashMap<>(); @@ -100,7 +108,8 @@ public EbeanConfig parse() { serverConfigs.put(key, serverConfig); } - return new DefaultEbeanConfig(ebeanConfig.getDefaultDatasource(), serverConfigs); + return new DefaultEbeanConfig( + ebeanConfig.getDdlGenerate(), ebeanConfig.getDefaultDatasource(), serverConfigs); } private void setServerConfigDataSource(String key, DatabaseConfig serverConfig) { diff --git a/play-ebean/src/main/java/play/db/ebean/EbeanConfig.java b/play-ebean/src/main/java/play/db/ebean/EbeanConfig.java index f8dd80b7..b1d9ec3b 100644 --- a/play-ebean/src/main/java/play/db/ebean/EbeanConfig.java +++ b/play-ebean/src/main/java/play/db/ebean/EbeanConfig.java @@ -8,6 +8,7 @@ import java.util.Map; public interface EbeanConfig { + Boolean ddlGenerate(); String defaultServer(); 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 d0e34c6c..7a870725 100644 --- a/play-ebean/src/main/java/play/db/ebean/EbeanDynamicEvolutions.java +++ b/play-ebean/src/main/java/play/db/ebean/EbeanDynamicEvolutions.java @@ -64,6 +64,9 @@ public void create() { if (environment.isProd()) { return; } + if (config.ddlGenerate().equals(Boolean.FALSE)) { + return; + } config .serverConfigs() .forEach( diff --git a/play-ebean/src/main/java/play/db/ebean/EbeanParsedConfig.java b/play-ebean/src/main/java/play/db/ebean/EbeanParsedConfig.java index 39e8d417..613741ce 100644 --- a/play-ebean/src/main/java/play/db/ebean/EbeanParsedConfig.java +++ b/play-ebean/src/main/java/play/db/ebean/EbeanParsedConfig.java @@ -17,14 +17,21 @@ */ public class EbeanParsedConfig { + private final Boolean ddlGenerate; private final String defaultDatasource; private final Map> datasourceModels; - public EbeanParsedConfig(String defaultDatasource, Map> datasourceModels) { + public EbeanParsedConfig( + Boolean ddlGenerate, String defaultDatasource, Map> datasourceModels) { + this.ddlGenerate = ddlGenerate; this.defaultDatasource = defaultDatasource; this.datasourceModels = datasourceModels; } + public Boolean getDdlGenerate() { + return ddlGenerate; + } + public String getDefaultDatasource() { return defaultDatasource; } @@ -42,8 +49,9 @@ public Map> getDatasourceModels() { */ public static EbeanParsedConfig parseFromConfig(Config config) { Config playEbeanConfig = config.getConfig("play.ebean"); - String defaultDatasource = playEbeanConfig.getString("defaultDatasource"); String ebeanConfigKey = playEbeanConfig.getString("config"); + Boolean ebeanDdlGenerateKey = playEbeanConfig.getBoolean("ddlGenerate"); + String ebeanDefaultDatasourceKey = playEbeanConfig.getString("defaultDatasource"); Map> datasourceModels = new HashMap<>(); @@ -64,6 +72,6 @@ public static EbeanParsedConfig parseFromConfig(Config config) { datasourceModels.put(key, models); }); } - return new EbeanParsedConfig(defaultDatasource, datasourceModels); + return new EbeanParsedConfig(ebeanDdlGenerateKey, ebeanDefaultDatasourceKey, datasourceModels); } } diff --git a/play-ebean/src/main/resources/reference.conf b/play-ebean/src/main/resources/reference.conf index 530ad5aa..e2864902 100644 --- a/play-ebean/src/main/resources/reference.conf +++ b/play-ebean/src/main/resources/reference.conf @@ -7,6 +7,9 @@ play { # The key for ebean config config = "ebean" + # The key to control the generation of the evolution SQL files + ddlGenerate = true + # The name of the default ebean datasource defaultDatasource = "default" } diff --git a/play-ebean/src/test/java/play/db/ebean/EbeanParsedConfigTest.java b/play-ebean/src/test/java/play/db/ebean/EbeanParsedConfigTest.java index 11f8c4c3..9448eb05 100644 --- a/play-ebean/src/test/java/play/db/ebean/EbeanParsedConfigTest.java +++ b/play-ebean/src/test/java/play/db/ebean/EbeanParsedConfigTest.java @@ -24,6 +24,7 @@ private EbeanParsedConfig parse(Map config) { @Test public void defaultConfig() { EbeanParsedConfig config = parse(Collections.emptyMap()); + assertThat(config.getDdlGenerate(), equalTo(Boolean.TRUE)); assertThat(config.getDefaultDatasource(), equalTo("default")); assertThat(config.getDatasourceModels().size(), equalTo(0)); } @@ -52,6 +53,12 @@ public void customDefault() { assertThat(config.getDefaultDatasource(), equalTo("custom")); } + @Test + public void disableDdlGenerate() { + EbeanParsedConfig config = parse(ImmutableMap.of("play.ebean.ddlGenerate", false)); + assertThat(config.getDdlGenerate(), equalTo(Boolean.FALSE)); + } + @Test public void customConfig() { EbeanParsedConfig config =