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 org.apache.tools.ant.BuildException; |
8 | |
|
9 | |
import java.io.PrintStream; |
10 | |
|
11 | 0 | public class GenerateChangeLogTask extends BaseLiquibaseTask { |
12 | |
|
13 | |
private String diffTypes; |
14 | |
private String dataDir; |
15 | |
|
16 | |
public String getDiffTypes() { |
17 | 0 | return diffTypes; |
18 | |
} |
19 | |
|
20 | |
public void setDiffTypes(String diffTypes) { |
21 | 0 | this.diffTypes = diffTypes; |
22 | 0 | } |
23 | |
|
24 | |
public String getDataDir() { |
25 | 0 | return dataDir; |
26 | |
} |
27 | |
|
28 | |
public void setDataDir(String dataDir) { |
29 | 0 | this.dataDir = dataDir; |
30 | 0 | } |
31 | |
|
32 | |
@Override |
33 | |
public void execute() throws BuildException { |
34 | 0 | super.execute(); |
35 | |
|
36 | 0 | Liquibase liquibase = null; |
37 | |
try { |
38 | 0 | PrintStream writer = createPrintStream(); |
39 | 0 | if (writer == null) { |
40 | 0 | throw new BuildException("generateChangeLog requires outputFile to be set"); |
41 | |
} |
42 | |
|
43 | 0 | liquibase = createLiquibase(); |
44 | |
|
45 | 0 | Database database = liquibase.getDatabase(); |
46 | 0 | Diff diff = new Diff(database, getDefaultSchemaName()); |
47 | 0 | if (getDiffTypes() != null) { |
48 | 0 | diff.setDiffTypes(getDiffTypes()); |
49 | |
} |
50 | |
|
51 | 0 | DiffResult diffResult = diff.compare(); |
52 | 0 | diffResult.setDataDir(getDataDir()); |
53 | |
|
54 | 0 | if (getChangeLogFile() == null) { |
55 | 0 | diffResult.printChangeLog(writer, database); |
56 | |
} else { |
57 | 0 | diffResult.printChangeLog(getChangeLogFile(), database); |
58 | |
} |
59 | |
|
60 | 0 | writer.flush(); |
61 | 0 | writer.close(); |
62 | 0 | } catch (Exception e) { |
63 | 0 | throw new BuildException(e); |
64 | |
} finally { |
65 | 0 | closeDatabase(liquibase); |
66 | 0 | } |
67 | 0 | } |
68 | |
} |