Search in sources :

Example 6 with LiquibaseException

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

the class DatabaseRollbackTask method executeWithLiquibaseClassloader.

@Override
public void executeWithLiquibaseClassloader() throws BuildException {
    Writer writer = null;
    Liquibase liquibase = getLiquibase();
    try {
        FileResource outputFile = getOutputFile();
        if (rollbackCount != null) {
            if (outputFile != null) {
                writer = getOutputFileWriter();
                liquibase.rollback(rollbackCount, rollbackScript, new Contexts(getContexts()), getLabels(), writer);
            } else {
                liquibase.rollback(rollbackCount, rollbackScript, new Contexts(getContexts()), getLabels());
            }
        } else if (rollbackTag != null) {
            if (outputFile != null) {
                writer = getOutputFileWriter();
                liquibase.rollback(rollbackTag, rollbackScript, new Contexts(getContexts()), getLabels(), writer);
            } else {
                liquibase.rollback(rollbackTag, rollbackScript, new Contexts(getContexts()), getLabels());
            }
        } else if (rollbackDate != null) {
            if (outputFile != null) {
                writer = getOutputFileWriter();
                liquibase.rollback(rollbackDate, rollbackScript, new Contexts(getContexts()), getLabels(), writer);
            } else {
                liquibase.rollback(rollbackDate, rollbackScript, new Contexts(getContexts()), getLabels());
            }
        } else {
            throw new BuildException("Unable to rollback database. No count, tag, or date set.");
        }
    } catch (LiquibaseException e) {
        throw new BuildException("Unable to rollback database. " + e.toString(), e);
    } catch (UnsupportedEncodingException e) {
        throw new BuildException("Unable to generate rollback SQL. Encoding [" + getOutputEncoding() + "] is not supported.", e);
    } catch (IOException e) {
        throw new BuildException("Unable to generate rollback SQL. Error creating output writer.", e);
    } finally {
        FileUtils.close(writer);
    }
}
Also used : Liquibase(liquibase.Liquibase) FileResource(org.apache.tools.ant.types.resources.FileResource) UnsupportedEncodingException(java.io.UnsupportedEncodingException) BuildException(org.apache.tools.ant.BuildException) LiquibaseException(liquibase.exception.LiquibaseException) IOException(java.io.IOException) Contexts(liquibase.Contexts) Writer(java.io.Writer)

Example 7 with LiquibaseException

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

the class DatabaseUpdateTask method executeWithLiquibaseClassloader.

@Override
public void executeWithLiquibaseClassloader() throws BuildException {
    Writer writer = null;
    Liquibase liquibase = getLiquibase();
    try {
        FileResource outputFile = getOutputFile();
        if (outputFile != null) {
            writer = getOutputFileWriter();
            liquibase.update(toTag, new Contexts(getContexts()), getLabels(), writer);
        } else {
            if (dropFirst) {
                liquibase.dropAll();
            }
            liquibase.update(toTag, new Contexts(getContexts()), getLabels());
        }
    } catch (LiquibaseException e) {
        throw new BuildException("Unable to update database. " + e.toString(), e);
    } catch (UnsupportedEncodingException e) {
        throw new BuildException("Unable to generate update SQL. Encoding [" + getOutputEncoding() + "] is not supported.", e);
    } catch (IOException e) {
        throw new BuildException("Unable to generate update SQL. Error creating output writer.", e);
    } finally {
        FileUtils.close(writer);
    }
}
Also used : Liquibase(liquibase.Liquibase) FileResource(org.apache.tools.ant.types.resources.FileResource) UnsupportedEncodingException(java.io.UnsupportedEncodingException) LiquibaseException(liquibase.exception.LiquibaseException) BuildException(org.apache.tools.ant.BuildException) IOException(java.io.IOException) Contexts(liquibase.Contexts) Writer(java.io.Writer)

Example 8 with LiquibaseException

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

the class Liquibase method futureRollbackSQL.

protected void futureRollbackSQL(Integer count, String tag, Contexts contexts, LabelExpression labelExpression, Writer output, boolean checkLiquibaseTables) throws LiquibaseException {
    changeLogParameters.setContexts(contexts);
    changeLogParameters.setLabels(labelExpression);
    LoggingExecutor outputTemplate = new LoggingExecutor(ExecutorService.getInstance().getExecutor(database), output, database);
    Executor oldTemplate = ExecutorService.getInstance().getExecutor(database);
    ExecutorService.getInstance().setExecutor(database, outputTemplate);
    outputHeader("SQL to roll back currently unexecuted changes");
    LockService lockService = LockServiceFactory.getInstance().getLockService(database);
    lockService.waitForLock();
    try {
        DatabaseChangeLog changeLog = getDatabaseChangeLog();
        if (checkLiquibaseTables) {
            checkLiquibaseTables(false, changeLog, contexts, labelExpression);
        }
        ChangeLogHistoryServiceFactory.getInstance().getChangeLogService(database).generateDeploymentId();
        changeLog.validate(database, contexts, labelExpression);
        ChangeLogIterator logIterator;
        if (count == null && tag == null) {
            logIterator = new ChangeLogIterator(changeLog, new NotRanChangeSetFilter(database.getRanChangeSetList()), new ContextChangeSetFilter(contexts), new LabelChangeSetFilter(labelExpression), new DbmsChangeSetFilter(database));
        } else if (count != null) {
            ChangeLogIterator forwardIterator = new ChangeLogIterator(changeLog, new NotRanChangeSetFilter(database.getRanChangeSetList()), new ContextChangeSetFilter(contexts), new LabelChangeSetFilter(labelExpression), new DbmsChangeSetFilter(database), new CountChangeSetFilter(count));
            final ListVisitor listVisitor = new ListVisitor();
            forwardIterator.run(listVisitor, new RuntimeEnvironment(database, contexts, labelExpression));
            logIterator = new ChangeLogIterator(changeLog, new NotRanChangeSetFilter(database.getRanChangeSetList()), new ContextChangeSetFilter(contexts), new LabelChangeSetFilter(labelExpression), new DbmsChangeSetFilter(database), new ChangeSetFilter() {

                @Override
                public ChangeSetFilterResult accepts(ChangeSet changeSet) {
                    return new ChangeSetFilterResult(listVisitor.getSeenChangeSets().contains(changeSet), null, null);
                }
            });
        } else {
            List<RanChangeSet> ranChangeSetList = database.getRanChangeSetList();
            ChangeLogIterator forwardIterator = new ChangeLogIterator(changeLog, new NotRanChangeSetFilter(ranChangeSetList), new ContextChangeSetFilter(contexts), new LabelChangeSetFilter(labelExpression), new DbmsChangeSetFilter(database), new UpToTagChangeSetFilter(tag, ranChangeSetList));
            final ListVisitor listVisitor = new ListVisitor();
            forwardIterator.run(listVisitor, new RuntimeEnvironment(database, contexts, labelExpression));
            logIterator = new ChangeLogIterator(changeLog, new NotRanChangeSetFilter(ranChangeSetList), new ContextChangeSetFilter(contexts), new LabelChangeSetFilter(labelExpression), new DbmsChangeSetFilter(database), new ChangeSetFilter() {

                @Override
                public ChangeSetFilterResult accepts(ChangeSet changeSet) {
                    return new ChangeSetFilterResult(listVisitor.getSeenChangeSets().contains(changeSet), null, null);
                }
            });
        }
        logIterator.run(new RollbackVisitor(database, changeExecListener), new RuntimeEnvironment(database, contexts, labelExpression));
    } finally {
        try {
            lockService.releaseLock();
        } catch (LockException e) {
            log.severe("Could not release lock", e);
        }
        ExecutorService.getInstance().setExecutor(database, oldTemplate);
        resetServices();
    }
    try {
        output.flush();
    } catch (IOException e) {
        throw new LiquibaseException(e);
    }
}
Also used : LockService(liquibase.lockservice.LockService) Executor(liquibase.executor.Executor) LoggingExecutor(liquibase.executor.LoggingExecutor) LockException(liquibase.exception.LockException) LoggingExecutor(liquibase.executor.LoggingExecutor) List(java.util.List) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) LiquibaseException(liquibase.exception.LiquibaseException)

Example 9 with LiquibaseException

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

the class Liquibase method reportStatus.

public void reportStatus(boolean verbose, Contexts contexts, LabelExpression labels, Writer out) throws LiquibaseException {
    changeLogParameters.setContexts(contexts);
    changeLogParameters.setLabels(labels);
    try {
        List<ChangeSet> unrunChangeSets = listUnrunChangeSets(contexts, labels, false);
        if (unrunChangeSets.size() == 0) {
            out.append(getDatabase().getConnection().getConnectionUserName());
            out.append("@");
            out.append(getDatabase().getConnection().getURL());
            out.append(" is up to date");
            out.append(StreamUtil.getLineSeparator());
        } else {
            out.append(String.valueOf(unrunChangeSets.size()));
            out.append(" change sets have not been applied to ");
            out.append(getDatabase().getConnection().getConnectionUserName());
            out.append("@");
            out.append(getDatabase().getConnection().getURL());
            out.append(StreamUtil.getLineSeparator());
            if (verbose) {
                for (ChangeSet changeSet : unrunChangeSets) {
                    out.append("     ").append(changeSet.toString(false)).append(StreamUtil.getLineSeparator());
                }
            }
        }
        out.flush();
    } catch (IOException e) {
        throw new LiquibaseException(e);
    }
}
Also used : UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) LiquibaseException(liquibase.exception.LiquibaseException)

Example 10 with LiquibaseException

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

the class Liquibase method update.

public void update(String tag, Contexts contexts, LabelExpression labelExpression, Writer output) throws LiquibaseException {
    if (tag == null) {
        update(contexts, labelExpression, output);
        return;
    }
    changeLogParameters.setContexts(contexts);
    changeLogParameters.setLabels(labelExpression);
    Executor oldTemplate = ExecutorService.getInstance().getExecutor(database);
    LoggingExecutor loggingExecutor = new LoggingExecutor(ExecutorService.getInstance().getExecutor(database), output, database);
    ExecutorService.getInstance().setExecutor(database, loggingExecutor);
    outputHeader("Update to '" + tag + "' Database Script");
    update(tag, contexts, labelExpression);
    try {
        output.flush();
    } catch (IOException e) {
        throw new LiquibaseException(e);
    }
    resetServices();
    ExecutorService.getInstance().setExecutor(database, oldTemplate);
}
Also used : Executor(liquibase.executor.Executor) LoggingExecutor(liquibase.executor.LoggingExecutor) LoggingExecutor(liquibase.executor.LoggingExecutor) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) LiquibaseException(liquibase.exception.LiquibaseException)

Aggregations

LiquibaseException (liquibase.exception.LiquibaseException)38 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)23 Executor (liquibase.executor.Executor)12 Liquibase (liquibase.Liquibase)10 LoggingExecutor (liquibase.executor.LoggingExecutor)10 BuildException (org.apache.tools.ant.BuildException)8 IOException (java.io.IOException)7 Database (liquibase.database.Database)7 Contexts (liquibase.Contexts)6 DatabaseException (liquibase.exception.DatabaseException)5 ArrayList (java.util.ArrayList)4 ResourceAccessor (liquibase.resource.ResourceAccessor)4 FileResource (org.apache.tools.ant.types.resources.FileResource)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 Writer (java.io.Writer)3 CatalogAndSchema (liquibase.CatalogAndSchema)3 OracleDatabase (liquibase.database.core.OracleDatabase)3 DiffOutputControl (liquibase.diff.output.DiffOutputControl)3 Test (org.junit.Test)3 File (java.io.File)2