liquibase.integration.UnexpectedChangeSetsValidator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of liquibase-core Show documentation
Show all versions of liquibase-core Show documentation
Liquibase is a tool for managing and executing database changes.
The newest version!
package liquibase.integration;
import liquibase.Contexts;
import liquibase.LabelExpression;
import liquibase.Liquibase;
import liquibase.exception.LiquibaseException;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import java.util.HashSet;
import java.util.Set;
import java.util.function.Consumer;
public class UnexpectedChangeSetsValidator implements Consumer {
private final Contexts contexts;
private final LabelExpression labelExpression;
private final Set applied, unexpected;
public UnexpectedChangeSetsValidator() {
contexts = new Contexts();
labelExpression = new LabelExpression();
applied = new HashSet<>();
unexpected = new HashSet<>();
}
private UnexpectedChangeSetsValidator(
Set applied,
Set unexpected,
Contexts contexts,
LabelExpression labelExpression) {
this.applied = applied;
this.unexpected = unexpected;
this.contexts = contexts;
this.labelExpression = labelExpression;
}
@Override
public void accept(T liquibase) {
try {
liquibase.getDatabaseChangeLog().getChangeSets().stream().map(changeSet -> new ChangeSetInfo(
changeSet.getId(),
changeSet.getFilePath(),
changeSet.getAuthor())).forEach(applied::add);
liquibase.listUnexpectedChangeSets(contexts, labelExpression).stream().map(changeSet -> new ChangeSetInfo(
changeSet.getId(),
changeSet.getChangeLog(),
changeSet.getAuthor())).forEach(unexpected::add);
} catch (LiquibaseException e) {
throw new IllegalStateException(e);
}
}
public UnexpectedChangeSetsValidator with(Contexts contexts, LabelExpression labelExpression) {
return new UnexpectedChangeSetsValidator<>(applied,
unexpected,
contexts,
labelExpression);
}
public void validate(Consumer> unexpectedChangeSetsConsumer) {
Set changeSets = new HashSet<>(unexpected);
changeSets.removeAll(applied);
if (!changeSets.isEmpty()) {
unexpectedChangeSetsConsumer.accept(changeSets);
}
}
@Getter
@EqualsAndHashCode
@RequiredArgsConstructor
public static class ChangeSetInfo {
private final String id, changeLog, author;
@Override
public String toString() {
return changeLog + "::" + id + "::" + author;
}
}
}