Skip to content
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

Improve status command with missing migrations #145

Merged
merged 5 commits into from
Jun 22, 2019
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
16 changes: 16 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
#
# Copyright 2010-2019 the original author or authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

FROM openjdk:8
RUN ["mkdir", "-p", "/opt/migrations"]

Expand Down
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
@@ -1,5 +1,5 @@
/**
* Copyright 2010-2017 the original author or authors.
* Copyright 2010-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -37,6 +37,11 @@ public Change(BigDecimal id, String appliedTimestamp, String description) {
this.description = description;
}

protected Change(Change toCopy) {
this(toCopy.getId(), toCopy.getAppliedTimestamp(), toCopy.getDescription());
this.filename = toCopy.getFilename();
}

public BigDecimal getId() {
return id;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2010-2017 the original author or authors.
* Copyright 2010-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,7 +18,9 @@
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.ibatis.migration.Change;
import org.apache.ibatis.migration.ConnectionProvider;
Expand All @@ -27,9 +29,10 @@
import org.apache.ibatis.migration.utils.Util;

public final class StatusOperation extends DatabaseOperation {
private int applied;

private int applied;
private int pending;
private int missing;

private List<Change> changes;

Expand All @@ -42,27 +45,40 @@ public StatusOperation operate(ConnectionProvider connectionProvider, MigrationL
println(printStream, Util.horizontalLine("", 80));
changes = new ArrayList<Change>();
List<Change> migrations = migrationsLoader.getMigrations();
List<Change> changelog = null;
if (changelogExists(connectionProvider, option)) {
List<Change> changelog = getChangelog(connectionProvider, option);
for (Change change : migrations) {
int index = changelog.indexOf(change);
if (index > -1) {
changes.add(changelog.get(index));
changelog = getChangelog(connectionProvider, option);

Set<Change> changelogAndMigrations = new HashSet<Change>();
changelogAndMigrations.addAll(changelog);
changelogAndMigrations.addAll(migrations);

for (Change change : changelogAndMigrations) {
if (!migrations.contains(change)) {
change = new MissingScript(change);
missing++;
} else if (change.getAppliedTimestamp() != null) {
applied++;
} else {
changes.add(change);
pending++;
}
changes.add(change);
}
} else {
changes.addAll(migrations);
pending = migrations.size();
}

Collections.sort(changes);
for (Change change : changes) {
println(printStream, change.toString());
}
println(printStream);

if (changelog != null) {
checkSkippedOrMissing(changelog, migrations, printStream);
}

return this;
}

Expand All @@ -74,7 +90,22 @@ public int getPendingCount() {
return pending;
}

public int getMissingCount() {
return missing;
}

public List<Change> getCurrentStatus() {
return changes;
}

class MissingScript extends Change {
public MissingScript(Change change) {
super(change);
}

@Override
public String toString() {
return super.toString() + " <=== MISSING!";
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2010-2017 the original author or authors.
* Copyright 2010-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -56,12 +56,16 @@ public class RuntimeMigrationTest {

private MigrationLoader migrationsLoader;

private MigrationLoader migrationsLoaderFromOtherBranch;

@Before
public void setup() throws Exception {
connectionProvider = new JdbcConnectionProvider("org.hsqldb.jdbcDriver", "jdbc:hsqldb:mem:javaapitest", "sa", "");
dbOption = new DatabaseOperationOption();
out = new ByteArrayOutputStream();
migrationsLoader = createMigrationsLoader();
migrationsLoader = createMigrationsLoader("org/apache/ibatis/migration/runtime_migration/scripts");
migrationsLoaderFromOtherBranch = createMigrationsLoader(
"org/apache/ibatis/migration/runtime_migration/scripts_from_other_branch");
}

@After
Expand Down Expand Up @@ -106,6 +110,23 @@ public void testUp() throws Exception {
assertEquals(3, status.getCurrentStatus().size());
}

@Test
public void testUpFromDifferentBranches() throws Exception {
new UpOperation().operate(connectionProvider, migrationsLoader, dbOption, new PrintStream(out));
new UpOperation().operate(connectionProvider, migrationsLoaderFromOtherBranch, dbOption, new PrintStream(out));
assertEquals("4", runQuery(connectionProvider, "select count(*) from changelog"));
assertEquals("0", runQuery(connectionProvider, "select count(*) from first_table"));
assertEquals("0", runQuery(connectionProvider, "select count(*) from second_table"));
assertEquals("0", runQuery(connectionProvider, "select count(*) from third_table"));

StatusOperation status = new StatusOperation().operate(connectionProvider, migrationsLoader, dbOption,
new PrintStream(out));
assertEquals(3, status.getAppliedCount());
assertEquals(0, status.getPendingCount());
assertEquals(1, status.getMissingCount());
assertEquals(4, status.getCurrentStatus().size());
}

@Test
public void testUpWithStep() throws Exception {
new UpOperation(2).operate(connectionProvider, migrationsLoader, dbOption, new PrintStream(out));
Expand Down Expand Up @@ -217,8 +238,8 @@ protected void assertTableDoesNotExist(ConnectionProvider connectionProvider, St
}
}

protected FileMigrationLoader createMigrationsLoader() {
URL url = getClass().getClassLoader().getResource("org/apache/ibatis/migration/runtime_migration/scripts");
protected FileMigrationLoader createMigrationsLoader(String resource) {
URL url = getClass().getClassLoader().getResource(resource);
File scriptsDir = new File(url.getFile());
Properties properties = new Properties();
properties.setProperty("changelog", "CHANGELOG");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
--
-- Copyright 2010-2019 the original author or authors.
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--

-- // Second migration.
-- Migration SQL that makes the change goes here.

CREATE TABLE third_table (
ID INTEGER NOT NULL,
NAME VARCHAR(16)
);


-- //@UNDO
-- SQL to undo the change goes here.

DROP TABLE third_table;