Skip to content

Commit a30d861

Browse files
rvbaastharawata
authored andcommitted
Added support for generating pending only scripts (#29)
* Added support for generating pending only scripts eg: migrate script pending migrate script pending_undo
1 parent 4cdaf58 commit a30d861

File tree

3 files changed

+70
-16
lines changed

3 files changed

+70
-16
lines changed

src/main/java/org/apache/ibatis/migration/Change.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,12 @@ public void setFilename(String filename) {
7171

7272
@Override
7373
public String toString() {
74-
return id + " " + (appliedTimestamp == null ? " ...pending... " : appliedTimestamp) + " " + description;
74+
return id + " " + (isPending() ? " ...pending... " : appliedTimestamp) + " " + description;
75+
}
76+
77+
public boolean isPending()
78+
{
79+
return appliedTimestamp == null;
7580
}
7681

7782
@Override

src/main/java/org/apache/ibatis/migration/commands/ScriptCommand.java

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.apache.ibatis.migration.Change;
2727
import org.apache.ibatis.migration.MigrationException;
2828
import org.apache.ibatis.migration.operations.DatabaseOperation;
29+
import org.apache.ibatis.migration.operations.StatusOperation;
2930
import org.apache.ibatis.migration.options.SelectedOptions;
3031

3132
public final class ScriptCommand extends BaseCommand {
@@ -41,23 +42,44 @@ public void execute(String... sparams) {
4142
throw new MigrationException("The script command requires a range of versions from v1 - v2.");
4243
}
4344
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) {
4558
throw new MigrationException("The script command requires a range of versions from v1 - v2.");
4659
}
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;
5272
}
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();
5577
Collections.sort(migrations);
5678
if (undo) {
5779
Collections.reverse(migrations);
5880
}
5981
for (Change change : migrations) {
60-
if (shouldRun(change, v1, v2)) {
82+
if (shouldRun(change, v1, v2, scriptPending || scriptPendingUndo)) {
6183
printStream.println("-- " + change.getFilename());
6284
Reader migrationReader = getMigrationLoader().getScriptReader(change, undo);
6385
char[] cbuf = new char[1024];
@@ -91,12 +113,17 @@ private String generateVersionDelete(Change change) {
91113
return "DELETE FROM " + changelogTable() + " WHERE ID = " + change.getId() + getDelimiter();
92114
}
93115

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();
100127
}
101128
}
102129

src/test/java/org/apache/ibatis/migration/MigratorTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.net.URL;
3232
import java.security.Permission;
3333
import java.sql.Connection;
34+
import java.sql.DriverManager;
3435
import java.sql.SQLException;
3536
import java.util.Map;
3637
import java.util.Properties;
@@ -86,10 +87,28 @@ public void checkExit(int status) {
8687

8788
@AfterClass
8889
public static void teardown() {
90+
91+
8992
System.setOut(out);
9093
System.setSecurityManager(null);
9194
}
9295

96+
private void testDoPendingScriptCommand(File f) throws Exception
97+
{
98+
99+
safeMigratorMain(args("--path=" + f.getAbsolutePath(), "script", "pending"));
100+
assertTrue(buffer.toString().contains("INSERT"));
101+
assertTrue(buffer.toString().contains("CHANGELOG"));
102+
assertFalse(buffer.toString().contains("-- @UNDO"));
103+
buffer.clear();
104+
105+
safeMigratorMain(args("--path=" + f.getAbsolutePath(), "script", "pending_undo"));
106+
assertTrue(buffer.toString().contains("DELETE"));
107+
assertTrue(buffer.toString().contains("CHANGELOG"));
108+
assertTrue(buffer.toString().contains("-- @UNDO"));
109+
buffer.clear();
110+
}
111+
93112
@Test
94113
public void shouldRunThroughFullMigrationUseCaseInOneTestToEnsureOrder() throws Throwable {
95114
try {
@@ -107,6 +126,9 @@ public void shouldRunThroughFullMigrationUseCaseInOneTestToEnsureOrder() throws
107126
testStatusContainsNoPendingMigrations(f);
108127
testDownCommandGiven2Steps(f);
109128
testStatusContainsPendingMigrations(f);
129+
130+
testDoPendingScriptCommand(f);
131+
110132
testVersionCommand(f);
111133
testStatusContainsNoPendingMigrations(f);
112134
testDownCommand(f);

0 commit comments

Comments
 (0)