Skip to content

Commit 9b1f1fb

Browse files
authored
Merge pull request #161 from harawata/version-without-changelog
Version command should not require changelog table
2 parents 4f36b06 + aeeb5e0 commit 9b1f1fb

File tree

6 files changed

+147
-2
lines changed

6 files changed

+147
-2
lines changed

src/main/java/org/apache/ibatis/migration/operations/VersionOperation.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.math.BigDecimal;
2020
import java.sql.Connection;
2121
import java.sql.SQLException;
22+
import java.util.Collections;
2223
import java.util.List;
2324

2425
import org.apache.ibatis.migration.Change;
@@ -50,7 +51,7 @@ public VersionOperation operate(ConnectionProvider connectionProvider, Migration
5051
option = new DatabaseOperationOption();
5152
}
5253
try (Connection con = connectionProvider.getConnection()) {
53-
List<Change> changesInDb = getChangelog(con, option);
54+
List<Change> changesInDb = changelogExists(con, option) ? getChangelog(con, option) : Collections.emptyList();
5455
List<Change> migrations = migrationsLoader.getMigrations();
5556
Change specified = new Change(version);
5657
if (!migrations.contains(specified)) {
@@ -61,7 +62,7 @@ public VersionOperation operate(ConnectionProvider connectionProvider, Migration
6162
println(printStream, "Upgrading to: " + version);
6263
int steps = 0;
6364
for (Change change : migrations) {
64-
if (change.compareTo(lastChangeInDb) > 0 && change.compareTo(specified) < 1) {
65+
if ((lastChangeInDb == null || change.compareTo(lastChangeInDb) > 0) && change.compareTo(specified) < 1) {
6566
steps++;
6667
}
6768
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Copyright 2010-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package it.version_without_changelog;
17+
18+
import static org.junit.Assert.*;
19+
20+
import java.io.File;
21+
22+
import org.apache.ibatis.io.Resources;
23+
import org.apache.ibatis.migration.Migrator;
24+
import org.apache.ibatis.migration.utils.TestUtil;
25+
import org.junit.Rule;
26+
import org.junit.Test;
27+
import org.junit.contrib.java.lang.system.SystemOutRule;
28+
29+
public class VersionWithoutChangeLogTest {
30+
@Rule
31+
public final SystemOutRule out = new SystemOutRule().enableLog();
32+
33+
@Test
34+
public void shouldUpToVersionEvenWithoutChangelog() throws Exception {
35+
// gh-160
36+
File dir = Resources.getResourceAsFile("it/version_without_changelog");
37+
Migrator.main(TestUtil.args("--path=" + dir.getAbsolutePath(), "version", "20000101000001"));
38+
String output = out.getLog();
39+
assertFalse(output.contains("FAILURE"));
40+
assertTrue(output.contains("20000101000000"));
41+
assertTrue(output.contains("20000101000001"));
42+
assertFalse(output.contains("20000101000002"));
43+
}
44+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#
2+
# Copyright 2010-2020 the original author or authors.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
17+
driver=org.hsqldb.jdbcDriver
18+
url=jdbc:hsqldb:mem:version_without_changelog
19+
username=sa
20+
password=
21+
changelog=CHANGELOG
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
--
2+
-- Copyright 2010-2020 the original author or authors.
3+
--
4+
-- Licensed under the Apache License, Version 2.0 (the "License");
5+
-- you may not use this file except in compliance with the License.
6+
-- You may obtain a copy of the License at
7+
--
8+
-- http://www.apache.org/licenses/LICENSE-2.0
9+
--
10+
-- Unless required by applicable law or agreed to in writing, software
11+
-- distributed under the License is distributed on an "AS IS" BASIS,
12+
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
-- See the License for the specific language governing permissions and
14+
-- limitations under the License.
15+
--
16+
17+
CREATE TABLE ${changelog} (
18+
ID NUMERIC(20,0) NOT NULL,
19+
APPLIED_AT VARCHAR(25) NOT NULL,
20+
DESCRIPTION VARCHAR(255) NOT NULL
21+
);
22+
23+
ALTER TABLE ${changelog}
24+
ADD CONSTRAINT PK_${changelog}
25+
PRIMARY KEY (id);
26+
27+
-- //@UNDO
28+
29+
DROP TABLE ${changelog};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--
2+
-- Copyright 2010-2020 the original author or authors.
3+
--
4+
-- Licensed under the Apache License, Version 2.0 (the "License");
5+
-- you may not use this file except in compliance with the License.
6+
-- You may obtain a copy of the License at
7+
--
8+
-- http://www.apache.org/licenses/LICENSE-2.0
9+
--
10+
-- Unless required by applicable law or agreed to in writing, software
11+
-- distributed under the License is distributed on an "AS IS" BASIS,
12+
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
-- See the License for the specific language governing permissions and
14+
-- limitations under the License.
15+
--
16+
17+
create table test1 (
18+
id int primary key,
19+
name varchar(10)
20+
);
21+
22+
-- //@UNDO
23+
24+
drop table test1;
25+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--
2+
-- Copyright 2010-2020 the original author or authors.
3+
--
4+
-- Licensed under the Apache License, Version 2.0 (the "License");
5+
-- you may not use this file except in compliance with the License.
6+
-- You may obtain a copy of the License at
7+
--
8+
-- http://www.apache.org/licenses/LICENSE-2.0
9+
--
10+
-- Unless required by applicable law or agreed to in writing, software
11+
-- distributed under the License is distributed on an "AS IS" BASIS,
12+
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
-- See the License for the specific language governing permissions and
14+
-- limitations under the License.
15+
--
16+
17+
create table test2 (
18+
id int primary key,
19+
name varchar(10)
20+
);
21+
22+
-- //@UNDO
23+
24+
drop table test2;
25+

0 commit comments

Comments
 (0)