Skip to content

Commit

Permalink
feat(core): add environments block to datasource configuration
Browse files Browse the repository at this point in the history
This will add datasource config for each environments (development, test, and production).

Fixes grails/grails-core#13103
  • Loading branch information
puneetbehl committed Mar 7, 2024
1 parent 954ef75 commit 0bb178c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@

public interface ConfigurationFeature extends OneOfFeature {

String ENV_KEY = "environments";
String DEV_ENV_KEY = "development";
String TEST_ENV_KEY = "test";
String PROD_ENV_KEY = "production";

@Override
default Class<?> getFeatureClass() {
return ConfigurationFeature.class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import java.util.Map;
import java.util.Optional;

import static org.grails.forge.feature.config.ConfigurationFeature.DEV_ENV_KEY;
import static org.grails.forge.feature.config.ConfigurationFeature.ENV_KEY;

/**
* A feature that configures a datasource with a driver
*/
Expand All @@ -34,7 +37,7 @@ public interface DatabaseDriverConfigurationFeature extends Feature {
String getPasswordKey();

default void applyDefaultConfig(DatabaseDriverFeature dbFeature, Map<String, Object> config) {
Optional.ofNullable(dbFeature.getJdbcUrl()).ifPresent(url -> config.put(getUrlKey(), url));
Optional.ofNullable(dbFeature.getJdbcUrl()).ifPresent(url -> config.put(ENV_KEY + "." + DEV_ENV_KEY + "." + getUrlKey(), url));
Optional.ofNullable(dbFeature.getDriverClass()).ifPresent(driver -> config.put(getDriverKey(), driver));
Optional.ofNullable(dbFeature.getDefaultUser()).ifPresent(user -> config.put(getUsernameKey(), user));
Optional.ofNullable(dbFeature.getDefaultPassword()).ifPresent(pass -> config.put(getPasswordKey(), pass));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.grails.forge.feature.database

import groovy.yaml.YamlSlurper
import org.grails.forge.ApplicationContextSpec
import org.grails.forge.BuildBuilder
import org.grails.forge.application.ApplicationType
Expand Down Expand Up @@ -70,4 +71,15 @@ class HibernateGormSpec extends ApplicationContextSpec implements CommandOutputF
ctx.configuration.containsKey("hibernate.cache.use_second_level_cache")
ctx.configuration.containsKey("hibernate.cache.use_query_cache")
}

void "test match values of datasource config"() {

when:
final def output = generate(ApplicationType.WEB, new Options(TestFramework.SPOCK, JdkVersion.JDK_11))
final String applicationYaml = output["grails-app/conf/application.yml"]
def config = new YamlSlurper().parseText(applicationYaml)

then:
config.environments.development.dataSource.url == 'jdbc:h2:mem:devDb;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE'
}
}

0 comments on commit 0bb178c

Please sign in to comment.