Add url,user,password to LiquibaseProperties
User can set those properties (optionally) to use a different DataSource than the default. Fixes gh-1558
This commit is contained in:
@@ -31,6 +31,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.data.jpa.EntityManagerFactoryDependsOnPostProcessor;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
@@ -88,12 +89,21 @@ public class LiquibaseAutoConfiguration {
|
||||
SpringLiquibase liquibase = new SpringLiquibase();
|
||||
liquibase.setChangeLog(this.properties.getChangeLog());
|
||||
liquibase.setContexts(this.properties.getContexts());
|
||||
liquibase.setDataSource(this.dataSource);
|
||||
liquibase.setDataSource(getDataSource());
|
||||
liquibase.setDefaultSchema(this.properties.getDefaultSchema());
|
||||
liquibase.setDropFirst(this.properties.isDropFirst());
|
||||
liquibase.setShouldRun(this.properties.isEnabled());
|
||||
return liquibase;
|
||||
}
|
||||
|
||||
private DataSource getDataSource() {
|
||||
if (this.properties.getUrl() == null) {
|
||||
return this.dataSource;
|
||||
}
|
||||
return DataSourceBuilder.create().url(this.properties.getUrl())
|
||||
.username(this.properties.getUser())
|
||||
.password(this.properties.getPassword()).build();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -44,6 +44,12 @@ public class LiquibaseProperties {
|
||||
|
||||
private boolean enabled = true;
|
||||
|
||||
private String user;
|
||||
|
||||
private String password;
|
||||
|
||||
private String url;
|
||||
|
||||
public String getChangeLog() {
|
||||
return this.changeLog;
|
||||
}
|
||||
@@ -91,4 +97,29 @@ public class LiquibaseProperties {
|
||||
public void setEnabled(boolean enabled) {
|
||||
this.enabled = enabled;
|
||||
}
|
||||
|
||||
public String getUser() {
|
||||
return this.user;
|
||||
}
|
||||
|
||||
public void setUser(String user) {
|
||||
this.user = user;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return this.password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
return this.url;
|
||||
}
|
||||
|
||||
public void setUrl(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -129,6 +129,19 @@ public class LiquibaseAutoConfigurationTests {
|
||||
assertTrue(liquibase.isDropFirst());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOverrideDataSource() throws Exception {
|
||||
EnvironmentTestUtils.addEnvironment(this.context,
|
||||
"liquibase.url:jdbc:hsqldb:mem:liquibase", "liquibase.user:sa");
|
||||
this.context.register(EmbeddedDataSourceConfiguration.class,
|
||||
LiquibaseAutoConfiguration.class,
|
||||
PropertyPlaceholderAutoConfiguration.class);
|
||||
this.context.refresh();
|
||||
SpringLiquibase liquibase = this.context.getBean(SpringLiquibase.class);
|
||||
assertEquals("jdbc:hsqldb:mem:liquibase", liquibase.getDataSource()
|
||||
.getConnection().getMetaData().getURL());
|
||||
}
|
||||
|
||||
@Test(expected = BeanCreationException.class)
|
||||
public void testChangeLogDoesNotExist() throws Exception {
|
||||
EnvironmentTestUtils.addEnvironment(this.context,
|
||||
|
||||
Reference in New Issue
Block a user