Search in sources :

Example 6 with CompositeLogService

use of liquibase.logging.core.CompositeLogService in project liquibase by liquibase.

the class Liquibase method update.

/**
 * Update to tag
 *
 * @param   tag                             Tag to update for
 * @param   contexts
 * @param   labelExpression
 * @throws  LiquibaseException
 */
public void update(String tag, Contexts contexts, LabelExpression labelExpression) throws LiquibaseException {
    if (tag == null) {
        update(contexts, labelExpression);
        return;
    }
    changeLogParameters.setContexts(contexts);
    changeLogParameters.setLabels(labelExpression);
    runInScope(new Scope.ScopedRunner() {

        @Override
        public void run() throws Exception {
            LockService lockService = LockServiceFactory.getInstance().getLockService(database);
            lockService.waitForLock();
            HubUpdater hubUpdater = null;
            Operation updateOperation = null;
            BufferedLogService bufferLog = new BufferedLogService();
            DatabaseChangeLog changeLog = 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
                // 
                List<RanChangeSet> ranChangeSetList = database.getRanChangeSetList();
                ChangeLogIterator listLogIterator = new ChangeLogIterator(changeLog, new ShouldRunChangeSetFilter(database), new ContextChangeSetFilter(contexts), new LabelChangeSetFilter(labelExpression), new DbmsChangeSetFilter(database), new IgnoreChangeSetFilter(), new UpToTagChangeSetFilter(tag, ranChangeSetList));
                // 
                // 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-to-tag", 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 UpToTagChangeSetFilter(tag, ranChangeSetList));
                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) InputStreamList(liquibase.resource.InputStreamList)

Example 7 with CompositeLogService

use of liquibase.logging.core.CompositeLogService in project liquibase by liquibase.

the class Liquibase method changeLogSync.

/**
 * Changelogsync or changelogsync to tag
 *
 * @param tag
 * @param contexts
 * @param labelExpression
 * @throws LiquibaseException
 */
public void changeLogSync(String tag, 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 changeLogSyncOperation = 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 = buildChangeLogIterator(tag, changeLog, contexts, labelExpression);
                // 
                // Create or retrieve the Connection
                // Make sure the Hub is available here by checking the return
                // 
                Connection connection = getConnection(changeLog);
                if (connection != null) {
                    String operationCommand = (tag == null ? "changelog-sync" : "changelog-sync-to-tag");
                    changeLogSyncOperation = hubUpdater.preUpdateHub("CHANGELOGSYNC", operationCommand, connection, changeLogFile, contexts, labelExpression, listLogIterator);
                }
                // 
                if (connection != null) {
                    changeLogSyncListener = new HubChangeExecListener(changeLogSyncOperation, changeExecListener);
                }
                ChangeLogIterator runChangeLogIterator = buildChangeLogIterator(tag, changeLog, contexts, labelExpression);
                CompositeLogService compositeLogService = new CompositeLogService(true, bufferLog);
                Scope.child(Scope.Attr.logService.name(), compositeLogService, () -> {
                    runChangeLogIterator.run(new ChangeLogSyncVisitor(database, changeLogSyncListener), new RuntimeEnvironment(database, contexts, labelExpression));
                });
                hubUpdater.postUpdateHub(changeLogSyncOperation, bufferLog);
            } catch (Exception e) {
                if (changeLogSyncOperation != null) {
                    hubUpdater.postUpdateHubExceptionHandling(changeLogSyncOperation, bufferLog, e.getMessage());
                }
                throw e;
            } finally {
                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 8 with CompositeLogService

use of liquibase.logging.core.CompositeLogService in project liquibase by liquibase.

the class ChangeLogIterator method run.

public void run(ChangeSetVisitor visitor, RuntimeEnvironment env) throws LiquibaseException {
    Logger log = Scope.getCurrentScope().getLog(getClass());
    databaseChangeLog.setRuntimeEnvironment(env);
    try {
        Scope.child(Scope.Attr.databaseChangeLog, databaseChangeLog, new Scope.ScopedRunner() {

            @Override
            public void run() throws Exception {
                List<ChangeSet> changeSetList = new ArrayList<>(databaseChangeLog.getChangeSets());
                if (visitor.getDirection().equals(ChangeSetVisitor.Direction.REVERSE)) {
                    Collections.reverse(changeSetList);
                }
                for (ChangeSet changeSet : changeSetList) {
                    boolean shouldVisit = true;
                    Set<ChangeSetFilterResult> reasonsAccepted = new HashSet<>();
                    Set<ChangeSetFilterResult> reasonsDenied = new HashSet<>();
                    if (changeSetFilters != null) {
                        for (ChangeSetFilter filter : changeSetFilters) {
                            ChangeSetFilterResult acceptsResult = filter.accepts(changeSet);
                            if (acceptsResult.isAccepted()) {
                                reasonsAccepted.add(acceptsResult);
                            } else {
                                shouldVisit = false;
                                reasonsDenied.add(acceptsResult);
                                break;
                            }
                        }
                    }
                    boolean finalShouldVisit = shouldVisit;
                    BufferedLogService bufferLog = new BufferedLogService();
                    CompositeLogService compositeLogService = new CompositeLogService(true, bufferLog);
                    Scope.child(Scope.Attr.changeSet.name(), changeSet, () -> {
                        if (finalShouldVisit && !alreadySaw(changeSet)) {
                            // 
                            if (visitor instanceof ValidatingVisitor) {
                                validateChangeSetExecutor(changeSet, env);
                            }
                            // 
                            // Execute the visit call in its own scope with a new
                            // CompositeLogService and BufferLogService in order
                            // to capture the logging for just this change set.  The
                            // log is sent to Hub if available
                            // 
                            Map<String, Object> values = new HashMap<>();
                            values.put(Scope.Attr.logService.name(), compositeLogService);
                            values.put(BufferedLogService.class.getName(), bufferLog);
                            Scope.child(values, () -> {
                                visitor.visit(changeSet, databaseChangeLog, env.getTargetDatabase(), reasonsAccepted);
                            });
                            markSeen(changeSet);
                        } else {
                            if (visitor instanceof SkippedChangeSetVisitor) {
                                ((SkippedChangeSetVisitor) visitor).skipped(changeSet, databaseChangeLog, env.getTargetDatabase(), reasonsDenied);
                            }
                        }
                    });
                }
            }
        });
    } catch (Exception e) {
        throw new LiquibaseException(e);
    } finally {
        databaseChangeLog.setRuntimeEnvironment(null);
    }
}
Also used : Logger(liquibase.logging.Logger) ChangeSetFilter(liquibase.changelog.filter.ChangeSetFilter) LiquibaseException(liquibase.exception.LiquibaseException) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) BufferedLogService(liquibase.logging.core.BufferedLogService) CompositeLogService(liquibase.logging.core.CompositeLogService) ValidatingVisitor(liquibase.changelog.visitor.ValidatingVisitor) SkippedChangeSetVisitor(liquibase.changelog.visitor.SkippedChangeSetVisitor) LiquibaseException(liquibase.exception.LiquibaseException) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) ChangeSetFilterResult(liquibase.changelog.filter.ChangeSetFilterResult)

Aggregations

BufferedLogService (liquibase.logging.core.BufferedLogService)8 CompositeLogService (liquibase.logging.core.CompositeLogService)8 DatabaseConnection (liquibase.database.DatabaseConnection)7 HubChangeExecListener (liquibase.hub.listener.HubChangeExecListener)7 Connection (liquibase.hub.model.Connection)7 Operation (liquibase.hub.model.Operation)7 LockService (liquibase.lockservice.LockService)7 IOException (java.io.IOException)6 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)6 CommandScope (liquibase.command.CommandScope)6 InvalidExampleException (liquibase.snapshot.InvalidExampleException)6 InputStreamList (liquibase.resource.InputStreamList)4 ChangeSetFilter (liquibase.changelog.filter.ChangeSetFilter)1 ChangeSetFilterResult (liquibase.changelog.filter.ChangeSetFilterResult)1 SkippedChangeSetVisitor (liquibase.changelog.visitor.SkippedChangeSetVisitor)1 ValidatingVisitor (liquibase.changelog.visitor.ValidatingVisitor)1 LiquibaseException (liquibase.exception.LiquibaseException)1 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)1 Logger (liquibase.logging.Logger)1