Skip to content

Commit 5302d91

Browse files
polarbear567wilkinsona
authored andcommitted
Fix Liquibase endpoint's output with multiple datasources
Previously, the endpoint used the same change log history service for for each SpringLiquibase bean that it processed. This resulted in pollution of the reported changes as the history of each bean was not isolated. This commit updates the endpoint to use a new history service for each SpringLiquibase bean that is processed. See gh-19171
1 parent 2be3057 commit 5302d91

File tree

3 files changed

+81
-3
lines changed

3 files changed

+81
-3
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,10 @@ public ApplicationLiquibaseBeans liquibaseBeans() {
6363
while (target != null) {
6464
Map<String, LiquibaseBean> liquibaseBeans = new HashMap<>();
6565
DatabaseFactory factory = DatabaseFactory.getInstance();
66-
StandardChangeLogHistoryService service = new StandardChangeLogHistoryService();
67-
this.context.getBeansOfType(SpringLiquibase.class)
68-
.forEach((name, liquibase) -> liquibaseBeans.put(name, createReport(liquibase, service, factory)));
66+
this.context.getBeansOfType(SpringLiquibase.class).forEach((name, liquibase) -> {
67+
StandardChangeLogHistoryService service = new StandardChangeLogHistoryService();
68+
liquibaseBeans.put(name, createReport(liquibase, service, factory));
69+
});
6970
ApplicationContext parent = target.getParent();
7071
contextBeans.put(target.getId(),
7172
new ContextLiquibaseBeans(liquibaseBeans, (parent != null) ? parent.getId() : null));

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222

2323
import javax.sql.DataSource;
2424

25+
import com.zaxxer.hikari.HikariConfig;
26+
import com.zaxxer.hikari.HikariDataSource;
27+
import liquibase.integration.spring.SpringLiquibase;
2528
import org.junit.Test;
2629

2730
import org.springframework.boot.actuate.liquibase.LiquibaseEndpoint.LiquibaseBean;
@@ -41,6 +44,7 @@
4144
* @author Eddú Meléndez
4245
* @author Andy Wilkinson
4346
* @author Stephane Nicoll
47+
* @author Leo Li
4448
*/
4549
public class LiquibaseEndpointTests {
4650

@@ -92,6 +96,20 @@ public void connectionAutoCommitPropertyIsReset() {
9296
});
9397
}
9498

99+
@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+
});
111+
}
112+
95113
private boolean getAutoCommit(DataSource dataSource) throws SQLException {
96114
try (Connection connection = dataSource.getConnection()) {
97115
return connection.getAutoCommit();
@@ -108,4 +126,43 @@ public LiquibaseEndpoint endpoint(ApplicationContext context) {
108126

109127
}
110128

129+
@Configuration
130+
static class LiquibaseConfiguration {
131+
132+
@Bean
133+
DataSource dataSource() {
134+
HikariConfig config = new HikariConfig();
135+
config.setJdbcUrl("jdbc:hsqldb:mem:test");
136+
config.setUsername("sa");
137+
return new HikariDataSource(config);
138+
}
139+
140+
@Bean
141+
DataSource dataSourceBackup() {
142+
HikariConfig config = new HikariConfig();
143+
config.setJdbcUrl("jdbc:hsqldb:mem:testBackup");
144+
config.setUsername("sa");
145+
return new HikariDataSource(config);
146+
}
147+
148+
@Bean
149+
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;
155+
}
156+
157+
@Bean
158+
SpringLiquibase liquibaseBackup(DataSource dataSourceBackup) {
159+
SpringLiquibase liquibase = new SpringLiquibase();
160+
liquibase.setChangeLog("classpath:/db/changelog/db.changelog-master-backup.yaml");
161+
liquibase.setShouldRun(true);
162+
liquibase.setDataSource(dataSourceBackup);
163+
return liquibase;
164+
}
165+
166+
}
167+
111168
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
databaseChangeLog:
2+
- changeSet:
3+
id: 1
4+
author: leoli
5+
changes:
6+
- createTable:
7+
tableName: customerbackup
8+
columns:
9+
- column:
10+
name: id
11+
type: int
12+
autoIncrement: true
13+
constraints:
14+
primaryKey: true
15+
nullable: false
16+
- column:
17+
name: name
18+
type: varchar(50)
19+
constraints:
20+
nullable: false

0 commit comments

Comments
 (0)