Skip to content

Commit 7c52bb3

Browse files
committed
Improve the structure of the Liquibase endpoint's response
Closes gh-9974
1 parent f38cb7b commit 7c52bb3

File tree

1 file changed

+155
-33
lines changed

1 file changed

+155
-33
lines changed

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

Lines changed: 155 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,19 @@
1616

1717
package org.springframework.boot.actuate.endpoint;
1818

19-
import java.util.ArrayList;
2019
import java.util.Collections;
20+
import java.util.Date;
21+
import java.util.HashMap;
2122
import java.util.List;
2223
import java.util.Map;
24+
import java.util.Set;
25+
import java.util.stream.Collectors;
2326

2427
import javax.sql.DataSource;
2528

29+
import liquibase.changelog.ChangeLogHistoryService;
30+
import liquibase.changelog.ChangeSet.ExecType;
31+
import liquibase.changelog.RanChangeSet;
2632
import liquibase.changelog.StandardChangeLogHistoryService;
2733
import liquibase.database.Database;
2834
import liquibase.database.DatabaseFactory;
@@ -41,7 +47,7 @@
4147
* @since 1.3.0
4248
*/
4349
@ConfigurationProperties(prefix = "endpoints.liquibase")
44-
public class LiquibaseEndpoint extends AbstractEndpoint<List<LiquibaseReport>> {
50+
public class LiquibaseEndpoint extends AbstractEndpoint<Map<String, LiquibaseReport>> {
4551

4652
private final Map<String, SpringLiquibase> liquibases;
4753

@@ -56,57 +62,173 @@ public LiquibaseEndpoint(Map<String, SpringLiquibase> liquibases) {
5662
}
5763

5864
@Override
59-
public List<LiquibaseReport> invoke() {
60-
List<LiquibaseReport> reports = new ArrayList<>();
65+
public Map<String, LiquibaseReport> invoke() {
66+
Map<String, LiquibaseReport> reports = new HashMap<>();
6167
DatabaseFactory factory = DatabaseFactory.getInstance();
6268
StandardChangeLogHistoryService service = new StandardChangeLogHistoryService();
6369
for (Map.Entry<String, SpringLiquibase> entry : this.liquibases.entrySet()) {
70+
reports.put(entry.getKey(), createReport(entry.getValue(), service, factory));
71+
}
72+
return reports;
73+
}
74+
75+
private LiquibaseReport createReport(SpringLiquibase liquibase,
76+
ChangeLogHistoryService service, DatabaseFactory factory) {
77+
try {
78+
DataSource dataSource = liquibase.getDataSource();
79+
JdbcConnection connection = new JdbcConnection(dataSource.getConnection());
6480
try {
65-
DataSource dataSource = entry.getValue().getDataSource();
66-
JdbcConnection connection = new JdbcConnection(
67-
dataSource.getConnection());
68-
try {
69-
Database database = factory
70-
.findCorrectDatabaseImplementation(connection);
71-
String defaultSchema = entry.getValue().getDefaultSchema();
72-
if (StringUtils.hasText(defaultSchema)) {
73-
database.setDefaultSchemaName(defaultSchema);
74-
}
75-
reports.add(new LiquibaseReport(entry.getKey(),
76-
service.queryDatabaseChangeLogTable(database)));
77-
}
78-
finally {
79-
connection.close();
81+
Database database = factory.findCorrectDatabaseImplementation(connection);
82+
String defaultSchema = liquibase.getDefaultSchema();
83+
if (StringUtils.hasText(defaultSchema)) {
84+
database.setDefaultSchemaName(defaultSchema);
8085
}
86+
service.setDatabase(database);
87+
return new LiquibaseReport(service.getRanChangeSets().stream()
88+
.map(ChangeSet::new).collect(Collectors.toList()));
8189
}
82-
catch (Exception ex) {
83-
throw new IllegalStateException("Unable to get Liquibase changelog", ex);
90+
finally {
91+
connection.close();
8492
}
8593
}
86-
87-
return reports;
94+
catch (Exception ex) {
95+
throw new IllegalStateException("Unable to get Liquibase change sets", ex);
96+
}
8897
}
8998

9099
/**
91-
* Liquibase report for one datasource.
100+
* Report for a single {@link SpringLiquibase} instance.
92101
*/
93102
public static class LiquibaseReport {
94103

95-
private final String name;
104+
private final List<ChangeSet> changeSets;
96105

97-
private final List<Map<String, ?>> changeLogs;
106+
public LiquibaseReport(List<ChangeSet> changeSets) {
107+
this.changeSets = changeSets;
108+
}
109+
110+
public List<ChangeSet> getChangeSets() {
111+
return this.changeSets;
112+
}
113+
114+
}
115+
116+
/**
117+
* A Liquibase change set.
118+
*/
119+
public static class ChangeSet {
120+
121+
private final String author;
122+
123+
private final String changeLog;
124+
125+
private final String comments;
126+
127+
private final ContextExpression contextExpression;
128+
129+
private final Date dateExecuted;
130+
131+
private final String deploymentId;
132+
133+
private final String description;
134+
135+
private final ExecType execType;
136+
137+
private final String id;
138+
139+
private final Set<String> labels;
140+
141+
private final String checksum;
142+
143+
private final Integer orderExecuted;
144+
145+
private final String tag;
146+
147+
public ChangeSet(RanChangeSet ranChangeSet) {
148+
this.author = ranChangeSet.getAuthor();
149+
this.changeLog = ranChangeSet.getChangeLog();
150+
this.comments = ranChangeSet.getComments();
151+
this.contextExpression = new ContextExpression(
152+
ranChangeSet.getContextExpression().getContexts());
153+
this.dateExecuted = ranChangeSet.getDateExecuted();
154+
this.deploymentId = ranChangeSet.getDeploymentId();
155+
this.description = ranChangeSet.getDescription();
156+
this.execType = ranChangeSet.getExecType();
157+
this.id = ranChangeSet.getId();
158+
this.labels = ranChangeSet.getLabels().getLabels();
159+
this.checksum = ranChangeSet.getLastCheckSum() == null ? null
160+
: ranChangeSet.getLastCheckSum().toString();
161+
this.orderExecuted = ranChangeSet.getOrderExecuted();
162+
this.tag = ranChangeSet.getTag();
163+
}
98164

99-
public LiquibaseReport(String name, List<Map<String, ?>> changeLogs) {
100-
this.name = name;
101-
this.changeLogs = changeLogs;
165+
public String getAuthor() {
166+
return this.author;
102167
}
103168

104-
public String getName() {
105-
return this.name;
169+
public String getChangeLog() {
170+
return this.changeLog;
171+
}
172+
173+
public String getComments() {
174+
return this.comments;
175+
}
176+
177+
public ContextExpression getContextExpression() {
178+
return this.contextExpression;
179+
}
180+
181+
public Date getDateExecuted() {
182+
return this.dateExecuted;
183+
}
184+
185+
public String getDeploymentId() {
186+
return this.deploymentId;
187+
}
188+
189+
public String getDescription() {
190+
return this.description;
191+
}
192+
193+
public ExecType getExecType() {
194+
return this.execType;
195+
}
196+
197+
public String getId() {
198+
return this.id;
199+
}
200+
201+
public Set<String> getLabels() {
202+
return this.labels;
203+
}
204+
205+
public String getChecksum() {
206+
return this.checksum;
207+
}
208+
209+
public Integer getOrderExecuted() {
210+
return this.orderExecuted;
211+
}
212+
213+
public String getTag() {
214+
return this.tag;
215+
}
216+
217+
}
218+
219+
/**
220+
* A context expression in a {@link ChangeSet}.
221+
*/
222+
public static class ContextExpression {
223+
224+
private final Set<String> contexts;
225+
226+
public ContextExpression(Set<String> contexts) {
227+
this.contexts = contexts;
106228
}
107229

108-
public List<Map<String, ?>> getChangeLogs() {
109-
return this.changeLogs;
230+
public Set<String> getContexts() {
231+
return this.contexts;
110232
}
111233

112234
}

0 commit comments

Comments
 (0)