use of liquibase.parser.core.ParsedNode in project liquibase by liquibase.
the class AbstractChangeLogParser method parse.
@Override
public DatabaseChangeLog parse(String physicalChangeLogLocation, ChangeLogParameters changeLogParameters, ResourceAccessor resourceAccessor) throws ChangeLogParseException {
ParsedNode parsedNode = parseToNode(physicalChangeLogLocation, changeLogParameters, resourceAccessor);
if (parsedNode == null) {
return null;
}
DatabaseChangeLog changeLog = new DatabaseChangeLog(physicalChangeLogLocation);
changeLog.setChangeLogParameters(changeLogParameters);
try {
changeLog.load(parsedNode, resourceAccessor);
} catch (Exception e) {
throw new ChangeLogParseException(e);
}
return changeLog;
}
use of liquibase.parser.core.ParsedNode in project liquibase by liquibase.
the class YamlChangeLogParser method parse.
@Override
public DatabaseChangeLog parse(String physicalChangeLogLocation, ChangeLogParameters changeLogParameters, ResourceAccessor resourceAccessor) throws ChangeLogParseException {
Yaml yaml = new Yaml();
try {
InputStream changeLogStream = StreamUtil.singleInputStream(physicalChangeLogLocation, resourceAccessor);
if (changeLogStream == null) {
throw new ChangeLogParseException(physicalChangeLogLocation + " does not exist");
}
Map parsedYaml;
try {
parsedYaml = yaml.loadAs(changeLogStream, Map.class);
} catch (Exception e) {
throw new ChangeLogParseException("Syntax error in " + getSupportedFileExtensions()[0] + ": " + e.getMessage(), e);
}
if (parsedYaml == null || parsedYaml.size() == 0) {
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) {
// TODO: not nice...
value = value.toString();
}
changeLogParameters.set((String) property.get("name"), (String) value, context, labels, (String) property.get("dbms"), global, changeLog);
} else if (property.containsKey("file")) {
Properties props = new Properties();
InputStream propertiesStream = StreamUtil.singleInputStream((String) property.get("file"), resourceAccessor);
if (propertiesStream == null) {
log.info("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);
}
}
}
}
}
replaceParameters(parsedYaml, changeLogParameters, changeLog);
changeLog.setChangeLogParameters(changeLogParameters);
ParsedNode databaseChangeLogNode = new ParsedNode(null, "databaseChangeLog");
databaseChangeLogNode.setValue(rootList);
changeLog.load(databaseChangeLogNode, resourceAccessor);
return changeLog;
} catch (Throwable e) {
if (e instanceof ChangeLogParseException) {
throw (ChangeLogParseException) e;
}
throw new ChangeLogParseException("Error parsing " + physicalChangeLogLocation, e);
}
}
use of liquibase.parser.core.ParsedNode in project liquibase by liquibase.
the class YamlSnapshotParser method parse.
@Override
public DatabaseSnapshot parse(String path, ResourceAccessor resourceAccessor) throws LiquibaseParseException {
Yaml yaml = new Yaml();
try {
InputStream stream = StreamUtil.singleInputStream(path, resourceAccessor);
if (stream == null) {
throw new LiquibaseParseException(path + " does not exist");
}
Map parsedYaml;
try {
parsedYaml = yaml.loadAs(new InputStreamReader(stream, LiquibaseConfiguration.getInstance().getConfiguration(GlobalConfiguration.class).getOutputEncoding()), Map.class);
} catch (Exception e) {
throw new LiquibaseParseException("Syntax error in " + getSupportedFileExtensions()[0] + ": " + e.getMessage(), e);
} finally {
try {
stream.close();
} catch (IOException ioe) {
}
}
Map rootList = (Map) parsedYaml.get("snapshot");
if (rootList == null) {
throw new LiquibaseParseException("Could not find root snapshot node");
}
String shortName = (String) ((Map) rootList.get("database")).get("shortName");
Database database = DatabaseFactory.getInstance().getDatabase(shortName).getClass().newInstance();
DatabaseSnapshot snapshot = new RestoredDatabaseSnapshot(database);
ParsedNode snapshotNode = new ParsedNode(null, "snapshot");
snapshotNode.setValue(rootList);
Map metadata = (Map) rootList.get("metadata");
if (metadata != null) {
snapshot.getMetadata().putAll(metadata);
}
snapshot.load(snapshotNode, resourceAccessor);
return snapshot;
} catch (Throwable e) {
if (e instanceof LiquibaseParseException) {
throw (LiquibaseParseException) e;
}
throw new LiquibaseParseException(e);
}
}
Aggregations