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);
}
}
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;
}
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);
}
}
}
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());
}
}
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()]);
}
}
Aggregations