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

feat(core): add environments block to datasource configuration #293

Closed
wants to merge 3 commits into from
Closed
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
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,11 @@
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.TEST_ENV_KEY;
import static org.grails.forge.feature.config.ConfigurationFeature.PROD_ENV_KEY;
import static org.grails.forge.feature.config.ConfigurationFeature.ENV_KEY;

/**
* A feature that configures a datasource with a driver
*/
Expand All @@ -33,11 +38,20 @@ public interface DatabaseDriverConfigurationFeature extends Feature {

String getPasswordKey();

String getDbCreateKey();

default void applyDefaultConfig(DatabaseDriverFeature dbFeature, Map<String, Object> config) {
Optional.ofNullable(dbFeature.getJdbcUrl()).ifPresent(url -> config.put(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));

config.put(ENV_KEY + "." + DEV_ENV_KEY + "." + getDbCreateKey(), "create-drop");
Optional.ofNullable(dbFeature.getJdbcDevUrl()).ifPresent(url -> config.put(ENV_KEY + "." + DEV_ENV_KEY + "." + getUrlKey(), url));
config.put(ENV_KEY + "." + TEST_ENV_KEY + "." + getDbCreateKey(), "update");
Optional.ofNullable(dbFeature.getJdbcTestUrl()).ifPresent(url -> config.put(ENV_KEY + "." + TEST_ENV_KEY + "." + getUrlKey(), url));
config.put(ENV_KEY + "." + PROD_ENV_KEY + "." + getDbCreateKey(), "none");
Optional.ofNullable(dbFeature.getJdbcProdUrl()).ifPresent(url -> config.put(ENV_KEY + "." + PROD_ENV_KEY + "." + getUrlKey(), url));

final Map<String, Object> additionalConfig = dbFeature.getAdditionalConfig();
if (!additionalConfig.isEmpty()) {
config.putAll(additionalConfig);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,16 @@ public String getCategory() {

public abstract boolean embedded();

public abstract String getJdbcUrl();
@Deprecated()
public String getJdbcUrl() {
return getJdbcProdUrl();
}

public abstract String getJdbcDevUrl();

public abstract String getJdbcTestUrl();

public abstract String getJdbcProdUrl();

public abstract String getDriverClass();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,20 @@ public String getDescription() {
}

@Override
public String getJdbcUrl() {
public String getJdbcDevUrl() {
return "jdbc:h2:mem:devDb;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE";
}

@Override
public String getJdbcTestUrl() {
return "jdbc:h2:mem:testDb;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE";
}

@Override
public String getJdbcProdUrl() {
return "jdbc:h2:./prodDb;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE";
}

@Override
public String getDriverClass() {
return "org.h2.Driver";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class HibernateGorm extends GormFeature implements DatabaseDriverConfigur
private static final String DRIVER_KEY = PREFIX + "driverClassName";
private static final String USERNAME_KEY = PREFIX + "username";
private static final String PASSWORD_KEY = PREFIX + "password";
private static final String DB_CREATE_KEY = PREFIX + "dbCreate";

private final DatabaseDriverFeature defaultDbFeature;

Expand Down Expand Up @@ -125,6 +126,11 @@ public String getPasswordKey() {
return PASSWORD_KEY;
}

@Override
public String getDbCreateKey() {
return DB_CREATE_KEY;
}

@Override
public boolean shouldApply(ApplicationType applicationType, Options options, Set<Feature> selectedFeatures) {
return selectedFeatures.stream().anyMatch(f -> f instanceof HibernateGorm) || options.getGormImpl() == GormImpl.HIBERNATE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,17 @@ public String getDescription() {
}

@Override
public String getJdbcUrl() {
public String getJdbcDevUrl() {
return "jdbc:mysql://localhost:3306/devDb";
}

@Override
public String getJdbcTestUrl() {
return "jdbc:mysql://localhost:3306/testDb";
}

@Override
public String getJdbcProdUrl() {
return "jdbc:mysql://localhost:3306/db";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,17 @@ public String getDescription() {
}

@Override
public String getJdbcUrl() {
public String getJdbcDevUrl() {
return "jdbc:postgresql://localhost:5432/devDb";
}

@Override
public String getJdbcTestUrl() {
return "jdbc:postgresql://localhost:5432/testDb";
}

@Override
public String getJdbcProdUrl() {
// postgres docker image uses default db name and username of postgres so we use the same
return "jdbc:postgresql://localhost:5432/postgres";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,17 @@ public String getDescription() {
}

@Override
public String getJdbcUrl() {
public String getJdbcDevUrl() {
return "jdbc:sqlserver://localhost:1433;databaseName=devDb";
}

@Override
public String getJdbcTestUrl() {
return "jdbc:sqlserver://localhost:1433;databaseName=testDb";
}

@Override
public String getJdbcProdUrl() {
return "jdbc:sqlserver://localhost:1433;databaseName=tempdb";
}

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 @@ -62,12 +63,28 @@ class HibernateGormSpec extends ApplicationContextSpec implements CommandOutputF
GeneratorContext ctx = buildGeneratorContext(['gorm-hibernate5'])

then:
ctx.configuration.containsKey("dataSource.url")
// ctx.configuration.containsKey("dataSource.url")
ctx.configuration.containsKey("dataSource.pooled")
ctx.configuration.containsKey("dataSource.jmxExport")
ctx.configuration.containsKey("dataSource.dbCreate")
ctx.configuration.containsKey("hibernate.cache.queries")
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.dbCreate == 'create-drop'
config.environments.test.dataSource.dbCreate == 'update'
config.environments.production.dataSource.dbCreate == 'none'
config.environments.development.dataSource.url == 'jdbc:h2:mem:devDb;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE'
config.environments.test.dataSource.url == 'jdbc:h2:mem:testDb;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE'
config.environments.production.dataSource.url == 'jdbc:h2:./prodDb;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE'
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ class MySQLSpec extends ApplicationContextSpec {
GeneratorContext ctx = buildGeneratorContext(["gorm-hibernate5", "mysql"])

then:
ctx.getConfiguration().get("dataSource.url") == 'jdbc:mysql://localhost:3306/db'
ctx.getConfiguration().get("dataSource.driverClassName") == 'com.mysql.cj.jdbc.Driver'
ctx.getConfiguration().get("dataSource.username") == 'root'
ctx.getConfiguration().get("dataSource.password") == ''
ctx.getConfiguration().get("environments.development.dataSource.url") == 'jdbc:mysql://localhost:3306/devDb'
ctx.getConfiguration().get("environments.test.dataSource.url") == 'jdbc:mysql://localhost:3306/testDb'
ctx.getConfiguration().get("environments.production.dataSource.url") == 'jdbc:mysql://localhost:3306/db'
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ class PostgresSpec extends ApplicationContextSpec {
GeneratorContext ctx = buildGeneratorContext(["gorm-hibernate5", "postgres"])

then:
ctx.getConfiguration().get("dataSource.url") == 'jdbc:postgresql://localhost:5432/postgres'
ctx.getConfiguration().get("dataSource.driverClassName") == 'org.postgresql.Driver'
ctx.getConfiguration().get("dataSource.username") == 'postgres'
ctx.getConfiguration().get("dataSource.password") == ''
ctx.getConfiguration().get("environments.development.dataSource.url") == 'jdbc:postgresql://localhost:5432/devDb'
ctx.getConfiguration().get("environments.test.dataSource.url") == 'jdbc:postgresql://localhost:5432/testDb'
ctx.getConfiguration().get("environments.production.dataSource.url") == 'jdbc:postgresql://localhost:5432/postgres'
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ class SQLServerSpec extends ApplicationContextSpec {
when:
GeneratorContext ctx = buildGeneratorContext(["gorm-hibernate5", "sqlserver"])

then:
ctx.getConfiguration().get("dataSource.url") == 'jdbc:sqlserver://localhost:1433;databaseName=tempdb'
ctx.getConfiguration().get("dataSource.driverClassName") == 'com.microsoft.sqlserver.jdbc.SQLServerDriver'
ctx.getConfiguration().get("dataSource.username") == 'sa'
ctx.getConfiguration().get("dataSource.password") == ''
then:
ctx.getConfiguration().get("environments.development.dataSource.url") == 'jdbc:sqlserver://localhost:1433;databaseName=devDb'
ctx.getConfiguration().get("environments.test.dataSource.url") == 'jdbc:sqlserver://localhost:1433;databaseName=testDb'
ctx.getConfiguration().get("environments.production.dataSource.url") == 'jdbc:sqlserver://localhost:1433;databaseName=tempdb'
}

}
Loading