Skip to content

Commit 9a7cff3

Browse files
committed
fixes #3 The lower version of the script command should be exclusive.
1 parent 407b533 commit 9a7cff3

File tree

3 files changed

+50
-9
lines changed

3 files changed

+50
-9
lines changed

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

+7-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ public void execute(String... sparams) {
3131
}
3232
BigDecimal v1 = new BigDecimal(parser.nextToken());
3333
BigDecimal v2 = new BigDecimal(parser.nextToken());
34-
boolean undo = v1.compareTo(v2) > 0;
34+
int comparison = v1.compareTo(v2);
35+
if (comparison == 0) {
36+
throw new MigrationException("The script command requires two different versions. Use 0 to include the first version.");
37+
}
38+
boolean undo = comparison > 0;
3539
Properties variables = environmentProperties();
3640
List<Change> migrations = getMigrations();
3741
Collections.sort(migrations);
@@ -77,9 +81,9 @@ private String generateVersionDelete(Change change) {
7781
private boolean shouldRun(Change change, BigDecimal v1, BigDecimal v2) {
7882
BigDecimal id = change.getId();
7983
if (v1.compareTo(v2) > 0) {
80-
return (id.compareTo(v2) >= 0 && id.compareTo(v1) <= 0);
84+
return (id.compareTo(v2) > 0 && id.compareTo(v1) <= 0);
8185
} else {
82-
return (id.compareTo(v1) >= 0 && id.compareTo(v2) <= 0);
86+
return (id.compareTo(v1) > 0 && id.compareTo(v2) <= 0);
8387
}
8488
}
8589

src/site/xdoc/script.xml

+12-4
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@
4040
central development databases!).</p>
4141

4242
<p>The script command is quite simple. It takes two version numbers as parameters. Think of it as generating a
43-
script to apply all of the identified versions (inclusive) in the order specified.
43+
script to apply all of the identified versions in the order specified (the lower version is exclusive and the
44+
higher version is inclusive).
4445
For example, given the following:</p>
4546

4647
<source>/home/cbegin/testdb$ migrate status
@@ -51,15 +52,22 @@ ID Applied At Description
5152
20090804225328 2009-08-05 24:55:23 create blog table
5253
20090804225333 2009-08-04 22:51:17 create post table</source>
5354

54-
<p>If we need to generate a “do” script to apply the two highlighted versions above, we’d run the following command:</p>
55+
<p>If we need to generate a “do” script to apply the last two versions above, we’d run the following command:</p>
5556

56-
<source>/home/cbegin/testdb$ migrate script 20090804225328 20090804225333 > do.sql</source>
57+
<source>/home/cbegin/testdb$ migrate script 20090804225207 20090804225333 > do.sql</source>
5758

5859
<p>The script command outputs to stdout, so you can let it print to the console, or pipe it to a file or command.</p>
5960

6061
<p>To generate the corresponding “undo” script, simply specify the version numbers in the opposite order:</p>
6162

62-
<source>/home/cbegin/testdb$ migrate script 20090804225333 20090804225328 > undo.sql</source>
63+
<source>/home/cbegin/testdb$ migrate script 20090804225333 20090804225207 > undo.sql</source>
64+
65+
<p>To generate a script which includes the first version, specify '0' as the lower version:</p>
66+
67+
<source>/home/cbegin/testdb$ migrate script 0 20090804225207 > do.sql</source>
68+
69+
<source>/home/cbegin/testdb$ migrate script 20090804225207 0 > undo.sql</source>
70+
6371
</section>
6472
</body>
6573

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

+31-2
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ public void shouldRunThroughFullMigrationUseCaseInOneTestToEnsureOrder() throws
101101
testHelpCommand(f);
102102
testDoScriptCommand(f);
103103
testUndoScriptCommand(f);
104+
testScriptCommandWithTheSameVersion(f);
104105

105106
} catch (Throwable t) {
106107
System.err.println(buffer);
@@ -188,12 +189,23 @@ private void testDoScriptCommand(File f) throws Exception {
188189
assertFalse(buffer.toString().contains("FAILURE"));
189190
assertFalse(buffer.toString().contains("20080827200210"));
190191
assertFalse(buffer.toString().contains("20080827200211"));
191-
assertTrue(buffer.toString().contains("20080827200212"));
192+
assertFalse(buffer.toString().contains("20080827200212"));
192193
assertTrue(buffer.toString().contains("20080827200213"));
193194
assertTrue(buffer.toString().contains("20080827200214"));
194195
assertFalse(buffer.toString().contains("20080827200215"));
195196
assertFalse(buffer.toString().contains("-- @UNDO"));
196197
buffer.clear();
198+
199+
safeMigratorMain(args("--path=" + f.getAbsolutePath(), "script", "0", "20080827200211"));
200+
assertFalse(buffer.toString().contains("FAILURE"));
201+
assertTrue(buffer.toString().contains("20080827200210"));
202+
assertTrue(buffer.toString().contains("20080827200211"));
203+
assertFalse(buffer.toString().contains("20080827200212"));
204+
assertFalse(buffer.toString().contains("20080827200213"));
205+
assertFalse(buffer.toString().contains("20080827200214"));
206+
assertFalse(buffer.toString().contains("20080827200215"));
207+
assertFalse(buffer.toString().contains("-- @UNDO"));
208+
buffer.clear();
197209
}
198210

199211
private void testUndoScriptCommand(File f) throws Exception {
@@ -202,11 +214,28 @@ private void testUndoScriptCommand(File f) throws Exception {
202214
assertFalse(buffer.toString().contains("20080827200210"));
203215
assertFalse(buffer.toString().contains("20080827200211"));
204216
assertFalse(buffer.toString().contains("20080827200212"));
205-
assertTrue(buffer.toString().contains("20080827200213"));
217+
assertFalse(buffer.toString().contains("20080827200213"));
206218
assertTrue(buffer.toString().contains("20080827200214"));
207219
assertTrue(buffer.toString().contains("20080827200215"));
208220
assertTrue(buffer.toString().contains("-- @UNDO"));
209221
buffer.clear();
222+
223+
safeMigratorMain(args("--path=" + f.getAbsolutePath(), "script", "20080827200211", "0"));
224+
assertFalse(buffer.toString().contains("FAILURE"));
225+
assertTrue(buffer.toString().contains("20080827200210"));
226+
assertTrue(buffer.toString().contains("20080827200211"));
227+
assertFalse(buffer.toString().contains("20080827200212"));
228+
assertFalse(buffer.toString().contains("20080827200213"));
229+
assertFalse(buffer.toString().contains("20080827200214"));
230+
assertFalse(buffer.toString().contains("20080827200215"));
231+
assertTrue(buffer.toString().contains("-- @UNDO"));
232+
buffer.clear();
233+
}
234+
235+
private void testScriptCommandWithTheSameVersion(File f) throws Exception {
236+
safeMigratorMain(args("--path=" + f.getAbsolutePath(), "script", "20080827200211", "20080827200211"));
237+
assertTrue(buffer.toString().contains("FAILURE"));
238+
buffer.clear();
210239
}
211240

212241
private void safeMigratorMain(String[] args) throws Exception {

0 commit comments

Comments
 (0)