Search in sources :

Example 1 with MigrationFailedException

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

the class UpdateVisitor method visit.

@Override
public void visit(ChangeSet changeSet, DatabaseChangeLog databaseChangeLog, Database database, Set<ChangeSetFilterResult> filterResults) throws LiquibaseException {
    Executor executor = Scope.getCurrentScope().getSingleton(ExecutorService.class).getExecutor("jdbc", database);
    if (!(executor instanceof LoggingExecutor)) {
        Scope.getCurrentScope().getUI().sendMessage("Running Changeset: " + changeSet);
    }
    ChangeSet.RunStatus runStatus = this.database.getRunStatus(changeSet);
    Scope.getCurrentScope().getLog(getClass()).fine("Running Changeset: " + changeSet);
    fireWillRun(changeSet, databaseChangeLog, database, runStatus);
    ExecType execType = null;
    ObjectQuotingStrategy previousStr = this.database.getObjectQuotingStrategy();
    try {
        execType = changeSet.execute(databaseChangeLog, execListener, this.database);
    } catch (MigrationFailedException e) {
        fireRunFailed(changeSet, databaseChangeLog, database, e);
        throw e;
    }
    if (!runStatus.equals(ChangeSet.RunStatus.NOT_RAN)) {
        execType = ChangeSet.ExecType.RERAN;
    }
    fireRan(changeSet, databaseChangeLog, database, execType);
    // reset object quoting strategy after running changeset
    this.database.setObjectQuotingStrategy(previousStr);
    this.database.markChangeSetExecStatus(changeSet, execType);
    this.database.commit();
}
Also used : LoggingExecutor(liquibase.executor.LoggingExecutor) Executor(liquibase.executor.Executor) MigrationFailedException(liquibase.exception.MigrationFailedException) LoggingExecutor(liquibase.executor.LoggingExecutor) ExecutorService(liquibase.executor.ExecutorService) RunStatus(liquibase.changelog.ChangeSet.RunStatus) ExecType(liquibase.changelog.ChangeSet.ExecType) ChangeSet(liquibase.changelog.ChangeSet) ObjectQuotingStrategy(liquibase.database.ObjectQuotingStrategy)

Example 2 with MigrationFailedException

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

the class PendingSQLWriter method writeBody.

@Override
protected void writeBody(Writer fileWriter, Object object, List<Change> ranChanges, List<Change> changesToRun) throws IOException, DatabaseHistoryException, DatabaseException {
    Executor oldTemplate = Scope.getCurrentScope().getSingleton(ExecutorService.class).getExecutor("jdbc", database);
    LoggingExecutor loggingExecutor = new LoggingExecutor(oldTemplate, fileWriter, database);
    Scope.getCurrentScope().getSingleton(ExecutorService.class).setExecutor("logging", database, loggingExecutor);
    Scope.getCurrentScope().getSingleton(ExecutorService.class).setExecutor("jdbc", database, loggingExecutor);
    try {
        if (changesToRun.isEmpty()) {
            fileWriter.append("<b>NONE</b>");
        }
        fileWriter.append("<code><pre>");
        ChangeSet lastRunChangeSet = null;
        for (Change change : changesToRun) {
            ChangeSet thisChangeSet = change.getChangeSet();
            if (thisChangeSet.equals(lastRunChangeSet)) {
                continue;
            }
            lastRunChangeSet = thisChangeSet;
            String anchor = thisChangeSet.toString(false).replaceAll("\\W", "_");
            fileWriter.append("<a name='").append(anchor).append("'/>");
            try {
                thisChangeSet.execute(databaseChangeLog, null, this.database);
            } catch (MigrationFailedException e) {
                fileWriter.append("EXECUTION ERROR: ").append(Scope.getCurrentScope().getSingleton(ChangeFactory.class).getChangeMetaData(change).getDescription()).append(": ").append(e.getMessage()).append("\n\n");
            }
        }
        fileWriter.append("</pre></code>");
    } finally {
        Scope.getCurrentScope().getSingleton(ExecutorService.class).setExecutor("jdbc", database, oldTemplate);
    }
}
Also used : LoggingExecutor(liquibase.executor.LoggingExecutor) Executor(liquibase.executor.Executor) MigrationFailedException(liquibase.exception.MigrationFailedException) ChangeFactory(liquibase.change.ChangeFactory) LoggingExecutor(liquibase.executor.LoggingExecutor) ExecutorService(liquibase.executor.ExecutorService) Change(liquibase.change.Change) ChangeSet(liquibase.changelog.ChangeSet)

Example 3 with MigrationFailedException

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

the class DatabaseTestTemplate method test.

private void test(DatabaseTest test, Set<Database> databasesToTestOn) throws Exception {
    for (Database database : databasesToTestOn) {
        if (database instanceof SQLiteDatabase) {
            //todo: find how to get tests to run correctly on SQLite
            continue;
        }
        JdbcExecutor writeExecutor = new JdbcExecutor();
        writeExecutor.setDatabase(database);
        ExecutorService.getInstance().setExecutor(database, writeExecutor);
        LockService lockService = LockServiceFactory.getInstance().getLockService(database);
        lockService.reset();
        if (database.getConnection() != null) {
            lockService.forceReleaseLock();
        }
        try {
            test.performTest(database);
        } catch (ComparisonFailure e) {
            String newMessage = "Database Test Failure on " + database;
            if (e.getMessage() != null) {
                newMessage += ": " + e.getMessage();
            }
            ComparisonFailure newError = new ComparisonFailure(newMessage, e.getExpected(), e.getActual());
            newError.setStackTrace(e.getStackTrace());
            throw newError;
        } catch (AssertionError e) {
            e.printStackTrace();
            String newMessage = "Database Test Failure on " + database;
            if (e.getMessage() != null) {
                newMessage += ": " + e.getMessage();
            }
            AssertionError newError = new AssertionError(newMessage);
            newError.setStackTrace(e.getStackTrace());
            throw newError;
        } catch (MigrationFailedException e) {
            e.printStackTrace();
            String newMessage = "Database Test Failure on " + database;
            if (e.getMessage() != null) {
                newMessage += ": " + e.getMessage();
            }
            AssertionError newError = new AssertionError(newMessage);
            newError.setStackTrace(e.getStackTrace());
            throw newError;
        } catch (Exception e) {
            e.printStackTrace();
            String newMessage = "Database Test Exception on " + database;
            if (e.getMessage() != null) {
                newMessage += ": " + e.getMessage();
            }
            Exception newError = e.getClass().getConstructor(String.class).newInstance(newMessage);
            if (e.getCause() == null) {
                newError.setStackTrace(e.getStackTrace());
            } else {
                newError.setStackTrace(e.getCause().getStackTrace());
            }
            throw newError;
        } finally {
            if (database.getConnection() != null && !database.getAutoCommitMode()) {
                database.rollback();
            }
        }
    }
}
Also used : LockService(liquibase.lockservice.LockService) MigrationFailedException(liquibase.exception.MigrationFailedException) SQLiteDatabase(liquibase.database.core.SQLiteDatabase) ComparisonFailure(org.junit.ComparisonFailure) SQLiteDatabase(liquibase.database.core.SQLiteDatabase) Database(liquibase.database.Database) JdbcExecutor(liquibase.executor.jvm.JdbcExecutor) MigrationFailedException(liquibase.exception.MigrationFailedException)

Aggregations

MigrationFailedException (liquibase.exception.MigrationFailedException)3 ChangeSet (liquibase.changelog.ChangeSet)2 Executor (liquibase.executor.Executor)2 ExecutorService (liquibase.executor.ExecutorService)2 LoggingExecutor (liquibase.executor.LoggingExecutor)2 Change (liquibase.change.Change)1 ChangeFactory (liquibase.change.ChangeFactory)1 ExecType (liquibase.changelog.ChangeSet.ExecType)1 RunStatus (liquibase.changelog.ChangeSet.RunStatus)1 Database (liquibase.database.Database)1 ObjectQuotingStrategy (liquibase.database.ObjectQuotingStrategy)1 SQLiteDatabase (liquibase.database.core.SQLiteDatabase)1 JdbcExecutor (liquibase.executor.jvm.JdbcExecutor)1 LockService (liquibase.lockservice.LockService)1 ComparisonFailure (org.junit.ComparisonFailure)1