More migrator updates

This commit is contained in:
James Agnew 2018-11-01 13:58:09 -04:00
parent e425d19cf6
commit 9906243d2d
3 changed files with 143 additions and 138 deletions

View File

@ -8,8 +8,6 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
import org.springframework.jdbc.datasource.SingleConnectionDataSource;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.support.TransactionTemplate;
@ -18,7 +16,6 @@ import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.SQLException;
import java.util.Properties;
/*-
* #%L
@ -77,13 +74,17 @@ public enum DriverTypeEnum {
throw new InternalErrorException("Unable to find driver class: " + myDriverClassName, e);
}
BasicDataSource dataSource = new BasicDataSource();
// dataSource.setAutoCommit(false);
BasicDataSource dataSource = new BasicDataSource(){
@Override
public Connection getConnection() throws SQLException {
ourLog.info("Creating new DB connection");
return super.getConnection();
}
};
dataSource.setDriverClassName(myDriverClassName);
dataSource.setUrl(theUrl);
dataSource.setUsername(theUsername);
dataSource.setPassword(thePassword);
// dataSource.setSuppressClose(true);
DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
transactionManager.setDataSource(dataSource);

View File

@ -45,56 +45,56 @@ public class JdbcUtils {
public static Set<String> getIndexNames(DriverTypeEnum.ConnectionProperties theConnectionProperties, String theTableName) throws SQLException {
DataSource dataSource = Objects.requireNonNull(theConnectionProperties.getDataSource());
Connection connection = dataSource.getConnection();
return theConnectionProperties.getTxTemplate().execute(t -> {
DatabaseMetaData metadata;
try {
metadata = connection.getMetaData();
ResultSet indexes = metadata.getIndexInfo(null, null, theTableName, false, true);
try (Connection connection = dataSource.getConnection()) {
return theConnectionProperties.getTxTemplate().execute(t -> {
DatabaseMetaData metadata;
try {
metadata = connection.getMetaData();
ResultSet indexes = metadata.getIndexInfo(null, null, theTableName, false, true);
Set<String> indexNames = new HashSet<>();
while (indexes.next()) {
Set<String> indexNames = new HashSet<>();
while (indexes.next()) {
ourLog.debug("*** Next index: {}", new ColumnMapRowMapper().mapRow(indexes, 0));
ourLog.debug("*** Next index: {}", new ColumnMapRowMapper().mapRow(indexes, 0));
String indexName = indexes.getString("INDEX_NAME");
indexName = toUpperCase(indexName, Locale.US);
indexNames.add(indexName);
String indexName = indexes.getString("INDEX_NAME");
indexName = toUpperCase(indexName, Locale.US);
indexNames.add(indexName);
}
return indexNames;
} catch (SQLException e) {
throw new InternalErrorException(e);
}
return indexNames;
} catch (SQLException e) {
throw new InternalErrorException(e);
}
});
});
}
}
@SuppressWarnings("ConstantConditions")
public static boolean isIndexUnique(DriverTypeEnum.ConnectionProperties theConnectionProperties, String theTableName, String theIndexName) throws SQLException {
DataSource dataSource = Objects.requireNonNull(theConnectionProperties.getDataSource());
Connection connection = dataSource.getConnection();
return theConnectionProperties.getTxTemplate().execute(t -> {
DatabaseMetaData metadata;
try {
metadata = connection.getMetaData();
ResultSet indexes = metadata.getIndexInfo(null, null, theTableName, false, false);
try (Connection connection = dataSource.getConnection()) {
return theConnectionProperties.getTxTemplate().execute(t -> {
DatabaseMetaData metadata;
try {
metadata = connection.getMetaData();
ResultSet indexes = metadata.getIndexInfo(null, null, theTableName, false, false);
while (indexes.next()) {
String indexName = indexes.getString("INDEX_NAME");
if (theIndexName.equalsIgnoreCase(indexName)) {
boolean nonUnique = indexes.getBoolean("NON_UNIQUE");
return !nonUnique;
while (indexes.next()) {
String indexName = indexes.getString("INDEX_NAME");
if (theIndexName.equalsIgnoreCase(indexName)) {
boolean nonUnique = indexes.getBoolean("NON_UNIQUE");
return !nonUnique;
}
}
} catch (SQLException e) {
throw new InternalErrorException(e);
}
} catch (SQLException e) {
throw new InternalErrorException(e);
}
throw new InternalErrorException("Can't find index: " + theIndexName + " on table " + theTableName);
});
throw new InternalErrorException("Can't find index: " + theIndexName + " on table " + theTableName);
});
}
}
/**
@ -153,34 +153,35 @@ public class JdbcUtils {
*/
public static Set<String> getForeignKeys(DriverTypeEnum.ConnectionProperties theConnectionProperties, String theTableName, String theForeignTable) throws SQLException {
DataSource dataSource = Objects.requireNonNull(theConnectionProperties.getDataSource());
Connection connection = dataSource.getConnection();
return theConnectionProperties.getTxTemplate().execute(t -> {
DatabaseMetaData metadata;
try {
metadata = connection.getMetaData();
ResultSet indexes = metadata.getCrossReference(null, null, theTableName, null, null, theForeignTable);
try (Connection connection = dataSource.getConnection()) {
return theConnectionProperties.getTxTemplate().execute(t -> {
DatabaseMetaData metadata;
try {
metadata = connection.getMetaData();
ResultSet indexes = metadata.getCrossReference(null, null, theTableName, null, null, theForeignTable);
Set<String> columnNames = new HashSet<>();
while (indexes.next()) {
String tableName = toUpperCase(indexes.getString("PKTABLE_NAME"), Locale.US);
if (!theTableName.equalsIgnoreCase(tableName)) {
continue;
}
tableName = toUpperCase(indexes.getString("FKTABLE_NAME"), Locale.US);
if (!theForeignTable.equalsIgnoreCase(tableName)) {
continue;
Set<String> columnNames = new HashSet<>();
while (indexes.next()) {
String tableName = toUpperCase(indexes.getString("PKTABLE_NAME"), Locale.US);
if (!theTableName.equalsIgnoreCase(tableName)) {
continue;
}
tableName = toUpperCase(indexes.getString("FKTABLE_NAME"), Locale.US);
if (!theForeignTable.equalsIgnoreCase(tableName)) {
continue;
}
String fkName = indexes.getString("FK_NAME");
fkName = toUpperCase(fkName, Locale.US);
columnNames.add(fkName);
}
String fkName = indexes.getString("FK_NAME");
fkName = toUpperCase(fkName, Locale.US);
columnNames.add(fkName);
return columnNames;
} catch (SQLException e) {
throw new InternalErrorException(e);
}
return columnNames;
} catch (SQLException e) {
throw new InternalErrorException(e);
}
});
});
}
}
/**
@ -188,95 +189,96 @@ public class JdbcUtils {
*/
public static Set<String> getColumnNames(DriverTypeEnum.ConnectionProperties theConnectionProperties, String theTableName) throws SQLException {
DataSource dataSource = Objects.requireNonNull(theConnectionProperties.getDataSource());
Connection connection = dataSource.getConnection();
return theConnectionProperties.getTxTemplate().execute(t -> {
DatabaseMetaData metadata;
try {
metadata = connection.getMetaData();
ResultSet indexes = metadata.getColumns(null, null, null, null);
try (Connection connection = dataSource.getConnection()) {
return theConnectionProperties.getTxTemplate().execute(t -> {
DatabaseMetaData metadata;
try {
metadata = connection.getMetaData();
ResultSet indexes = metadata.getColumns(null, null, null, null);
Set<String> columnNames = new HashSet<>();
while (indexes.next()) {
String tableName = toUpperCase(indexes.getString("TABLE_NAME"), Locale.US);
if (!theTableName.equalsIgnoreCase(tableName)) {
continue;
Set<String> columnNames = new HashSet<>();
while (indexes.next()) {
String tableName = toUpperCase(indexes.getString("TABLE_NAME"), Locale.US);
if (!theTableName.equalsIgnoreCase(tableName)) {
continue;
}
String columnName = indexes.getString("COLUMN_NAME");
columnName = toUpperCase(columnName, Locale.US);
columnNames.add(columnName);
}
String columnName = indexes.getString("COLUMN_NAME");
columnName = toUpperCase(columnName, Locale.US);
columnNames.add(columnName);
return columnNames;
} catch (SQLException e) {
throw new InternalErrorException(e);
}
return columnNames;
} catch (SQLException e) {
throw new InternalErrorException(e);
}
});
});
}
}
public static Set<String> getTableNames(DriverTypeEnum.ConnectionProperties theConnectionProperties) throws SQLException {
DataSource dataSource = Objects.requireNonNull(theConnectionProperties.getDataSource());
Connection connection = dataSource.getConnection();
return theConnectionProperties.getTxTemplate().execute(t -> {
DatabaseMetaData metadata;
try {
metadata = connection.getMetaData();
ResultSet tables = metadata.getTables(null, null, null, null);
try (Connection connection = dataSource.getConnection()) {
return theConnectionProperties.getTxTemplate().execute(t -> {
DatabaseMetaData metadata;
try {
metadata = connection.getMetaData();
ResultSet tables = metadata.getTables(null, null, null, null);
Set<String> columnNames = new HashSet<>();
while (tables.next()) {
String tableName = tables.getString("TABLE_NAME");
tableName = toUpperCase(tableName, Locale.US);
Set<String> columnNames = new HashSet<>();
while (tables.next()) {
String tableName = tables.getString("TABLE_NAME");
tableName = toUpperCase(tableName, Locale.US);
String tableType = tables.getString("TABLE_TYPE");
if ("SYSTEM TABLE".equalsIgnoreCase(tableType)) {
continue;
String tableType = tables.getString("TABLE_TYPE");
if ("SYSTEM TABLE".equalsIgnoreCase(tableType)) {
continue;
}
columnNames.add(tableName);
}
columnNames.add(tableName);
return columnNames;
} catch (SQLException e) {
throw new InternalErrorException(e);
}
return columnNames;
} catch (SQLException e) {
throw new InternalErrorException(e);
}
});
});
}
}
public static boolean isColumnNullable(DriverTypeEnum.ConnectionProperties theConnectionProperties, String theTableName, String theColumnName) throws SQLException {
DataSource dataSource = Objects.requireNonNull(theConnectionProperties.getDataSource());
Connection connection = dataSource.getConnection();
//noinspection ConstantConditions
return theConnectionProperties.getTxTemplate().execute(t -> {
DatabaseMetaData metadata;
try {
metadata = connection.getMetaData();
ResultSet tables = metadata.getColumns(null, null, null, null);
try (Connection connection = dataSource.getConnection()) {
//noinspection ConstantConditions
return theConnectionProperties.getTxTemplate().execute(t -> {
DatabaseMetaData metadata;
try {
metadata = connection.getMetaData();
ResultSet tables = metadata.getColumns(null, null, null, null);
while (tables.next()) {
String tableName = toUpperCase(tables.getString("TABLE_NAME"), Locale.US);
if (!theTableName.equalsIgnoreCase(tableName)) {
continue;
}
while (tables.next()) {
String tableName = toUpperCase(tables.getString("TABLE_NAME"), Locale.US);
if (!theTableName.equalsIgnoreCase(tableName)) {
continue;
}
if (theColumnName.equalsIgnoreCase(tables.getString("COLUMN_NAME"))) {
String nullable = tables.getString("IS_NULLABLE");
if ("YES".equalsIgnoreCase(nullable)) {
return true;
} else if ("NO".equalsIgnoreCase(nullable)) {
return false;
} else {
throw new IllegalStateException("Unknown nullable: " + nullable);
if (theColumnName.equalsIgnoreCase(tables.getString("COLUMN_NAME"))) {
String nullable = tables.getString("IS_NULLABLE");
if ("YES".equalsIgnoreCase(nullable)) {
return true;
} else if ("NO".equalsIgnoreCase(nullable)) {
return false;
} else {
throw new IllegalStateException("Unknown nullable: " + nullable);
}
}
}
throw new IllegalStateException("Did not find column " + theColumnName);
} catch (SQLException e) {
throw new InternalErrorException(e);
}
throw new IllegalStateException("Did not find column " + theColumnName);
} catch (SQLException e) {
throw new InternalErrorException(e);
}
});
});
}
}
}

View File

@ -277,7 +277,6 @@ public class HapiFhirJpaMigrationTasks extends BaseMigrationTasks<VersionEnum> {
Builder.BuilderWithTableName spp = version.onTable("HFJ_RES_PARAM_PRESENT");
version.startSectionWithMessage("Starting work on table: " + spp.getTableName());
spp.dropIndex("IDX_RESPARMPRESENT_SPID_RESID");
spp.dropColumn("SP_ID");
spp
.addColumn("HASH_PRESENCE")
.nullable()
@ -307,6 +306,9 @@ public class HapiFhirJpaMigrationTasks extends BaseMigrationTasks<VersionEnum> {
});
version.addTask(consolidateSearchParamPresenceIndexesTask);
// SP_ID is no longer needed
spp.dropColumn("SP_ID");
// Concept
Builder.BuilderWithTableName trmConcept = version.onTable("TRM_CONCEPT");
version.startSectionWithMessage("Starting work on table: " + trmConcept.getTableName());