Search in sources :

Example 1 with InputStreamList

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
        }
    }
}
Also used : ResourceAccessor(liquibase.resource.ResourceAccessor) InputStreamList(liquibase.resource.InputStreamList) RandomAccessFile(java.io.RandomAccessFile) InputStream(java.io.InputStream) IOException(java.io.IOException) URI(java.net.URI) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 2 with InputStreamList

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;
}
Also used : InputStreamList(liquibase.resource.InputStreamList) ClassPathResource(org.springframework.core.io.ClassPathResource) ContextResource(org.springframework.core.io.ContextResource) Resource(org.springframework.core.io.Resource) FileNotFoundException(java.io.FileNotFoundException)

Example 3 with InputStreamList

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();
}
Also used : InputStreamList(liquibase.resource.InputStreamList)

Example 4 with InputStreamList

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();
}
Also used : InputStreamList(liquibase.resource.InputStreamList) RawSQLChange(liquibase.change.core.RawSQLChange) Executor(liquibase.executor.Executor) LoggingExecutor(liquibase.executor.LoggingExecutor) HubChangeExecListener(liquibase.hub.listener.HubChangeExecListener) ExecutorService(liquibase.executor.ExecutorService) IOException(java.io.IOException)

Example 5 with InputStreamList

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;
}
Also used : InputStreamList(liquibase.resource.InputStreamList) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) URL(java.net.URL)

Aggregations

InputStreamList (liquibase.resource.InputStreamList)7 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 URI (java.net.URI)3 File (java.io.File)2 RandomAccessFile (java.io.RandomAccessFile)2 ResourceAccessor (liquibase.resource.ResourceAccessor)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 RawSQLChange (liquibase.change.core.RawSQLChange)1 Executor (liquibase.executor.Executor)1 ExecutorService (liquibase.executor.ExecutorService)1 LoggingExecutor (liquibase.executor.LoggingExecutor)1 HubChangeExecListener (liquibase.hub.listener.HubChangeExecListener)1 ClassPathResource (org.springframework.core.io.ClassPathResource)1 ContextResource (org.springframework.core.io.ContextResource)1