-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
YamlChangeLogParser.java
200 lines (170 loc) · 8.32 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
package liquibase.parser.core.yaml;
import liquibase.ContextExpression;
import liquibase.Labels;
import liquibase.Scope;
import liquibase.changelog.ChangeLogParameters;
import liquibase.changelog.DatabaseChangeLog;
import liquibase.exception.ChangeLogParseException;
import liquibase.exception.LiquibaseException;
import liquibase.exception.UnexpectedLiquibaseException;
import liquibase.parser.ChangeLogParser;
import liquibase.parser.core.ParsedNode;
import liquibase.resource.Resource;
import liquibase.resource.ResourceAccessor;
import liquibase.util.FileUtil;
import org.yaml.snakeyaml.LoaderOptions;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.SafeConstructor;
import org.yaml.snakeyaml.nodes.Tag;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
public class YamlChangeLogParser extends YamlParser implements ChangeLogParser {
private static final String DATABASE_CHANGE_LOG = "databaseChangeLog";
@Override
public DatabaseChangeLog parse(String physicalChangeLogLocation, ChangeLogParameters changeLogParameters, ResourceAccessor resourceAccessor) throws ChangeLogParseException {
Yaml yaml = new Yaml(new CustomSafeConstructor(createLoaderOptions()));
try {
Resource changelog = resourceAccessor.get(physicalChangeLogLocation);
if (!changelog.exists()) {
throw new ChangeLogParseException(physicalChangeLogLocation + " does not exist");
}
Map parsedYaml;
try (InputStream changeLogStream = changelog.openInputStream()) {
parsedYaml = parseYamlStream(physicalChangeLogLocation, yaml, changeLogStream);
}
if ((parsedYaml == null) || parsedYaml.isEmpty()) {
throw new ChangeLogParseException("Empty file " + physicalChangeLogLocation);
}
DatabaseChangeLog changeLog = new DatabaseChangeLog(DatabaseChangeLog.normalizePath(physicalChangeLogLocation));
if (!parsedYaml.containsKey(DATABASE_CHANGE_LOG)) {
throw new ChangeLogParseException("Could not find databaseChangeLog node");
}
Object rootList = parsedYaml.get(DATABASE_CHANGE_LOG);
if (rootList == null) {
changeLog.setChangeLogParameters(changeLogParameters);
return changeLog;
}
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) {
if (((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");
changeLogParameters.set((String) property.get("name"), 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, DATABASE_CHANGE_LOG);
databaseChangeLogNode.setValue(rootList);
changeLog.load(databaseChangeLogNode, resourceAccessor);
return changeLog;
} catch (ChangeLogParseException e) {
throw e;
} catch (Exception e) {
throw new ChangeLogParseException("Error parsing " + physicalChangeLogLocation + " : " + e.getMessage(), e);
}
}
private Map parseYamlStream(String physicalChangeLogLocation, Yaml yaml, InputStream changeLogStream) throws ChangeLogParseException {
Map parsedYaml;
try {
parsedYaml = yaml.load(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, LiquibaseException {
Properties props = new Properties();
Boolean relativeToChangelogFile = (Boolean) property.get("relativeToChangelogFile");
Boolean errorIfMissing = (Boolean) property.get("errorIfMissing");
String file = (String) property.get("file");
if (relativeToChangelogFile == null) {
relativeToChangelogFile = false;
}
if (errorIfMissing == null) {
errorIfMissing = true;
}
Resource resource;
if (relativeToChangelogFile) {
resource = resourceAccessor.get(changeLog.getPhysicalFilePath()).resolveSibling(file);
} else {
resource = resourceAccessor.get(file);
}
if (!resource.exists()) {
if (errorIfMissing) {
throw new UnexpectedLiquibaseException(FileUtil.getFileNotFoundMessage(file));
}
else {
Scope.getCurrentScope().getLog(getClass()).warning(FileUtil.getFileNotFoundMessage(file));
}
} else {
try (InputStream stream = resource.openInputStream()) {
props.load(stream);
}
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) {
global = true;
} else {
global = (Boolean) globalObj;
}
return global;
}
protected void replaceParameters(Object obj, ChangeLogParameters changeLogParameters, DatabaseChangeLog changeLog) throws ChangeLogParseException {
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));
}
}
}
}
static class CustomSafeConstructor extends SafeConstructor {
/**
* Create an instance
*
* @param loaderOptions - the configuration options
*/
public CustomSafeConstructor(LoaderOptions loaderOptions) {
super(loaderOptions);
this.yamlConstructors.put(Tag.TIMESTAMP, new CustomConstructYamlTimestamp());
}
}
}