Search in sources :

Example 1 with PreconditionErrorException

use of liquibase.exception.PreconditionErrorException 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 2 with PreconditionErrorException

use of liquibase.exception.PreconditionErrorException 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 3 with PreconditionErrorException

use of liquibase.exception.PreconditionErrorException 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)

Example 4 with PreconditionErrorException

use of liquibase.exception.PreconditionErrorException in project liquibase by liquibase.

the class ViewExistsPrecondition method check.

@Override
public void check(Database database, DatabaseChangeLog changeLog, ChangeSet changeSet, ChangeExecListener changeExecListener) throws PreconditionFailedException, PreconditionErrorException {
    String currentSchemaName;
    String currentCatalogName;
    try {
        currentCatalogName = getCatalogName();
        currentSchemaName = getSchemaName();
        if (!SnapshotGeneratorFactory.getInstance().has(new View().setName(database.correctObjectName(getViewName(), View.class)).setSchema(new Schema(currentCatalogName, currentSchemaName)), database)) {
            throw new PreconditionFailedException("View " + database.escapeTableName(currentCatalogName, currentSchemaName, getViewName()) + " does not exist", changeLog, this);
        }
    } catch (PreconditionFailedException e) {
        throw e;
    } catch (Exception e) {
        throw new PreconditionErrorException(e, changeLog, this);
    }
}
Also used : Schema(liquibase.structure.core.Schema) PreconditionFailedException(liquibase.exception.PreconditionFailedException) View(liquibase.structure.core.View) PreconditionFailedException(liquibase.exception.PreconditionFailedException) PreconditionErrorException(liquibase.exception.PreconditionErrorException) PreconditionErrorException(liquibase.exception.PreconditionErrorException)

Example 5 with PreconditionErrorException

use of liquibase.exception.PreconditionErrorException in project liquibase by liquibase.

the class PrimaryKeyExistsPrecondition method check.

@Override
public void check(Database database, DatabaseChangeLog changeLog, ChangeSet changeSet, ChangeExecListener changeExecListener) throws PreconditionFailedException, PreconditionErrorException {
    try {
        PrimaryKey example = new PrimaryKey();
        Table table = new Table();
        table.setSchema(new Schema(getCatalogName(), getSchemaName()));
        if (StringUtil.trimToNull(getTableName()) != null) {
            table.setName(getTableName());
        }
        example.setTable(table);
        example.setName(getPrimaryKeyName());
        if (!SnapshotGeneratorFactory.getInstance().has(example, database)) {
            if (tableName != null) {
                throw new PreconditionFailedException("Primary Key does not exist on " + database.escapeObjectName(getTableName(), Table.class), changeLog, this);
            } else {
                throw new PreconditionFailedException("Primary Key " + database.escapeObjectName(getPrimaryKeyName(), PrimaryKey.class) + " does not exist", changeLog, this);
            }
        }
    } catch (PreconditionFailedException e) {
        throw e;
    } catch (Exception e) {
        throw new PreconditionErrorException(e, changeLog, this);
    }
}
Also used : Table(liquibase.structure.core.Table) Schema(liquibase.structure.core.Schema) PrimaryKey(liquibase.structure.core.PrimaryKey) PreconditionFailedException(liquibase.exception.PreconditionFailedException) PreconditionFailedException(liquibase.exception.PreconditionFailedException) PreconditionErrorException(liquibase.exception.PreconditionErrorException) PreconditionErrorException(liquibase.exception.PreconditionErrorException)

Aggregations

PreconditionErrorException (liquibase.exception.PreconditionErrorException)10 PreconditionFailedException (liquibase.exception.PreconditionFailedException)10 Schema (liquibase.structure.core.Schema)4 RanChangeSet (liquibase.changelog.RanChangeSet)3 Table (liquibase.structure.core.Table)3 ChangeSet (liquibase.changelog.ChangeSet)2 ObjectQuotingStrategy (liquibase.database.ObjectQuotingStrategy)2 TableRowCountStatement (liquibase.statement.core.TableRowCountStatement)2 ArrayList (java.util.ArrayList)1 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)1 Executor (liquibase.executor.Executor)1 ExecutorService (liquibase.executor.ExecutorService)1 ErrorPrecondition (liquibase.precondition.ErrorPrecondition)1 FailedPrecondition (liquibase.precondition.FailedPrecondition)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 Index (liquibase.structure.core.Index)1 PrimaryKey (liquibase.structure.core.PrimaryKey)1