Search in sources :

Example 26 with Change

use of liquibase.change.Change in project liquibase by liquibase.

the class ValidatingVisitor method visit.

@Override
public void visit(ChangeSet changeSet, DatabaseChangeLog databaseChangeLog, Database database, Set<ChangeSetFilterResult> filterResults) throws LiquibaseException {
    RanChangeSet ranChangeSet = findChangeSet(changeSet);
    boolean ran = ranChangeSet != null;
    boolean shouldValidate = !ran || changeSet.shouldRunOnChange() || changeSet.shouldAlwaysRun();
    for (Change change : changeSet.getChanges()) {
        try {
            change.finishInitialization();
        } catch (SetupException se) {
            setupExceptions.add(se);
        }
        if (shouldValidate) {
            warnings.addAll(change.warn(database));
            try {
                ValidationErrors foundErrors = change.validate(database);
                if (foundErrors != null && foundErrors.hasErrors()) {
                    if (changeSet.getOnValidationFail().equals(ChangeSet.ValidationFailOption.MARK_RAN)) {
                        LogFactory.getLogger().info("Skipping changeSet " + changeSet + " due to validation error(s): " + StringUtils.join(foundErrors.getErrorMessages(), ", "));
                        changeSet.setValidationFailed(true);
                    } else {
                        validationErrors.addAll(foundErrors, changeSet);
                    }
                }
            } catch (Throwable e) {
                changeValidationExceptions.add(e);
            }
        }
    }
    if (ranChangeSet != null) {
        if (!changeSet.isCheckSumValid(ranChangeSet.getLastCheckSum())) {
            if (!changeSet.shouldRunOnChange()) {
                invalidMD5Sums.add(changeSet.toString(false) + " was: " + ranChangeSet.getLastCheckSum().toString() + " but is now: " + changeSet.generateCheckSum().toString());
            }
        }
    }
    String changeSetString = changeSet.toString(false);
    if (seenChangeSets.contains(changeSetString)) {
        duplicateChangeSets.add(changeSet);
    } else {
        seenChangeSets.add(changeSetString);
    }
}
Also used : Change(liquibase.change.Change) RanChangeSet(liquibase.changelog.RanChangeSet)

Example 27 with Change

use of liquibase.change.Change in project liquibase by liquibase.

the class ChangeSet method generateCheckSum.

public CheckSum generateCheckSum() {
    if (checkSum == null) {
        StringBuffer stringToMD5 = new StringBuffer();
        for (Change change : getChanges()) {
            stringToMD5.append(change.generateCheckSum()).append(":");
        }
        for (SqlVisitor visitor : this.getSqlVisitors()) {
            stringToMD5.append(visitor.generateCheckSum()).append(";");
        }
        checkSum = CheckSum.compute(stringToMD5.toString());
    }
    return checkSum;
}
Also used : Change(liquibase.change.Change) EmptyChange(liquibase.change.core.EmptyChange) RawSQLChange(liquibase.change.core.RawSQLChange) DbmsTargetedChange(liquibase.change.DbmsTargetedChange) SqlVisitor(liquibase.sql.visitor.SqlVisitor)

Example 28 with Change

use of liquibase.change.Change in project liquibase by liquibase.

the class ChangeSet method handleChildNode.

protected void handleChildNode(ParsedNode child, ResourceAccessor resourceAccessor) throws ParsedNodeException {
    if (child.getName().equals("rollback")) {
        handleRollbackNode(child, resourceAccessor);
    } else if (child.getName().equals("validCheckSum") || child.getName().equals("validCheckSums")) {
        if (child.getValue() == null) {
            return;
        }
        if (child.getValue() instanceof Collection) {
            for (Object checksum : (Collection) child.getValue()) {
                addValidCheckSum((String) checksum);
            }
        } else {
            addValidCheckSum(child.getValue(String.class));
        }
    } else if (child.getName().equals("modifySql")) {
        String dbmsString = StringUtils.trimToNull(child.getChildValue(null, "dbms", String.class));
        String contextString = StringUtils.trimToNull(child.getChildValue(null, "context", String.class));
        String labelsString = StringUtils.trimToNull(child.getChildValue(null, "labels", String.class));
        boolean applyToRollback = child.getChildValue(null, "applyToRollback", false);
        Set<String> dbms = new HashSet<String>();
        if (dbmsString != null) {
            dbms.addAll(StringUtils.splitAndTrim(dbmsString, ","));
        }
        ContextExpression context = null;
        if (contextString != null) {
            context = new ContextExpression(contextString);
        }
        Labels labels = null;
        if (labelsString != null) {
            labels = new Labels(labelsString);
        }
        List<ParsedNode> potentialVisitors = child.getChildren();
        for (ParsedNode node : potentialVisitors) {
            SqlVisitor sqlVisitor = SqlVisitorFactory.getInstance().create(node.getName());
            if (sqlVisitor != null) {
                sqlVisitor.setApplyToRollback(applyToRollback);
                if (dbms.size() > 0) {
                    sqlVisitor.setApplicableDbms(dbms);
                }
                sqlVisitor.setContexts(context);
                sqlVisitor.setLabels(labels);
                sqlVisitor.load(node, resourceAccessor);
                addSqlVisitor(sqlVisitor);
            }
        }
    } else if (child.getName().equals("preConditions")) {
        this.preconditions = new PreconditionContainer();
        try {
            this.preconditions.load(child, resourceAccessor);
        } catch (ParsedNodeException e) {
            e.printStackTrace();
        }
    } else if (child.getName().equals("changes")) {
        for (ParsedNode changeNode : child.getChildren()) {
            handleChildNode(changeNode, resourceAccessor);
        }
    } else {
        Change change = toChange(child, resourceAccessor);
        if (change == null && child.getValue() instanceof String) {
            this.setAttribute(child.getName(), child.getValue());
        } else {
            addChange(change);
        }
    }
}
Also used : ParsedNode(liquibase.parser.core.ParsedNode) PreconditionContainer(liquibase.precondition.core.PreconditionContainer) ContextExpression(liquibase.ContextExpression) Labels(liquibase.Labels) Change(liquibase.change.Change) EmptyChange(liquibase.change.core.EmptyChange) RawSQLChange(liquibase.change.core.RawSQLChange) DbmsTargetedChange(liquibase.change.DbmsTargetedChange) SqlVisitor(liquibase.sql.visitor.SqlVisitor) ParsedNodeException(liquibase.parser.core.ParsedNodeException)

Example 29 with Change

use of liquibase.change.Change in project liquibase by liquibase.

the class FormattedSqlChangeLogSerializer method serialize.

@Override
public String serialize(LiquibaseSerializable object, boolean pretty) {
    if (object instanceof ChangeSet) {
        StringBuilder builder = new StringBuilder();
        ChangeSet changeSet = (ChangeSet) object;
        Database database = getTargetDatabase(changeSet);
        String author = (changeSet.getAuthor()).replaceAll("\\s+", "_");
        author = author.replace("_(generated)", "");
        builder.append("--changeset ").append(author).append(":").append(changeSet.getId()).append("\n");
        for (Change change : changeSet.getChanges()) {
            Sql[] sqls = SqlGeneratorFactory.getInstance().generateSql(change.generateStatements(database), database);
            if (sqls != null) {
                for (Sql sql : sqls) {
                    builder.append(sql.toSql()).append(sql.getEndDelimiter()).append("\n");
                }
            }
        }
        return builder.toString();
    } else {
        throw new UnexpectedLiquibaseException("Cannot serialize object type: " + object.getClass().getName());
    }
}
Also used : Database(liquibase.database.Database) Change(liquibase.change.Change) ChangeSet(liquibase.changelog.ChangeSet) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) Sql(liquibase.sql.Sql)

Example 30 with Change

use of liquibase.change.Change in project liquibase by liquibase.

the class ChangedSequenceChangeGenerator method fixChanged.

@Override
public Change[] fixChanged(DatabaseObject changedObject, ObjectDifferences differences, DiffOutputControl control, Database referenceDatabase, Database comparisonDatabase, ChangeGeneratorChain chain) {
    Sequence sequence = (Sequence) changedObject;
    List<Change> changes = new ArrayList<Change>();
    if (differences.isDifferent("incrementBy")) {
        AlterSequenceChange change = createAlterSequenceChange(sequence, control);
        change.setIncrementBy(sequence.getIncrementBy());
        changes.add(change);
    }
    if (differences.isDifferent("maxValue")) {
        AlterSequenceChange change = createAlterSequenceChange(sequence, control);
        change.setMaxValue(sequence.getMaxValue());
        changes.add(change);
    }
    if (differences.isDifferent("ordered")) {
        AlterSequenceChange change = createAlterSequenceChange(sequence, control);
        change.setOrdered(sequence.getOrdered());
        changes.add(change);
    }
    if (differences.isDifferent("cacheSize")) {
        AlterSequenceChange change = createAlterSequenceChange(sequence, control);
        change.setCacheSize(sequence.getCacheSize());
        changes.add(change);
    }
    if (differences.isDifferent("willCycle")) {
        AlterSequenceChange change = createAlterSequenceChange(sequence, control);
        change.setCycle(sequence.getWillCycle());
        changes.add(change);
    }
    if (changes.size() == 0) {
        return null;
    } else {
        return changes.toArray(new Change[changes.size()]);
    }
}
Also used : ArrayList(java.util.ArrayList) Sequence(liquibase.structure.core.Sequence) AlterSequenceChange(liquibase.change.core.AlterSequenceChange) Change(liquibase.change.Change) AlterSequenceChange(liquibase.change.core.AlterSequenceChange)

Aggregations

Change (liquibase.change.Change)44 ArrayList (java.util.ArrayList)9 Column (liquibase.structure.core.Column)9 ChangeSet (liquibase.changelog.ChangeSet)7 DatabaseObject (liquibase.structure.DatabaseObject)7 DbmsTargetedChange (liquibase.change.DbmsTargetedChange)6 EmptyChange (liquibase.change.core.EmptyChange)6 RawSQLChange (liquibase.change.core.RawSQLChange)6 OracleDatabase (liquibase.database.core.OracleDatabase)6 Database (liquibase.database.Database)4 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)4 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)4 AddColumnConfig (liquibase.change.AddColumnConfig)3 ChangeFactory (liquibase.change.ChangeFactory)3 ChangeMetaData (liquibase.change.ChangeMetaData)3 ChangeParameterMetaData (liquibase.change.ChangeParameterMetaData)3 CreateIndexChange (liquibase.change.core.CreateIndexChange)3 Difference (liquibase.diff.Difference)3 Executor (liquibase.executor.Executor)3 ParsedNodeException (liquibase.parser.core.ParsedNodeException)3