use of liquibase.resource.InputStreamList in project liquibase by liquibase.
the class MockResourceAccessor method openStreams.
@Override
public InputStreamList openStreams(String relativeTo, String streamPath) throws IOException {
InputStream stream = null;
if (contentByFileName.containsKey(streamPath)) {
stream = new ByteArrayInputStream(contentByFileName.get(streamPath).getBytes(GlobalConfiguration.OUTPUT_FILE_ENCODING.getCurrentValue()));
}
if (stream == null) {
return null;
} else {
InputStreamList list = new InputStreamList();
list.add(URI.create(streamPath), stream);
return list;
}
}
use of liquibase.resource.InputStreamList in project liquibase by liquibase.
the class ChangelogRewriter method addChangeLogId.
/**
* Add the changelog ID from the changelog file and update the XSD version
*
* @param changeLogFile The changelog file we are updating
* @param changeLogId The changelog ID we are adding
* @param databaseChangeLog The DatabaseChangeLog object to set the ID in
* @return ChangeLogRewriterResult A result object with a message and a success flag
*/
public static ChangeLogRewriterResult addChangeLogId(String changeLogFile, String changeLogId, DatabaseChangeLog databaseChangeLog) {
//
// Make changes to the changelog file
//
final ResourceAccessor resourceAccessor = Scope.getCurrentScope().getResourceAccessor();
InputStreamList list = null;
try {
list = resourceAccessor.openStreams("", changeLogFile);
List<URI> uris = list.getURIs();
InputStream is = list.iterator().next();
String encoding = GlobalConfiguration.OUTPUT_FILE_ENCODING.getCurrentValue();
String changeLogString = StreamUtil.readStreamAsString(is, encoding);
if (changeLogFile.toLowerCase().endsWith(".xml")) {
String patternString = "(?ms).*<databaseChangeLog[^>]*>";
Pattern pattern = Pattern.compile(patternString);
Matcher matcher = pattern.matcher(changeLogString);
if (matcher.find()) {
//
// Update the XSD versions
//
String header = changeLogString.substring(matcher.start(), matcher.end() - 1);
String xsdPatternString = "([dbchangelog|liquibase-pro])-3.[0-9]?[0-9]?.xsd";
Pattern xsdPattern = Pattern.compile(xsdPatternString);
Matcher xsdMatcher = xsdPattern.matcher(header);
String editedString = xsdMatcher.replaceAll("$1-" + XMLChangeLogSAXParser.getSchemaVersion() + ".xsd");
//
// Add the changeLogId attribute
//
final String outputChangeLogString = " changeLogId=\"" + changeLogId + "\"";
if (changeLogString.trim().endsWith("/>")) {
changeLogString = changeLogString.replaceFirst("/>", outputChangeLogString + "/>");
} else {
String outputHeader = editedString + outputChangeLogString + ">";
changeLogString = changeLogString.replaceFirst(patternString, outputHeader);
}
}
} else if (changeLogFile.toLowerCase().endsWith(".sql")) {
//
// Formatted SQL changelog
//
String newChangeLogString = changeLogString.replaceFirst("--(\\s*)liquibase formatted sql", "-- liquibase formatted sql changeLogId:" + changeLogId);
if (newChangeLogString.equals(changeLogString)) {
return new ChangeLogRewriterResult("Unable to update changeLogId in changelog file '" + changeLogFile + "'", false);
}
changeLogString = newChangeLogString;
} else if (changeLogFile.toLowerCase().endsWith(".json")) {
//
// JSON changelog
//
changeLogString = changeLogString.replaceFirst("\\[", "\\[\n" + "\"changeLogId\"" + ":" + "\"" + changeLogId + "\",\n");
} else if (changeLogFile.toLowerCase().endsWith(".yml") || changeLogFile.toLowerCase().endsWith(".yaml")) {
//
// YAML changelog
//
changeLogString = changeLogString.replaceFirst("^databaseChangeLog:(\\s*)\n", "databaseChangeLog:$1\n" + "- changeLogId: " + changeLogId + "$1\n");
} else {
return new ChangeLogRewriterResult("Changelog file '" + changeLogFile + "' is not a supported format", false);
}
//
// Write out the file again
//
File f = new File(uris.get(0).getPath());
try (RandomAccessFile randomAccessFile = new RandomAccessFile(f, "rw")) {
randomAccessFile.write(changeLogString.getBytes(encoding));
}
//
if (databaseChangeLog != null) {
databaseChangeLog.setChangeLogId(changeLogId);
}
} catch (IOException ioe) {
return new ChangeLogRewriterResult("* Changelog file '" + changeLogFile + "' with changelog ID '" + changeLogId + "' was not registered due to an error: " + ioe.getMessage(), false);
} finally {
try {
if (list != null) {
list.close();
}
} catch (IOException ioe) {
// consume
}
}
return new ChangeLogRewriterResult("* Changelog file '" + changeLogFile + "' has been updated with changelog ID '" + changeLogId + "'.", true);
}
Aggregations