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

View File

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

@ -9,9 +9,9 @@ package ca.uhn.fhir.jpa.migrate.tasks;
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -277,7 +277,6 @@ public class HapiFhirJpaMigrationTasks extends BaseMigrationTasks<VersionEnum> {
Builder.BuilderWithTableName spp = version.onTable("HFJ_RES_PARAM_PRESENT"); Builder.BuilderWithTableName spp = version.onTable("HFJ_RES_PARAM_PRESENT");
version.startSectionWithMessage("Starting work on table: " + spp.getTableName()); version.startSectionWithMessage("Starting work on table: " + spp.getTableName());
spp.dropIndex("IDX_RESPARMPRESENT_SPID_RESID"); spp.dropIndex("IDX_RESPARMPRESENT_SPID_RESID");
spp.dropColumn("SP_ID");
spp spp
.addColumn("HASH_PRESENCE") .addColumn("HASH_PRESENCE")
.nullable() .nullable()
@ -307,6 +306,9 @@ public class HapiFhirJpaMigrationTasks extends BaseMigrationTasks<VersionEnum> {
}); });
version.addTask(consolidateSearchParamPresenceIndexesTask); version.addTask(consolidateSearchParamPresenceIndexesTask);
// SP_ID is no longer needed
spp.dropColumn("SP_ID");
// Concept // Concept
Builder.BuilderWithTableName trmConcept = version.onTable("TRM_CONCEPT"); Builder.BuilderWithTableName trmConcept = version.onTable("TRM_CONCEPT");
version.startSectionWithMessage("Starting work on table: " + trmConcept.getTableName()); version.startSectionWithMessage("Starting work on table: " + trmConcept.getTableName());