1 | |
package liquibase.integration.ant; |
2 | |
|
3 | |
import liquibase.Liquibase; |
4 | |
import liquibase.database.Database; |
5 | |
import liquibase.diff.Diff; |
6 | |
import liquibase.diff.DiffResult; |
7 | |
import liquibase.exception.DatabaseException; |
8 | |
import liquibase.logging.LogFactory; |
9 | |
import liquibase.util.StringUtils; |
10 | |
import org.apache.tools.ant.BuildException; |
11 | |
|
12 | |
import java.io.PrintStream; |
13 | |
|
14 | 0 | public class DiffDatabaseTask extends BaseLiquibaseTask { |
15 | |
|
16 | |
private String referenceDriver; |
17 | |
private String referenceUrl; |
18 | |
private String referenceUsername; |
19 | |
private String referencePassword; |
20 | |
private String referenceDefaultSchemaName; |
21 | |
private String diffTypes; |
22 | |
private String dataDir; |
23 | |
|
24 | |
public String getDiffTypes() { |
25 | 0 | return diffTypes; |
26 | |
} |
27 | |
|
28 | |
public void setDiffTypes(String diffTypes) { |
29 | 0 | this.diffTypes = diffTypes; |
30 | 0 | } |
31 | |
|
32 | |
public String getDataDir() { |
33 | 0 | return dataDir; |
34 | |
} |
35 | |
|
36 | |
public void setDataDir(String dataDir) { |
37 | 0 | this.dataDir = dataDir; |
38 | 0 | } |
39 | |
|
40 | |
public String getReferenceDriver() { |
41 | 0 | return referenceDriver; |
42 | |
} |
43 | |
|
44 | |
public void setReferenceDriver(String referenceDriver) { |
45 | 0 | this.referenceDriver = referenceDriver; |
46 | 0 | } |
47 | |
|
48 | |
public String getReferenceUrl() { |
49 | 0 | return referenceUrl; |
50 | |
} |
51 | |
|
52 | |
public void setReferenceUrl(String referenceUrl) { |
53 | 0 | this.referenceUrl = referenceUrl; |
54 | 0 | } |
55 | |
|
56 | |
public String getReferenceUsername() { |
57 | 0 | return referenceUsername; |
58 | |
} |
59 | |
|
60 | |
public void setReferenceUsername(String referenceUsername) { |
61 | 0 | this.referenceUsername = referenceUsername; |
62 | 0 | } |
63 | |
|
64 | |
public String getReferencePassword() { |
65 | 0 | return referencePassword; |
66 | |
} |
67 | |
|
68 | |
public void setReferencePassword(String referencePassword) { |
69 | 0 | this.referencePassword = referencePassword; |
70 | 0 | } |
71 | |
|
72 | |
public String getReferenceDefaultSchemaName() { |
73 | 0 | return referenceDefaultSchemaName; |
74 | |
} |
75 | |
|
76 | |
public void setReferenceDefaultSchemaName(String referenceDefaultSchemaName) { |
77 | 0 | this.referenceDefaultSchemaName = referenceDefaultSchemaName; |
78 | 0 | } |
79 | |
|
80 | |
@Override |
81 | |
public void execute() throws BuildException { |
82 | 0 | if (StringUtils.trimToNull(getReferenceUrl()) == null) { |
83 | 0 | throw new BuildException("diffDatabase requires referenceUrl to be set"); |
84 | |
} |
85 | |
|
86 | 0 | super.execute(); |
87 | |
|
88 | 0 | Liquibase liquibase = null; |
89 | 0 | Database referenceDatabase = null; |
90 | |
try { |
91 | 0 | PrintStream writer = createPrintStream(); |
92 | 0 | if (writer == null && getChangeLogFile() == null) { |
93 | 0 | throw new BuildException("diffDatabase requires outputFile or changeLogFile to be set"); |
94 | |
} |
95 | |
|
96 | 0 | liquibase = createLiquibase(); |
97 | |
|
98 | 0 | referenceDatabase = createDatabaseObject(getReferenceDriver(), getReferenceUrl(), getReferenceUsername(), |
99 | |
getReferencePassword(), getReferenceDefaultSchemaName(), getDatabaseClass()); |
100 | |
|
101 | 0 | Diff diff = new Diff(referenceDatabase, liquibase.getDatabase()); |
102 | 0 | if (getDiffTypes() != null) { |
103 | 0 | diff.setDiffTypes(getDiffTypes()); |
104 | |
} |
105 | |
|
106 | 0 | DiffResult diffResult = diff.compare(); |
107 | 0 | if (getDataDir() != null) { |
108 | 0 | diffResult.setDataDir(getDataDir()); |
109 | |
} |
110 | |
|
111 | 0 | outputDiff(writer, diffResult, liquibase.getDatabase()); |
112 | |
|
113 | 0 | if (writer != null) { |
114 | 0 | writer.flush(); |
115 | 0 | writer.close(); |
116 | |
} |
117 | 0 | } catch (Exception e) { |
118 | 0 | throw new BuildException(e); |
119 | |
} finally { |
120 | 0 | try { |
121 | 0 | closeDatabase(liquibase); |
122 | |
} finally { |
123 | 0 | if (referenceDatabase != null && referenceDatabase.getConnection() != null) { |
124 | |
try { |
125 | 0 | referenceDatabase.close(); |
126 | 0 | } catch (DatabaseException e) { |
127 | 0 | LogFactory.getLogger().severe("Error closing referenceDatabase", e); |
128 | 0 | } |
129 | |
} |
130 | |
} |
131 | 0 | } |
132 | 0 | } |
133 | |
|
134 | |
protected void outputDiff(PrintStream writer, DiffResult diffResult, Database targetDatabase) throws Exception { |
135 | 0 | diffResult.printResult(writer); |
136 | 0 | } |
137 | |
} |