1 | |
|
2 | |
|
3 | |
package org.liquibase.maven.plugins; |
4 | |
|
5 | |
import java.text.*; |
6 | |
import liquibase.exception.LiquibaseException; |
7 | |
import liquibase.Liquibase; |
8 | |
import org.apache.maven.plugin.MojoFailureException; |
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | 3 | public class LiquibaseRollback extends AbstractLiquibaseChangeLogMojo { |
17 | |
|
18 | 3 | protected enum RollbackType { |
19 | |
|
20 | 0 | TAG, DATE, COUNT |
21 | |
} |
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
protected String rollbackTag; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
protected int rollbackCount; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
protected String rollbackDate; |
44 | |
|
45 | |
|
46 | |
protected RollbackType type; |
47 | |
|
48 | |
@Override |
49 | |
protected void checkRequiredParametersAreSpecified() throws MojoFailureException { |
50 | 0 | super.checkRequiredParametersAreSpecified(); |
51 | |
|
52 | 0 | if (rollbackCount == -1 && rollbackDate == null && rollbackTag == null) { |
53 | 0 | throw new MojoFailureException("One of the rollback options must be specified, " |
54 | |
+ "please specify one of rollbackTag, rollbackCount " + "or rollbackDate"); |
55 | |
} |
56 | |
|
57 | 0 | if (rollbackCount != -1 && rollbackCount <= 0) { |
58 | 0 | throw new MojoFailureException("A rollback count of " + rollbackCount + " is meaningless, please " |
59 | |
+ "select a value greater than 0"); |
60 | |
} |
61 | |
|
62 | 0 | String message = "Cannot specify multiple rollbackXXX options, please select only" |
63 | |
+ " one of rollbackTag, rollbackCount, rollbackDate."; |
64 | |
|
65 | 0 | if (rollbackCount > 0) { |
66 | 0 | if (rollbackDate != null || rollbackTag != null) { |
67 | 0 | throw new MojoFailureException(message); |
68 | |
} |
69 | 0 | type = RollbackType.COUNT; |
70 | 0 | } else if (rollbackDate != null) { |
71 | 0 | if (rollbackTag != null || rollbackCount > 0) { |
72 | 0 | throw new MojoFailureException(message); |
73 | |
} |
74 | 0 | type = RollbackType.DATE; |
75 | 0 | } else if (rollbackTag != null) { |
76 | 0 | if (rollbackCount > 0 || rollbackDate != null) { |
77 | 0 | throw new MojoFailureException(message); |
78 | |
} |
79 | 0 | type = RollbackType.TAG; |
80 | |
} |
81 | 0 | } |
82 | |
|
83 | |
@Override |
84 | |
protected void printSettings(String indent) { |
85 | 0 | super.printSettings(indent); |
86 | 0 | getLog().info(indent + "rollback Count: " + rollbackCount); |
87 | 0 | getLog().info(indent + "rollback Date: " + rollbackDate); |
88 | 0 | getLog().info(indent + "rollback Tag: " + rollbackTag); |
89 | 0 | } |
90 | |
|
91 | |
@Override |
92 | |
protected void performLiquibaseTask(Liquibase liquibase) throws LiquibaseException { |
93 | 0 | switch (type) { |
94 | |
case COUNT: { |
95 | 0 | liquibase.rollback(rollbackCount, contexts); |
96 | 0 | break; |
97 | |
} |
98 | |
case DATE: { |
99 | 0 | DateFormat format = DateFormat.getDateInstance(); |
100 | |
try { |
101 | 0 | liquibase.rollback(format.parse(rollbackDate), contexts); |
102 | 0 | } catch (ParseException e) { |
103 | 0 | String message = "Error parsing rollbackDate: " + e.getMessage(); |
104 | 0 | if (format instanceof SimpleDateFormat) { |
105 | 0 | message += "\nDate must match pattern: " + ((SimpleDateFormat) format).toPattern(); |
106 | |
} |
107 | 0 | throw new LiquibaseException(message, e); |
108 | 0 | } |
109 | |
break; |
110 | |
} |
111 | |
case TAG: { |
112 | 0 | liquibase.rollback(rollbackTag, contexts); |
113 | 0 | break; |
114 | |
} |
115 | |
default: { |
116 | 0 | throw new IllegalStateException("Unexpected rollback type, " + type); |
117 | |
} |
118 | |
} |
119 | 0 | } |
120 | |
} |