Merge pull request #3032 from hapifhir/jr-20210924-index-name-npe
fix a null pointer exception thrown during database migration
This commit is contained in:
commit
8c2747f903
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
type: fix
|
||||
issue: 3031
|
||||
jira: SMILE-3178
|
||||
title: "Fixes a bug that was causing a null pointer exception to be thrown during database migrations that add or drop indexes."
|
|
@ -61,6 +61,7 @@ import java.util.List;
|
|||
import java.util.Locale;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class JdbcUtils {
|
||||
private static final Logger ourLog = LoggerFactory.getLogger(JdbcUtils.class);
|
||||
|
@ -86,7 +87,6 @@ public class JdbcUtils {
|
|||
while (indexes.next()) {
|
||||
ourLog.debug("*** Next index: {}", new ColumnMapRowMapper().mapRow(indexes, 0));
|
||||
String indexName = indexes.getString("INDEX_NAME");
|
||||
indexName = indexName.toUpperCase(Locale.US);
|
||||
indexNames.add(indexName);
|
||||
}
|
||||
|
||||
|
@ -94,11 +94,15 @@ public class JdbcUtils {
|
|||
while (indexes.next()) {
|
||||
ourLog.debug("*** Next index: {}", new ColumnMapRowMapper().mapRow(indexes, 0));
|
||||
String indexName = indexes.getString("INDEX_NAME");
|
||||
indexName = indexName.toUpperCase(Locale.US);
|
||||
indexNames.add(indexName);
|
||||
}
|
||||
|
||||
indexNames.removeIf(i -> i == null);
|
||||
indexNames = indexNames
|
||||
.stream()
|
||||
.filter(Objects::nonNull) // filter out the nulls first
|
||||
.map(s -> s.toUpperCase(Locale.US)) // then convert the non-null entries to upper case
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
return indexNames;
|
||||
|
||||
} catch (SQLException e) {
|
||||
|
|
|
@ -10,10 +10,14 @@ import javax.sql.DataSource;
|
|||
import java.sql.Connection;
|
||||
import java.sql.DatabaseMetaData;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.ResultSetMetaData;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Types;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
|
@ -64,4 +68,45 @@ public class JdbcUtilsTest {
|
|||
|
||||
assertEquals(theExpectedColumnType, testColumnType.getColumnTypeEnum());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetIndexNames_verifyNullHandling() throws SQLException {
|
||||
|
||||
// setup
|
||||
ResultSet mockTableResultSet = mock(ResultSet.class);
|
||||
when(mockTableResultSet.next()).thenReturn(true, false);
|
||||
when(mockTableResultSet.getString("TABLE_NAME")).thenReturn("TEST_TABLE");
|
||||
when(mockTableResultSet.getString("TABLE_TYPE")).thenReturn("USER TABLE");
|
||||
|
||||
ResultSetMetaData mockResultSetMetaData = mock(ResultSetMetaData.class);
|
||||
when(mockResultSetMetaData.getColumnCount()).thenReturn(0);
|
||||
|
||||
ResultSet mockIndicesResultSet = mock(ResultSet.class);
|
||||
when(mockIndicesResultSet.next()).thenReturn(true, true, true, false);
|
||||
when(mockIndicesResultSet.getString("INDEX_NAME")).thenReturn("IDX_1", null, "idx_2");
|
||||
when(mockIndicesResultSet.getMetaData()).thenReturn(mockResultSetMetaData);
|
||||
|
||||
ResultSet mockUniqueIndicesResultSet = mock(ResultSet.class);
|
||||
when(mockUniqueIndicesResultSet.next()).thenReturn(true, true, false);
|
||||
when(mockUniqueIndicesResultSet.getString("INDEX_NAME")).thenReturn(null, "Idx_3");
|
||||
when(mockUniqueIndicesResultSet.getMetaData()).thenReturn(mockResultSetMetaData);
|
||||
|
||||
when(myDatabaseMetaData.getTables("Catalog", "Schema", null, null)).thenReturn(mockTableResultSet);
|
||||
when(myDatabaseMetaData.getIndexInfo("Catalog", "Schema", "TEST_TABLE", false, true)).thenReturn(mockIndicesResultSet);
|
||||
when(myDatabaseMetaData.getIndexInfo("Catalog", "Schema", "TEST_TABLE", true, true)).thenReturn(mockUniqueIndicesResultSet);
|
||||
when(myConnection.getMetaData()).thenReturn(myDatabaseMetaData);
|
||||
when(myConnection.getCatalog()).thenReturn("Catalog");
|
||||
when(myConnection.getSchema()).thenReturn("Schema");
|
||||
when(myDataSource.getConnection()).thenReturn(myConnection);
|
||||
DriverTypeEnum.ConnectionProperties myConnectionProperties = DriverTypeEnum.H2_EMBEDDED.newConnectionProperties(myDataSource);
|
||||
|
||||
//execute
|
||||
Set<String> indexNames = JdbcUtils.getIndexNames(myConnectionProperties, "TEST_TABLE");
|
||||
|
||||
// verify
|
||||
assertEquals(3, indexNames.size());
|
||||
assertTrue(indexNames.contains("IDX_1"));
|
||||
assertTrue(indexNames.contains("IDX_2"));
|
||||
assertTrue(indexNames.contains("IDX_3"));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue