use of liquibase.resource.InputStreamList in project liquibase by liquibase.
the class ChangelogRewriter method removeChangeLogId.
/**
* Remove the changelog ID from the changelog file
*
* @param changeLogFile The changelog file we are updating
* @param changeLogId The changelog ID we are removing
* @param databaseChangeLog The DatabaseChangeLog object to reset the ID in
* @return ChangeLogRewriterResult A result object with a message and a success flag
*/
public static ChangeLogRewriterResult removeChangeLogId(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")) {
//
// Remove the changelog ID
//
final String changeLogIdString = " changeLogId=\"" + changeLogId + "\"";
String editedString = changeLogString.replaceFirst(changeLogIdString, "");
if (editedString.equals(changeLogString)) {
return new ChangeLogRewriterResult("Unable to update changeLogId in changelog file '" + changeLogFile + "'", false);
}
changeLogString = editedString;
} else if (changeLogFile.toLowerCase().endsWith(".sql")) {
//
// Formatted SQL changelog
//
String newChangeLogString = changeLogString.replaceFirst("--(\\s*)liquibase formatted sql changeLogId:(\\s*)" + changeLogId, "-- liquibase formatted sql");
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("\"changeLogId\"" + ":" + "\"" + changeLogId + "\",", "\n");
} else if (changeLogFile.toLowerCase().endsWith(".yml") || changeLogFile.toLowerCase().endsWith(".yaml")) {
//
// YAML changelog
//
changeLogString = changeLogString.replaceFirst("- changeLogId: " + changeLogId, "");
} else {
return new ChangeLogRewriterResult("Changelog file '" + changeLogFile + "' is not a supported format", false);
}
//
// Write out the file again. We reset the length before writing
// because the length will not shorten otherwise
//
File f = new File(uris.get(0).getPath());
try (RandomAccessFile randomAccessFile = new RandomAccessFile(f, "rw")) {
randomAccessFile.setLength(0);
randomAccessFile.write(changeLogString.getBytes(encoding));
}
//
if (databaseChangeLog != null) {
databaseChangeLog.setChangeLogId(null);
}
String message = "The changeLogId has been removed from changelog '" + changeLogFile + "'.";
Scope.getCurrentScope().getLog(ChangelogRewriter.class).info(message);
return new ChangeLogRewriterResult(message, true);
} catch (IOException ioe) {
String errorMessage = "Changelog file '" + changeLogFile + "' with changelog ID '" + changeLogId + "' was not deactivated due to an error: " + ioe.getMessage();
Scope.getCurrentScope().getLog(ChangelogRewriter.class).warning(errorMessage);
return new ChangeLogRewriterResult(errorMessage, false);
} finally {
try {
if (list != null) {
list.close();
}
} catch (IOException ioe) {
// consume
}
}
}
use of liquibase.resource.InputStreamList in project liquibase by liquibase.
the class SpringResourceAccessor method openStreams.
@Override
public InputStreamList openStreams(String relativeTo, String streamPath) throws IOException {
String searchPath = getCompletePath(relativeTo, streamPath);
searchPath = finalizeSearchPath(searchPath);
final Resource[] resources = ResourcePatternUtils.getResourcePatternResolver(resourceLoader).getResources(searchPath);
InputStreamList returnList = new InputStreamList();
for (Resource foundResource : resources) {
try {
returnList.add(foundResource.getURI(), foundResource.getInputStream());
} catch (FileNotFoundException ignored) {
// don't add it to the return list
}
}
return returnList;
}
use of liquibase.resource.InputStreamList in project liquibase by liquibase.
the class ExecutablePreparedStatementBase method getResourceAsStream.
@java.lang.SuppressWarnings("squid:S2095")
private InputStream getResourceAsStream(String valueLobFile) throws IOException, LiquibaseException {
String fileName = getFileName(valueLobFile);
InputStreamList streams = this.resourceAccessor.openStreams(null, fileName);
if ((streams == null) || streams.isEmpty()) {
return null;
}
if (streams.size() > 1) {
for (InputStream stream : streams) {
stream.close();
}
throw new IOException(streams.size() + " matched " + valueLobFile);
}
return streams.iterator().next();
}
use of liquibase.resource.InputStreamList in project liquibase by liquibase.
the class Liquibase method executeRollbackScript.
protected void executeRollbackScript(String rollbackScript, List<ChangeSet> changeSets, Contexts contexts, LabelExpression labelExpression) throws LiquibaseException {
final Executor executor = Scope.getCurrentScope().getSingleton(ExecutorService.class).getExecutor("jdbc", database);
String rollbackScriptContents;
try (InputStreamList streams = resourceAccessor.openStreams(null, rollbackScript)) {
if ((streams == null) || streams.isEmpty()) {
throw new LiquibaseException("WARNING: The rollback script '" + rollbackScript + "' was not located. Please check your parameters. No rollback was performed");
} else if (streams.size() > 1) {
throw new LiquibaseException("Found multiple rollbackScripts named " + rollbackScript);
}
rollbackScriptContents = StreamUtil.readStreamAsString(streams.iterator().next());
} catch (IOException e) {
throw new LiquibaseException("Error reading rollbackScript " + executor + ": " + e.getMessage());
}
//
// Expand changelog properties
//
changeLogParameters.setContexts(contexts);
changeLogParameters.setLabels(labelExpression);
DatabaseChangeLog changelog = getDatabaseChangeLog();
rollbackScriptContents = changeLogParameters.expandExpressions(rollbackScriptContents, changelog);
RawSQLChange rollbackChange = buildRawSQLChange(rollbackScriptContents);
try {
((HubChangeExecListener) changeExecListener).setRollbackScriptContents(rollbackScriptContents);
sendRollbackMessages(changeSets, changelog, RollbackMessageType.WILL_ROLLBACK, contexts, labelExpression, null);
executor.execute(rollbackChange);
sendRollbackMessages(changeSets, changelog, RollbackMessageType.ROLLED_BACK, contexts, labelExpression, null);
} catch (DatabaseException e) {
Scope.getCurrentScope().getLog(getClass()).warning(e.getMessage());
LOG.severe("Error executing rollback script: " + e.getMessage());
if (changeExecListener != null) {
sendRollbackMessages(changeSets, changelog, RollbackMessageType.ROLLBACK_FAILED, contexts, labelExpression, e);
}
throw new DatabaseException("Error executing rollback script", e);
}
database.commit();
}
use of liquibase.resource.InputStreamList in project iaf by ibissource.
the class LiquibaseResourceAccessor method openStreams.
/**
* This method is primarily used by Liquibase to get the xsd
* (/www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.3.xsd) to validate against. (the path literally contains www.liquibase.org !)
* Since the XSD is in the jar file and we do not want to override it, simply return null.
* Then the default XSD in the Liquibase jar will be used.
*/
@Override
public InputStreamList openStreams(String relativeTo, String streamPath) throws IOException {
String path = streamPath;
if (!streamPath.startsWith("/")) {
// only allow for absolute classpath files.
path = "/" + streamPath;
}
URL url = LiquibaseResourceAccessor.class.getResource(path);
if (url != null) {
try {
URI uri = url.toURI();
return new InputStreamList(uri, url.openStream());
} catch (URISyntaxException e) {
LogUtil.getLogger(this).warn("unable to convert resource url [" + url + "]", e);
}
}
return null;
}
Aggregations