16
16
17
17
package org .springframework .boot .actuate .endpoint ;
18
18
19
- import java .util .ArrayList ;
20
19
import java .util .Collections ;
20
+ import java .util .Date ;
21
+ import java .util .HashMap ;
21
22
import java .util .List ;
22
23
import java .util .Map ;
24
+ import java .util .Set ;
25
+ import java .util .stream .Collectors ;
23
26
24
27
import javax .sql .DataSource ;
25
28
29
+ import liquibase .changelog .ChangeLogHistoryService ;
30
+ import liquibase .changelog .ChangeSet .ExecType ;
31
+ import liquibase .changelog .RanChangeSet ;
26
32
import liquibase .changelog .StandardChangeLogHistoryService ;
27
33
import liquibase .database .Database ;
28
34
import liquibase .database .DatabaseFactory ;
41
47
* @since 1.3.0
42
48
*/
43
49
@ ConfigurationProperties (prefix = "endpoints.liquibase" )
44
- public class LiquibaseEndpoint extends AbstractEndpoint <List < LiquibaseReport >> {
50
+ public class LiquibaseEndpoint extends AbstractEndpoint <Map < String , LiquibaseReport >> {
45
51
46
52
private final Map <String , SpringLiquibase > liquibases ;
47
53
@@ -56,57 +62,173 @@ public LiquibaseEndpoint(Map<String, SpringLiquibase> liquibases) {
56
62
}
57
63
58
64
@ 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 <>();
61
67
DatabaseFactory factory = DatabaseFactory .getInstance ();
62
68
StandardChangeLogHistoryService service = new StandardChangeLogHistoryService ();
63
69
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 ());
64
80
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 );
80
85
}
86
+ service .setDatabase (database );
87
+ return new LiquibaseReport (service .getRanChangeSets ().stream ()
88
+ .map (ChangeSet ::new ).collect (Collectors .toList ()));
81
89
}
82
- catch ( Exception ex ) {
83
- throw new IllegalStateException ( "Unable to get Liquibase changelog" , ex );
90
+ finally {
91
+ connection . close ( );
84
92
}
85
93
}
86
-
87
- return reports ;
94
+ catch (Exception ex ) {
95
+ throw new IllegalStateException ("Unable to get Liquibase change sets" , ex );
96
+ }
88
97
}
89
98
90
99
/**
91
- * Liquibase report for one datasource .
100
+ * Report for a single {@link SpringLiquibase} instance .
92
101
*/
93
102
public static class LiquibaseReport {
94
103
95
- private final String name ;
104
+ private final List < ChangeSet > changeSets ;
96
105
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
+ }
98
164
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 ;
102
167
}
103
168
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 ;
106
228
}
107
229
108
- public List < Map < String , ?>> getChangeLogs () {
109
- return this .changeLogs ;
230
+ public Set < String > getContexts () {
231
+ return this .contexts ;
110
232
}
111
233
112
234
}
0 commit comments