001: package liquibase.ant;
002:
003: import liquibase.database.Database;
004: import liquibase.diff.Diff;
005: import liquibase.diff.DiffResult;
006: import liquibase.Liquibase;
007: import liquibase.util.StringUtils;
008: import org.apache.tools.ant.BuildException;
009:
010: import java.io.PrintStream;
011:
012: public class DiffDatabaseTask extends BaseLiquibaseTask {
013:
014: private String baseDriver;
015: private String baseUrl;
016: private String baseUsername;
017: private String basePassword;
018: private String baseDefaultSchemaName;
019:
020: public String getBaseDriver() {
021: if (baseDriver == null) {
022: return getDriver();
023: }
024: return baseDriver;
025: }
026:
027: public void setBaseDriver(String baseDriver) {
028: this .baseDriver = baseDriver;
029: }
030:
031: public String getBaseUrl() {
032: return baseUrl;
033: }
034:
035: public void setBaseUrl(String baseUrl) {
036: this .baseUrl = baseUrl;
037: }
038:
039: public String getBaseUsername() {
040: return baseUsername;
041: }
042:
043: public void setBaseUsername(String baseUsername) {
044: this .baseUsername = baseUsername;
045: }
046:
047: public String getBasePassword() {
048: return basePassword;
049: }
050:
051: public void setBasePassword(String basePassword) {
052: this .basePassword = basePassword;
053: }
054:
055: public String getBaseDefaultSchemaName() {
056: return baseDefaultSchemaName;
057: }
058:
059: public void setBaseDefaultSchemaName(String baseDefaultSchemaName) {
060: this .baseDefaultSchemaName = baseDefaultSchemaName;
061: }
062:
063: public void execute() throws BuildException {
064: if (StringUtils.trimToNull(getBaseUrl()) == null) {
065: throw new BuildException(
066: "diffDatabase requires baseUrl to be set");
067: }
068: if (StringUtils.trimToNull(getBaseUsername()) == null) {
069: throw new BuildException(
070: "diffDatabase requires baseUsername to be set");
071: }
072: if (StringUtils.trimToNull(getBasePassword()) == null) {
073: throw new BuildException(
074: "diffDatabase requires basePassword to be set");
075: }
076:
077: Liquibase liquibase = null;
078: try {
079: PrintStream writer = createPrintStream();
080: if (writer == null) {
081: throw new BuildException(
082: "diffDatabase requires outputFile to be set");
083: }
084:
085: liquibase = createLiquibase();
086:
087: Database baseDatabase = createDatabaseObject(
088: getBaseDriver(), getBaseUrl(), getBaseUsername(),
089: getBasePassword(), getBaseDefaultSchemaName());
090:
091: Diff diff = new Diff(baseDatabase, liquibase.getDatabase());
092: // diff.addStatusListener(new OutDiffStatusListener());
093: DiffResult diffResult = diff.compare();
094:
095: outputDiff(writer, diffResult, liquibase.getDatabase());
096:
097: writer.flush();
098: writer.close();
099: } catch (Exception e) {
100: throw new BuildException(e);
101: } finally {
102: closeDatabase(liquibase);
103: }
104: }
105:
106: protected void outputDiff(PrintStream writer,
107: DiffResult diffResult, Database targetDatabase)
108: throws Exception {
109: diffResult.printResult(writer);
110: }
111: }
|