01: package liquibase.ant;
02:
03: import liquibase.Liquibase;
04: import org.apache.tools.ant.BuildException;
05:
06: import java.io.Writer;
07: import java.util.Date;
08:
09: /**
10: * Ant task for rolling back a database.
11: */
12: public class DatabaseRollbackTask extends BaseLiquibaseTask {
13:
14: private Date rollbackDate;
15: private String rollbackTag;
16: private Integer rollbackCount;
17:
18: public Date getRollbackDate() {
19: if (rollbackDate == null) {
20: return null;
21: }
22:
23: return (Date) rollbackDate.clone();
24: }
25:
26: public void setRollbackDate(Date rollbackDate) {
27: if (rollbackDate != null) {
28: this .rollbackDate = new Date(rollbackDate.getTime());
29: }
30: }
31:
32: public String getRollbackTag() {
33: return rollbackTag;
34: }
35:
36: public void setRollbackTag(String rollbackTag) {
37: this .rollbackTag = rollbackTag;
38: }
39:
40: public Integer getRollbackCount() {
41: return rollbackCount;
42: }
43:
44: public void setRollbackCount(Integer rollbackCount) {
45: this .rollbackCount = rollbackCount;
46: }
47:
48: public void execute() throws BuildException {
49: if (getRollbackDate() == null && getRollbackCount() == null
50: && getRollbackTag() == null) {
51: throw new BuildException(
52: "rollbackDatabase requires rollbackTag, rollbackDate, or rollbackCount to be set");
53: }
54: Liquibase liquibase = null;
55: try {
56: liquibase = createLiquibase();
57: Writer writer = createOutputWriter();
58: if (getRollbackCount() != null) {
59: if (writer == null) {
60: liquibase.rollback(getRollbackCount(),
61: getContexts());
62: } else {
63: liquibase.rollback(getRollbackCount(),
64: getContexts(), writer);
65: }
66: } else if (getRollbackDate() != null) {
67: if (writer == null) {
68: liquibase
69: .rollback(getRollbackDate(), getContexts());
70: } else {
71: liquibase.rollback(getRollbackDate(),
72: getContexts(), writer);
73: }
74: } else if (getRollbackTag() != null) {
75: if (writer == null) {
76: liquibase.rollback(getRollbackTag(), getContexts());
77: } else {
78: liquibase.rollback(getRollbackTag(), getContexts(),
79: writer);
80: }
81: } else {
82: throw new BuildException(
83: "Must specify rollbackCount, rollbackDate, or rollbackTag");
84: }
85: if (writer != null) {
86: writer.flush();
87: writer.close();
88: }
89: } catch (Exception e) {
90: throw new BuildException(e);
91: } finally {
92: closeDatabase(liquibase);
93: }
94: }
95: }
|