Search in sources :

Example 6 with InputStreamList

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

Example 7 with InputStreamList

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

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