Search in sources :

Example 1 with LoggingExecutor

use of liquibase.executor.LoggingExecutor 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)

Example 2 with LoggingExecutor

use of liquibase.executor.LoggingExecutor 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 3 with LoggingExecutor

use of liquibase.executor.LoggingExecutor 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);
    runInScope(new Scope.ScopedRunner() {

        @Override
        public void run() throws Exception {
            LoggingExecutor outputTemplate = new LoggingExecutor(Scope.getCurrentScope().getSingleton(ExecutorService.class).getExecutor(database), output, database);
            Executor oldTemplate = getAndReplaceJdbcExecutor(output);
            Scope.getCurrentScope().getSingleton(ExecutorService.class).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 IgnoreChangeSetFilter(), 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 IgnoreChangeSetFilter(), 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 IgnoreChangeSetFilter(), new ChangeSetFilter() {

                        @Override
                        public ChangeSetFilterResult accepts(ChangeSet changeSet) {
                            return new ChangeSetFilterResult(listVisitor.getSeenChangeSets().contains(changeSet), null, null);
                        }
                    });
                } else {
                    List<RanChangeSet> ranChangeSetList = database.getRanChangeSetList();
                    UpToTagChangeSetFilter upToTagChangeSetFilter = new UpToTagChangeSetFilter(tag, ranChangeSetList);
                    ChangeLogIterator forwardIterator = new ChangeLogIterator(changeLog, new NotRanChangeSetFilter(ranChangeSetList), new ContextChangeSetFilter(contexts), new LabelChangeSetFilter(labelExpression), new DbmsChangeSetFilter(database), new IgnoreChangeSetFilter(), upToTagChangeSetFilter);
                    final ListVisitor listVisitor = new ListVisitor();
                    forwardIterator.run(listVisitor, new RuntimeEnvironment(database, contexts, labelExpression));
                    // 
                    if (!upToTagChangeSetFilter.isSeenTag()) {
                        String message = "No tag matching '" + tag + "' found";
                        Scope.getCurrentScope().getUI().sendMessage("ERROR: " + message);
                        Scope.getCurrentScope().getLog(Liquibase.class).severe(message);
                        throw new LiquibaseException(new IllegalArgumentException(message));
                    }
                    logIterator = new ChangeLogIterator(changeLog, new NotRanChangeSetFilter(ranChangeSetList), new ContextChangeSetFilter(contexts), new LabelChangeSetFilter(labelExpression), new DbmsChangeSetFilter(database), new IgnoreChangeSetFilter(), new ChangeSetFilter() {

                        @Override
                        public ChangeSetFilterResult accepts(ChangeSet changeSet) {
                            return new ChangeSetFilterResult(listVisitor.getSeenChangeSets().contains(changeSet), null, null);
                        }
                    });
                }
                logIterator.run(createRollbackVisitor(), new RuntimeEnvironment(database, contexts, labelExpression));
            } finally {
                try {
                    lockService.releaseLock();
                } catch (LockException e) {
                    LOG.severe(MSG_COULD_NOT_RELEASE_LOCK, e);
                }
                Scope.getCurrentScope().getSingleton(ExecutorService.class).setExecutor("jdbc", database, oldTemplate);
                resetServices();
            }
            flushOutputWriter(output);
        }
    });
}
Also used : Executor(liquibase.executor.Executor) LoggingExecutor(liquibase.executor.LoggingExecutor) LockService(liquibase.lockservice.LockService) InvalidExampleException(liquibase.snapshot.InvalidExampleException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) CommandScope(liquibase.command.CommandScope) LoggingExecutor(liquibase.executor.LoggingExecutor) ExecutorService(liquibase.executor.ExecutorService)

Example 4 with LoggingExecutor

use of liquibase.executor.LoggingExecutor in project liquibase by liquibase.

the class Liquibase method getAndReplaceJdbcExecutor.

private Executor getAndReplaceJdbcExecutor(Writer output) {
    /* We have no other choice than to save the current Executor here. */
    @SuppressWarnings("squid:S1941") Executor oldTemplate = Scope.getCurrentScope().getSingleton(ExecutorService.class).getExecutor("jdbc", database);
    final LoggingExecutor loggingExecutor = new LoggingExecutor(oldTemplate, output, database);
    Scope.getCurrentScope().getSingleton(ExecutorService.class).setExecutor("logging", database, loggingExecutor);
    Scope.getCurrentScope().getSingleton(ExecutorService.class).setExecutor("jdbc", database, loggingExecutor);
    return oldTemplate;
}
Also used : Executor(liquibase.executor.Executor) LoggingExecutor(liquibase.executor.LoggingExecutor) LoggingExecutor(liquibase.executor.LoggingExecutor) ExecutorService(liquibase.executor.ExecutorService)

Example 5 with LoggingExecutor

use of liquibase.executor.LoggingExecutor in project liquibase by liquibase.

the class ExecuteShellCommandChange method generateStatements.

@Override
public SqlStatement[] generateStatements(final Database database) {
    boolean shouldRun = true;
    if ((os != null) && (!os.isEmpty())) {
        String currentOS = System.getProperty("os.name");
        if (!os.contains(currentOS)) {
            shouldRun = false;
            Scope.getCurrentScope().getLog(getClass()).info("Not executing on os " + currentOS + " when " + os + " was " + "specified");
        }
    }
    // check if running under not-executed mode (logging output)
    boolean nonExecutedMode = false;
    Executor executor = Scope.getCurrentScope().getSingleton(ExecutorService.class).getExecutor("jdbc", database);
    if (executor instanceof LoggingExecutor) {
        nonExecutedMode = true;
    }
    this.finalCommandArray = createFinalCommandArray(database);
    if (shouldRun && !nonExecutedMode) {
        return new SqlStatement[] { new RuntimeStatement() {

            @Override
            public Sql[] generate(Database database) {
                try {
                    executeCommand(database);
                } catch (Exception e) {
                    throw new UnexpectedLiquibaseException("Error executing command: " + e.getLocalizedMessage(), e);
                }
                return null;
            }
        } };
    }
    if (nonExecutedMode) {
        try {
            return new SqlStatement[] { new CommentStatement(getCommandString()) };
        } finally {
            nonExecutedCleanup();
        }
    }
    return new SqlStatement[0];
}
Also used : SqlStatement(liquibase.statement.SqlStatement) Executor(liquibase.executor.Executor) LoggingExecutor(liquibase.executor.LoggingExecutor) CommentStatement(liquibase.statement.core.CommentStatement) LoggingExecutor(liquibase.executor.LoggingExecutor) ExecutorService(liquibase.executor.ExecutorService) Database(liquibase.database.Database) RuntimeStatement(liquibase.statement.core.RuntimeStatement) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) ParsedNodeException(liquibase.parser.core.ParsedNodeException) TimeoutException(java.util.concurrent.TimeoutException) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) Sql(liquibase.sql.Sql)

Aggregations

LoggingExecutor (liquibase.executor.LoggingExecutor)20 Executor (liquibase.executor.Executor)19 ExecutorService (liquibase.executor.ExecutorService)14 LiquibaseException (liquibase.exception.LiquibaseException)11 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)8 MigrationFailedException (liquibase.exception.MigrationFailedException)3 ChangeSet (liquibase.changelog.ChangeSet)2 LockService (liquibase.lockservice.LockService)2 InvalidExampleException (liquibase.snapshot.InvalidExampleException)2 File (java.io.File)1 IOException (java.io.IOException)1 SQLException (java.sql.SQLException)1 Date (java.util.Date)1 TimeoutException (java.util.concurrent.TimeoutException)1 Level (java.util.logging.Level)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 Change (liquibase.change.Change)1 ChangeFactory (liquibase.change.ChangeFactory)1 EmptyChange (liquibase.change.core.EmptyChange)1 RawSQLChange (liquibase.change.core.RawSQLChange)1