Search in sources :

Example 1 with ChangeSetFilter

use of liquibase.changelog.filter.ChangeSetFilter 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

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 BufferedLogService (liquibase.logging.core.BufferedLogService)1 CompositeLogService (liquibase.logging.core.CompositeLogService)1