Search in sources :

Example 1 with HubChangeExecListener

use of liquibase.hub.listener.HubChangeExecListener in project liquibase by liquibase.

the class Liquibase method update.

/**
 * Liquibase update
 *
 * @param   contexts
 * @param   labelExpression
 * @param   checkLiquibaseTables
 * @throws  LiquibaseException
 */
public void update(Contexts contexts, LabelExpression labelExpression, boolean checkLiquibaseTables) throws LiquibaseException {
    runInScope(() -> {
        LockService lockService = LockServiceFactory.getInstance().getLockService(database);
        lockService.waitForLock();
        changeLogParameters.setContexts(contexts);
        changeLogParameters.setLabels(labelExpression);
        Operation updateOperation = null;
        BufferedLogService bufferLog = new BufferedLogService();
        DatabaseChangeLog changeLog = null;
        HubUpdater hubUpdater = null;
        try {
            changeLog = getDatabaseChangeLog();
            if (checkLiquibaseTables) {
                checkLiquibaseTables(true, changeLog, contexts, labelExpression);
            }
            ChangeLogHistoryServiceFactory.getInstance().getChangeLogService(database).generateDeploymentId();
            changeLog.validate(database, contexts, labelExpression);
            // 
            // Let the user know that they can register for Hub
            // 
            hubUpdater = new HubUpdater(new Date(), changeLog, database);
            hubUpdater.register(changeLogFile);
            // 
            // Create or retrieve the Connection if this is not SQL generation
            // Make sure the Hub is available here by checking the return
            // We do not need a connection if we are using a LoggingExecutor
            // 
            ChangeLogIterator changeLogIterator = getStandardChangelogIterator(contexts, labelExpression, changeLog);
            Connection connection = getConnection(changeLog);
            if (connection != null) {
                updateOperation = hubUpdater.preUpdateHub("UPDATE", "update", connection, changeLogFile, contexts, labelExpression, changeLogIterator);
            }
            // 
            if (connection != null) {
                changeExecListener = new HubChangeExecListener(updateOperation, changeExecListener);
            }
            // 
            // Create another iterator to run
            // 
            ChangeLogIterator runChangeLogIterator = getStandardChangelogIterator(contexts, labelExpression, changeLog);
            CompositeLogService compositeLogService = new CompositeLogService(true, bufferLog);
            Scope.child(Scope.Attr.logService.name(), compositeLogService, () -> {
                runChangeLogIterator.run(createUpdateVisitor(), new RuntimeEnvironment(database, contexts, labelExpression));
            });
            // 
            // Update Hub with the operation information
            // 
            hubUpdater.postUpdateHub(updateOperation, bufferLog);
        } catch (Throwable e) {
            if (hubUpdater != null) {
                hubUpdater.postUpdateHubExceptionHandling(updateOperation, bufferLog, e.getMessage());
            }
            throw e;
        } finally {
            database.setObjectQuotingStrategy(ObjectQuotingStrategy.LEGACY);
            try {
                lockService.releaseLock();
            } catch (LockException e) {
                LOG.severe(MSG_COULD_NOT_RELEASE_LOCK, e);
            }
            resetServices();
            setChangeExecListener(null);
        }
    });
}
Also used : LockService(liquibase.lockservice.LockService) DatabaseConnection(liquibase.database.DatabaseConnection) Connection(liquibase.hub.model.Connection) Operation(liquibase.hub.model.Operation) BufferedLogService(liquibase.logging.core.BufferedLogService) CompositeLogService(liquibase.logging.core.CompositeLogService) HubChangeExecListener(liquibase.hub.listener.HubChangeExecListener)

Example 2 with HubChangeExecListener

use of liquibase.hub.listener.HubChangeExecListener in project liquibase by liquibase.

the class Liquibase method executeRollbackScript.

protected void executeRollbackScript(String rollbackScript, List<ChangeSet> changeSets, Contexts contexts, LabelExpression labelExpression) throws LiquibaseException {
    final Executor executor = Scope.getCurrentScope().getSingleton(ExecutorService.class).getExecutor("jdbc", database);
    String rollbackScriptContents;
    try (InputStreamList streams = resourceAccessor.openStreams(null, rollbackScript)) {
        if ((streams == null) || streams.isEmpty()) {
            throw new LiquibaseException("WARNING: The rollback script '" + rollbackScript + "' was not located.  Please check your parameters. No rollback was performed");
        } else if (streams.size() > 1) {
            throw new LiquibaseException("Found multiple rollbackScripts named " + rollbackScript);
        }
        rollbackScriptContents = StreamUtil.readStreamAsString(streams.iterator().next());
    } catch (IOException e) {
        throw new LiquibaseException("Error reading rollbackScript " + executor + ": " + e.getMessage());
    }
    // 
    // Expand changelog properties
    // 
    changeLogParameters.setContexts(contexts);
    changeLogParameters.setLabels(labelExpression);
    DatabaseChangeLog changelog = getDatabaseChangeLog();
    rollbackScriptContents = changeLogParameters.expandExpressions(rollbackScriptContents, changelog);
    RawSQLChange rollbackChange = buildRawSQLChange(rollbackScriptContents);
    try {
        ((HubChangeExecListener) changeExecListener).setRollbackScriptContents(rollbackScriptContents);
        sendRollbackMessages(changeSets, changelog, RollbackMessageType.WILL_ROLLBACK, contexts, labelExpression, null);
        executor.execute(rollbackChange);
        sendRollbackMessages(changeSets, changelog, RollbackMessageType.ROLLED_BACK, contexts, labelExpression, null);
    } catch (DatabaseException e) {
        Scope.getCurrentScope().getLog(getClass()).warning(e.getMessage());
        LOG.severe("Error executing rollback script: " + e.getMessage());
        if (changeExecListener != null) {
            sendRollbackMessages(changeSets, changelog, RollbackMessageType.ROLLBACK_FAILED, contexts, labelExpression, e);
        }
        throw new DatabaseException("Error executing rollback script", e);
    }
    database.commit();
}
Also used : InputStreamList(liquibase.resource.InputStreamList) RawSQLChange(liquibase.change.core.RawSQLChange) Executor(liquibase.executor.Executor) LoggingExecutor(liquibase.executor.LoggingExecutor) HubChangeExecListener(liquibase.hub.listener.HubChangeExecListener) ExecutorService(liquibase.executor.ExecutorService) IOException(java.io.IOException)

Example 3 with HubChangeExecListener

use of liquibase.hub.listener.HubChangeExecListener in project liquibase by liquibase.

the class Liquibase method rollback.

/**
 * Rollback to date
 *
 * @param dateToRollBackTo
 * @param rollbackScript
 * @param contexts
 * @param labelExpression
 * @throws LiquibaseException
 */
public void rollback(Date dateToRollBackTo, String rollbackScript, Contexts contexts, LabelExpression labelExpression) throws LiquibaseException {
    changeLogParameters.setContexts(contexts);
    changeLogParameters.setLabels(labelExpression);
    runInScope(new Scope.ScopedRunner() {

        @Override
        public void run() throws Exception {
            LockService lockService = LockServiceFactory.getInstance().getLockService(database);
            lockService.waitForLock();
            Operation rollbackOperation = null;
            BufferedLogService bufferLog = new BufferedLogService();
            DatabaseChangeLog changeLog = null;
            Date startTime = new Date();
            HubUpdater hubUpdater = null;
            try {
                changeLog = getDatabaseChangeLog();
                checkLiquibaseTables(false, changeLog, contexts, labelExpression);
                changeLog.validate(database, contexts, labelExpression);
                // 
                // Let the user know that they can register for Hub
                // 
                hubUpdater = new HubUpdater(startTime, changeLog, database);
                hubUpdater.register(changeLogFile);
                // 
                // Create an iterator which will be used with a ListVisitor
                // to grab the list of change sets for the update
                // 
                List<RanChangeSet> ranChangeSetList = database.getRanChangeSetList();
                ChangeLogIterator listLogIterator = new ChangeLogIterator(ranChangeSetList, changeLog, new ExecutedAfterChangeSetFilter(dateToRollBackTo, ranChangeSetList), new AlreadyRanChangeSetFilter(ranChangeSetList), new ContextChangeSetFilter(contexts), new LabelChangeSetFilter(labelExpression), new IgnoreChangeSetFilter(), new DbmsChangeSetFilter(database));
                // 
                // Create or retrieve the Connection
                // Make sure the Hub is available here by checking the return
                // 
                Connection connection = getConnection(changeLog);
                if (connection != null) {
                    rollbackOperation = hubUpdater.preUpdateHub("ROLLBACK", "rollback-to-date", connection, changeLogFile, contexts, labelExpression, listLogIterator);
                }
                // 
                if (connection != null) {
                    changeExecListener = new HubChangeExecListener(rollbackOperation, changeExecListener);
                }
                // 
                // Create another iterator to run
                // 
                ChangeLogIterator logIterator = new ChangeLogIterator(ranChangeSetList, changeLog, new ExecutedAfterChangeSetFilter(dateToRollBackTo, ranChangeSetList), new AlreadyRanChangeSetFilter(ranChangeSetList), new ContextChangeSetFilter(contexts), new LabelChangeSetFilter(labelExpression), new IgnoreChangeSetFilter(), new DbmsChangeSetFilter(database));
                CompositeLogService compositeLogService = new CompositeLogService(true, bufferLog);
                if (rollbackScript == null) {
                    Scope.child(Scope.Attr.logService.name(), compositeLogService, () -> {
                        logIterator.run(createRollbackVisitor(), new RuntimeEnvironment(database, contexts, labelExpression));
                    });
                } else {
                    List<ChangeSet> changeSets = determineRollbacks(logIterator, contexts, labelExpression);
                    Map<String, Object> values = new HashMap<>();
                    values.put(Scope.Attr.logService.name(), compositeLogService);
                    values.put(BufferedLogService.class.getName(), bufferLog);
                    Scope.child(values, () -> {
                        executeRollbackScript(rollbackScript, changeSets, contexts, labelExpression);
                    });
                    removeRunStatus(changeSets, contexts, labelExpression);
                }
                hubUpdater.postUpdateHub(rollbackOperation, bufferLog);
            } catch (Throwable t) {
                if (hubUpdater != null) {
                    hubUpdater.postUpdateHubExceptionHandling(rollbackOperation, bufferLog, t.getMessage());
                }
                throw t;
            } finally {
                try {
                    lockService.releaseLock();
                } catch (LockException e) {
                    LOG.severe(MSG_COULD_NOT_RELEASE_LOCK, e);
                }
                resetServices();
                setChangeExecListener(null);
            }
        }
    });
}
Also used : Operation(liquibase.hub.model.Operation) CompositeLogService(liquibase.logging.core.CompositeLogService) HubChangeExecListener(liquibase.hub.listener.HubChangeExecListener) InputStreamList(liquibase.resource.InputStreamList) LockService(liquibase.lockservice.LockService) DatabaseConnection(liquibase.database.DatabaseConnection) Connection(liquibase.hub.model.Connection) InvalidExampleException(liquibase.snapshot.InvalidExampleException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) BufferedLogService(liquibase.logging.core.BufferedLogService) CommandScope(liquibase.command.CommandScope)

Example 4 with HubChangeExecListener

use of liquibase.hub.listener.HubChangeExecListener in project liquibase by liquibase.

the class Liquibase method update.

/**
 * Update to count
 *
 * @param  changesToApply
 * @param  contexts
 * @param  labelExpression
 * @throws LiquibaseException
 */
public void update(int changesToApply, Contexts contexts, LabelExpression labelExpression) throws LiquibaseException {
    changeLogParameters.setContexts(contexts);
    changeLogParameters.setLabels(labelExpression);
    runInScope(new Scope.ScopedRunner() {

        @Override
        public void run() throws Exception {
            LockService lockService = LockServiceFactory.getInstance().getLockService(database);
            lockService.waitForLock();
            Operation updateOperation = null;
            BufferedLogService bufferLog = new BufferedLogService();
            DatabaseChangeLog changeLog = null;
            HubUpdater hubUpdater = null;
            try {
                changeLog = getDatabaseChangeLog();
                checkLiquibaseTables(true, changeLog, contexts, labelExpression);
                ChangeLogHistoryServiceFactory.getInstance().getChangeLogService(database).generateDeploymentId();
                changeLog.validate(database, contexts, labelExpression);
                // 
                // Let the user know that they can register for Hub
                // 
                hubUpdater = new HubUpdater(new Date(), changeLog, database);
                hubUpdater.register(changeLogFile);
                // 
                // Create an iterator which will be used with a ListVisitor
                // to grab the list of change sets for the update
                // 
                ChangeLogIterator listLogIterator = new ChangeLogIterator(changeLog, new ShouldRunChangeSetFilter(database), new ContextChangeSetFilter(contexts), new LabelChangeSetFilter(labelExpression), new DbmsChangeSetFilter(database), new IgnoreChangeSetFilter(), new CountChangeSetFilter(changesToApply));
                // 
                // Create or retrieve the Connection
                // Make sure the Hub is available here by checking the return
                // 
                Connection connection = getConnection(changeLog);
                if (connection != null) {
                    updateOperation = hubUpdater.preUpdateHub("UPDATE", "update-count", connection, changeLogFile, contexts, labelExpression, listLogIterator);
                }
                // 
                if (connection != null) {
                    changeExecListener = new HubChangeExecListener(updateOperation, changeExecListener);
                }
                // 
                // Create another iterator to run
                // 
                ChangeLogIterator runChangeLogIterator = new ChangeLogIterator(changeLog, new ShouldRunChangeSetFilter(database), new ContextChangeSetFilter(contexts), new LabelChangeSetFilter(labelExpression), new DbmsChangeSetFilter(database), new IgnoreChangeSetFilter(), new CountChangeSetFilter(changesToApply));
                CompositeLogService compositeLogService = new CompositeLogService(true, bufferLog);
                Scope.child(Scope.Attr.logService.name(), compositeLogService, () -> {
                    runChangeLogIterator.run(createUpdateVisitor(), new RuntimeEnvironment(database, contexts, labelExpression));
                });
                hubUpdater.postUpdateHub(updateOperation, bufferLog);
            } catch (Throwable e) {
                if (hubUpdater != null) {
                    hubUpdater.postUpdateHubExceptionHandling(updateOperation, bufferLog, e.getMessage());
                }
                throw e;
            } finally {
                database.setObjectQuotingStrategy(ObjectQuotingStrategy.LEGACY);
                try {
                    lockService.releaseLock();
                } catch (LockException e) {
                    LOG.severe(MSG_COULD_NOT_RELEASE_LOCK, e);
                }
                resetServices();
                setChangeExecListener(null);
            }
        }
    });
}
Also used : LockService(liquibase.lockservice.LockService) DatabaseConnection(liquibase.database.DatabaseConnection) Connection(liquibase.hub.model.Connection) Operation(liquibase.hub.model.Operation) InvalidExampleException(liquibase.snapshot.InvalidExampleException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) BufferedLogService(liquibase.logging.core.BufferedLogService) CompositeLogService(liquibase.logging.core.CompositeLogService) CommandScope(liquibase.command.CommandScope) HubChangeExecListener(liquibase.hub.listener.HubChangeExecListener)

Example 5 with HubChangeExecListener

use of liquibase.hub.listener.HubChangeExecListener in project liquibase by liquibase.

the class Liquibase method rollback.

/**
 * Rollback to tag
 *
 * @param tagToRollBackTo
 * @param rollbackScript
 * @param contexts
 * @param labelExpression
 * @throws LiquibaseException
 */
public void rollback(String tagToRollBackTo, String rollbackScript, Contexts contexts, LabelExpression labelExpression) throws LiquibaseException {
    changeLogParameters.setContexts(contexts);
    changeLogParameters.setLabels(labelExpression);
    runInScope(new Scope.ScopedRunner() {

        @Override
        public void run() throws Exception {
            LockService lockService = LockServiceFactory.getInstance().getLockService(database);
            lockService.waitForLock();
            Operation rollbackOperation = null;
            BufferedLogService bufferLog = new BufferedLogService();
            DatabaseChangeLog changeLog = null;
            Date startTime = new Date();
            HubUpdater hubUpdater = null;
            try {
                changeLog = getDatabaseChangeLog();
                checkLiquibaseTables(false, changeLog, contexts, labelExpression);
                changeLog.validate(database, contexts, labelExpression);
                // 
                // Let the user know that they can register for Hub
                // 
                hubUpdater = new HubUpdater(startTime, changeLog, database);
                hubUpdater.register(changeLogFile);
                // 
                // Create an iterator which will be used with a ListVisitor
                // to grab the list of change sets for the update
                // 
                List<RanChangeSet> ranChangeSetList = database.getRanChangeSetList();
                ChangeLogIterator listLogIterator = new ChangeLogIterator(ranChangeSetList, changeLog, new AfterTagChangeSetFilter(tagToRollBackTo, ranChangeSetList), new AlreadyRanChangeSetFilter(ranChangeSetList), new ContextChangeSetFilter(contexts), new LabelChangeSetFilter(labelExpression), new IgnoreChangeSetFilter(), new DbmsChangeSetFilter(database));
                // 
                // Create or retrieve the Connection
                // Make sure the Hub is available here by checking the return
                // 
                Connection connection = getConnection(changeLog);
                if (connection != null) {
                    rollbackOperation = hubUpdater.preUpdateHub("ROLLBACK", "rollback", connection, changeLogFile, contexts, labelExpression, listLogIterator);
                }
                // 
                if (connection != null) {
                    changeExecListener = new HubChangeExecListener(rollbackOperation, changeExecListener);
                }
                // 
                // Create another iterator to run
                // 
                ChangeLogIterator logIterator = new ChangeLogIterator(ranChangeSetList, changeLog, new AfterTagChangeSetFilter(tagToRollBackTo, ranChangeSetList), new AlreadyRanChangeSetFilter(ranChangeSetList), new ContextChangeSetFilter(contexts), new LabelChangeSetFilter(labelExpression), new IgnoreChangeSetFilter(), new DbmsChangeSetFilter(database));
                CompositeLogService compositeLogService = new CompositeLogService(true, bufferLog);
                if (rollbackScript == null) {
                    Scope.child(Scope.Attr.logService.name(), compositeLogService, () -> {
                        logIterator.run(createRollbackVisitor(), new RuntimeEnvironment(database, contexts, labelExpression));
                    });
                } else {
                    List<ChangeSet> changeSets = determineRollbacks(logIterator, contexts, labelExpression);
                    Map<String, Object> values = new HashMap<>();
                    values.put(Scope.Attr.logService.name(), compositeLogService);
                    values.put(BufferedLogService.class.getName(), bufferLog);
                    Scope.child(values, () -> {
                        executeRollbackScript(rollbackScript, changeSets, contexts, labelExpression);
                    });
                    removeRunStatus(changeSets, contexts, labelExpression);
                }
                hubUpdater.postUpdateHub(rollbackOperation, bufferLog);
            } catch (Throwable t) {
                if (hubUpdater != null) {
                    hubUpdater.postUpdateHubExceptionHandling(rollbackOperation, bufferLog, t.getMessage());
                }
                throw t;
            } finally {
                try {
                    lockService.releaseLock();
                } catch (LockException e) {
                    LOG.severe(MSG_COULD_NOT_RELEASE_LOCK, e);
                }
            }
            resetServices();
            setChangeExecListener(null);
        }
    });
}
Also used : Operation(liquibase.hub.model.Operation) CompositeLogService(liquibase.logging.core.CompositeLogService) HubChangeExecListener(liquibase.hub.listener.HubChangeExecListener) InputStreamList(liquibase.resource.InputStreamList) LockService(liquibase.lockservice.LockService) DatabaseConnection(liquibase.database.DatabaseConnection) Connection(liquibase.hub.model.Connection) InvalidExampleException(liquibase.snapshot.InvalidExampleException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) BufferedLogService(liquibase.logging.core.BufferedLogService) CommandScope(liquibase.command.CommandScope)

Aggregations

HubChangeExecListener (liquibase.hub.listener.HubChangeExecListener)8 IOException (java.io.IOException)7 DatabaseConnection (liquibase.database.DatabaseConnection)7 Connection (liquibase.hub.model.Connection)7 Operation (liquibase.hub.model.Operation)7 LockService (liquibase.lockservice.LockService)7 BufferedLogService (liquibase.logging.core.BufferedLogService)7 CompositeLogService (liquibase.logging.core.CompositeLogService)7 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)6 CommandScope (liquibase.command.CommandScope)6 InvalidExampleException (liquibase.snapshot.InvalidExampleException)6 InputStreamList (liquibase.resource.InputStreamList)5 RawSQLChange (liquibase.change.core.RawSQLChange)1 Executor (liquibase.executor.Executor)1 ExecutorService (liquibase.executor.ExecutorService)1 LoggingExecutor (liquibase.executor.LoggingExecutor)1