From 1d5f648070dea5a4828d33e09fb6b390491d8b9f Mon Sep 17 00:00:00 2001 From: Ken Stevens Date: Wed, 3 Apr 2019 13:32:10 -0400 Subject: [PATCH] fixed second expunge bug reported by FMCNA --- .../ca/uhn/fhir/jpa/dao/BaseHapiFhirDao.java | 8 +++++-- .../dao/data/IResourceHistoryTableDao.java | 17 +++++++++----- .../ResourceProviderExpungeDstu3Test.java | 23 ++++++++++++++++++- 3 files changed, 39 insertions(+), 9 deletions(-) diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/BaseHapiFhirDao.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/BaseHapiFhirDao.java index 250866bd3ca..60635b0f763 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/BaseHapiFhirDao.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/BaseHapiFhirDao.java @@ -310,8 +310,12 @@ public abstract class BaseHapiFhirDao implements IDao, */ Pageable page = PageRequest.of(0, remainingCount.get()); Slice historicalIds = txTemplate.execute(t -> { - if (theResourceId != null && theVersion != null) { - return toSlice(myResourceHistoryTableDao.findForIdAndVersion(theResourceId, theVersion)); + if (theResourceId != null) { + if (theVersion != null) { + return toSlice(myResourceHistoryTableDao.findForIdAndVersion(theResourceId, theVersion)); + } else { + return myResourceHistoryTableDao.findIdsOfPreviousVersionsOfResourceId(page, theResourceId); + } } else { if (theResourceName != null) { return myResourceHistoryTableDao.findIdsOfPreviousVersionsOfResources(page, theResourceName); diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/data/IResourceHistoryTableDao.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/data/IResourceHistoryTableDao.java index 47183ac7d8a..df5f6a84f73 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/data/IResourceHistoryTableDao.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/data/IResourceHistoryTableDao.java @@ -1,10 +1,6 @@ package ca.uhn.fhir.jpa.dao.data; -import java.util.Collection; -import java.util.Date; - -import javax.persistence.TemporalType; - +import ca.uhn.fhir.jpa.model.entity.ResourceHistoryTable; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Slice; import org.springframework.data.jpa.repository.JpaRepository; @@ -13,7 +9,9 @@ import org.springframework.data.jpa.repository.Query; import org.springframework.data.jpa.repository.Temporal; import org.springframework.data.repository.query.Param; -import ca.uhn.fhir.jpa.model.entity.ResourceHistoryTable; +import javax.persistence.TemporalType; +import java.util.Collection; +import java.util.Date; /* * #%L @@ -74,6 +72,13 @@ public interface IResourceHistoryTableDao extends JpaRepository findForResourceId(Pageable thePage, @Param("resId") Long theId, @Param("dontWantVersion") Long theDontWantVersion); + @Query("" + + "SELECT v.myId FROM ResourceHistoryTable v " + + "LEFT OUTER JOIN ResourceTable t ON (v.myResourceId = t.myId) " + + "WHERE v.myResourceVersion != t.myVersion AND " + + "t.myId = :resId") + Slice findIdsOfPreviousVersionsOfResourceId(Pageable thePage, @Param("resId") Long theResourceId); + @Query("" + "SELECT v.myId FROM ResourceHistoryTable v " + "LEFT OUTER JOIN ResourceTable t ON (v.myResourceId = t.myId) " + diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/dstu3/ResourceProviderExpungeDstu3Test.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/dstu3/ResourceProviderExpungeDstu3Test.java index 68c89f73335..05648fbaa19 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/dstu3/ResourceProviderExpungeDstu3Test.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/dstu3/ResourceProviderExpungeDstu3Test.java @@ -18,7 +18,8 @@ import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; public class ResourceProviderExpungeDstu3Test extends BaseResourceProviderDstu3Test { @@ -326,6 +327,26 @@ public class ResourceProviderExpungeDstu3Test extends BaseResourceProviderDstu3T assertGone(myDeletedObservationId); } + @Test + public void testExpungeNothing() { + + myPatientDao.expunge(myOneVersionPatientId.toUnqualifiedVersionless(), new ExpungeOptions() + .setExpungeDeletedResources(true) + .setExpungeOldVersions(true)); + + // Patients + assertStillThere(myOneVersionPatientId); + assertStillThere(myTwoVersionPatientId.withVersion("1")); + assertStillThere(myTwoVersionPatientId.withVersion("2")); + assertGone(myDeletedPatientId); + + // No observations deleted + assertStillThere(myOneVersionObservationId); + assertStillThere(myTwoVersionObservationId.withVersion("1")); + assertStillThere(myTwoVersionObservationId.withVersion("2")); + assertGone(myDeletedObservationId); + } + @Test public void testParameters() { Parameters p = new Parameters();