Search in sources :

Example 1 with CSVWriter

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

the class OfflineChangeLogHistoryService method appendChangeSet.

protected void appendChangeSet(ChangeSet changeSet, ChangeSet.ExecType execType) 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) {
            csvWriter.writeNext(line);
        }
        String[] newLine = new String[14];
        newLine[COLUMN_ID] = changeSet.getId();
        newLine[COLUMN_AUTHOR] = changeSet.getAuthor();
        newLine[COLUMN_FILENAME] = changeSet.getFilePath();
        newLine[COLUMN_DATEEXECUTED] = new ISODateFormat().format(new java.sql.Timestamp(new Date().getTime()));
        newLine[COLUMN_ORDEREXECUTED] = String.valueOf(getNextSequenceValue());
        newLine[COLUMN_EXECTYPE] = execType.value;
        newLine[COLUMN_MD5SUM] = changeSet.generateCheckSum().toString();
        newLine[COLUMN_DESCRIPTION] = changeSet.getDescription();
        newLine[COLUMN_COMMENTS] = changeSet.getComments();
        newLine[COLUMN_TAG] = "";
        newLine[COLUMN_LIQUIBASE] = LiquibaseUtil.getBuildVersion().replaceAll("SNAPSHOT", "SNP");
        if (newLine.length > 11) {
            newLine[COLUMN_CONTEXTS] = changeSet.getContexts() == null ? null : changeSet.getContexts().toString();
            newLine[COLUMN_LABELS] = changeSet.getLabels() == null ? null : changeSet.getLabels().toString();
        }
        if (newLine.length > 13) {
            newLine[DEPLOYMENT_ID] = getDeploymentId();
        }
        csvWriter.writeNext(newLine);
        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) ISODateFormat(liquibase.util.ISODateFormat) DatabaseException(liquibase.exception.DatabaseException) CSVWriter(liquibase.util.csv.CSVWriter)

Example 2 with CSVWriter

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

the class OfflineChangeLogHistoryService method writeHeader.

protected void writeHeader(File file) throws IOException {
    Writer writer = null;
    try {
        writer = new OutputStreamWriter(new FileOutputStream(file), LiquibaseConfiguration.getInstance().getConfiguration(GlobalConfiguration.class).getOutputEncoding());
        CSVWriter csvWriter = new CSVWriter(writer);
        csvWriter.writeNext(new String[] { "ID", "AUTHOR", "FILENAME", "DATEEXECUTED", "ORDEREXECUTED", "EXECTYPE", "MD5SUM", "DESCRIPTION", "COMMENTS", "TAG", "LIQUIBASE", "CONTEXTS", "LABELS" });
    } finally {
        if (writer != null) {
            writer.flush();
            writer.close();
        }
    }
}
Also used : GlobalConfiguration(liquibase.configuration.GlobalConfiguration) CSVWriter(liquibase.util.csv.CSVWriter) CSVWriter(liquibase.util.csv.CSVWriter)

Example 3 with CSVWriter

use of liquibase.util.csv.CSVWriter 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)

Example 4 with CSVWriter

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

the class MissingDataExternalFileChangeGenerator method fixMissing.

@Override
public Change[] fixMissing(DatabaseObject missingObject, DiffOutputControl outputControl, Database referenceDatabase, Database comparisionDatabase, ChangeGeneratorChain chain) {
    Statement stmt = null;
    ResultSet rs = null;
    try {
        Data data = (Data) missingObject;
        Table table = data.getTable();
        if (referenceDatabase.isLiquibaseObject(table)) {
            return null;
        }
        String sql = "SELECT * FROM " + referenceDatabase.escapeTableName(table.getSchema().getCatalogName(), table.getSchema().getName(), table.getName());
        stmt = ((JdbcConnection) referenceDatabase.getConnection()).createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
        stmt.setFetchSize(100);
        rs = stmt.executeQuery(sql);
        List<String> columnNames = new ArrayList<String>();
        for (int i = 0; i < rs.getMetaData().getColumnCount(); i++) {
            columnNames.add(rs.getMetaData().getColumnName(i + 1));
        }
        String fileName = table.getName().toLowerCase() + ".csv";
        if (dataDir != null) {
            fileName = dataDir + "/" + fileName;
            File parentDir = new File(dataDir);
            if (!parentDir.exists()) {
                parentDir.mkdirs();
            }
            if (!parentDir.isDirectory()) {
                throw new RuntimeException(parentDir + " is not a directory");
            }
        }
        CSVWriter outputFile = new CSVWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), LiquibaseConfiguration.getInstance().getConfiguration(GlobalConfiguration.class).getOutputEncoding())));
        String[] dataTypes = new String[columnNames.size()];
        String[] line = new String[columnNames.size()];
        for (int i = 0; i < columnNames.size(); i++) {
            line[i] = columnNames.get(i);
        }
        outputFile.writeNext(line);
        int rowNum = 0;
        while (rs.next()) {
            line = new String[columnNames.size()];
            for (int i = 0; i < columnNames.size(); i++) {
                Object value = JdbcUtils.getResultSetValue(rs, i + 1);
                if (dataTypes[i] == null && value != null) {
                    if (value instanceof Number) {
                        dataTypes[i] = "NUMERIC";
                    } else if (value instanceof Boolean) {
                        dataTypes[i] = "BOOLEAN";
                    } else if (value instanceof Date) {
                        dataTypes[i] = "DATE";
                    } else {
                        dataTypes[i] = "STRING";
                    }
                }
                if (value == null) {
                    line[i] = "NULL";
                } else {
                    if (value instanceof Date) {
                        line[i] = new ISODateFormat().format(((Date) value));
                    } else {
                        line[i] = value.toString();
                    }
                }
            }
            outputFile.writeNext(line);
            rowNum++;
            if (rowNum % 5000 == 0) {
                outputFile.flush();
            }
        }
        outputFile.flush();
        outputFile.close();
        LoadDataChange change = new LoadDataChange();
        change.setFile(fileName);
        change.setEncoding(LiquibaseConfiguration.getInstance().getConfiguration(GlobalConfiguration.class).getOutputEncoding());
        if (outputControl.getIncludeCatalog()) {
            change.setCatalogName(table.getSchema().getCatalogName());
        }
        if (outputControl.getIncludeSchema()) {
            change.setSchemaName(table.getSchema().getName());
        }
        change.setTableName(table.getName());
        for (int i = 0; i < columnNames.size(); i++) {
            String colName = columnNames.get(i);
            LoadDataColumnConfig columnConfig = new LoadDataColumnConfig();
            columnConfig.setHeader(colName);
            columnConfig.setName(colName);
            columnConfig.setType(dataTypes[i]);
            change.addColumn(columnConfig);
        }
        return new Change[] { change };
    } catch (Exception e) {
        throw new UnexpectedLiquibaseException(e);
    } finally {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException ignore) {
            }
        }
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException ignore) {
            }
        }
    }
}
Also used : SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) CSVWriter(liquibase.util.csv.CSVWriter) BufferedWriter(java.io.BufferedWriter) ISODateFormat(liquibase.util.ISODateFormat) ResultSet(java.sql.ResultSet) LoadDataColumnConfig(liquibase.change.core.LoadDataColumnConfig) LoadDataChange(liquibase.change.core.LoadDataChange) Table(liquibase.structure.core.Table) Statement(java.sql.Statement) Data(liquibase.structure.core.Data) Change(liquibase.change.Change) LoadDataChange(liquibase.change.core.LoadDataChange) Date(java.util.Date) SQLException(java.sql.SQLException) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) DatabaseObject(liquibase.structure.DatabaseObject) File(java.io.File) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException)

Aggregations

CSVWriter (liquibase.util.csv.CSVWriter)4 GlobalConfiguration (liquibase.configuration.GlobalConfiguration)3 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)3 DatabaseException (liquibase.exception.DatabaseException)2 LiquibaseException (liquibase.exception.LiquibaseException)2 ISODateFormat (liquibase.util.ISODateFormat)2 CSVReader (liquibase.util.csv.CSVReader)2 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 Change (liquibase.change.Change)1 LoadDataChange (liquibase.change.core.LoadDataChange)1 LoadDataColumnConfig (liquibase.change.core.LoadDataColumnConfig)1 DatabaseObject (liquibase.structure.DatabaseObject)1