use of liquibase.exception.PreconditionFailedException in project liquibase by liquibase.
the class AndPrecondition method check.
@Override
public void check(Database database, DatabaseChangeLog changeLog, ChangeSet changeSet) throws PreconditionFailedException, PreconditionErrorException {
boolean allPassed = true;
List<FailedPrecondition> failures = new ArrayList<FailedPrecondition>();
for (Precondition precondition : getNestedPreconditions()) {
try {
precondition.check(database, changeLog, changeSet);
} catch (PreconditionFailedException e) {
failures.addAll(e.getFailedPreconditions());
allPassed = false;
break;
}
}
if (!allPassed) {
throw new PreconditionFailedException(failures);
}
}
use of liquibase.exception.PreconditionFailedException in project liquibase by liquibase.
the class ChangeLogPropertyDefinedPrecondition method check.
@Override
public void check(Database database, DatabaseChangeLog changeLog, ChangeSet changeSet) throws PreconditionFailedException, PreconditionErrorException {
ChangeLogParameters changeLogParameters = changeLog.getChangeLogParameters();
if (changeLogParameters == null) {
throw new PreconditionFailedException("No Changelog properties were set", changeLog, this);
}
Object propertyValue = changeLogParameters.getValue(property, changeLog);
if (propertyValue == null) {
throw new PreconditionFailedException("Changelog property '" + property + "' was not set", changeLog, this);
}
if (value != null && !propertyValue.toString().equals(value)) {
throw new PreconditionFailedException("Expected changelog property '" + property + "' to have a value of '" + value + "'. Got '" + propertyValue + "'", changeLog, this);
}
}
use of liquibase.exception.PreconditionFailedException in project liquibase by liquibase.
the class ChangeSetExecutedPrecondition method check.
@Override
public void check(Database database, DatabaseChangeLog changeLog, ChangeSet changeSet) throws PreconditionFailedException, PreconditionErrorException {
ObjectQuotingStrategy objectQuotingStrategy = null;
if (changeSet == null) {
objectQuotingStrategy = ObjectQuotingStrategy.LEGACY;
} else {
objectQuotingStrategy = changeSet.getObjectQuotingStrategy();
}
String changeLogFile = getChangeLogFile();
if (changeLogFile == null) {
changeLogFile = changeLog.getLogicalFilePath();
}
ChangeSet interestedChangeSet = new ChangeSet(getId(), getAuthor(), false, false, changeLogFile, null, null, false, objectQuotingStrategy, changeLog);
RanChangeSet ranChangeSet;
try {
ranChangeSet = database.getRanChangeSet(interestedChangeSet);
} catch (Exception e) {
throw new PreconditionErrorException(e, changeLog, this);
}
if (ranChangeSet == null || ranChangeSet.getExecType() == null || !ranChangeSet.getExecType().ran) {
throw new PreconditionFailedException("Change Set '" + interestedChangeSet.toString(false) + "' has not been run", changeLog, this);
}
}
use of liquibase.exception.PreconditionFailedException in project liquibase by liquibase.
the class ChangeSetExecutedPrecondition method check.
@Override
public void check(Database database, DatabaseChangeLog changeLog, ChangeSet changeSet, ChangeExecListener changeExecListener) throws PreconditionFailedException, PreconditionErrorException {
ObjectQuotingStrategy objectQuotingStrategy = null;
if (changeSet == null) {
objectQuotingStrategy = ObjectQuotingStrategy.LEGACY;
} else {
objectQuotingStrategy = changeSet.getObjectQuotingStrategy();
}
String changeLogFile = getChangeLogFile();
if (changeLogFile == null) {
changeLogFile = changeLog.getLogicalFilePath();
}
ChangeSet interestedChangeSet = new ChangeSet(getId(), getAuthor(), false, false, changeLogFile, null, null, false, objectQuotingStrategy, changeLog);
RanChangeSet ranChangeSet;
try {
ranChangeSet = database.getRanChangeSet(interestedChangeSet);
} catch (Exception e) {
throw new PreconditionErrorException(e, changeLog, this);
}
if ((ranChangeSet == null) || (ranChangeSet.getExecType() == null) || !ranChangeSet.getExecType().ran) {
throw new PreconditionFailedException("Change Set '" + interestedChangeSet.toString(false) + "' has not been run", changeLog, this);
}
}
use of liquibase.exception.PreconditionFailedException in project liquibase by liquibase.
the class PreconditionContainer method check.
@Override
public void check(Database database, DatabaseChangeLog changeLog, ChangeSet changeSet, ChangeExecListener changeExecListener) throws PreconditionFailedException, PreconditionErrorException {
String ranOn = String.valueOf(changeLog);
if (changeSet != null) {
ranOn = String.valueOf(changeSet);
}
Executor executor = Scope.getCurrentScope().getSingleton(ExecutorService.class).getExecutor("jdbc", database);
try {
// Three cases for preConditions onUpdateSQL:
// 1. TEST: preConditions should be run, as in regular update mode
// 2. FAIL: the preConditions should fail if there are any
// 3. IGNORE: act as if preConditions don't exist
boolean testPrecondition = false;
if (executor.updatesDatabase()) {
testPrecondition = true;
} else {
if (this.getOnSqlOutput().equals(PreconditionContainer.OnSqlOutputOption.TEST)) {
testPrecondition = true;
} else if (this.getOnSqlOutput().equals(PreconditionContainer.OnSqlOutputOption.FAIL)) {
throw new PreconditionFailedException("Unexpected precondition in updateSQL mode with onUpdateSQL value: " + this.getOnSqlOutput(), changeLog, this);
} else if (this.getOnSqlOutput().equals(PreconditionContainer.OnSqlOutputOption.IGNORE)) {
testPrecondition = false;
}
}
if (testPrecondition) {
super.check(database, changeLog, changeSet, changeExecListener);
}
} catch (PreconditionFailedException e) {
StringBuilder message = new StringBuilder();
message.append(" ").append(e.getFailedPreconditions().size()).append(" preconditions failed").append(StreamUtil.getLineSeparator());
for (FailedPrecondition invalid : e.getFailedPreconditions()) {
message.append(" ").append(invalid.toString());
message.append(StreamUtil.getLineSeparator());
}
if (getOnFailMessage() != null) {
message = new StringBuilder(getOnFailMessage());
}
if (this.getOnFail().equals(PreconditionContainer.FailOption.WARN)) {
final String exceptionMessage = "Executing " + ranOn + " despite precondition failure due to onFail='WARN':\n " + message;
Scope.getCurrentScope().getUI().sendMessage("WARNING: " + exceptionMessage);
Scope.getCurrentScope().getLog(getClass()).warning(exceptionMessage);
if (changeExecListener != null) {
changeExecListener.preconditionFailed(e, FailOption.WARN);
}
} else {
if (getOnFailMessage() == null) {
throw e;
} else {
throw new PreconditionFailedException(getOnFailMessage(), changeLog, this);
}
}
} catch (PreconditionErrorException e) {
StringBuilder message = new StringBuilder();
message.append(" ").append(e.getErrorPreconditions().size()).append(" preconditions failed").append(StreamUtil.getLineSeparator());
for (ErrorPrecondition invalid : e.getErrorPreconditions()) {
message.append(" ").append(invalid.toString());
message.append(StreamUtil.getLineSeparator());
}
if (this.getOnError().equals(PreconditionContainer.ErrorOption.CONTINUE)) {
Scope.getCurrentScope().getLog(getClass()).info("Continuing past: " + toString() + " despite precondition error:\n " + message);
throw e;
} else if (this.getOnError().equals(PreconditionContainer.ErrorOption.WARN)) {
Scope.getCurrentScope().getLog(getClass()).warning("Continuing past: " + toString() + " despite precondition error:\n " + message);
if (changeExecListener != null) {
changeExecListener.preconditionErrored(e, ErrorOption.WARN);
}
} else {
if (getOnErrorMessage() == null) {
throw e;
} else {
throw new PreconditionErrorException(getOnErrorMessage(), e.getErrorPreconditions());
}
}
}
}
Aggregations