Commit e8eace2d authored by Andy Wilkinson's avatar Andy Wilkinson

Polish "Fix Liquibase endpoint's output with multiple datasources"

See gh-19171
parent 5302d919
...@@ -25,7 +25,6 @@ import java.util.stream.Collectors; ...@@ -25,7 +25,6 @@ import java.util.stream.Collectors;
import javax.sql.DataSource; import javax.sql.DataSource;
import liquibase.changelog.ChangeLogHistoryService;
import liquibase.changelog.ChangeSet.ExecType; import liquibase.changelog.ChangeSet.ExecType;
import liquibase.changelog.RanChangeSet; import liquibase.changelog.RanChangeSet;
import liquibase.changelog.StandardChangeLogHistoryService; import liquibase.changelog.StandardChangeLogHistoryService;
...@@ -63,10 +62,8 @@ public class LiquibaseEndpoint { ...@@ -63,10 +62,8 @@ public class LiquibaseEndpoint {
while (target != null) { while (target != null) {
Map<String, LiquibaseBean> liquibaseBeans = new HashMap<>(); Map<String, LiquibaseBean> liquibaseBeans = new HashMap<>();
DatabaseFactory factory = DatabaseFactory.getInstance(); DatabaseFactory factory = DatabaseFactory.getInstance();
this.context.getBeansOfType(SpringLiquibase.class).forEach((name, liquibase) -> { this.context.getBeansOfType(SpringLiquibase.class)
StandardChangeLogHistoryService service = new StandardChangeLogHistoryService(); .forEach((name, liquibase) -> liquibaseBeans.put(name, createReport(liquibase, factory)));
liquibaseBeans.put(name, createReport(liquibase, service, factory));
});
ApplicationContext parent = target.getParent(); ApplicationContext parent = target.getParent();
contextBeans.put(target.getId(), contextBeans.put(target.getId(),
new ContextLiquibaseBeans(liquibaseBeans, (parent != null) ? parent.getId() : null)); new ContextLiquibaseBeans(liquibaseBeans, (parent != null) ? parent.getId() : null));
...@@ -75,8 +72,7 @@ public class LiquibaseEndpoint { ...@@ -75,8 +72,7 @@ public class LiquibaseEndpoint {
return new ApplicationLiquibaseBeans(contextBeans); return new ApplicationLiquibaseBeans(contextBeans);
} }
private LiquibaseBean createReport(SpringLiquibase liquibase, ChangeLogHistoryService service, private LiquibaseBean createReport(SpringLiquibase liquibase, DatabaseFactory factory) {
DatabaseFactory factory) {
try { try {
DataSource dataSource = liquibase.getDataSource(); DataSource dataSource = liquibase.getDataSource();
JdbcConnection connection = new JdbcConnection(dataSource.getConnection()); JdbcConnection connection = new JdbcConnection(dataSource.getConnection());
...@@ -89,6 +85,7 @@ public class LiquibaseEndpoint { ...@@ -89,6 +85,7 @@ public class LiquibaseEndpoint {
} }
database.setDatabaseChangeLogTableName(liquibase.getDatabaseChangeLogTable()); database.setDatabaseChangeLogTableName(liquibase.getDatabaseChangeLogTable());
database.setDatabaseChangeLogLockTableName(liquibase.getDatabaseChangeLogLockTable()); database.setDatabaseChangeLogLockTableName(liquibase.getDatabaseChangeLogLockTable());
StandardChangeLogHistoryService service = new StandardChangeLogHistoryService();
service.setDatabase(database); service.setDatabase(database);
return new LiquibaseBean( return new LiquibaseBean(
service.getRanChangeSets().stream().map(ChangeSet::new).collect(Collectors.toList())); service.getRanChangeSets().stream().map(ChangeSet::new).collect(Collectors.toList()));
......
...@@ -22,8 +22,6 @@ import java.util.Map; ...@@ -22,8 +22,6 @@ import java.util.Map;
import javax.sql.DataSource; import javax.sql.DataSource;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import liquibase.integration.spring.SpringLiquibase; import liquibase.integration.spring.SpringLiquibase;
import org.junit.Test; import org.junit.Test;
...@@ -31,10 +29,12 @@ import org.springframework.boot.actuate.liquibase.LiquibaseEndpoint.LiquibaseBea ...@@ -31,10 +29,12 @@ import org.springframework.boot.actuate.liquibase.LiquibaseEndpoint.LiquibaseBea
import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration; import org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration;
import org.springframework.boot.jdbc.EmbeddedDatabaseConnection;
import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
...@@ -97,17 +97,18 @@ public class LiquibaseEndpointTests { ...@@ -97,17 +97,18 @@ public class LiquibaseEndpointTests {
} }
@Test @Test
public void multipleLiquibaseReportIsReturned() { public void whenMultipleLiquibaseBeansArePresentChangeSetsAreCorrectlyReportedForEachBean() {
this.contextRunner.withUserConfiguration(Config.class, LiquibaseConfiguration.class).run((context) -> { this.contextRunner.withUserConfiguration(Config.class, MultipleDataSourceLiquibaseConfiguration.class)
Map<String, LiquibaseBean> liquibaseBeans = context.getBean(LiquibaseEndpoint.class).liquibaseBeans() .run((context) -> {
.getContexts().get(context.getId()).getLiquibaseBeans(); Map<String, LiquibaseBean> liquibaseBeans = context.getBean(LiquibaseEndpoint.class)
assertThat(liquibaseBeans.get("liquibase").getChangeSets()).hasSize(1); .liquibaseBeans().getContexts().get(context.getId()).getLiquibaseBeans();
assertThat(liquibaseBeans.get("liquibase").getChangeSets().get(0).getChangeLog()) assertThat(liquibaseBeans.get("liquibase").getChangeSets()).hasSize(1);
.isEqualTo("classpath:/db/changelog/db.changelog-master.yaml"); assertThat(liquibaseBeans.get("liquibase").getChangeSets().get(0).getChangeLog())
assertThat(liquibaseBeans.get("liquibaseBackup").getChangeSets()).hasSize(1); .isEqualTo("classpath:/db/changelog/db.changelog-master.yaml");
assertThat(liquibaseBeans.get("liquibaseBackup").getChangeSets().get(0).getChangeLog()) assertThat(liquibaseBeans.get("liquibaseBackup").getChangeSets()).hasSize(1);
.isEqualTo("classpath:/db/changelog/db.changelog-master-backup.yaml"); assertThat(liquibaseBeans.get("liquibaseBackup").getChangeSets().get(0).getChangeLog())
}); .isEqualTo("classpath:/db/changelog/db.changelog-master-backup.yaml");
});
} }
private boolean getAutoCommit(DataSource dataSource) throws SQLException { private boolean getAutoCommit(DataSource dataSource) throws SQLException {
...@@ -127,39 +128,38 @@ public class LiquibaseEndpointTests { ...@@ -127,39 +128,38 @@ public class LiquibaseEndpointTests {
} }
@Configuration @Configuration
static class LiquibaseConfiguration { static class MultipleDataSourceLiquibaseConfiguration {
@Bean @Bean
DataSource dataSource() { DataSource dataSource() {
HikariConfig config = new HikariConfig(); return createEmbeddedDatabase();
config.setJdbcUrl("jdbc:hsqldb:mem:test");
config.setUsername("sa");
return new HikariDataSource(config);
} }
@Bean @Bean
DataSource dataSourceBackup() { DataSource dataSourceBackup() {
HikariConfig config = new HikariConfig(); return createEmbeddedDatabase();
config.setJdbcUrl("jdbc:hsqldb:mem:testBackup");
config.setUsername("sa");
return new HikariDataSource(config);
} }
@Bean @Bean
SpringLiquibase liquibase(DataSource dataSource) { SpringLiquibase liquibase(DataSource dataSource) {
SpringLiquibase liquibase = new SpringLiquibase(); return createSpringLiquibase("db.changelog-master.yaml", dataSource);
liquibase.setChangeLog("classpath:/db/changelog/db.changelog-master.yaml");
liquibase.setShouldRun(true);
liquibase.setDataSource(dataSource);
return liquibase;
} }
@Bean @Bean
SpringLiquibase liquibaseBackup(DataSource dataSourceBackup) { SpringLiquibase liquibaseBackup(DataSource dataSourceBackup) {
return createSpringLiquibase("db.changelog-master-backup.yaml", dataSourceBackup);
}
private DataSource createEmbeddedDatabase() {
return new EmbeddedDatabaseBuilder().generateUniqueName(true)
.setType(EmbeddedDatabaseConnection.HSQL.getType()).build();
}
private SpringLiquibase createSpringLiquibase(String changeLog, DataSource dataSource) {
SpringLiquibase liquibase = new SpringLiquibase(); SpringLiquibase liquibase = new SpringLiquibase();
liquibase.setChangeLog("classpath:/db/changelog/db.changelog-master-backup.yaml"); liquibase.setChangeLog("classpath:/db/changelog/" + changeLog);
liquibase.setShouldRun(true); liquibase.setShouldRun(true);
liquibase.setDataSource(dataSourceBackup); liquibase.setDataSource(dataSource);
return liquibase; return liquibase;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment