Skip to content

Commit

Permalink
Introduce configuration parameter to control the generation of the ev…
Browse files Browse the repository at this point in the history
…olution SQL files

(cherry picked from commit d2910e3)
  • Loading branch information
Vadim Vera authored and mergify[bot] committed Jan 16, 2024
1 parent 863c805 commit d810de4
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 6 deletions.
15 changes: 12 additions & 3 deletions play-ebean/src/main/java/play/db/ebean/DefaultEbeanConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,24 @@
/** Ebean server configuration. */
@Singleton
public class DefaultEbeanConfig implements EbeanConfig {
private final Boolean ddlGenerate;

private final String defaultServer;

private final Map<String, DatabaseConfig> serverConfigs;

public DefaultEbeanConfig(String defaultServer, Map<String, DatabaseConfig> serverConfigs) {
public DefaultEbeanConfig(
Boolean ddlGenerate, String defaultServer, Map<String, DatabaseConfig> serverConfigs) {
this.ddlGenerate = ddlGenerate;
this.defaultServer = defaultServer;
this.serverConfigs = serverConfigs;
}

@Override
public Boolean ddlGenerate() {
return ddlGenerate;
}

@Override
public String defaultServer() {
return defaultServer;
Expand Down Expand Up @@ -70,7 +79,6 @@ public EbeanConfig get() {
* @return a config for Ebean servers.
*/
public EbeanConfig parse() {

EbeanParsedConfig ebeanConfig = EbeanParsedConfig.parseFromConfig(config);

Map<String, DatabaseConfig> serverConfigs = new HashMap<>();
Expand Down Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions play-ebean/src/main/java/play/db/ebean/EbeanConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.Map;

public interface EbeanConfig {
Boolean ddlGenerate();

String defaultServer();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ public void create() {
if (environment.isProd()) {
return;
}
if (config.ddlGenerate().equals(Boolean.FALSE)) {
return;
}
config
.serverConfigs()
.forEach(
Expand Down
14 changes: 11 additions & 3 deletions play-ebean/src/main/java/play/db/ebean/EbeanParsedConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,21 @@
*/
public class EbeanParsedConfig {

private final Boolean ddlGenerate;
private final String defaultDatasource;
private final Map<String, List<String>> datasourceModels;

public EbeanParsedConfig(String defaultDatasource, Map<String, List<String>> datasourceModels) {
public EbeanParsedConfig(
Boolean ddlGenerate, String defaultDatasource, Map<String, List<String>> datasourceModels) {
this.ddlGenerate = ddlGenerate;
this.defaultDatasource = defaultDatasource;
this.datasourceModels = datasourceModels;
}

public Boolean getDdlGenerate() {
return ddlGenerate;
}

public String getDefaultDatasource() {
return defaultDatasource;
}
Expand All @@ -42,8 +49,9 @@ public Map<String, List<String>> 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<String, List<String>> datasourceModels = new HashMap<>();

Expand All @@ -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);
}
}
3 changes: 3 additions & 0 deletions play-ebean/src/main/resources/reference.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ private EbeanParsedConfig parse(Map<String, ?> 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));
}
Expand Down Expand Up @@ -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 =
Expand Down

0 comments on commit d810de4

Please sign in to comment.