Skip to content

Added support for generating pending only scripts #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 11, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/main/java/org/apache/ibatis/migration/Change.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,12 @@ public void setFilename(String filename) {

@Override
public String toString() {
return id + " " + (appliedTimestamp == null ? " ...pending... " : appliedTimestamp) + " " + description;
return id + " " + (isPending() ? " ...pending... " : appliedTimestamp) + " " + description;
}

public boolean isPending()
{
return appliedTimestamp == null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.ibatis.migration.Change;
import org.apache.ibatis.migration.MigrationException;
import org.apache.ibatis.migration.operations.DatabaseOperation;
import org.apache.ibatis.migration.operations.StatusOperation;
import org.apache.ibatis.migration.options.SelectedOptions;

public final class ScriptCommand extends BaseCommand {
Expand All @@ -41,23 +42,44 @@ public void execute(String... sparams) {
throw new MigrationException("The script command requires a range of versions from v1 - v2.");
}
StringTokenizer parser = new StringTokenizer(sparams[0]);
if (parser.countTokens() != 2) {
int tokenCount = parser.countTokens();
boolean scriptPending = false;
boolean scriptPendingUndo = false;

String firstToken = parser.nextToken();

if(tokenCount == 1 && firstToken.equals("pending")){
scriptPending = true;
} else if(tokenCount == 1 && firstToken.equals("pending_undo")) {
scriptPendingUndo = true;
}

else if (!scriptPending&& !scriptPendingUndo && tokenCount != 2) {
throw new MigrationException("The script command requires a range of versions from v1 - v2.");
}
BigDecimal v1 = new BigDecimal(parser.nextToken());
BigDecimal v2 = new BigDecimal(parser.nextToken());
int comparison = v1.compareTo(v2);
if (comparison == 0) {
throw new MigrationException("The script command requires two different versions. Use 0 to include the first version.");

BigDecimal v1 = (scriptPending || scriptPendingUndo) ? null : new BigDecimal(firstToken);
BigDecimal v2 = (scriptPending || scriptPendingUndo) ? null :new BigDecimal(parser.nextToken());

boolean undo;
undo = scriptPendingUndo;
if(!scriptPending && !scriptPendingUndo) {
int comparison = v1.compareTo(v2);
if (comparison == 0) {
throw new MigrationException("The script command requires two different versions. Use 0 to include the first version.");
}
undo = comparison > 0;
}
boolean undo = comparison > 0;
List<Change> migrations = getMigrationLoader().getMigrations();

List<Change> migrations = (scriptPending || scriptPendingUndo) ?
new StatusOperation().operate(getConnectionProvider(), getMigrationLoader(), getDatabaseOperationOption(), null).getCurrentStatus() :
getMigrationLoader().getMigrations();
Collections.sort(migrations);
if (undo) {
Collections.reverse(migrations);
}
for (Change change : migrations) {
if (shouldRun(change, v1, v2)) {
if (shouldRun(change, v1, v2, scriptPending || scriptPendingUndo)) {
printStream.println("-- " + change.getFilename());
Reader migrationReader = getMigrationLoader().getScriptReader(change, undo);
char[] cbuf = new char[1024];
Expand Down Expand Up @@ -91,12 +113,17 @@ private String generateVersionDelete(Change change) {
return "DELETE FROM " + changelogTable() + " WHERE ID = " + change.getId() + getDelimiter();
}

private boolean shouldRun(Change change, BigDecimal v1, BigDecimal v2) {
BigDecimal id = change.getId();
if (v1.compareTo(v2) > 0) {
return (id.compareTo(v2) > 0 && id.compareTo(v1) <= 0);
} else {
return (id.compareTo(v1) > 0 && id.compareTo(v2) <= 0);
private boolean shouldRun(Change change, BigDecimal v1, BigDecimal v2, boolean pendingOnly) {
if(!pendingOnly) {
BigDecimal id = change.getId();
if (v1.compareTo(v2) > 0) {
return (id.compareTo(v2) > 0 && id.compareTo(v1) <= 0);
} else {
return (id.compareTo(v1) > 0 && id.compareTo(v2) <= 0);
}
}
else {
return change.isPending();
}
}

Expand Down
22 changes: 22 additions & 0 deletions src/test/java/org/apache/ibatis/migration/MigratorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.net.URL;
import java.security.Permission;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Map;
import java.util.Properties;
Expand Down Expand Up @@ -86,10 +87,28 @@ public void checkExit(int status) {

@AfterClass
public static void teardown() {


System.setOut(out);
System.setSecurityManager(null);
}

private void testDoPendingScriptCommand(File f) throws Exception
{

safeMigratorMain(args("--path=" + f.getAbsolutePath(), "script", "pending"));
assertTrue(buffer.toString().contains("INSERT"));
assertTrue(buffer.toString().contains("CHANGELOG"));
assertFalse(buffer.toString().contains("-- @UNDO"));
buffer.clear();

safeMigratorMain(args("--path=" + f.getAbsolutePath(), "script", "pending_undo"));
assertTrue(buffer.toString().contains("DELETE"));
assertTrue(buffer.toString().contains("CHANGELOG"));
assertTrue(buffer.toString().contains("-- @UNDO"));
buffer.clear();
}

@Test
public void shouldRunThroughFullMigrationUseCaseInOneTestToEnsureOrder() throws Throwable {
try {
Expand All @@ -107,6 +126,9 @@ public void shouldRunThroughFullMigrationUseCaseInOneTestToEnsureOrder() throws
testStatusContainsNoPendingMigrations(f);
testDownCommandGiven2Steps(f);
testStatusContainsPendingMigrations(f);

testDoPendingScriptCommand(f);

testVersionCommand(f);
testStatusContainsNoPendingMigrations(f);
testDownCommand(f);
Expand Down