Search in sources :

Example 1 with PreconditionFailedException

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);
    }
}
Also used : FailedPrecondition(liquibase.precondition.FailedPrecondition) Precondition(liquibase.precondition.Precondition) FailedPrecondition(liquibase.precondition.FailedPrecondition) ArrayList(java.util.ArrayList) PreconditionFailedException(liquibase.exception.PreconditionFailedException)

Example 2 with PreconditionFailedException

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);
    }
}
Also used : ChangeLogParameters(liquibase.changelog.ChangeLogParameters) PreconditionFailedException(liquibase.exception.PreconditionFailedException)

Example 3 with PreconditionFailedException

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);
    }
}
Also used : PreconditionFailedException(liquibase.exception.PreconditionFailedException) RanChangeSet(liquibase.changelog.RanChangeSet) ChangeSet(liquibase.changelog.ChangeSet) ObjectQuotingStrategy(liquibase.database.ObjectQuotingStrategy) PreconditionFailedException(liquibase.exception.PreconditionFailedException) PreconditionErrorException(liquibase.exception.PreconditionErrorException) RanChangeSet(liquibase.changelog.RanChangeSet) PreconditionErrorException(liquibase.exception.PreconditionErrorException)

Example 4 with PreconditionFailedException

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);
    }
}
Also used : PreconditionFailedException(liquibase.exception.PreconditionFailedException) RanChangeSet(liquibase.changelog.RanChangeSet) ChangeSet(liquibase.changelog.ChangeSet) ObjectQuotingStrategy(liquibase.database.ObjectQuotingStrategy) PreconditionFailedException(liquibase.exception.PreconditionFailedException) PreconditionErrorException(liquibase.exception.PreconditionErrorException) RanChangeSet(liquibase.changelog.RanChangeSet) PreconditionErrorException(liquibase.exception.PreconditionErrorException)

Example 5 with PreconditionFailedException

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());
            }
        }
    }
}
Also used : Executor(liquibase.executor.Executor) ErrorPrecondition(liquibase.precondition.ErrorPrecondition) FailedPrecondition(liquibase.precondition.FailedPrecondition) ExecutorService(liquibase.executor.ExecutorService) PreconditionFailedException(liquibase.exception.PreconditionFailedException) PreconditionErrorException(liquibase.exception.PreconditionErrorException)

Aggregations

PreconditionFailedException (liquibase.exception.PreconditionFailedException)16 PreconditionErrorException (liquibase.exception.PreconditionErrorException)10 ArrayList (java.util.ArrayList)5 FailedPrecondition (liquibase.precondition.FailedPrecondition)5 Precondition (liquibase.precondition.Precondition)4 Schema (liquibase.structure.core.Schema)4 RanChangeSet (liquibase.changelog.RanChangeSet)3 Table (liquibase.structure.core.Table)3 ChangeLogParameters (liquibase.changelog.ChangeLogParameters)2 ChangeSet (liquibase.changelog.ChangeSet)2 ObjectQuotingStrategy (liquibase.database.ObjectQuotingStrategy)2 TableRowCountStatement (liquibase.statement.core.TableRowCountStatement)2 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)1 Executor (liquibase.executor.Executor)1 ExecutorService (liquibase.executor.ExecutorService)1 ErrorPrecondition (liquibase.precondition.ErrorPrecondition)1 DBMSPrecondition (liquibase.precondition.core.DBMSPrecondition)1 PreconditionContainer (liquibase.precondition.core.PreconditionContainer)1 Column (liquibase.structure.core.Column)1 ForeignKey (liquibase.structure.core.ForeignKey)1