Skip to content

Commit b6fb251

Browse files
committed
Merge branch '2.2.x'
Closes gh-19191
2 parents 90285ac + 189f742 commit b6fb251

File tree

3 files changed

+80
-5
lines changed

3 files changed

+80
-5
lines changed

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

Lines changed: 3 additions & 5 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,9 +62,8 @@ public ApplicationLiquibaseBeans liquibaseBeans() {
6362
while (target != null) {
6463
Map<String, LiquibaseBean> liquibaseBeans = new HashMap<>();
6564
DatabaseFactory factory = DatabaseFactory.getInstance();
66-
StandardChangeLogHistoryService service = new StandardChangeLogHistoryService();
6765
this.context.getBeansOfType(SpringLiquibase.class)
68-
.forEach((name, liquibase) -> liquibaseBeans.put(name, createReport(liquibase, service, factory)));
66+
.forEach((name, liquibase) -> liquibaseBeans.put(name, createReport(liquibase, factory)));
6967
ApplicationContext parent = target.getParent();
7068
contextBeans.put(target.getId(),
7169
new ContextLiquibaseBeans(liquibaseBeans, (parent != null) ? parent.getId() : null));
@@ -74,8 +72,7 @@ public ApplicationLiquibaseBeans liquibaseBeans() {
7472
return new ApplicationLiquibaseBeans(contextBeans);
7573
}
7674

77-
private LiquibaseBean createReport(SpringLiquibase liquibase, ChangeLogHistoryService service,
78-
DatabaseFactory factory) {
75+
private LiquibaseBean createReport(SpringLiquibase liquibase, DatabaseFactory factory) {
7976
try {
8077
DataSource dataSource = liquibase.getDataSource();
8178
JdbcConnection connection = new JdbcConnection(dataSource.getConnection());
@@ -88,6 +85,7 @@ private LiquibaseBean createReport(SpringLiquibase liquibase, ChangeLogHistorySe
8885
}
8986
database.setDatabaseChangeLogTableName(liquibase.getDatabaseChangeLogTable());
9087
database.setDatabaseChangeLogLockTableName(liquibase.getDatabaseChangeLogLockTable());
88+
StandardChangeLogHistoryService service = new StandardChangeLogHistoryService();
9189
service.setDatabase(database);
9290
return new LiquibaseBean(
9391
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: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,19 @@
2222

2323
import javax.sql.DataSource;
2424

25+
import liquibase.integration.spring.SpringLiquibase;
2526
import org.junit.jupiter.api.Test;
2627

2728
import org.springframework.boot.actuate.liquibase.LiquibaseEndpoint.LiquibaseBean;
2829
import org.springframework.boot.autoconfigure.AutoConfigurations;
2930
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
3031
import org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration;
32+
import org.springframework.boot.jdbc.EmbeddedDatabaseConnection;
3133
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
3234
import org.springframework.context.ApplicationContext;
3335
import org.springframework.context.annotation.Bean;
3436
import org.springframework.context.annotation.Configuration;
37+
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
3538

3639
import static org.assertj.core.api.Assertions.assertThat;
3740

@@ -41,6 +44,7 @@
4144
* @author Eddú Meléndez
4245
* @author Andy Wilkinson
4346
* @author Stephane Nicoll
47+
* @author Leo Li
4448
*/
4549
class LiquibaseEndpointTests {
4650

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

99+
@Test
100+
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+
});
112+
}
113+
95114
private boolean getAutoCommit(DataSource dataSource) throws SQLException {
96115
try (Connection connection = dataSource.getConnection()) {
97116
return connection.getAutoCommit();
@@ -108,4 +127,42 @@ LiquibaseEndpoint endpoint(ApplicationContext context) {
108127

109128
}
110129

130+
@Configuration(proxyBeanMethods = false)
131+
static class MultipleDataSourceLiquibaseConfiguration {
132+
133+
@Bean
134+
DataSource dataSource() {
135+
return createEmbeddedDatabase();
136+
}
137+
138+
@Bean
139+
DataSource dataSourceBackup() {
140+
return createEmbeddedDatabase();
141+
}
142+
143+
@Bean
144+
SpringLiquibase liquibase(DataSource dataSource) {
145+
return createSpringLiquibase("db.changelog-master.yaml", dataSource);
146+
}
147+
148+
@Bean
149+
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) {
159+
SpringLiquibase liquibase = new SpringLiquibase();
160+
liquibase.setChangeLog("classpath:/db/changelog/" + changeLog);
161+
liquibase.setShouldRun(true);
162+
liquibase.setDataSource(dataSource);
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)