Skip to content

Commit e8eace2

Browse files
committed
Polish "Fix Liquibase endpoint's output with multiple datasources"
See gh-19171
1 parent 5302d91 commit e8eace2

File tree

2 files changed

+33
-36
lines changed

2 files changed

+33
-36
lines changed

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/liquibase/LiquibaseEndpoint.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525

2626
import javax.sql.DataSource;
2727

28-
import liquibase.changelog.ChangeLogHistoryService;
2928
import liquibase.changelog.ChangeSet.ExecType;
3029
import liquibase.changelog.RanChangeSet;
3130
import liquibase.changelog.StandardChangeLogHistoryService;
@@ -63,10 +62,8 @@ public ApplicationLiquibaseBeans liquibaseBeans() {
6362
while (target != null) {
6463
Map<String, LiquibaseBean> liquibaseBeans = new HashMap<>();
6564
DatabaseFactory factory = DatabaseFactory.getInstance();
66-
this.context.getBeansOfType(SpringLiquibase.class).forEach((name, liquibase) -> {
67-
StandardChangeLogHistoryService service = new StandardChangeLogHistoryService();
68-
liquibaseBeans.put(name, createReport(liquibase, service, factory));
69-
});
65+
this.context.getBeansOfType(SpringLiquibase.class)
66+
.forEach((name, liquibase) -> liquibaseBeans.put(name, createReport(liquibase, factory)));
7067
ApplicationContext parent = target.getParent();
7168
contextBeans.put(target.getId(),
7269
new ContextLiquibaseBeans(liquibaseBeans, (parent != null) ? parent.getId() : null));
@@ -75,8 +72,7 @@ public ApplicationLiquibaseBeans liquibaseBeans() {
7572
return new ApplicationLiquibaseBeans(contextBeans);
7673
}
7774

78-
private LiquibaseBean createReport(SpringLiquibase liquibase, ChangeLogHistoryService service,
79-
DatabaseFactory factory) {
75+
private LiquibaseBean createReport(SpringLiquibase liquibase, DatabaseFactory factory) {
8076
try {
8177
DataSource dataSource = liquibase.getDataSource();
8278
JdbcConnection connection = new JdbcConnection(dataSource.getConnection());
@@ -89,6 +85,7 @@ private LiquibaseBean createReport(SpringLiquibase liquibase, ChangeLogHistorySe
8985
}
9086
database.setDatabaseChangeLogTableName(liquibase.getDatabaseChangeLogTable());
9187
database.setDatabaseChangeLogLockTableName(liquibase.getDatabaseChangeLogLockTable());
88+
StandardChangeLogHistoryService service = new StandardChangeLogHistoryService();
9289
service.setDatabase(database);
9390
return new LiquibaseBean(
9491
service.getRanChangeSets().stream().map(ChangeSet::new).collect(Collectors.toList()));

spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/liquibase/LiquibaseEndpointTests.java

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,19 @@
2222

2323
import javax.sql.DataSource;
2424

25-
import com.zaxxer.hikari.HikariConfig;
26-
import com.zaxxer.hikari.HikariDataSource;
2725
import liquibase.integration.spring.SpringLiquibase;
2826
import org.junit.Test;
2927

3028
import org.springframework.boot.actuate.liquibase.LiquibaseEndpoint.LiquibaseBean;
3129
import org.springframework.boot.autoconfigure.AutoConfigurations;
3230
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
3331
import org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration;
32+
import org.springframework.boot.jdbc.EmbeddedDatabaseConnection;
3433
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
3534
import org.springframework.context.ApplicationContext;
3635
import org.springframework.context.annotation.Bean;
3736
import org.springframework.context.annotation.Configuration;
37+
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
3838

3939
import static org.assertj.core.api.Assertions.assertThat;
4040

@@ -97,17 +97,18 @@ public void connectionAutoCommitPropertyIsReset() {
9797
}
9898

9999
@Test
100-
public void multipleLiquibaseReportIsReturned() {
101-
this.contextRunner.withUserConfiguration(Config.class, LiquibaseConfiguration.class).run((context) -> {
102-
Map<String, LiquibaseBean> liquibaseBeans = context.getBean(LiquibaseEndpoint.class).liquibaseBeans()
103-
.getContexts().get(context.getId()).getLiquibaseBeans();
104-
assertThat(liquibaseBeans.get("liquibase").getChangeSets()).hasSize(1);
105-
assertThat(liquibaseBeans.get("liquibase").getChangeSets().get(0).getChangeLog())
106-
.isEqualTo("classpath:/db/changelog/db.changelog-master.yaml");
107-
assertThat(liquibaseBeans.get("liquibaseBackup").getChangeSets()).hasSize(1);
108-
assertThat(liquibaseBeans.get("liquibaseBackup").getChangeSets().get(0).getChangeLog())
109-
.isEqualTo("classpath:/db/changelog/db.changelog-master-backup.yaml");
110-
});
100+
public void whenMultipleLiquibaseBeansArePresentChangeSetsAreCorrectlyReportedForEachBean() {
101+
this.contextRunner.withUserConfiguration(Config.class, MultipleDataSourceLiquibaseConfiguration.class)
102+
.run((context) -> {
103+
Map<String, LiquibaseBean> liquibaseBeans = context.getBean(LiquibaseEndpoint.class)
104+
.liquibaseBeans().getContexts().get(context.getId()).getLiquibaseBeans();
105+
assertThat(liquibaseBeans.get("liquibase").getChangeSets()).hasSize(1);
106+
assertThat(liquibaseBeans.get("liquibase").getChangeSets().get(0).getChangeLog())
107+
.isEqualTo("classpath:/db/changelog/db.changelog-master.yaml");
108+
assertThat(liquibaseBeans.get("liquibaseBackup").getChangeSets()).hasSize(1);
109+
assertThat(liquibaseBeans.get("liquibaseBackup").getChangeSets().get(0).getChangeLog())
110+
.isEqualTo("classpath:/db/changelog/db.changelog-master-backup.yaml");
111+
});
111112
}
112113

113114
private boolean getAutoCommit(DataSource dataSource) throws SQLException {
@@ -127,39 +128,38 @@ public LiquibaseEndpoint endpoint(ApplicationContext context) {
127128
}
128129

129130
@Configuration
130-
static class LiquibaseConfiguration {
131+
static class MultipleDataSourceLiquibaseConfiguration {
131132

132133
@Bean
133134
DataSource dataSource() {
134-
HikariConfig config = new HikariConfig();
135-
config.setJdbcUrl("jdbc:hsqldb:mem:test");
136-
config.setUsername("sa");
137-
return new HikariDataSource(config);
135+
return createEmbeddedDatabase();
138136
}
139137

140138
@Bean
141139
DataSource dataSourceBackup() {
142-
HikariConfig config = new HikariConfig();
143-
config.setJdbcUrl("jdbc:hsqldb:mem:testBackup");
144-
config.setUsername("sa");
145-
return new HikariDataSource(config);
140+
return createEmbeddedDatabase();
146141
}
147142

148143
@Bean
149144
SpringLiquibase liquibase(DataSource dataSource) {
150-
SpringLiquibase liquibase = new SpringLiquibase();
151-
liquibase.setChangeLog("classpath:/db/changelog/db.changelog-master.yaml");
152-
liquibase.setShouldRun(true);
153-
liquibase.setDataSource(dataSource);
154-
return liquibase;
145+
return createSpringLiquibase("db.changelog-master.yaml", dataSource);
155146
}
156147

157148
@Bean
158149
SpringLiquibase liquibaseBackup(DataSource dataSourceBackup) {
150+
return createSpringLiquibase("db.changelog-master-backup.yaml", dataSourceBackup);
151+
}
152+
153+
private DataSource createEmbeddedDatabase() {
154+
return new EmbeddedDatabaseBuilder().generateUniqueName(true)
155+
.setType(EmbeddedDatabaseConnection.HSQL.getType()).build();
156+
}
157+
158+
private SpringLiquibase createSpringLiquibase(String changeLog, DataSource dataSource) {
159159
SpringLiquibase liquibase = new SpringLiquibase();
160-
liquibase.setChangeLog("classpath:/db/changelog/db.changelog-master-backup.yaml");
160+
liquibase.setChangeLog("classpath:/db/changelog/" + changeLog);
161161
liquibase.setShouldRun(true);
162-
liquibase.setDataSource(dataSourceBackup);
162+
liquibase.setDataSource(dataSource);
163163
return liquibase;
164164
}
165165

0 commit comments

Comments
 (0)