Skip to content
This repository has been archived by the owner on May 16, 2023. It is now read-only.

Commit

Permalink
feat: cleanup by created date (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
jhagestedt authored Nov 4, 2020
1 parent 29984aa commit f9cd87d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,18 @@ public interface TestResultRepository extends JpaRepository<TestResultEntity, Lo

Optional<TestResultEntity> 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);
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,5 @@ testresult:
days: 21
rate: 3600000
delete:
days: 90
days: 60
rate: 3600000
14 changes: 12 additions & 2 deletions src/test/java/app/coronawarn/testresult/TestResultCleanupTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
)
Expand All @@ -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<TestResultEntity> 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();
Expand All @@ -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<TestResultEntity> 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();
Expand Down

0 comments on commit f9cd87d

Please sign in to comment.