Search in sources :

Example 1 with TagDatabaseChange

use of liquibase.change.core.TagDatabaseChange 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");
    try (Reader reader = new InputStreamReader(new FileInputStream(oldFile), GlobalConfiguration.OUTPUT_FILE_ENCODING.getCurrentValue());
        Writer writer = new OutputStreamWriter(new FileOutputStream(newFile), GlobalConfiguration.OUTPUT_FILE_ENCODING.getCurrentValue());
        CSVReader csvReader = new CSVReader(reader);
        CSVWriter csvWriter = new CSVWriter(writer)) {
        String[] line;
        while ((line = csvReader.readNext()) != null) {
            csvWriter.writeNext(line);
        }
        String tag = "";
        for (Change change : changeSet.getChanges()) {
            if (change instanceof TagDatabaseChange) {
                TagDatabaseChange tagChange = (TagDatabaseChange) change;
                tag = tagChange.getTag();
            }
        }
        String[] newLine = new String[Columns.values().length];
        newLine[Columns.ID.ordinal()] = changeSet.getId();
        newLine[Columns.AUTHOR.ordinal()] = changeSet.getAuthor();
        newLine[Columns.FILENAME.ordinal()] = changeSet.getFilePath();
        newLine[Columns.DATEEXECUTED.ordinal()] = new ISODateFormat().format(new java.sql.Timestamp(new Date().getTime()));
        newLine[Columns.ORDEREXECUTED.ordinal()] = String.valueOf(getNextSequenceValue());
        newLine[Columns.EXECTYPE.ordinal()] = execType.value;
        newLine[Columns.MD5SUM.ordinal()] = changeSet.generateCheckSum().toString();
        newLine[Columns.DESCRIPTION.ordinal()] = changeSet.getDescription();
        newLine[Columns.COMMENTS.ordinal()] = changeSet.getComments();
        newLine[Columns.TAG.ordinal()] = tag;
        newLine[Columns.LIQUIBASE.ordinal()] = LiquibaseUtil.getBuildVersion();
        newLine[Columns.CONTEXTS.ordinal()] = (changeSet.getContexts() == null) ? null : changeSet.getContexts().toString();
        newLine[Columns.LABELS.ordinal()] = (changeSet.getLabels() == null) ? null : changeSet.getLabels().toString();
        newLine[Columns.DEPLOYMENT_ID.ordinal()] = getDeploymentId();
        csvWriter.writeNext(newLine);
    } catch (Exception e) {
        throw new DatabaseException(e);
    }
    oldFile.delete();
    newFile.renameTo(oldFile);
}
Also used : CSVReader(liquibase.util.csv.CSVReader) TagDatabaseChange(liquibase.change.core.TagDatabaseChange) CSVReader(liquibase.util.csv.CSVReader) CSVWriter(liquibase.util.csv.CSVWriter) Change(liquibase.change.Change) TagDatabaseChange(liquibase.change.core.TagDatabaseChange) Date(java.util.Date) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) DatabaseException(liquibase.exception.DatabaseException) LiquibaseException(liquibase.exception.LiquibaseException) ISODateFormat(liquibase.util.ISODateFormat) DatabaseException(liquibase.exception.DatabaseException) CSVWriter(liquibase.util.csv.CSVWriter)

Example 2 with TagDatabaseChange

use of liquibase.change.core.TagDatabaseChange in project liquibase by liquibase.

the class MarkChangeSetRanGenerator method generateSql.

@Override
public Sql[] generateSql(MarkChangeSetRanStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
    String dateValue = database.getCurrentDateTimeFunction();
    ChangeSet changeSet = statement.getChangeSet();
    SqlStatement runStatement;
    // use LEGACY quoting since we're dealing with system objects
    ObjectQuotingStrategy currentStrategy = database.getObjectQuotingStrategy();
    database.setObjectQuotingStrategy(ObjectQuotingStrategy.LEGACY);
    try {
        try {
            if (statement.getExecType().equals(ChangeSet.ExecType.FAILED) || statement.getExecType().equals(ChangeSet.ExecType.SKIPPED)) {
                // don't mark
                return new Sql[0];
            }
            String tag = null;
            for (Change change : changeSet.getChanges()) {
                if (change instanceof TagDatabaseChange) {
                    TagDatabaseChange tagChange = (TagDatabaseChange) change;
                    tag = tagChange.getTag();
                }
            }
            if (statement.getExecType().ranBefore) {
                runStatement = new UpdateStatement(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), database.getDatabaseChangeLogTableName()).addNewColumnValue("DATEEXECUTED", new DatabaseFunction(dateValue)).addNewColumnValue("ORDEREXECUTED", ChangeLogHistoryServiceFactory.getInstance().getChangeLogService(database).getNextSequenceValue()).addNewColumnValue("MD5SUM", changeSet.generateCheckSum().toString()).addNewColumnValue("EXECTYPE", statement.getExecType().value).addNewColumnValue("DEPLOYMENT_ID", ChangeLogHistoryServiceFactory.getInstance().getChangeLogService(database).getDeploymentId()).addNewColumnValue(COMMENTS, getCommentsColumn(changeSet)).addNewColumnValue(CONTEXTS, getContextsColumn(changeSet)).addNewColumnValue(LABELS, getLabelsColumn(changeSet)).setWhereClause(database.escapeObjectName("ID", LiquibaseColumn.class) + " = ? " + "AND " + database.escapeObjectName("AUTHOR", LiquibaseColumn.class) + " = ? " + "AND " + database.escapeObjectName("FILENAME", LiquibaseColumn.class) + " = ?").addWhereParameters(changeSet.getId(), changeSet.getAuthor(), changeSet.getFilePath());
                if (tag != null) {
                    ((UpdateStatement) runStatement).addNewColumnValue("TAG", tag);
                }
            } else {
                runStatement = new InsertStatement(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), database.getDatabaseChangeLogTableName()).addColumnValue("ID", changeSet.getId()).addColumnValue("AUTHOR", changeSet.getAuthor()).addColumnValue("FILENAME", changeSet.getFilePath()).addColumnValue("DATEEXECUTED", new DatabaseFunction(dateValue)).addColumnValue("ORDEREXECUTED", ChangeLogHistoryServiceFactory.getInstance().getChangeLogService(database).getNextSequenceValue()).addColumnValue("MD5SUM", changeSet.generateCheckSum().toString()).addColumnValue("DESCRIPTION", limitSize(changeSet.getDescription())).addColumnValue(COMMENTS, getCommentsColumn(changeSet)).addColumnValue("EXECTYPE", statement.getExecType().value).addColumnValue(CONTEXTS, getContextsColumn(changeSet)).addColumnValue(LABELS, getLabelsColumn(changeSet)).addColumnValue("LIQUIBASE", StringUtil.limitSize(LiquibaseUtil.getBuildVersion().replaceAll("SNAPSHOT", "SNP").replaceAll("beta", "b").replaceAll("alpha", "b"), 20)).addColumnValue("DEPLOYMENT_ID", ChangeLogHistoryServiceFactory.getInstance().getChangeLogService(database).getDeploymentId());
                if (tag != null) {
                    ((InsertStatement) runStatement).addColumnValue("TAG", tag);
                }
            }
        } catch (LiquibaseException e) {
            throw new UnexpectedLiquibaseException(e);
        }
        return SqlGeneratorFactory.getInstance().generateSql(runStatement, database);
    } finally {
        database.setObjectQuotingStrategy(currentStrategy);
    }
}
Also used : UpdateStatement(liquibase.statement.core.UpdateStatement) DatabaseFunction(liquibase.statement.DatabaseFunction) TagDatabaseChange(liquibase.change.core.TagDatabaseChange) Change(liquibase.change.Change) TagDatabaseChange(liquibase.change.core.TagDatabaseChange) InsertStatement(liquibase.statement.core.InsertStatement) Sql(liquibase.sql.Sql) SqlStatement(liquibase.statement.SqlStatement) LiquibaseColumn(liquibase.changelog.column.LiquibaseColumn) LiquibaseException(liquibase.exception.LiquibaseException) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) ChangeSet(liquibase.changelog.ChangeSet) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) ObjectQuotingStrategy(liquibase.database.ObjectQuotingStrategy)

Aggregations

Change (liquibase.change.Change)2 TagDatabaseChange (liquibase.change.core.TagDatabaseChange)2 LiquibaseException (liquibase.exception.LiquibaseException)2 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)2 Date (java.util.Date)1 ChangeSet (liquibase.changelog.ChangeSet)1 LiquibaseColumn (liquibase.changelog.column.LiquibaseColumn)1 ObjectQuotingStrategy (liquibase.database.ObjectQuotingStrategy)1 DatabaseException (liquibase.exception.DatabaseException)1 Sql (liquibase.sql.Sql)1 DatabaseFunction (liquibase.statement.DatabaseFunction)1 SqlStatement (liquibase.statement.SqlStatement)1 InsertStatement (liquibase.statement.core.InsertStatement)1 UpdateStatement (liquibase.statement.core.UpdateStatement)1 ISODateFormat (liquibase.util.ISODateFormat)1 CSVReader (liquibase.util.csv.CSVReader)1 CSVWriter (liquibase.util.csv.CSVWriter)1