Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Optional;

/**
* Created by takezoe on 15/11/23.
Expand Down Expand Up @@ -37,6 +38,15 @@ public String getCurrentVersion(String moduleId) throws Exception {
return selectStringFromDatabase(conn, "SELECT VERSION FROM VERSIONS WHERE MODULE_ID = ?", moduleId);
}

@Override
public Optional<String> findCurrentVersion(String moduleId) throws Exception {
if (!checkTableExist()) {
return Optional.empty();
} else {
return Optional.ofNullable(getCurrentVersion(moduleId));
}
}

protected boolean checkTableExist(){
try {
ResultSet rs = conn.getMetaData().getTables(null, null, "%", new String[]{ "TABLE" });
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.github.gitbucket.solidbase.manager;

import java.util.Optional;

public interface VersionManager {

void initialize() throws Exception;
Expand All @@ -8,4 +10,6 @@ public interface VersionManager {

String getCurrentVersion(String moduleId) throws Exception;

Optional<String> findCurrentVersion(String moduleId) throws Exception;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package io.github.gitbucket.solidbase.manager;

import org.junit.Test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Optional;

import static org.junit.Assert.*;

public class JDBCVersionManagerTest {

@Test(expected = Exception.class)
public void testGetCurrentVersionThrowsWhenVersionsTableIsMissing() throws Exception {
try (Connection conn = DriverManager.getConnection("jdbc:h2:mem:test-getCurrentVersion-missing-table", "sa", "sa")) {
JDBCVersionManager manager = new JDBCVersionManager(conn);
manager.getCurrentVersion("test");
}
}

@Test
public void testFindCurrentVersionIsEmptyWhenVersionsTableIsMissing() throws Exception {
try (Connection conn = DriverManager.getConnection("jdbc:h2:mem:test-findCurrentVersion-missing-table", "sa", "sa")) {
JDBCVersionManager manager = new JDBCVersionManager(conn);

assertEquals(Optional.empty(), manager.findCurrentVersion("test"));
}
}

@Test
public void testFindCurrentVersionReturnsVersionWhenVersionsTableIsPresent() throws Exception {
try (Connection conn = DriverManager.getConnection("jdbc:h2:mem:test-findCurrentVersion-present-table", "sa", "sa")) {
JDBCVersionManager manager = new JDBCVersionManager(conn);
manager.initialize();
manager.updateVersion("test", "1.0.0");

assertEquals(Optional.of("1.0.0"), manager.findCurrentVersion("test"));
}
}
}