From f9cd87d12656b308d3e24a14189e7ac437666ab4 Mon Sep 17 00:00:00 2001 From: Julien Hagestedt Date: Wed, 4 Nov 2020 10:58:17 +0100 Subject: [PATCH] feat: cleanup by created date (#82) --- .../coronawarn/testresult/TestResultCleanup.java | 4 ++-- .../testresult/TestResultRepository.java | 8 ++++++++ src/main/resources/application.yml | 2 +- .../testresult/TestResultCleanupTest.java | 14 ++++++++++++-- 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/main/java/app/coronawarn/testresult/TestResultCleanup.java b/src/main/java/app/coronawarn/testresult/TestResultCleanup.java index 5c89bcd..ef88318 100644 --- a/src/main/java/app/coronawarn/testresult/TestResultCleanup.java +++ b/src/main/java/app/coronawarn/testresult/TestResultCleanup.java @@ -26,7 +26,7 @@ public class TestResultCleanup { ) @Transactional public void redeem() { - Integer redeemed = testResultRepository.updateResultByResultDateBefore( + Integer redeemed = testResultRepository.updateResultByCreatedAtBefore( TestResultEntity.Result.REDEEMED.ordinal(), LocalDateTime.now().minus(Period.ofDays(testResultConfig.getCleanup().getRedeem().getDays()))); log.info("Cleanup redeemed {} test results.", redeemed); @@ -40,7 +40,7 @@ public void redeem() { ) @Transactional public void delete() { - Integer deleted = testResultRepository.deleteByResultDateBefore( + Integer deleted = testResultRepository.deleteByCreatedAtBefore( LocalDateTime.now().minus(Period.ofDays(testResultConfig.getCleanup().getDelete().getDays()))); log.info("Cleanup deleted {} test results.", deleted); } diff --git a/src/main/java/app/coronawarn/testresult/TestResultRepository.java b/src/main/java/app/coronawarn/testresult/TestResultRepository.java index ee6392c..c4ee756 100644 --- a/src/main/java/app/coronawarn/testresult/TestResultRepository.java +++ b/src/main/java/app/coronawarn/testresult/TestResultRepository.java @@ -32,10 +32,18 @@ public interface TestResultRepository extends JpaRepository findByResultId(String resultId); + @Modifying + @Query("update TestResultEntity t set t.result = ?1 where t.result != ?1 and t.createdAt < ?2") + Integer updateResultByCreatedAtBefore(Integer result, LocalDateTime before); + @Modifying @Query("update TestResultEntity t set t.result = ?1 where t.result != ?1 and t.resultDate < ?2") Integer updateResultByResultDateBefore(Integer result, LocalDateTime before); + @Modifying + @Query("delete from TestResultEntity t where t.createdAt < ?1") + Integer deleteByCreatedAtBefore(LocalDateTime before); + @Modifying @Query("delete from TestResultEntity t where t.resultDate < ?1") Integer deleteByResultDateBefore(LocalDateTime before); diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index b9df781..65d5b79 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -50,5 +50,5 @@ testresult: days: 21 rate: 3600000 delete: - days: 90 + days: 60 rate: 3600000 diff --git a/src/test/java/app/coronawarn/testresult/TestResultCleanupTest.java b/src/test/java/app/coronawarn/testresult/TestResultCleanupTest.java index c7cf986..d5ce9c3 100644 --- a/src/test/java/app/coronawarn/testresult/TestResultCleanupTest.java +++ b/src/test/java/app/coronawarn/testresult/TestResultCleanupTest.java @@ -20,7 +20,7 @@ properties = { "testresult.cleanup.redeem.days=21", "testresult.cleanup.redeem.rate=1000", - "testresult.cleanup.delete.days=90", + "testresult.cleanup.delete.days=60", "testresult.cleanup.delete.rate=1000" } ) @@ -43,18 +43,23 @@ public void shouldCleanupRedeem() { String resultId = "a".repeat(64); Integer resultRedeemed = TestResultEntity.Result.REDEEMED.ordinal(); LocalDateTime resultDate = LocalDateTime.now().minus(Period.ofDays(21)); + LocalDateTime createdAt = LocalDateTime.now().minus(Period.ofDays(21)); // create TestResultEntity create = testResultRepository.save(new TestResultEntity() .setResult(1) .setResultId(resultId) .setResultDate(resultDate) ); + create.setCreatedAt(createdAt); + testResultRepository.save(create); Assert.assertNotNull(create); + Assert.assertEquals(createdAt, create.getCreatedAt()); Assert.assertEquals(resultId, create.getResultId()); // find Optional find = testResultRepository.findByResultId(resultId); Assert.assertTrue(find.isPresent()); Assert.assertEquals(resultId, find.get().getResultId()); + Assert.assertEquals(createdAt, find.get().getCreatedAt()); Assert.assertEquals(resultDate, find.get().getResultDate()); // wait Single.fromCallable(() -> true).delay(2, TimeUnit.SECONDS).toBlocking().value(); @@ -71,19 +76,24 @@ public void shouldCleanupDelete() { testResultRepository.deleteAll(); // data String resultId = "d".repeat(64); - LocalDateTime resultDate = LocalDateTime.now().minus(Period.ofDays(90)); + LocalDateTime resultDate = LocalDateTime.now().minus(Period.ofDays(60)); + LocalDateTime createdAt = LocalDateTime.now().minus(Period.ofDays(60)); // create TestResultEntity create = testResultRepository.save(new TestResultEntity() .setResult(1) .setResultId(resultId) .setResultDate(resultDate) ); + create.setCreatedAt(createdAt); + testResultRepository.save(create); Assert.assertNotNull(create); + Assert.assertEquals(createdAt, create.getCreatedAt()); Assert.assertEquals(resultId, create.getResultId()); // find Optional find = testResultRepository.findByResultId(resultId); Assert.assertTrue(find.isPresent()); Assert.assertEquals(resultId, find.get().getResultId()); + Assert.assertEquals(createdAt, find.get().getCreatedAt()); Assert.assertEquals(resultDate, find.get().getResultDate()); // wait Single.fromCallable(() -> true).delay(2, TimeUnit.SECONDS).toBlocking().value();