From b6c8658f8ceb355b6ab6c456167683fdfb07cc8e Mon Sep 17 00:00:00 2001 From: Jason Roberts Date: Tue, 21 Sep 2021 10:42:59 -0400 Subject: [PATCH 1/3] Fix MS SQLServer database migration --- .../changelog/5_6_0/3012-mssql-database-migration.yaml | 4 ++++ .../src/main/java/ca/uhn/fhir/jpa/util/TestUtil.java | 8 ++++++++ .../src/main/java/ca/uhn/fhir/jpa/migrate/JdbcUtils.java | 7 +++++++ .../java/ca/uhn/fhir/jpa/migrate/tasks/api/Builder.java | 5 +++++ 4 files changed, 24 insertions(+) create mode 100644 hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/5_6_0/3012-mssql-database-migration.yaml diff --git a/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/5_6_0/3012-mssql-database-migration.yaml b/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/5_6_0/3012-mssql-database-migration.yaml new file mode 100644 index 00000000000..899aacb26a5 --- /dev/null +++ b/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/5_6_0/3012-mssql-database-migration.yaml @@ -0,0 +1,4 @@ +--- +type: fix +issue: 3012 +title: "Fixes some recent database schema migration failures on MS SQLServer." diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/util/TestUtil.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/util/TestUtil.java index 3b1ed6553fd..20bd94a1586 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/util/TestUtil.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/util/TestUtil.java @@ -221,6 +221,14 @@ public class TestUtil { } + for (Class innerClass : theClazz.getDeclaredClasses()) { + Embeddable embeddable = innerClass.getAnnotation(Embeddable.class); + if (embeddable != null) { + scanClassOrSuperclass(theNames, innerClass, false, columnNameToLength); + } + + } + if (theClazz.getSuperclass().equals(Object.class)) { return; } 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 fb4af862dd6..ec0d881b699 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 @@ -254,6 +254,13 @@ public class JdbcUtils { return new ColumnType(ColumnTypeEnum.DATE_TIMESTAMP, length); case Types.BLOB: return new ColumnType(ColumnTypeEnum.BLOB, length); + case Types.VARBINARY: + if (theConnectionProperties.getDriverType().equals(DriverTypeEnum.MSSQL_2012)) { + // MS SQLServer seems to be mapping BLOB to VARBINARY under the covers, so we need to reverse that mapping + return new ColumnType(ColumnTypeEnum.BLOB, length); + } else { + throw new IllegalArgumentException("Don't know how to handle datatype " + dataType + " for column " + theColumnName + " on table " + theTableName); + } case Types.CLOB: return new ColumnType(ColumnTypeEnum.CLOB, length); case Types.DOUBLE: diff --git a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/tasks/api/Builder.java b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/tasks/api/Builder.java index 34c8805de97..259a1e6f8c5 100644 --- a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/tasks/api/Builder.java +++ b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/tasks/api/Builder.java @@ -148,6 +148,11 @@ public class Builder { super.addTask(theTask); } } + + public BuilderAddTableByColumns failureAllowed() { + myTask.setFailureAllowed(true); + return this; + } } public static class BuilderWithTableName implements BaseMigrationTasks.IAcceptsTasks { From 645386c99953911da95a21b8bd1cfb5826e2c9c8 Mon Sep 17 00:00:00 2001 From: JasonRoberts-smile <85363818+JasonRoberts-smile@users.noreply.github.com> Date: Tue, 21 Sep 2021 12:26:21 -0400 Subject: [PATCH 2/3] Apply suggestions from code review Co-authored-by: Ken Stevens --- .../fhir/changelog/5_6_0/3012-mssql-database-migration.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/5_6_0/3012-mssql-database-migration.yaml b/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/5_6_0/3012-mssql-database-migration.yaml index 899aacb26a5..46ce08c8fa0 100644 --- a/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/5_6_0/3012-mssql-database-migration.yaml +++ b/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/5_6_0/3012-mssql-database-migration.yaml @@ -1,4 +1,4 @@ --- type: fix issue: 3012 -title: "Fixes some recent database schema migration failures on MS SQLServer." +title: "Fixes migration of VARBINARY columns on MS SQLServer." From 68b26f7a5e0fb9679fa8ab0d22cee10ea4c64dfb Mon Sep 17 00:00:00 2001 From: Jason Roberts Date: Tue, 21 Sep 2021 12:28:15 -0400 Subject: [PATCH 3/3] apply recommendations from code review --- .../src/main/java/ca/uhn/fhir/jpa/migrate/JdbcUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hapi-fhir-sql-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/JdbcUtils.java b/hapi-fhir-sql-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/JdbcUtils.java index 21975ad9270..df3c904f687 100644 --- a/hapi-fhir-sql-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/JdbcUtils.java +++ b/hapi-fhir-sql-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/JdbcUtils.java @@ -186,7 +186,7 @@ public class JdbcUtils { case Types.BLOB: return new ColumnType(ColumnTypeEnum.BLOB, length); case Types.VARBINARY: - if (theConnectionProperties.getDriverType().equals(DriverTypeEnum.MSSQL_2012)) { + if (DriverTypeEnum.MSSQL_2012.equals(theConnectionProperties.getDriverType())) { // MS SQLServer seems to be mapping BLOB to VARBINARY under the covers, so we need to reverse that mapping return new ColumnType(ColumnTypeEnum.BLOB, length); } else {