-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
YamlChangeLogParser.java
224 lines (189 loc) · 9.01 KB
/
YamlChangeLogParser.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package liquibase.parser.core.yaml;
import liquibase.ContextExpression;
import liquibase.Labels;
import liquibase.changelog.ChangeLogParameters;
import liquibase.changelog.DatabaseChangeLog;
import liquibase.exception.ChangeLogParseException;
import liquibase.logging.LogType;
import liquibase.parser.ChangeLogParser;
import liquibase.parser.core.ParsedNode;
import liquibase.resource.ResourceAccessor;
import liquibase.util.StreamUtil;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.SafeConstructor;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.*;
public class YamlChangeLogParser extends YamlParser implements ChangeLogParser {
@Override
public DatabaseChangeLog parse(String physicalChangeLogLocation, ChangeLogParameters changeLogParameters, ResourceAccessor resourceAccessor) throws ChangeLogParseException {
Yaml yaml = new Yaml(new SafeConstructor());
try (InputStream changeLogStream = StreamUtil.singleInputStream(physicalChangeLogLocation, resourceAccessor)) {
if (changeLogStream == null) {
throw new ChangeLogParseException(physicalChangeLogLocation + " does not exist");
}
Map parsedYaml = parseYamlStream(physicalChangeLogLocation, yaml, changeLogStream);
if ((parsedYaml == null) || parsedYaml.isEmpty()) {
throw new ChangeLogParseException("Empty file " + physicalChangeLogLocation);
}
DatabaseChangeLog changeLog = new DatabaseChangeLog(physicalChangeLogLocation);
Object rootList = parsedYaml.get("databaseChangeLog");
if (rootList == null) {
throw new ChangeLogParseException("Could not find databaseChangeLog node");
}
if (!(rootList instanceof List)) {
throw new ChangeLogParseException("databaseChangeLog does not contain a list of entries. Each changeSet must begin ' - changeSet:'");
}
for (Object obj : (List) rootList) {
if ((obj instanceof Map) && ((Map) obj).containsKey("property")) {
Map property = (Map) ((Map) obj).get("property");
ContextExpression context = new ContextExpression((String) property.get("context"));
Labels labels = new Labels((String) property.get("labels"));
Boolean global = getGlobalParam(property);
if (property.containsKey("name")) {
Object value = property.get("value");
if (value != null) {
value = value.toString(); // TODO: not nice...
}
changeLogParameters.set((String) property.get("name"), (String) value, context, labels, (String) property.get("dbms"), global, changeLog);
} else if (property.containsKey("file")) {
loadChangeLogParametersFromFile(changeLogParameters, resourceAccessor, changeLog, property,
context, labels, global);
}
}
}
replaceParameters(parsedYaml, changeLogParameters, changeLog);
changeLog.setChangeLogParameters(changeLogParameters);
ParsedNode databaseChangeLogNode = new ParsedNode(null, "databaseChangeLog");
databaseChangeLogNode.setValue(rootList);
changeLog.load(databaseChangeLogNode, resourceAccessor);
return changeLog;
} catch (ChangeLogParseException e) {
throw e;
} catch (Exception e) {
throw new ChangeLogParseException("Error parsing "+physicalChangeLogLocation, e);
}
}
/**
* A simple "interceptor" InputStream to ensure that the YAML stream doesn't contain two "databaseChangeLog" tags,
* which would otherwise cause silent omissions if something is copy/pasted wrongly into the file.
*/
static class DoubleChangelogCheckerInputStream extends FilterInputStream {
byte[] databaseChangeLog = "databaseChangeLog".getBytes(StandardCharsets.UTF_8);
int scanPos;
boolean databaseChangeLogSeen;
DoubleChangelogCheckerInputStream(InputStream in) {
super(in);
scanPos = 0;
databaseChangeLogSeen = false;
}
private void processByte(int nextByte) {
// scanPos will be -1 if we aren't in a position where "databaseChangeLog" would cause a problem
// i.e. the beginning of a file or of a line
if (nextByte == 10) {
// a new line means we should expect to see it again
scanPos = 0;
} else if (scanPos > -1 && nextByte == databaseChangeLog[scanPos]) {
scanPos++;
if (scanPos == databaseChangeLog.length) {
if (databaseChangeLogSeen) {
throw new IllegalStateException("Two 'databaseChangeLog' tags found in a single file!");
} else {
databaseChangeLogSeen = true;
scanPos = -1;
}
}
} else {
// no point in scanning since the string was not found
scanPos = -1;
}
}
@Override
public int read() throws IOException {
int nextByte = super.read();
processByte(nextByte);
return nextByte;
}
@Override
public int read(byte[] b) throws IOException {
int numBytes = super.read(b);
for (int i=0; i<numBytes; i++) {
processByte(b[i]);
}
return numBytes;
}
@Override
public int read(byte[] b, int off, int len) throws IOException {
int numBytes = super.read(b, off, len);
for (int i=off; i < off + numBytes; i++) {
processByte(b[i]);
}
return numBytes;
}
}
private Map parseYamlStream(String physicalChangeLogLocation, Yaml yaml, InputStream changeLogStream) throws ChangeLogParseException {
Map parsedYaml;
try {
parsedYaml = (Map) yaml.load(new DoubleChangelogCheckerInputStream(changeLogStream));
} catch (Exception e) {
throw new ChangeLogParseException("Syntax error in file " + physicalChangeLogLocation + ": " + e.getMessage(), e);
}
return parsedYaml;
}
private void loadChangeLogParametersFromFile(ChangeLogParameters changeLogParameters, ResourceAccessor resourceAccessor, DatabaseChangeLog changeLog, Map property, ContextExpression context, Labels labels, Boolean global) throws IOException {
Properties props = new Properties();
try (
InputStream propertiesStream = StreamUtil.singleInputStream(
(String) property.get("file"), resourceAccessor))
{
if (propertiesStream == null) {
log.info(LogType.LOG, "Could not open properties file " + property.get("file"));
} else {
props.load(propertiesStream);
for (Map.Entry entry : props.entrySet()) {
changeLogParameters.set(entry.getKey().toString(), entry.getValue().toString(), context, labels, (String) property.get("dbms"), global, changeLog);
}
}
}
}
/**
* Extract the global parameter from the properties.
*
* @param property the map of props
* @return the global param
*/
private Boolean getGlobalParam(Map property) {
Boolean global = null;
Object globalObj = property.get("global");
if (globalObj == null) {
// default behaviour before liquibase 3.4
global = true;
} else {
global = (Boolean) globalObj;
}
return global;
}
protected void replaceParameters(Object obj, ChangeLogParameters changeLogParameters, DatabaseChangeLog changeLog) {
if (obj instanceof Map) {
for (Map.Entry entry : (Set<Map.Entry>) ((Map) obj).entrySet()) {
if ((entry.getValue() instanceof Map) || (entry.getValue() instanceof Collection)) {
replaceParameters(entry.getValue(), changeLogParameters, changeLog);
} else if (entry.getValue() instanceof String) {
entry.setValue(changeLogParameters.expandExpressions((String) entry.getValue(), changeLog));
}
}
} else if (obj instanceof Collection) {
ListIterator iterator = ((List) obj).listIterator();
while (iterator.hasNext()) {
Object child = iterator.next();
if ((child instanceof Map) || (child instanceof Collection)) {
replaceParameters(child, changeLogParameters, changeLog);
} else if (child instanceof String) {
iterator.set(changeLogParameters.expandExpressions((String) child, changeLog));
}
}
}
}
}