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