Search in sources :

Example 6 with PreconditionErrorException

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

the class RowCountPrecondition method check.

@Override
public void check(Database database, DatabaseChangeLog changeLog, ChangeSet changeSet) throws PreconditionFailedException, PreconditionErrorException {
    try {
        TableRowCountStatement statement = new TableRowCountStatement(catalogName, schemaName, tableName);
        int result = ExecutorService.getInstance().getExecutor(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 7 with PreconditionErrorException

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

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

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

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

the class ValidatingVisitorPreConditionsTest method testPreConditionsForOracleOnMSSQLWithPreconditionTag.

/**
 * Test only the precondition tag with a precondition requiring oracle but
 * giving a MSSQL database.
 */
@Test
public void testPreConditionsForOracleOnMSSQLWithPreconditionTag() {
    // create the pre condition
    PreconditionContainer preCondition = new PreconditionContainer();
    preCondition.setOnFail(PreconditionContainer.FailOption.MARK_RAN.toString());
    DBMSPrecondition dbmsPrecondition = new DBMSPrecondition();
    dbmsPrecondition.setType("oracle");
    preCondition.addNestedPrecondition(dbmsPrecondition);
    changeSet1.setPreconditions(preCondition);
    MSSQLDatabase mssqlDb = new MSSQLDatabase() {

        @Override
        public List<RanChangeSet> getRanChangeSetList() throws DatabaseException {
            return new ArrayList<RanChangeSet>();
        }

        @Override
        public void rollback() throws DatabaseException {
        // super.rollback();
        }
    };
    boolean failedExceptionThrown = false;
    boolean errorExceptionThrown = false;
    try {
        preCondition.check(mssqlDb, changeLog, changeSet1, null);
    } catch (PreconditionFailedException ex) {
        failedExceptionThrown = true;
    } catch (PreconditionErrorException ex) {
        errorExceptionThrown = true;
    }
    assertTrue(failedExceptionThrown);
    assertFalse(errorExceptionThrown);
}
Also used : PreconditionContainer(liquibase.precondition.core.PreconditionContainer) DBMSPrecondition(liquibase.precondition.core.DBMSPrecondition) ArrayList(java.util.ArrayList) PreconditionFailedException(liquibase.exception.PreconditionFailedException) MSSQLDatabase(liquibase.database.core.MSSQLDatabase) RanChangeSet(liquibase.changelog.RanChangeSet) PreconditionErrorException(liquibase.exception.PreconditionErrorException) Test(org.junit.Test)

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