26
26
import org .apache .ibatis .migration .Change ;
27
27
import org .apache .ibatis .migration .MigrationException ;
28
28
import org .apache .ibatis .migration .operations .DatabaseOperation ;
29
+ import org .apache .ibatis .migration .operations .StatusOperation ;
29
30
import org .apache .ibatis .migration .options .SelectedOptions ;
30
31
31
32
public final class ScriptCommand extends BaseCommand {
@@ -41,23 +42,44 @@ public void execute(String... sparams) {
41
42
throw new MigrationException ("The script command requires a range of versions from v1 - v2." );
42
43
}
43
44
StringTokenizer parser = new StringTokenizer (sparams [0 ]);
44
- if (parser .countTokens () != 2 ) {
45
+ int tokenCount = parser .countTokens ();
46
+ boolean scriptPending = false ;
47
+ boolean scriptPendingUndo = false ;
48
+
49
+ String firstToken = parser .nextToken ();
50
+
51
+ if (tokenCount == 1 && firstToken .equals ("pending" )){
52
+ scriptPending = true ;
53
+ } else if (tokenCount == 1 && firstToken .equals ("pending_undo" )) {
54
+ scriptPendingUndo = true ;
55
+ }
56
+
57
+ else if (!scriptPending && !scriptPendingUndo && tokenCount != 2 ) {
45
58
throw new MigrationException ("The script command requires a range of versions from v1 - v2." );
46
59
}
47
- BigDecimal v1 = new BigDecimal (parser .nextToken ());
48
- BigDecimal v2 = new BigDecimal (parser .nextToken ());
49
- int comparison = v1 .compareTo (v2 );
50
- if (comparison == 0 ) {
51
- throw new MigrationException ("The script command requires two different versions. Use 0 to include the first version." );
60
+
61
+ BigDecimal v1 = (scriptPending || scriptPendingUndo ) ? null : new BigDecimal (firstToken );
62
+ BigDecimal v2 = (scriptPending || scriptPendingUndo ) ? null :new BigDecimal (parser .nextToken ());
63
+
64
+ boolean undo ;
65
+ undo = scriptPendingUndo ;
66
+ if (!scriptPending && !scriptPendingUndo ) {
67
+ int comparison = v1 .compareTo (v2 );
68
+ if (comparison == 0 ) {
69
+ throw new MigrationException ("The script command requires two different versions. Use 0 to include the first version." );
70
+ }
71
+ undo = comparison > 0 ;
52
72
}
53
- boolean undo = comparison > 0 ;
54
- List <Change > migrations = getMigrationLoader ().getMigrations ();
73
+
74
+ List <Change > migrations = (scriptPending || scriptPendingUndo ) ?
75
+ new StatusOperation ().operate (getConnectionProvider (), getMigrationLoader (), getDatabaseOperationOption (), null ).getCurrentStatus () :
76
+ getMigrationLoader ().getMigrations ();
55
77
Collections .sort (migrations );
56
78
if (undo ) {
57
79
Collections .reverse (migrations );
58
80
}
59
81
for (Change change : migrations ) {
60
- if (shouldRun (change , v1 , v2 )) {
82
+ if (shouldRun (change , v1 , v2 , scriptPending || scriptPendingUndo )) {
61
83
printStream .println ("-- " + change .getFilename ());
62
84
Reader migrationReader = getMigrationLoader ().getScriptReader (change , undo );
63
85
char [] cbuf = new char [1024 ];
@@ -91,12 +113,17 @@ private String generateVersionDelete(Change change) {
91
113
return "DELETE FROM " + changelogTable () + " WHERE ID = " + change .getId () + getDelimiter ();
92
114
}
93
115
94
- private boolean shouldRun (Change change , BigDecimal v1 , BigDecimal v2 ) {
95
- BigDecimal id = change .getId ();
96
- if (v1 .compareTo (v2 ) > 0 ) {
97
- return (id .compareTo (v2 ) > 0 && id .compareTo (v1 ) <= 0 );
98
- } else {
99
- return (id .compareTo (v1 ) > 0 && id .compareTo (v2 ) <= 0 );
116
+ private boolean shouldRun (Change change , BigDecimal v1 , BigDecimal v2 , boolean pendingOnly ) {
117
+ if (!pendingOnly ) {
118
+ BigDecimal id = change .getId ();
119
+ if (v1 .compareTo (v2 ) > 0 ) {
120
+ return (id .compareTo (v2 ) > 0 && id .compareTo (v1 ) <= 0 );
121
+ } else {
122
+ return (id .compareTo (v1 ) > 0 && id .compareTo (v2 ) <= 0 );
123
+ }
124
+ }
125
+ else {
126
+ return change .isPending ();
100
127
}
101
128
}
102
129
0 commit comments