diff --git a/liquibase-cli/src/main/java/liquibase/integration/commandline/CommandRunner.java b/liquibase-cli/src/main/java/liquibase/integration/commandline/CommandRunner.java index 37354f28f46..61ee04bd360 100644 --- a/liquibase-cli/src/main/java/liquibase/integration/commandline/CommandRunner.java +++ b/liquibase-cli/src/main/java/liquibase/integration/commandline/CommandRunner.java @@ -6,7 +6,6 @@ import liquibase.command.CommonArgumentNames; import liquibase.exception.CommandValidationException; import liquibase.exception.MissingRequiredArgumentException; -import liquibase.resource.OpenOptions; import liquibase.resource.PathHandlerFactory; import liquibase.util.StringUtil; import picocli.CommandLine; @@ -48,7 +47,7 @@ public CommandResults call() throws Exception { try { if (outputFile != null) { final PathHandlerFactory pathHandlerFactory = Scope.getCurrentScope().getSingleton(PathHandlerFactory.class); - outputStream = pathHandlerFactory.openResourceOutputStream(outputFile, new OpenOptions()); + outputStream = pathHandlerFactory.openResourceOutputStream(outputFile, true); commandScope.setOutput(outputStream); } diff --git a/liquibase-cli/src/main/java/liquibase/integration/commandline/LiquibaseCommandLine.java b/liquibase-cli/src/main/java/liquibase/integration/commandline/LiquibaseCommandLine.java index cdb4bc198f2..0453bbe238c 100644 --- a/liquibase-cli/src/main/java/liquibase/integration/commandline/LiquibaseCommandLine.java +++ b/liquibase-cli/src/main/java/liquibase/integration/commandline/LiquibaseCommandLine.java @@ -60,7 +60,7 @@ public class LiquibaseCommandLine { private Level configuredLogLevel; private final CommandLine commandLine; - private Handler fileHandler; + private FileHandler fileHandler; private final ResourceBundle coreBundle = getBundle("liquibase/i18n/liquibase-core"); @@ -89,6 +89,7 @@ public static void main(String[] args) { private void cleanup() { if (fileHandler != null) { fileHandler.flush(); + fileHandler.close(); } } @@ -352,9 +353,9 @@ public int execute(String[] args) { int response = commandLine.execute(finalArgs); if (!wasHelpOrVersionRequested()) { - final ConfiguredValue logFile = LiquibaseCommandLineConfiguration.LOG_FILE.getCurrentConfiguredValue(); + final ConfiguredValue logFile = LiquibaseCommandLineConfiguration.LOG_FILE.getCurrentConfiguredValue(); if (logFile.found()) { - Scope.getCurrentScope().getUI().sendMessage("Logs saved to " + logFile.getValue()); + Scope.getCurrentScope().getUI().sendMessage("Logs saved to " + logFile.getValue().getAbsolutePath()); } final ConfiguredValue outputFile = LiquibaseCommandLineConfiguration.OUTPUT_FILE.getCurrentConfiguredValue(); @@ -610,7 +611,7 @@ private void configureVersionInfo() { protected Map configureLogging() throws IOException { Map returnMap = new HashMap<>(); final ConfiguredValue currentConfiguredValue = LiquibaseCommandLineConfiguration.LOG_LEVEL.getCurrentConfiguredValue(); - final String logFile = LiquibaseCommandLineConfiguration.LOG_FILE.getCurrentValue(); + final File logFile = LiquibaseCommandLineConfiguration.LOG_FILE.getCurrentValue(); Level logLevel = Level.OFF; if (!currentConfiguredValue.wasDefaultValueUsed()) { @@ -629,7 +630,7 @@ protected Map configureLogging() throws IOException { return returnMap; } - private void configureLogging(Level logLevel, String logFile) throws IOException { + private void configureLogging(Level logLevel, File logFile) throws IOException { configuredLogLevel = logLevel; final JavaLogService logService = (JavaLogService) Scope.getCurrentScope().get(Scope.Attr.logService, LogService.class); @@ -643,9 +644,8 @@ private void configureLogging(Level logLevel, String logFile) throws IOException if (logFile != null) { if (fileHandler == null) { - final PathHandlerFactory pathHandlerFactory = Scope.getCurrentScope().getSingleton(PathHandlerFactory.class); - OutputStream outputStream = pathHandlerFactory.openResourceOutputStream(logFile, new OpenOptions().setAppend(true)); - fileHandler = new StreamHandler(outputStream, new SimpleFormatter()); + fileHandler = new FileHandler(logFile.getAbsolutePath(), true); + fileHandler.setFormatter(new SimpleFormatter()); rootLogger.addHandler(fileHandler); } diff --git a/liquibase-core/src/main/java/liquibase/changelog/ChangelogRewriter.java b/liquibase-core/src/main/java/liquibase/changelog/ChangelogRewriter.java index 18b5693dfe4..f5ab9b98044 100644 --- a/liquibase-core/src/main/java/liquibase/changelog/ChangelogRewriter.java +++ b/liquibase-core/src/main/java/liquibase/changelog/ChangelogRewriter.java @@ -1,16 +1,15 @@ package liquibase.changelog; -import liquibase.GlobalConfiguration; import liquibase.Scope; +import liquibase.GlobalConfiguration; import liquibase.parser.core.xml.XMLChangeLogSAXParser; -import liquibase.resource.OpenOptions; +import liquibase.resource.PathHandlerFactory; import liquibase.resource.Resource; import liquibase.resource.ResourceAccessor; import liquibase.util.StreamUtil; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; +import java.io.*; +import java.util.SortedSet; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -74,7 +73,7 @@ public static ChangeLogRewriterResult removeChangeLogId(String changeLogFile, St } } - try (OutputStream outputStream = resource.openOutputStream(new OpenOptions())) { + try (OutputStream outputStream = resource.openOutputStream(true)) { outputStream.write(changeLogString.getBytes(encoding)); } @@ -171,7 +170,7 @@ public static ChangeLogRewriterResult addChangeLogId(String changeLogFile, Strin // // Write out the file again // - try (OutputStream outputStream = resource.openOutputStream(new OpenOptions())) { + try (OutputStream outputStream = resource.openOutputStream(true)) { outputStream.write(changeLogString.getBytes(encoding)); } diff --git a/liquibase-core/src/main/java/liquibase/changelog/visitor/DBDocVisitor.java b/liquibase-core/src/main/java/liquibase/changelog/visitor/DBDocVisitor.java index 7b71b47dea5..6e819372b33 100644 --- a/liquibase-core/src/main/java/liquibase/changelog/visitor/DBDocVisitor.java +++ b/liquibase-core/src/main/java/liquibase/changelog/visitor/DBDocVisitor.java @@ -7,7 +7,6 @@ import liquibase.database.Database; import liquibase.dbdoc.*; import liquibase.exception.LiquibaseException; -import liquibase.resource.OpenOptions; import liquibase.resource.Resource; import liquibase.resource.ResourceAccessor; import liquibase.snapshot.DatabaseSnapshot; @@ -177,7 +176,7 @@ private void copyFile(String fileToCopy, Resource rootOutputDir) throws IOExcept if (inputStream == null) { throw new IOException("Can not find " + fileToCopy); } - outputStream = rootOutputDir.resolve(fileToCopy.replaceFirst(".*\\/", "")).openOutputStream(new OpenOptions()); + outputStream = rootOutputDir.resolve(fileToCopy.replaceFirst(".*\\/", "")).openOutputStream(true); StreamUtil.copy(inputStream, outputStream); } finally { if (outputStream != null) { diff --git a/liquibase-core/src/main/java/liquibase/dbdoc/ChangeLogWriter.java b/liquibase-core/src/main/java/liquibase/dbdoc/ChangeLogWriter.java index 2241306080e..1cf9068f9ac 100644 --- a/liquibase-core/src/main/java/liquibase/dbdoc/ChangeLogWriter.java +++ b/liquibase-core/src/main/java/liquibase/dbdoc/ChangeLogWriter.java @@ -1,7 +1,6 @@ package liquibase.dbdoc; import liquibase.GlobalConfiguration; -import liquibase.resource.OpenOptions; import liquibase.resource.Resource; import liquibase.resource.ResourceAccessor; import liquibase.util.StreamUtil; @@ -21,7 +20,7 @@ public void writeChangeLog(String changeLog, String physicalFilePath) throws IOE String changeLogOutFile = changeLog.replace(":", "_"); Resource xmlFile = outputDir.resolve(changeLogOutFile.toLowerCase() + ".html"); - try (BufferedWriter changeLogStream = new BufferedWriter(new OutputStreamWriter(xmlFile.openOutputStream(new OpenOptions()), + try (BufferedWriter changeLogStream = new BufferedWriter(new OutputStreamWriter(xmlFile.openOutputStream(true), GlobalConfiguration.OUTPUT_FILE_ENCODING.getCurrentValue()))) { Resource stylesheet = resourceAccessor.get(physicalFilePath); if (stylesheet == null) { diff --git a/liquibase-core/src/main/java/liquibase/dbdoc/HTMLListWriter.java b/liquibase-core/src/main/java/liquibase/dbdoc/HTMLListWriter.java index d69283efb20..9dc57135bdc 100644 --- a/liquibase-core/src/main/java/liquibase/dbdoc/HTMLListWriter.java +++ b/liquibase-core/src/main/java/liquibase/dbdoc/HTMLListWriter.java @@ -1,7 +1,6 @@ package liquibase.dbdoc; import liquibase.GlobalConfiguration; -import liquibase.resource.OpenOptions; import liquibase.resource.Resource; import liquibase.util.StringUtil; @@ -22,7 +21,7 @@ public HTMLListWriter(String title, String filename, String subdir, Resource out } public void writeHTML(SortedSet objects) throws IOException { - Writer fileWriter = new OutputStreamWriter(outputDir.resolve(filename).openOutputStream(new OpenOptions()), GlobalConfiguration.OUTPUT_FILE_ENCODING.getCurrentValue()); + Writer fileWriter = new OutputStreamWriter(outputDir.resolve(filename).openOutputStream(true), GlobalConfiguration.OUTPUT_FILE_ENCODING.getCurrentValue()); try { fileWriter.append("\n" + "\n" + "\n"); diff --git a/liquibase-core/src/main/java/liquibase/dbdoc/HTMLWriter.java b/liquibase-core/src/main/java/liquibase/dbdoc/HTMLWriter.java index 388b21b6dff..c8099ae50e0 100644 --- a/liquibase-core/src/main/java/liquibase/dbdoc/HTMLWriter.java +++ b/liquibase-core/src/main/java/liquibase/dbdoc/HTMLWriter.java @@ -2,17 +2,15 @@ import liquibase.change.Change; import liquibase.changelog.ChangeSet; +import liquibase.GlobalConfiguration; import liquibase.database.Database; import liquibase.exception.DatabaseException; import liquibase.exception.DatabaseHistoryException; -import liquibase.resource.OpenOptions; import liquibase.resource.Resource; import liquibase.util.LiquibaseUtil; import liquibase.util.StringUtil; -import java.io.IOException; -import java.io.OutputStreamWriter; -import java.io.Writer; +import java.io.*; import java.text.DateFormat; import java.util.Date; import java.util.List; @@ -29,7 +27,7 @@ public HTMLWriter(Resource outputDir, Database database) { protected abstract void writeCustomHTML(Writer fileWriter, Object object, List<Change> changes, Database database) throws IOException; private Writer createFileWriter(Object object) throws IOException { - return new OutputStreamWriter(outputDir.resolve(DBDocUtil.toFileName(object.toString().toLowerCase()) + ".html").openOutputStream(new OpenOptions())); + return new OutputStreamWriter(outputDir.resolve(DBDocUtil.toFileName(object.toString().toLowerCase()) + ".html").openOutputStream(true)); } public void writeHTML(Object object, List<Change> ranChanges, List<Change> changesToRun, String changeLog) throws IOException, DatabaseHistoryException, DatabaseException { diff --git a/liquibase-core/src/main/java/liquibase/diff/output/changelog/DiffToChangeLog.java b/liquibase-core/src/main/java/liquibase/diff/output/changelog/DiffToChangeLog.java index d550097d90c..5d0400bbb1d 100644 --- a/liquibase-core/src/main/java/liquibase/diff/output/changelog/DiffToChangeLog.java +++ b/liquibase-core/src/main/java/liquibase/diff/output/changelog/DiffToChangeLog.java @@ -16,7 +16,6 @@ import liquibase.exception.UnexpectedLiquibaseException; import liquibase.executor.Executor; import liquibase.executor.ExecutorService; -import liquibase.resource.OpenOptions; import liquibase.resource.PathHandlerFactory; import liquibase.resource.Resource; import liquibase.serializer.ChangeLogSerializer; @@ -158,7 +157,7 @@ public void run() { String toInsert = " " + innerXml + lineSeparator; fileContents.insert(endTagIndex, toInsert); } - try (OutputStream outputStream = file.openOutputStream(new OpenOptions())) { + try (OutputStream outputStream = file.openOutputStream(true)) { outputStream.write(fileContents.toString().getBytes()); } } @@ -212,7 +211,7 @@ public void printNew(ChangeLogSerializer changeLogSerializer, Resource file) thr Scope.getCurrentScope().getLog(getClass()).info(file + " does not exist, creating and adding " + changeSets.size() + " changesets."); } - try (OutputStream stream = file.openOutputStream(new OpenOptions()); + try (OutputStream stream = file.openOutputStream(true); PrintStream out = new PrintStream(stream, true, GlobalConfiguration.OUTPUT_FILE_ENCODING.getCurrentValue())) { changeLogSerializer.write(changeSets, out); } diff --git a/liquibase-core/src/main/java/liquibase/diff/output/changelog/core/MissingDataExternalFileChangeGenerator.java b/liquibase-core/src/main/java/liquibase/diff/output/changelog/core/MissingDataExternalFileChangeGenerator.java index 48583416b90..30fc7b33278 100644 --- a/liquibase-core/src/main/java/liquibase/diff/output/changelog/core/MissingDataExternalFileChangeGenerator.java +++ b/liquibase-core/src/main/java/liquibase/diff/output/changelog/core/MissingDataExternalFileChangeGenerator.java @@ -10,7 +10,6 @@ import liquibase.diff.output.DiffOutputControl; import liquibase.diff.output.changelog.ChangeGeneratorChain; import liquibase.exception.UnexpectedLiquibaseException; -import liquibase.resource.OpenOptions; import liquibase.resource.PathHandlerFactory; import liquibase.resource.Resource; import liquibase.servicelocator.LiquibaseService; @@ -85,7 +84,7 @@ public Change[] fixMissing(DatabaseObject missingObject, DiffOutputControl outpu String[] dataTypes = new String[0]; try ( - OutputStream fileOutputStream = externalFileResource.openOutputStream(new OpenOptions()); + OutputStream fileOutputStream = externalFileResource.openOutputStream(true); OutputStreamWriter outputStreamWriter = new OutputStreamWriter( fileOutputStream, GlobalConfiguration.OUTPUT_FILE_ENCODING.getCurrentValue()); CSVWriter outputFile = new CSVWriter(new BufferedWriter(outputStreamWriter)); diff --git a/liquibase-core/src/main/java/liquibase/integration/commandline/LiquibaseCommandLineConfiguration.java b/liquibase-core/src/main/java/liquibase/integration/commandline/LiquibaseCommandLineConfiguration.java index 9d65a090e61..140a77e0686 100644 --- a/liquibase-core/src/main/java/liquibase/integration/commandline/LiquibaseCommandLineConfiguration.java +++ b/liquibase-core/src/main/java/liquibase/integration/commandline/LiquibaseCommandLineConfiguration.java @@ -22,7 +22,7 @@ public class LiquibaseCommandLineConfiguration implements AutoloadedConfiguratio public static final ConfigurationDefinition<String> DEFAULTS_FILE; public static final ConfigurationDefinition<Level> LOG_LEVEL; public static final ConfigurationDefinition<String> LOG_CHANNELS; - public static final ConfigurationDefinition<String> LOG_FILE; + public static final ConfigurationDefinition<File> LOG_FILE; public static final ConfigurationDefinition<String> OUTPUT_FILE; public static final ConfigurationDefinition<Boolean> SHOULD_RUN; public static final ConfigurationDefinition<ArgumentConverter> ARGUMENT_CONVERTER; @@ -66,7 +66,7 @@ public class LiquibaseCommandLineConfiguration implements AutoloadedConfiguratio .setDefaultValue("liquibase", "Controls which log channels have their level set by the liquibase.logLevel setting. Comma separate multiple values. To set the level of all channels, use 'all'. Example: liquibase,org.mariadb.jdbc") .build(); - LOG_FILE = builder.define("logFile", String.class).build(); + LOG_FILE = builder.define("logFile", File.class).build(); OUTPUT_FILE = builder.define("outputFile", String.class).build(); MONITOR_PERFORMANCE = builder.define("monitorPerformance", String.class) diff --git a/liquibase-core/src/main/java/liquibase/integration/spring/SpringResource.java b/liquibase-core/src/main/java/liquibase/integration/spring/SpringResource.java index cf0040228d2..466cb542fa6 100644 --- a/liquibase-core/src/main/java/liquibase/integration/spring/SpringResource.java +++ b/liquibase-core/src/main/java/liquibase/integration/spring/SpringResource.java @@ -1,7 +1,6 @@ package liquibase.integration.spring; import liquibase.exception.UnexpectedLiquibaseException; -import liquibase.resource.OpenOptions; import org.springframework.core.io.Resource; import org.springframework.core.io.WritableResource; @@ -60,13 +59,10 @@ public InputStream openInputStream() throws IOException { } @Override - public OutputStream openOutputStream(OpenOptions openOptions) throws IOException { - if (!resource.exists() && !openOptions.isCreateIfNeeded()) { + public OutputStream openOutputStream(boolean createIfNeeded) throws IOException { + if (!resource.exists() && !createIfNeeded) { throw new IOException("Resource "+getUri()+" does not exist"); } - if (openOptions != null && openOptions.isAppend() && exists()) { - throw new IOException("Spring only supports truncating the existing resources."); - } if (resource instanceof WritableResource) { return ((WritableResource) resource).getOutputStream(); } diff --git a/liquibase-core/src/main/java/liquibase/resource/AbstractResource.java b/liquibase-core/src/main/java/liquibase/resource/AbstractResource.java index 2a62d2deed8..673fdb0d905 100644 --- a/liquibase-core/src/main/java/liquibase/resource/AbstractResource.java +++ b/liquibase-core/src/main/java/liquibase/resource/AbstractResource.java @@ -1,5 +1,7 @@ package liquibase.resource; +import liquibase.util.StringUtil; + import java.io.IOException; import java.io.OutputStream; import java.net.URI; @@ -38,7 +40,7 @@ public boolean isWritable() { } @Override - public OutputStream openOutputStream(OpenOptions openOptions) throws IOException { + public OutputStream openOutputStream(boolean createIfNeeded) throws IOException { if (!isWritable()) { throw new IOException("Read only"); } diff --git a/liquibase-core/src/main/java/liquibase/resource/OpenOptions.java b/liquibase-core/src/main/java/liquibase/resource/OpenOptions.java deleted file mode 100644 index 786d02a43ca..00000000000 --- a/liquibase-core/src/main/java/liquibase/resource/OpenOptions.java +++ /dev/null @@ -1,55 +0,0 @@ -package liquibase.resource; - -/** - * Defines the options for opening {@link Resource}s in Liquibase. - */ -public class OpenOptions { - private boolean truncate; - private boolean createIfNeeded; - - /** - * Use default options of truncate = true, createIfNeeded = true; - */ - public OpenOptions() { - this.truncate = true; - this.createIfNeeded = true; - } - - /** - * Should an existing file be truncated when opened. Both this and {@link #isAppend()} - * are automatically kept in sync with each other. - */ - public boolean isTruncate() { - return truncate; - } - - public OpenOptions setTruncate(boolean truncate) { - this.truncate = truncate; - return this; - } - - /** - * Should an existing file be appended to when opened. Both this and {@link #isTruncate()} - * are automatically kept in sync with each other. - */ - public boolean isAppend() { - return !isTruncate(); - } - - public OpenOptions setAppend(boolean append) { - this.truncate = !append; - return this; - } - - /** - * If true, create the resource if it does not exist. If false, do not create the resource. - */ - public boolean isCreateIfNeeded() { - return createIfNeeded; - } - - public OpenOptions setCreateIfNeeded(boolean createIfNeeded) { - this.createIfNeeded = createIfNeeded; - return this; - } -} diff --git a/liquibase-core/src/main/java/liquibase/resource/PathHandlerFactory.java b/liquibase-core/src/main/java/liquibase/resource/PathHandlerFactory.java index d78b1547f6a..b4f445645ee 100644 --- a/liquibase-core/src/main/java/liquibase/resource/PathHandlerFactory.java +++ b/liquibase-core/src/main/java/liquibase/resource/PathHandlerFactory.java @@ -1,5 +1,6 @@ package liquibase.resource; +import liquibase.Scope; import liquibase.exception.UnexpectedLiquibaseException; import liquibase.plugin.AbstractPluginFactory; @@ -74,30 +75,17 @@ public Resource getResource(String resourcePath) throws IOException { * * @return null if resourcePath does not exist and createIfNotExists is false * @throws IOException if there is an error opening the stream - * - * @deprecated use {@link #openResourceOutputStream(String, OpenOptions)} */ - @Deprecated public OutputStream openResourceOutputStream(String resourcePath, boolean createIfNotExists) throws IOException { - return openResourceOutputStream(resourcePath, new OpenOptions().setCreateIfNeeded(createIfNotExists)); - } - - /** - * Returns the outputStream from {@link #getResource(String)}, using settings from the passed {@link OpenOptions}. - * - * @return null if resourcePath does not exist and {@link OpenOptions#isCreateIfNeeded()} is false - * @throws IOException if there is an error opening the stream - */ - public OutputStream openResourceOutputStream(String resourcePath, OpenOptions openOptions) throws IOException { Resource resource = getResource(resourcePath); if (!resource.exists()) { - if (openOptions.isCreateIfNeeded()) { + if (createIfNotExists) { return createResource(resourcePath); } else { return null; } } - return resource.openOutputStream(openOptions); + return resource.openOutputStream(createIfNotExists); } /** diff --git a/liquibase-core/src/main/java/liquibase/resource/PathResource.java b/liquibase-core/src/main/java/liquibase/resource/PathResource.java index 7ea4908da70..d4ebc03839d 100644 --- a/liquibase-core/src/main/java/liquibase/resource/PathResource.java +++ b/liquibase-core/src/main/java/liquibase/resource/PathResource.java @@ -4,12 +4,7 @@ import java.io.*; import java.nio.file.Files; -import java.nio.file.OpenOption; import java.nio.file.Path; -import java.nio.file.StandardOpenOption; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; import java.util.zip.GZIPInputStream; public class PathResource extends AbstractResource { @@ -60,9 +55,9 @@ public boolean isWritable() { } @Override - public OutputStream openOutputStream(OpenOptions openOptions) throws IOException { + public OutputStream openOutputStream(boolean createIfNeeded) throws IOException { if (!exists()) { - if (openOptions.isCreateIfNeeded()) { + if (createIfNeeded) { Path parent = path.getParent(); if (parent != null) { boolean mkdirs = parent.toFile().mkdirs(); @@ -78,19 +73,7 @@ public OutputStream openOutputStream(OpenOptions openOptions) throws IOException if (Files.isDirectory(this.path)) { throw new FileNotFoundException(this.getPath() + " is a directory"); } else { - List<StandardOpenOption> options = new ArrayList<>(); - if (openOptions.isCreateIfNeeded()) { - options.add(StandardOpenOption.CREATE); - } - - if (openOptions.isTruncate()) { - options.addAll(Arrays.asList(StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE)); - } else { - options.add(StandardOpenOption.APPEND); - } - - return Files.newOutputStream(this.path, options.toArray(new OpenOption[0])); - + return Files.newOutputStream(this.path); } } } diff --git a/liquibase-core/src/main/java/liquibase/resource/Resource.java b/liquibase-core/src/main/java/liquibase/resource/Resource.java index 85ec37fcbd8..67323312d6d 100644 --- a/liquibase-core/src/main/java/liquibase/resource/Resource.java +++ b/liquibase-core/src/main/java/liquibase/resource/Resource.java @@ -50,21 +50,14 @@ public interface Resource { */ Resource resolveSibling(String other); - /** - * Opens an output stream given the passed {@link OpenOptions}. - * Cannot pass a null OpenOptions value - */ - OutputStream openOutputStream(OpenOptions openOptions) throws IOException; /** - * Opens an output stream to write to this resource using the default {@link OpenOptions#OpenOptions()} settings plus the passed createIfNeeded value. + * Opens an output stream to write to this resource. Note that calling this method will truncate (erase) the existing file. * - * @deprecated use {@link #openOutputStream(OpenOptions)} + * @param createIfNeeded if true, create the resource if it does not exist. If false, throw an exception if it does not exist + * @throws IOException if there is an error writing to the resource, including if the resource does not exist or permission don't allow writing. */ - @Deprecated - default OutputStream openOutputStream(boolean createIfNeeded) throws IOException { - return openOutputStream(new OpenOptions().setCreateIfNeeded(createIfNeeded)); - } + OutputStream openOutputStream(boolean createIfNeeded) throws IOException; /** * Returns a unique and complete identifier for this resource. diff --git a/liquibase-core/src/main/java/liquibase/resource/ResourceAccessor.java b/liquibase-core/src/main/java/liquibase/resource/ResourceAccessor.java index 2505b25e1a2..b86914e0677 100644 --- a/liquibase-core/src/main/java/liquibase/resource/ResourceAccessor.java +++ b/liquibase-core/src/main/java/liquibase/resource/ResourceAccessor.java @@ -253,8 +253,8 @@ public Resource resolveSibling(String other) { } @Override - public OutputStream openOutputStream(OpenOptions openOptions) throws IOException { - return openOutputStream(new OpenOptions()); + public OutputStream openOutputStream(boolean createIfNeeded) throws IOException { + throw new UnexpectedLiquibaseException("Resource does not exist"); } } } diff --git a/liquibase-core/src/test/groovy/liquibase/resource/PathHandlerFactoryTest.groovy b/liquibase-core/src/test/groovy/liquibase/resource/PathHandlerFactoryTest.groovy index 55fdb7eeb69..5179c351949 100644 --- a/liquibase-core/src/test/groovy/liquibase/resource/PathHandlerFactoryTest.groovy +++ b/liquibase-core/src/test/groovy/liquibase/resource/PathHandlerFactoryTest.groovy @@ -52,10 +52,10 @@ class PathHandlerFactoryTest extends Specification { def pathHandlerFactory = Scope.currentScope.getSingleton(PathHandlerFactory) then: - pathHandlerFactory.openResourceOutputStream(path, new OpenOptions().setCreateIfNeeded(false)) == null //when createIfNotExists is false + pathHandlerFactory.openResourceOutputStream(path, false) == null //when createIfNotExists is false when: - def stream = pathHandlerFactory.openResourceOutputStream(path, new OpenOptions().setCreateIfNeeded(true)) //createIfNotExists is true + def stream = pathHandlerFactory.openResourceOutputStream(path, true) //createIfNotExists is true stream.withWriter { it.write("test") } @@ -66,7 +66,7 @@ class PathHandlerFactoryTest extends Specification { when: //can update file - stream = pathHandlerFactory.openResourceOutputStream(path, new OpenOptions()) + stream = pathHandlerFactory.openResourceOutputStream(path, true) stream.withWriter { it.write("test 2") }