Search in sources :

Example 1 with ChangeSetFilterResult

use of liquibase.changelog.filter.ChangeSetFilterResult in project liquibase by liquibase.

the class XMLChangeLogSAXParserTest method testIgnoreDuplicateChangeSets.

@Test
public void testIgnoreDuplicateChangeSets() throws ChangeLogParseException, Exception {
    XMLChangeLogSAXParser xmlParser = new XMLChangeLogSAXParser();
    DatabaseChangeLog changeLog = xmlParser.parse("liquibase/parser/core/xml/ignoreDuplicatedChangeLogs/master.changelog.xml", new ChangeLogParameters(), new JUnitResourceAccessor());
    final List<ChangeSet> changeSets = new ArrayList<ChangeSet>();
    new ChangeLogIterator(changeLog).run(new ChangeSetVisitor() {

        @Override
        public Direction getDirection() {
            return Direction.FORWARD;
        }

        @Override
        public void visit(ChangeSet changeSet, DatabaseChangeLog databaseChangeLog, Database database, Set<ChangeSetFilterResult> filterResults) throws LiquibaseException {
            changeSets.add(changeSet);
        }
    }, new RuntimeEnvironment(new MockDatabase(), new Contexts(), new LabelExpression()));
    Assert.assertEquals(8, changeSets.size());
    Assert.assertEquals("liquibase/parser/core/xml/ignoreDuplicatedChangeLogs/included.changelog4.xml::1::testuser", changeSets.get(0).toString());
    Assert.assertEquals("liquibase/parser/core/xml/ignoreDuplicatedChangeLogs/included.changelog4.xml::1::testuser", changeSets.get(1).toString());
    Assert.assertEquals(1, changeSets.get(1).getContexts().getContexts().size());
    Assert.assertEquals("liquibase/parser/core/xml/ignoreDuplicatedChangeLogs/included.changelog4.xml::1::testuser", changeSets.get(2).toString());
    Assert.assertEquals(1, changeSets.get(2).getLabels().getLabels().size());
    Assert.assertEquals("liquibase/parser/core/xml/ignoreDuplicatedChangeLogs/included.changelog4.xml::1::testuser", changeSets.get(3).toString());
    Assert.assertEquals(2, changeSets.get(3).getLabels().getLabels().size());
    Assert.assertEquals("liquibase/parser/core/xml/ignoreDuplicatedChangeLogs/included.changelog4.xml::1::testuser", changeSets.get(4).toString());
    Assert.assertEquals(1, changeSets.get(4).getDbmsSet().size());
    Assert.assertEquals("liquibase/parser/core/xml/ignoreDuplicatedChangeLogs/included.changelog1.xml::1::testuser", changeSets.get(5).toString());
    Assert.assertEquals("liquibase/parser/core/xml/ignoreDuplicatedChangeLogs/included.changelog3.xml::1::testuser", changeSets.get(6).toString());
    Assert.assertEquals("liquibase/parser/core/xml/ignoreDuplicatedChangeLogs/included.changelog2.xml::1::testuser", changeSets.get(7).toString());
}
Also used : ChangeSetVisitor(liquibase.changelog.visitor.ChangeSetVisitor) RuntimeEnvironment(liquibase.RuntimeEnvironment) JUnitResourceAccessor(liquibase.test.JUnitResourceAccessor) ArrayList(java.util.ArrayList) Contexts(liquibase.Contexts) DatabaseChangeLog(liquibase.changelog.DatabaseChangeLog) ChangeLogIterator(liquibase.changelog.ChangeLogIterator) ChangeLogParameters(liquibase.changelog.ChangeLogParameters) MockDatabase(liquibase.sdk.database.MockDatabase) Database(liquibase.database.Database) LabelExpression(liquibase.LabelExpression) MockDatabase(liquibase.sdk.database.MockDatabase) LiquibaseException(liquibase.exception.LiquibaseException) ChangeSetFilterResult(liquibase.changelog.filter.ChangeSetFilterResult) ChangeSet(liquibase.changelog.ChangeSet) Test(org.junit.Test)

Example 2 with ChangeSetFilterResult

use of liquibase.changelog.filter.ChangeSetFilterResult in project liquibase by liquibase.

the class StatusVisitor method getStatuses.

/**
 * Return the status of all changeSets, in the order they exist in the databasechangelog.
 * Any change sets not in the current change log but previously ran against the database will be at the front of the List
 * with a not run reason type of {@link liquibase.changelog.filter.NotInChangeLogChangeSetFilter}
 */
public List<ChangeSetStatus> getStatuses() {
    ArrayList<ChangeSetStatus> returnList = new ArrayList<>();
    for (RanChangeSet changeSet : ranChangeSets) {
        ChangeSetStatus status = new ChangeSetStatus(new ChangeSet(changeSet.getId(), changeSet.getAuthor(), false, false, changeSet.getChangeLog(), null, null, null));
        status.setPreviouslyRan(true);
        status.setDateLastExecuted(changeSet.getDateExecuted());
        status.setStoredCheckSum(changeSet.getLastCheckSum());
        status.setComments(changeSet.getComments());
        status.setDescription(changeSet.getDescription());
        status.setWillRun(false);
        status.setFilterResults(new HashSet<>(Arrays.asList(new ChangeSetFilterResult(false, "Change set is not in change log", NotInChangeLogChangeSetFilter.class))));
        status.setRanChangeSet(changeSet);
        returnList.add(status);
    }
    returnList.addAll(changeSetStatuses.values());
    return returnList;
}
Also used : NotInChangeLogChangeSetFilter(liquibase.changelog.filter.NotInChangeLogChangeSetFilter) ChangeSetFilterResult(liquibase.changelog.filter.ChangeSetFilterResult)

Example 3 with ChangeSetFilterResult

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

ChangeSetFilterResult (liquibase.changelog.filter.ChangeSetFilterResult)3 LiquibaseException (liquibase.exception.LiquibaseException)2 ArrayList (java.util.ArrayList)1 Contexts (liquibase.Contexts)1 LabelExpression (liquibase.LabelExpression)1 RuntimeEnvironment (liquibase.RuntimeEnvironment)1 ChangeLogIterator (liquibase.changelog.ChangeLogIterator)1 ChangeLogParameters (liquibase.changelog.ChangeLogParameters)1 ChangeSet (liquibase.changelog.ChangeSet)1 DatabaseChangeLog (liquibase.changelog.DatabaseChangeLog)1 ChangeSetFilter (liquibase.changelog.filter.ChangeSetFilter)1 NotInChangeLogChangeSetFilter (liquibase.changelog.filter.NotInChangeLogChangeSetFilter)1 ChangeSetVisitor (liquibase.changelog.visitor.ChangeSetVisitor)1 SkippedChangeSetVisitor (liquibase.changelog.visitor.SkippedChangeSetVisitor)1 ValidatingVisitor (liquibase.changelog.visitor.ValidatingVisitor)1 Database (liquibase.database.Database)1 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)1 Logger (liquibase.logging.Logger)1 BufferedLogService (liquibase.logging.core.BufferedLogService)1 CompositeLogService (liquibase.logging.core.CompositeLogService)1