Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[7.0.x] Introduce configuration parameter to control the generation of the evolution SQL files (backport #433) by @vadimvera #436

Merged
merged 4 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ val previousVersion: Option[String] = Some("7.0.0")
lazy val mimaSettings = Seq(
mimaPreviousArtifacts := previousVersion.map(organization.value %% moduleName.value % _).toSet,
mimaBinaryIssueFilters ++= Seq(
ProblemFilters.exclude[ReversedMissingMethodProblem]("play.db.ebean.EbeanConfig.generateEvolutionsScripts"),
)
)

Expand Down
25 changes: 23 additions & 2 deletions play-ebean/src/main/java/play/db/ebean/DefaultEbeanConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,22 @@
public class DefaultEbeanConfig implements EbeanConfig {

private final String defaultServer;

private final Map<String, DatabaseConfig> serverConfigs;

public DefaultEbeanConfig(String defaultServer, Map<String, DatabaseConfig> serverConfigs) {
private final boolean generateEvolutionsScripts;

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

public DefaultEbeanConfig(String defaultServer, Map<String, DatabaseConfig> serverConfigs) {
this(defaultServer, serverConfigs, true);
}

@Override
Expand All @@ -43,11 +54,18 @@ public Map<String, DatabaseConfig> serverConfigs() {
return serverConfigs;
}

@Override
public boolean generateEvolutionsScripts() {
return generateEvolutionsScripts;
}

@Singleton
public static class EbeanConfigParser implements Provider<EbeanConfig> {

private final Config config;

private final Environment environment;

private final DBApi dbApi;

private static final Logger.ALogger LOGGER = Logger.of(DefaultEbeanConfig.class);
Expand Down Expand Up @@ -100,7 +118,10 @@ public EbeanConfig parse() {
serverConfigs.put(key, serverConfig);
}

return new DefaultEbeanConfig(ebeanConfig.getDefaultDatasource(), serverConfigs);
return new DefaultEbeanConfig(
ebeanConfig.getDefaultDatasource(),
serverConfigs,
ebeanConfig.generateEvolutionsScripts());
}

private void setServerConfigDataSource(String key, DatabaseConfig serverConfig) {
Expand Down
2 changes: 2 additions & 0 deletions play-ebean/src/main/java/play/db/ebean/EbeanConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ public interface EbeanConfig {
String defaultServer();

Map<String, DatabaseConfig> serverConfigs();

boolean generateEvolutionsScripts();
}
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.generateEvolutionsScripts()) {
return;
}
config
.serverConfigs()
.forEach(
Expand Down
20 changes: 18 additions & 2 deletions play-ebean/src/main/java/play/db/ebean/EbeanParsedConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,22 @@
public class EbeanParsedConfig {

private final String defaultDatasource;

private final Map<String, List<String>> datasourceModels;

public EbeanParsedConfig(String defaultDatasource, Map<String, List<String>> datasourceModels) {
private final boolean generateEvolutionsScripts;

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

public EbeanParsedConfig(String defaultDatasource, Map<String, List<String>> datasourceModels) {
this(defaultDatasource, datasourceModels, true);
}

public String getDefaultDatasource() {
Expand All @@ -33,6 +44,10 @@ public Map<String, List<String>> getDatasourceModels() {
return datasourceModels;
}

public boolean generateEvolutionsScripts() {
return generateEvolutionsScripts;
}

/**
* Parse a play configuration.
*
Expand All @@ -44,6 +59,7 @@ public static EbeanParsedConfig parseFromConfig(Config config) {
Config playEbeanConfig = config.getConfig("play.ebean");
String defaultDatasource = playEbeanConfig.getString("defaultDatasource");
String ebeanConfigKey = playEbeanConfig.getString("config");
boolean generateEvolutionsScripts = playEbeanConfig.getBoolean("generateEvolutionsScripts");

Map<String, List<String>> datasourceModels = new HashMap<>();

Expand All @@ -64,6 +80,6 @@ public static EbeanParsedConfig parseFromConfig(Config config) {
datasourceModels.put(key, models);
});
}
return new EbeanParsedConfig(defaultDatasource, datasourceModels);
return new EbeanParsedConfig(defaultDatasource, datasourceModels, generateEvolutionsScripts);
}
}
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 @@ -9,5 +9,8 @@ play {

# The name of the default ebean datasource
defaultDatasource = "default"

# The key to control the generation of the Evolutions scripts
generateEvolutionsScripts = true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public void defaultConfig() {
EbeanParsedConfig config = parse(Collections.emptyMap());
assertThat(config.getDefaultDatasource(), equalTo("default"));
assertThat(config.getDatasourceModels().size(), equalTo(0));
assertThat(config.generateEvolutionsScripts(), equalTo(Boolean.TRUE));
}

@Test
Expand All @@ -52,6 +53,13 @@ public void customDefault() {
assertThat(config.getDefaultDatasource(), equalTo("custom"));
}

@Test
public void disableGenerateEvolutionsScripts() {
EbeanParsedConfig config =
parse(ImmutableMap.of("play.ebean.generateEvolutionsScripts", false));
assertThat(config.generateEvolutionsScripts(), equalTo(Boolean.FALSE));
}

@Test
public void customConfig() {
EbeanParsedConfig config =
Expand Down
Loading