Search in sources :

Example 11 with PreconditionFailedException

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

the class IndexExistsPrecondition method check.

@Override
public void check(Database database, DatabaseChangeLog changeLog, ChangeSet changeSet, ChangeExecListener changeExecListener) throws PreconditionFailedException, PreconditionErrorException {
    try {
        Schema schema = new Schema(getCatalogName(), getSchemaName());
        Index example = new Index();
        String tableName = StringUtil.trimToNull(getTableName());
        if (tableName != null) {
            example.setRelation((Table) new Table().setName(database.correctObjectName(getTableName(), Table.class)).setSchema(schema));
        }
        example.setName(database.correctObjectName(getIndexName(), Index.class));
        if (StringUtil.trimToNull(getColumnNames()) != null) {
            for (String column : getColumnNames().split("\\s*,\\s*")) {
                example.addColumn(new Column(database.correctObjectName(column, Column.class)));
            }
        }
        if (!SnapshotGeneratorFactory.getInstance().has(example, database)) {
            String name = "";
            if (getIndexName() != null) {
                name += database.escapeObjectName(getIndexName(), Index.class);
            }
            if (tableName != null) {
                name += " on " + database.escapeObjectName(getTableName(), Table.class);
                if (StringUtil.trimToNull(getColumnNames()) != null) {
                    name += " columns " + getColumnNames();
                }
            }
            throw new PreconditionFailedException("Index " + name + " does not exist", changeLog, this);
        }
    } catch (Exception e) {
        if (e instanceof PreconditionFailedException) {
            throw (((PreconditionFailedException) e));
        }
        throw new PreconditionErrorException(e, changeLog, this);
    }
}
Also used : Table(liquibase.structure.core.Table) Column(liquibase.structure.core.Column) Schema(liquibase.structure.core.Schema) Index(liquibase.structure.core.Index) PreconditionFailedException(liquibase.exception.PreconditionFailedException) PreconditionFailedException(liquibase.exception.PreconditionFailedException) PreconditionErrorException(liquibase.exception.PreconditionErrorException) PreconditionErrorException(liquibase.exception.PreconditionErrorException)

Example 12 with PreconditionFailedException

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

the class RowCountPrecondition method check.

@Override
public void check(Database database, DatabaseChangeLog changeLog, ChangeSet changeSet, ChangeExecListener changeExecListener) throws PreconditionFailedException, PreconditionErrorException {
    try {
        TableRowCountStatement statement = new TableRowCountStatement(catalogName, schemaName, tableName);
        int result = Scope.getCurrentScope().getSingleton(ExecutorService.class).getExecutor("jdbc", database).queryForInt(statement);
        if (result != expectedRows) {
            throw new PreconditionFailedException(getFailureMessage(result), changeLog, this);
        }
    } catch (PreconditionFailedException e) {
        throw e;
    } catch (Exception e) {
        throw new PreconditionErrorException(e, changeLog, this);
    }
}
Also used : PreconditionFailedException(liquibase.exception.PreconditionFailedException) TableRowCountStatement(liquibase.statement.core.TableRowCountStatement) PreconditionFailedException(liquibase.exception.PreconditionFailedException) PreconditionErrorException(liquibase.exception.PreconditionErrorException) PreconditionErrorException(liquibase.exception.PreconditionErrorException)

Example 13 with PreconditionFailedException

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

the class ForeignKeyExistsPrecondition method check.

@Override
public void check(Database database, DatabaseChangeLog changeLog, ChangeSet changeSet, ChangeExecListener changeExecListener) throws PreconditionFailedException, PreconditionErrorException {
    try {
        ForeignKey example = new ForeignKey();
        example.setName(getForeignKeyName());
        example.setForeignKeyTable(new Table());
        if (StringUtil.trimToNull(getForeignKeyTableName()) != null) {
            example.getForeignKeyTable().setName(getForeignKeyTableName());
        }
        example.getForeignKeyTable().setSchema(new Schema(getCatalogName(), getSchemaName()));
        if (!SnapshotGeneratorFactory.getInstance().has(example, database)) {
            throw new PreconditionFailedException("Foreign Key " + database.escapeIndexName(catalogName, schemaName, foreignKeyName) + " 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) PreconditionFailedException(liquibase.exception.PreconditionFailedException) ForeignKey(liquibase.structure.core.ForeignKey) PreconditionFailedException(liquibase.exception.PreconditionFailedException) PreconditionErrorException(liquibase.exception.PreconditionErrorException) PreconditionErrorException(liquibase.exception.PreconditionErrorException)

Example 14 with PreconditionFailedException

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

the class OrPrecondition method check.

@Override
public void check(Database database, DatabaseChangeLog changeLog, ChangeSet changeSet, ChangeExecListener changeExecListener) throws PreconditionFailedException, PreconditionErrorException {
    boolean onePassed = false;
    List<FailedPrecondition> failures = new ArrayList<>();
    for (Precondition precondition : getNestedPreconditions()) {
        try {
            precondition.check(database, changeLog, changeSet, changeExecListener);
            onePassed = true;
            break;
        } catch (PreconditionFailedException e) {
            failures.addAll(e.getFailedPreconditions());
        }
    }
    if (!onePassed) {
        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 15 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, ChangeExecListener changeExecListener) throws PreconditionFailedException, PreconditionErrorException {
    boolean allPassed = true;
    List<FailedPrecondition> failures = new ArrayList<>();
    for (Precondition precondition : getNestedPreconditions()) {
        try {
            precondition.check(database, changeLog, changeSet, changeExecListener);
        } 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)

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