Search in sources :

Example 11 with LiquibaseException

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

the class CDILiquibase method performUpdate.

private void performUpdate() throws LiquibaseException {
    Connection c = null;
    Liquibase liquibase = null;
    try {
        c = dataSource.getConnection();
        liquibase = createLiquibase(c);
        liquibase.getDatabase();
        liquibase.update(new Contexts(config.getContexts()), new LabelExpression(config.getLabels()));
        updateSuccessful = true;
    } catch (SQLException e) {
        throw new DatabaseException(e);
    } catch (LiquibaseException ex) {
        updateSuccessful = false;
        throw ex;
    } finally {
        if (liquibase != null && liquibase.getDatabase() != null) {
            liquibase.getDatabase().close();
        } else if (c != null) {
            try {
                c.rollback();
                c.close();
            } catch (SQLException e) {
            //nothing to do
            }
        }
    }
}
Also used : Liquibase(liquibase.Liquibase) SQLException(java.sql.SQLException) Connection(java.sql.Connection) JdbcConnection(liquibase.database.jvm.JdbcConnection) LabelExpression(liquibase.LabelExpression) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) LiquibaseException(liquibase.exception.LiquibaseException) Contexts(liquibase.Contexts) DatabaseException(liquibase.exception.DatabaseException)

Example 12 with LiquibaseException

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

the class Liquibase method markNextChangeSetRan.

public void markNextChangeSetRan(Contexts contexts, LabelExpression labelExpression, Writer output) 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 add all changesets to database history table");
    markNextChangeSetRan(contexts, labelExpression);
    try {
        output.flush();
    } catch (IOException e) {
        throw new LiquibaseException(e);
    }
    ExecutorService.getInstance().setExecutor(database, oldTemplate);
    resetServices();
}
Also used : Executor(liquibase.executor.Executor) LoggingExecutor(liquibase.executor.LoggingExecutor) LoggingExecutor(liquibase.executor.LoggingExecutor) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) LiquibaseException(liquibase.exception.LiquibaseException)

Example 13 with LiquibaseException

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

the class Liquibase method generateDocumentation.

public void generateDocumentation(String outputDirectory, Contexts contexts, LabelExpression labelExpression) throws LiquibaseException {
    log.info("Generating Database Documentation");
    changeLogParameters.setContexts(contexts);
    changeLogParameters.setLabels(labelExpression);
    LockService lockService = LockServiceFactory.getInstance().getLockService(database);
    lockService.waitForLock();
    try {
        DatabaseChangeLog changeLog = getDatabaseChangeLog();
        checkLiquibaseTables(false, changeLog, new Contexts(), new LabelExpression());
        changeLog.validate(database, contexts, labelExpression);
        ChangeLogIterator logIterator = new ChangeLogIterator(changeLog, new DbmsChangeSetFilter(database));
        DBDocVisitor visitor = new DBDocVisitor(database);
        logIterator.run(visitor, new RuntimeEnvironment(database, contexts, labelExpression));
        visitor.writeHTML(new File(outputDirectory), resourceAccessor);
    } catch (IOException e) {
        throw new LiquibaseException(e);
    } finally {
        try {
            lockService.releaseLock();
        } catch (LockException e) {
            log.severe("Could not release lock", e);
        }
    }
//        try {
//            if (!LockService.getExecutor(database).waitForLock()) {
//                return;
//            }
//
//            DBDocChangeLogHandler changeLogHandler = new DBDocChangeLogHandler(outputDirectory, this, changeLogFile,resourceAccessor);
//            runChangeLogs(changeLogHandler);
//
//            changeLogHandler.writeHTML(this);
//        } finally {
//            releaseLock();
//        }
}
Also used : LockService(liquibase.lockservice.LockService) LockException(liquibase.exception.LockException) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) LiquibaseException(liquibase.exception.LiquibaseException)

Example 14 with LiquibaseException

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

the class Liquibase method rollback.

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

Example 15 with LiquibaseException

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

the class MarkChangeSetRanGenerator method generateSql.

@Override
public Sql[] generateSql(MarkChangeSetRanStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
    String dateValue = database.getCurrentDateTimeFunction();
    ChangeSet changeSet = statement.getChangeSet();
    SqlStatement runStatement;
    try {
        if (statement.getExecType().equals(ChangeSet.ExecType.FAILED) || statement.getExecType().equals(ChangeSet.ExecType.SKIPPED)) {
            //don't mark
            return new Sql[0];
        }
        String tag = null;
        for (Change change : changeSet.getChanges()) {
            if (change instanceof TagDatabaseChange) {
                TagDatabaseChange tagChange = (TagDatabaseChange) change;
                tag = tagChange.getTag();
            }
        }
        if (statement.getExecType().ranBefore) {
            runStatement = new UpdateStatement(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), database.getDatabaseChangeLogTableName()).addNewColumnValue("DATEEXECUTED", new DatabaseFunction(dateValue)).addNewColumnValue("ORDEREXECUTED", ChangeLogHistoryServiceFactory.getInstance().getChangeLogService(database).getNextSequenceValue()).addNewColumnValue("MD5SUM", changeSet.generateCheckSum().toString()).addNewColumnValue("EXECTYPE", statement.getExecType().value).addNewColumnValue("DEPLOYMENT_ID", ChangeLogHistoryServiceFactory.getInstance().getChangeLogService(database).getDeploymentId()).setWhereClause(database.escapeObjectName("ID", Column.class) + " = ? " + "AND " + database.escapeObjectName("AUTHOR", Column.class) + " = ? " + "AND " + database.escapeObjectName("FILENAME", Column.class) + " = ?").addWhereParameters(changeSet.getId(), changeSet.getAuthor(), changeSet.getFilePath());
            if (tag != null) {
                ((UpdateStatement) runStatement).addNewColumnValue("TAG", tag);
            }
        } else {
            runStatement = new InsertStatement(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), database.getDatabaseChangeLogTableName()).addColumnValue("ID", changeSet.getId()).addColumnValue("AUTHOR", changeSet.getAuthor()).addColumnValue("FILENAME", changeSet.getFilePath()).addColumnValue("DATEEXECUTED", new DatabaseFunction(dateValue)).addColumnValue("ORDEREXECUTED", ChangeLogHistoryServiceFactory.getInstance().getChangeLogService(database).getNextSequenceValue()).addColumnValue("MD5SUM", changeSet.generateCheckSum().toString()).addColumnValue("DESCRIPTION", limitSize(changeSet.getDescription())).addColumnValue("COMMENTS", limitSize(StringUtils.trimToEmpty(changeSet.getComments()))).addColumnValue("EXECTYPE", statement.getExecType().value).addColumnValue("CONTEXTS", changeSet.getContexts() == null || changeSet.getContexts().isEmpty() ? null : changeSet.getContexts().toString()).addColumnValue("LABELS", changeSet.getLabels() == null || changeSet.getLabels().isEmpty() ? null : changeSet.getLabels().toString()).addColumnValue("LIQUIBASE", LiquibaseUtil.getBuildVersion().replaceAll("SNAPSHOT", "SNP")).addColumnValue("DEPLOYMENT_ID", ChangeLogHistoryServiceFactory.getInstance().getChangeLogService(database).getDeploymentId());
            if (tag != null) {
                ((InsertStatement) runStatement).addColumnValue("TAG", tag);
            }
        }
    } catch (LiquibaseException e) {
        throw new UnexpectedLiquibaseException(e);
    }
    return SqlGeneratorFactory.getInstance().generateSql(runStatement, database);
}
Also used : SqlStatement(liquibase.statement.SqlStatement) UpdateStatement(liquibase.statement.core.UpdateStatement) DatabaseFunction(liquibase.statement.DatabaseFunction) Column(liquibase.structure.core.Column) TagDatabaseChange(liquibase.change.core.TagDatabaseChange) Change(liquibase.change.Change) TagDatabaseChange(liquibase.change.core.TagDatabaseChange) LiquibaseException(liquibase.exception.LiquibaseException) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) ChangeSet(liquibase.changelog.ChangeSet) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) InsertStatement(liquibase.statement.core.InsertStatement) Sql(liquibase.sql.Sql)

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