diff --git a/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/8_6_0/7375-fix-packageInstallation-withPartitions.yaml b/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/8_6_0/7375-fix-packageInstallation-withPartitions.yaml new file mode 100644 index 00000000000..0aca66db99c --- /dev/null +++ b/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/8_6_0/7375-fix-packageInstallation-withPartitions.yaml @@ -0,0 +1,4 @@ +--- +type: fix +issue: 7375 +title: "PackageInstallerSvcImpl now consistently applies the default partition when running in STORE_AND_INSTALL mode. Terminology resources imported from NPM packages no longer fail in partitioned JPA servers with the error about writing a non-partitionable resource into a partition." diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/packages/PackageInstallerSvcImpl.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/packages/PackageInstallerSvcImpl.java index 5b4b7d65c10..85a38a84484 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/packages/PackageInstallerSvcImpl.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/packages/PackageInstallerSvcImpl.java @@ -27,7 +27,6 @@ import ca.uhn.fhir.context.support.IValidationSupport; import ca.uhn.fhir.context.support.ValidationSupportContext; import ca.uhn.fhir.i18n.Msg; -import ca.uhn.fhir.interceptor.model.RequestPartitionId; import ca.uhn.fhir.jpa.api.config.JpaStorageSettings; import ca.uhn.fhir.jpa.api.dao.DaoRegistry; import ca.uhn.fhir.jpa.api.dao.IFhirResourceDao; @@ -188,7 +187,7 @@ public PackageInstallOutcomeJson install(PackageInstallationSpec theInstallation boolean exists = myTxService .withSystemRequest() - .withRequestPartitionId(RequestPartitionId.defaultPartition()) + .withRequestPartitionId(myPartitionSettings.getDefaultRequestPartitionId()) .execute(() -> { Optional existing = myPackageVersionDao.findByPackageIdAndVersion( theInstallationSpec.getName(), theInstallationSpec.getVersion()); @@ -550,10 +549,11 @@ private DaoMethodOutcome updateResource(IFhirResourceDao theDao, IBaseResource t return outcome; } - private RequestDetails createRequestDetails() { + @VisibleForTesting + RequestDetails createRequestDetails() { SystemRequestDetails requestDetails = new SystemRequestDetails(); if (myPartitionSettings.isPartitioningEnabled()) { - requestDetails.setRequestPartitionId(RequestPartitionId.defaultPartition()); + requestDetails.setRequestPartitionId(myPartitionSettings.getDefaultRequestPartitionId()); } return requestDetails; } diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/packages/PackageInstallerSvcImplTest.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/packages/PackageInstallerSvcImplTest.java index 8357c11618e..20b594e715d 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/packages/PackageInstallerSvcImplTest.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/packages/PackageInstallerSvcImplTest.java @@ -3,6 +3,7 @@ import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.context.FhirVersionEnum; import ca.uhn.fhir.context.support.IValidationSupport; +import ca.uhn.fhir.interceptor.model.RequestPartitionId; import ca.uhn.fhir.jpa.api.config.JpaStorageSettings; import ca.uhn.fhir.jpa.api.dao.DaoRegistry; import ca.uhn.fhir.jpa.api.dao.IFhirResourceDao; @@ -17,6 +18,7 @@ import ca.uhn.fhir.jpa.searchparam.util.SearchParameterHelper; import ca.uhn.fhir.mdm.log.Logs; import ca.uhn.fhir.rest.api.server.RequestDetails; +import ca.uhn.fhir.rest.api.server.SystemRequestDetails; import ca.uhn.fhir.rest.server.SimpleBundleProvider; import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; import ca.uhn.hapi.converters.canonical.VersionCanonicalizer; @@ -230,6 +232,40 @@ public void testValidForUpload_WhenSearchParameterValidatorThrowsAnExceptionOthe + @Test + public void testCreateRequestDetailsUsesDefaultPartition() { + myPartitionSettings.setPartitioningEnabled(true); + myPartitionSettings.setDefaultPartitionId(42); + + RequestDetails requestDetails = mySvc.createRequestDetails(); + assertTrue(requestDetails instanceof SystemRequestDetails); + SystemRequestDetails systemRequestDetails = (SystemRequestDetails) requestDetails; + + assertEquals(RequestPartitionId.fromPartitionId(42), systemRequestDetails.getRequestPartitionId()); + } + + @Test + public void testInstallPackageUsesDefaultPartition() throws IOException { + myPartitionSettings.setPartitioningEnabled(true); + myPartitionSettings.setDefaultPartitionId(7); + + CodeSystem newCodeSystem = new CodeSystem(); + newCodeSystem.setId("CodeSystem/newcs"); + newCodeSystem.setUrl("http://partitioned-code-system"); + newCodeSystem.setContent(CodeSystem.CodeSystemContentMode.COMPLETE); + + PackageInstallationSpec spec = setupResourceInPackage(null, newCodeSystem, myCodeSystemDao); + + mySvc.install(spec); + + verify(myCodeSystemDao).create(any(CodeSystem.class), myRequestDetailsCaptor.capture()); + RequestDetails requestDetails = myRequestDetailsCaptor.getValue(); + + assertTrue(requestDetails instanceof SystemRequestDetails); + SystemRequestDetails systemRequestDetails = (SystemRequestDetails) requestDetails; + assertEquals(RequestPartitionId.fromPartitionId(7), systemRequestDetails.getRequestPartitionId()); + } + @Test public void testDontTryToInstallDuplicateCodeSystem_CodeSystemAlreadyExistsWithDifferentId() throws IOException { // Setup