Search in sources :

Example 6 with CSVReader

use of liquibase.util.csv.CSVReader in project liquibase by liquibase.

the class OfflineChangeLogHistoryService method replaceChangeSet.

protected void replaceChangeSet(ChangeSet changeSet, ReplaceChangeSetLogic replaceLogic) throws DatabaseException {
    File oldFile = this.changeLogFile;
    File newFile = new File(oldFile.getParentFile(), oldFile.getName() + ".new");
    Reader reader = null;
    Writer writer = null;
    try {
        reader = new InputStreamReader(new FileInputStream(oldFile), LiquibaseConfiguration.getInstance().getConfiguration(GlobalConfiguration.class).getOutputEncoding());
        writer = new OutputStreamWriter(new FileOutputStream(newFile), LiquibaseConfiguration.getInstance().getConfiguration(GlobalConfiguration.class).getOutputEncoding());
        CSVReader csvReader = new CSVReader(reader);
        CSVWriter csvWriter = new CSVWriter(writer);
        String[] line;
        while ((line = csvReader.readNext()) != null) {
            if (changeSet == null || (line[COLUMN_ID].equals(changeSet.getId()) && line[COLUMN_AUTHOR].equals(changeSet.getAuthor()) && line[COLUMN_FILENAME].equals(changeSet.getFilePath()))) {
                line = replaceLogic.execute(line);
            }
            if (line != null) {
                csvWriter.writeNext(line);
            }
        }
        csvWriter.flush();
        csvWriter.close();
        writer = null;
        csvReader.close();
        reader = null;
        oldFile.delete();
        newFile.renameTo(oldFile);
    } catch (Exception e) {
        throw new DatabaseException(e);
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException ignore) {
            }
        }
        if (writer != null) {
            try {
                writer.flush();
                writer.close();
            } catch (IOException ignore) {
            }
        }
    }
}
Also used : GlobalConfiguration(liquibase.configuration.GlobalConfiguration) CSVReader(liquibase.util.csv.CSVReader) CSVReader(liquibase.util.csv.CSVReader) CSVWriter(liquibase.util.csv.CSVWriter) DatabaseException(liquibase.exception.DatabaseException) LiquibaseException(liquibase.exception.LiquibaseException) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) DatabaseException(liquibase.exception.DatabaseException) CSVWriter(liquibase.util.csv.CSVWriter)

Aggregations

UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)6 CSVReader (liquibase.util.csv.CSVReader)6 GlobalConfiguration (liquibase.configuration.GlobalConfiguration)4 DatabaseException (liquibase.exception.DatabaseException)4 LiquibaseException (liquibase.exception.LiquibaseException)4 ISODateFormat (liquibase.util.ISODateFormat)2 CSVWriter (liquibase.util.csv.CSVWriter)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Reader (java.io.Reader)1 ContextExpression (liquibase.ContextExpression)1 Labels (liquibase.Labels)1 ColumnConfig (liquibase.change.ColumnConfig)1 AbstractJdbcDatabase (liquibase.database.AbstractJdbcDatabase)1 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)1 MySQLDatabase (liquibase.database.core.MySQLDatabase)1 PostgresDatabase (liquibase.database.core.PostgresDatabase)1 EmptyLineAndCommentSkippingInputStream (liquibase.io.EmptyLineAndCommentSkippingInputStream)1 Logger (liquibase.logging.Logger)1 ResourceAccessor (liquibase.resource.ResourceAccessor)1