001: package liquibase;
002:
003: import liquibase.database.Database;
004: import liquibase.exception.LiquibaseException;
005: import liquibase.exception.ValidationFailedException;
006: import liquibase.parser.ChangeLogIterator;
007: import liquibase.parser.filter.DbmsChangeSetFilter;
008: import liquibase.parser.visitor.ValidatingVisitor;
009: import liquibase.preconditions.AndPrecondition;
010:
011: import java.util.ArrayList;
012: import java.util.List;
013:
014: /**
015: * Encapsulates the information stored in the change log XML file.
016: */
017: public class DatabaseChangeLog implements Comparable<DatabaseChangeLog> {
018: private AndPrecondition preconditions;
019: private String physicalFilePath;
020: private String logicalFilePath;
021:
022: private List<ChangeSet> changeSets = new ArrayList<ChangeSet>();
023:
024: public DatabaseChangeLog(String physicalFilePath) {
025: this .physicalFilePath = physicalFilePath;
026: }
027:
028: public AndPrecondition getPreconditions() {
029: return preconditions;
030: }
031:
032: public void setPreconditions(AndPrecondition precond) {
033: preconditions = precond;
034: }
035:
036: public String getPhysicalFilePath() {
037: return physicalFilePath;
038: }
039:
040: public void setPhysicalFilePath(String physicalFilePath) {
041: this .physicalFilePath = physicalFilePath;
042: }
043:
044: public String getLogicalFilePath() {
045: if (logicalFilePath == null) {
046: return physicalFilePath;
047: }
048: return logicalFilePath;
049: }
050:
051: public void setLogicalFilePath(String logicalFilePath) {
052: this .logicalFilePath = logicalFilePath;
053: }
054:
055: public String getFilePath() {
056: if (logicalFilePath == null) {
057: return physicalFilePath;
058: } else {
059: return logicalFilePath;
060: }
061: }
062:
063: public String toString() {
064: return getFilePath();
065: }
066:
067: public int compareTo(DatabaseChangeLog o) {
068: return getFilePath().compareTo(o.getFilePath());
069: }
070:
071: public List<ChangeSet> getChangeSets() {
072: return changeSets;
073: }
074:
075: public void addChangeSet(ChangeSet changeSet) {
076: this .changeSets.add(changeSet);
077: }
078:
079: public boolean equals(Object o) {
080: if (this == o)
081: return true;
082: if (o == null || getClass() != o.getClass())
083: return false;
084:
085: DatabaseChangeLog that = (DatabaseChangeLog) o;
086:
087: return getFilePath().equals(that.getFilePath());
088:
089: }
090:
091: public int hashCode() {
092: return getFilePath().hashCode();
093: }
094:
095: /**
096: * Checks changelogs for bad MD5Sums and preconditions before attempting a migration
097: */
098: public void validate(Database database) throws LiquibaseException {
099:
100: ChangeLogIterator logIterator = new ChangeLogIterator(this ,
101: new DbmsChangeSetFilter(database));
102:
103: ValidatingVisitor validatingVisitor = new ValidatingVisitor(
104: database.getRanChangeSetList());
105: validatingVisitor.checkPreconditions(database, this );
106: logIterator.run(validatingVisitor);
107:
108: if (!validatingVisitor.validationPassed()) {
109: throw new ValidationFailedException(validatingVisitor);
110: }
111: }
112:
113: }
|