From 9906243d2db95bbbd2ac05f34b1e22bf05bd7ee0 Mon Sep 17 00:00:00 2001 From: James Agnew Date: Thu, 1 Nov 2018 13:58:09 -0400 Subject: [PATCH] More migrator updates --- .../uhn/fhir/jpa/migrate/DriverTypeEnum.java | 13 +- .../ca/uhn/fhir/jpa/migrate/JdbcUtils.java | 260 +++++++++--------- .../tasks/HapiFhirJpaMigrationTasks.java | 8 +- 3 files changed, 143 insertions(+), 138 deletions(-) diff --git a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/DriverTypeEnum.java b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/DriverTypeEnum.java index c3d8a8725ba..98e4cd3a5b7 100644 --- a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/DriverTypeEnum.java +++ b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/DriverTypeEnum.java @@ -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); diff --git a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/JdbcUtils.java b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/JdbcUtils.java index 4a23cb61366..0b800dbb344 100644 --- a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/JdbcUtils.java +++ b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/JdbcUtils.java @@ -45,56 +45,56 @@ public class JdbcUtils { public static Set 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 indexNames = new HashSet<>(); - while (indexes.next()) { + Set 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 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 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 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 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 columnNames = new HashSet<>(); - while (indexes.next()) { - String tableName = toUpperCase(indexes.getString("TABLE_NAME"), Locale.US); - if (!theTableName.equalsIgnoreCase(tableName)) { - continue; + Set 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 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 columnNames = new HashSet<>(); - while (tables.next()) { - String tableName = tables.getString("TABLE_NAME"); - tableName = toUpperCase(tableName, Locale.US); + Set 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); - } - }); - + }); + } } } diff --git a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/tasks/HapiFhirJpaMigrationTasks.java b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/tasks/HapiFhirJpaMigrationTasks.java index c02246a3a14..1c1beac3280 100644 --- a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/tasks/HapiFhirJpaMigrationTasks.java +++ b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/tasks/HapiFhirJpaMigrationTasks.java @@ -9,9 +9,9 @@ package ca.uhn.fhir.jpa.migrate.tasks; * 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. @@ -277,7 +277,6 @@ public class HapiFhirJpaMigrationTasks extends BaseMigrationTasks { 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 { }); 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());