diff --git a/hapi-fhir-jpaserver-test-utilities/src/main/java/ca/uhn/fhir/jpa/embedded/MsSqlEmbeddedDatabase.java b/hapi-fhir-jpaserver-test-utilities/src/main/java/ca/uhn/fhir/jpa/embedded/MsSqlEmbeddedDatabase.java index 5dbe58e1bd8c..a75b7fbbdb50 100644 --- a/hapi-fhir-jpaserver-test-utilities/src/main/java/ca/uhn/fhir/jpa/embedded/MsSqlEmbeddedDatabase.java +++ b/hapi-fhir-jpaserver-test-utilities/src/main/java/ca/uhn/fhir/jpa/embedded/MsSqlEmbeddedDatabase.java @@ -20,6 +20,8 @@ package ca.uhn.fhir.jpa.embedded; import ca.uhn.fhir.jpa.migrate.DriverTypeEnum; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.testcontainers.containers.MSSQLServerContainer; import org.testcontainers.utility.DockerImageName; @@ -34,6 +36,7 @@ * @see MS SQL Server TestContainer */ public class MsSqlEmbeddedDatabase extends JpaEmbeddedDatabase { + private static final Logger ourLog = LoggerFactory.getLogger(MsSqlEmbeddedDatabase.class); private final MSSQLServerContainer myContainer; @@ -70,8 +73,18 @@ private void dropForeignKeys() { private void dropRemainingConstraints() { List> queryResults = getJdbcTemplate().queryForList("SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS"); for(Map row : queryResults){ - String tableName = row.get("TABLE_NAME").toString(); - String constraintName = row.get("CONSTRAINT_NAME").toString(); + Object tableNameEntry = row.get("TABLE_NAME"); + if (tableNameEntry == null) { + ourLog.warn("Found a constraint with no table name: {}", row); + continue; + } + String tableName = tableNameEntry.toString(); + Object constraintNameEntry = row.get("CONSTRAINT_NAME"); + if (constraintNameEntry == null) { + ourLog.warn("Found a constraint with no constraint name: {}", row); + continue; + } + String constraintName = constraintNameEntry.toString(); getJdbcTemplate().execute(String.format("ALTER TABLE \"%s\" DROP CONSTRAINT \"%s\"", tableName, constraintName)); } }