Merge pull request #5788 from eddumelendez/gh-5402

* pr/5788:
  Support different schema/data DB script users
pull/5784/merge
Phillip Webb 9 years ago
commit 78639fffd0

@ -1,5 +1,5 @@
/*
* Copyright 2012-2015 the original author or authors.
* Copyright 2012-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -41,6 +41,7 @@ import org.springframework.util.StringUtils;
*
* @author Dave Syer
* @author Phillip Webb
* @author Eddú Meléndez
* @since 1.1.0
* @see DataSourceAutoConfiguration
*/
@ -78,7 +79,9 @@ class DataSourceInitializer implements ApplicationListener<DataSourceInitialized
private void runSchemaScripts() {
List<Resource> scripts = getScripts(this.properties.getSchema(), "schema");
if (!scripts.isEmpty()) {
runScripts(scripts);
String username = this.properties.getSchemaUsername();
String password = this.properties.getSchemaPassword();
runScripts(scripts, username, password);
try {
this.applicationContext
.publishEvent(new DataSourceInitializedEvent(this.dataSource));
@ -111,7 +114,9 @@ class DataSourceInitializer implements ApplicationListener<DataSourceInitialized
private void runDataScripts() {
List<Resource> scripts = getScripts(this.properties.getData(), "data");
runScripts(scripts);
String username = this.properties.getDataUsername();
String password = this.properties.getDataPassword();
runScripts(scripts, username, password);
}
private List<Resource> getScripts(String locations, String fallback) {
@ -141,7 +146,7 @@ class DataSourceInitializer implements ApplicationListener<DataSourceInitialized
return resources;
}
private void runScripts(List<Resource> resources) {
private void runScripts(List<Resource> resources, String username, String password) {
if (resources.isEmpty()) {
return;
}
@ -154,7 +159,14 @@ class DataSourceInitializer implements ApplicationListener<DataSourceInitialized
for (Resource resource : resources) {
populator.addScript(resource);
}
DatabasePopulatorUtils.execute(populator, this.dataSource);
DataSource dataSource = this.dataSource;
if (StringUtils.hasText(username) && StringUtils.hasText(password)) {
dataSource = DataSourceBuilder.create(this.properties.getClassLoader())
.driverClassName(this.properties.determineDriverClassName())
.url(this.properties.determineUrl()).username(username)
.password(password).build();
}
DatabasePopulatorUtils.execute(populator, dataSource);
}
}

@ -41,6 +41,7 @@ import org.springframework.util.StringUtils;
* @author Maciej Walkowiak
* @author Stephane Nicoll
* @author Benedikt Ritter
* @author Eddú Meléndez
* @since 1.1.0
*/
@ConfigurationProperties(prefix = "spring.datasource")
@ -103,11 +104,31 @@ public class DataSourceProperties
*/
private String schema;
/**
* User of the database to execute DDL scripts (if different).
*/
private String schemaUsername;
/**
* Password of the database to execute DDL scripts (if different).
*/
private String schemaPassword;
/**
* Data (DML) script resource reference.
*/
private String data;
/**
* User of the database to execute DML scripts.
*/
private String dataUsername;
/**
* Password of the database to execute DML scripts.
*/
private String dataPassword;
/**
* Do not stop if an error occurs while initializing the database.
*/
@ -338,6 +359,22 @@ public class DataSourceProperties
this.schema = schema;
}
public String getSchemaUsername() {
return this.schemaUsername;
}
public void setSchemaUsername(String schemaUsername) {
this.schemaUsername = schemaUsername;
}
public String getSchemaPassword() {
return this.schemaPassword;
}
public void setSchemaPassword(String schemaPassword) {
this.schemaPassword = schemaPassword;
}
public String getData() {
return this.data;
}
@ -346,6 +383,22 @@ public class DataSourceProperties
this.data = script;
}
public String getDataUsername() {
return this.dataUsername;
}
public void setDataUsername(String dataUsername) {
this.dataUsername = dataUsername;
}
public String getDataPassword() {
return this.dataPassword;
}
public void setDataPassword(String dataPassword) {
this.dataPassword = dataPassword;
}
public boolean isContinueOnError() {
return this.continueOnError;
}

@ -25,6 +25,7 @@ import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.UnsatisfiedDependencyException;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
@ -200,6 +201,50 @@ public class DataSourceInitializerTests {
}
}
@Test
public void testDataSourceInitializedWithSchemaCredentials() {
this.context.register(DataSourceAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
EnvironmentTestUtils.addEnvironment(this.context,
"spring.datasource.initialize:true",
"spring.datasource.sqlScriptEncoding:UTF-8",
"spring.datasource.schema:" + ClassUtils
.addResourcePathToPackagePath(getClass(), "encoding-schema.sql"),
"spring.datasource.data:" + ClassUtils
.addResourcePathToPackagePath(getClass(), "encoding-data.sql"),
"spring.datasource.schema-username:admin",
"spring.datasource.schema-password:admin");
try {
this.context.refresh();
fail("User does not exist");
}
catch (Exception ex) {
assertThat(ex).isInstanceOf(UnsatisfiedDependencyException.class);
}
}
@Test
public void testDataSourceInitializedWithDataCredentials() {
this.context.register(DataSourceAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
EnvironmentTestUtils.addEnvironment(this.context,
"spring.datasource.initialize:true",
"spring.datasource.sqlScriptEncoding:UTF-8",
"spring.datasource.schema:" + ClassUtils
.addResourcePathToPackagePath(getClass(), "encoding-schema.sql"),
"spring.datasource.data:" + ClassUtils
.addResourcePathToPackagePath(getClass(), "encoding-data.sql"),
"spring.datasource.data-username:admin",
"spring.datasource.data-password:admin");
try {
this.context.refresh();
fail("User does not exist");
}
catch (Exception ex) {
assertThat(ex).isInstanceOf(UnsatisfiedDependencyException.class);
}
}
@Configuration
@EnableConfigurationProperties
protected static class TwoDataSources {

@ -25,6 +25,7 @@ import static org.assertj.core.api.Assertions.assertThat;
*
* @author Maciej Walkowiak
* @author Stephane Nicoll
* @author Eddú Meléndez
*/
public class DataSourcePropertiesTests {
@ -99,4 +100,22 @@ public class DataSourcePropertiesTests {
assertThat(properties.determinePassword()).isEqualTo("bar");
}
@Test
public void determineCredentialsForSchemaScripts() {
DataSourceProperties properties = new DataSourceProperties();
properties.setSchemaUsername("foo");
properties.setSchemaPassword("bar");
assertThat(properties.getSchemaUsername()).isEqualTo("foo");
assertThat(properties.getSchemaPassword()).isEqualTo("bar");
}
@Test
public void determineCredentialsForDataScripts() {
DataSourceProperties properties = new DataSourceProperties();
properties.setDataUsername("foo");
properties.setDataPassword("bar");
assertThat(properties.getDataUsername()).isEqualTo("foo");
assertThat(properties.getDataPassword()).isEqualTo("bar");
}
}

@ -590,6 +590,8 @@ content into your application; rather pick only the properties that you need.
# DATASOURCE ({sc-spring-boot-autoconfigure}/jdbc/DataSourceAutoConfiguration.{sc-ext}[DataSourceAutoConfiguration] & {sc-spring-boot-autoconfigure}/jdbc/DataSourceProperties.{sc-ext}[DataSourceProperties])
spring.datasource.continue-on-error=false # Do not stop if an error occurs while initializing the database.
spring.datasource.data= # Data (DML) script resource reference.
spring.datasource.data-username= # User of the database to execute DML scripts (if different).
spring.datasource.data-password= # Password of the database to execute DML scripts (if different).
spring.datasource.dbcp.*= # Commons DBCP specific settings
spring.datasource.dbcp2.*= # Commons DBCP2 specific settings
spring.datasource.driver-class-name= # Fully qualified name of the JDBC driver. Auto-detected based on the URL by default.
@ -601,6 +603,8 @@ content into your application; rather pick only the properties that you need.
spring.datasource.password= # Login password of the database.
spring.datasource.platform=all # Platform to use in the schema resource (schema-${platform}.sql).
spring.datasource.schema= # Schema (DDL) script resource reference.
spring.datasource.schema-username= # User of the database to execute DDL scripts (if different).
spring.datasource.schema-password= # Password of the database to execute DDL scripts (if different).
spring.datasource.separator=; # Statement separator in SQL initialization scripts.
spring.datasource.sql-script-encoding= # SQL scripts encoding.
spring.datasource.tomcat.*= # Tomcat datasource specific settings

Loading…
Cancel
Save