diff --git a/src/main/java/com/powsybl/caseserver/CaseService.java b/src/main/java/com/powsybl/caseserver/CaseService.java index c237966..e4207ad 100644 --- a/src/main/java/com/powsybl/caseserver/CaseService.java +++ b/src/main/java/com/powsybl/caseserver/CaseService.java @@ -44,8 +44,8 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardCopyOption; -import java.time.LocalDateTime; -import java.time.ZoneOffset; +import java.time.Instant; +import java.time.temporal.ChronoUnit; import java.util.ArrayList; import java.util.Collection; import java.util.List; @@ -249,9 +249,9 @@ UUID duplicateCase(UUID sourceCaseUuid, boolean withExpiration) { } private void createCaseMetadataEntity(UUID newCaseUuid, boolean withExpiration) { - LocalDateTime expirationTime = null; + Instant expirationTime = null; if (withExpiration) { - expirationTime = LocalDateTime.now(ZoneOffset.UTC).plusHours(1); + expirationTime = Instant.now().plus(1, ChronoUnit.HOURS); } caseMetadataRepository.save(new CaseMetadataEntity(newCaseUuid, expirationTime)); } diff --git a/src/main/java/com/powsybl/caseserver/ScheduledCaseCleaner.java b/src/main/java/com/powsybl/caseserver/ScheduledCaseCleaner.java index 0bd5389..011bdd3 100644 --- a/src/main/java/com/powsybl/caseserver/ScheduledCaseCleaner.java +++ b/src/main/java/com/powsybl/caseserver/ScheduledCaseCleaner.java @@ -12,8 +12,7 @@ import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; -import java.time.LocalDateTime; -import java.time.ZoneOffset; +import java.time.Instant; /** * @author Abdelsalem Hedhili @@ -34,10 +33,10 @@ public ScheduledCaseCleaner(CaseMetadataRepository caseMetadataRepository, CaseS @Scheduled(cron = "${cleaning-cases-cron}", zone = "UTC") public void deleteExpiredCases() { - LocalDateTime localDateTime = LocalDateTime.now(ZoneOffset.UTC); - LOGGER.info("Cleaning cases cron starting execution at {}", localDateTime); + Instant now = Instant.now(); + LOGGER.info("Cleaning cases cron starting execution at {}", now); caseMetadataRepository.findAll().stream().filter(caseMetadataEntity -> caseMetadataEntity.getExpirationDate() != null) - .filter(caseMetadataEntity -> localDateTime.isAfter(caseMetadataEntity.getExpirationDate())) + .filter(caseMetadataEntity -> now.isAfter(caseMetadataEntity.getExpirationDate())) .forEach(caseMetadataEntity -> { caseService.deleteCase(caseMetadataEntity.getId()); caseMetadataRepository.deleteById(caseMetadataEntity.getId()); diff --git a/src/main/java/com/powsybl/caseserver/repository/CaseMetadataEntity.java b/src/main/java/com/powsybl/caseserver/repository/CaseMetadataEntity.java index a3efa34..ebacd0e 100644 --- a/src/main/java/com/powsybl/caseserver/repository/CaseMetadataEntity.java +++ b/src/main/java/com/powsybl/caseserver/repository/CaseMetadataEntity.java @@ -6,7 +6,7 @@ */ package com.powsybl.caseserver.repository; -import java.time.LocalDateTime; +import java.time.Instant; import java.util.UUID; import lombok.*; @@ -31,6 +31,6 @@ public class CaseMetadataEntity { @Column(name = "id") private UUID id; - @Column(name = "expirationDate") - private LocalDateTime expirationDate; + @Column(name = "expirationDate", columnDefinition = "timestamptz") + private Instant expirationDate; } diff --git a/src/main/resources/db/changelog/changesets/changelog_20240620T100317Z.xml b/src/main/resources/db/changelog/changesets/changelog_20240620T100317Z.xml new file mode 100644 index 0000000..26f6fc1 --- /dev/null +++ b/src/main/resources/db/changelog/changesets/changelog_20240620T100317Z.xml @@ -0,0 +1,6 @@ + + + + + + diff --git a/src/main/resources/db/changelog/db.changelog-master.yaml b/src/main/resources/db/changelog/db.changelog-master.yaml index 8258ab0..7768178 100644 --- a/src/main/resources/db/changelog/db.changelog-master.yaml +++ b/src/main/resources/db/changelog/db.changelog-master.yaml @@ -1,4 +1,8 @@ databaseChangeLog: - include: file: changesets/changelog_20221206T100716Z.xml + relativeToChangelogFile: true + + - include: + file: changesets/changelog_20240620T100317Z.xml relativeToChangelogFile: true \ No newline at end of file diff --git a/src/test/java/com/powsybl/caseserver/CaseControllerTest.java b/src/test/java/com/powsybl/caseserver/CaseControllerTest.java index 8a45be2..f53be5e 100644 --- a/src/test/java/com/powsybl/caseserver/CaseControllerTest.java +++ b/src/test/java/com/powsybl/caseserver/CaseControllerTest.java @@ -41,9 +41,9 @@ import java.nio.file.FileSystem; import java.nio.file.Files; import java.nio.file.Path; -import java.time.LocalDateTime; -import java.time.ZoneOffset; +import java.time.Instant; import java.time.format.DateTimeFormatter; +import java.time.temporal.ChronoUnit; import java.util.List; import java.util.UUID; @@ -332,9 +332,9 @@ public void test() throws Exception { assertNull(caseMetadataEntity.getExpirationDate()); // import a case with expiration - LocalDateTime beforeImportDate = LocalDateTime.now(ZoneOffset.UTC).plusHours(1); + Instant beforeImportDate = Instant.now().plus(1, ChronoUnit.HOURS); UUID thirdCaseUuid = importCase(TEST_CASE, true); - LocalDateTime afterImportDate = LocalDateTime.now(ZoneOffset.UTC).plusHours(1); + Instant afterImportDate = Instant.now().plus(1, ChronoUnit.HOURS); // assert that the broker message has been sent messageImport = outputDestination.receive(1000, "case.import.destination"); diff --git a/src/test/java/com/powsybl/caseserver/ScheduledCaseCleanerTest.java b/src/test/java/com/powsybl/caseserver/ScheduledCaseCleanerTest.java index a634e95..6408a1e 100644 --- a/src/test/java/com/powsybl/caseserver/ScheduledCaseCleanerTest.java +++ b/src/test/java/com/powsybl/caseserver/ScheduledCaseCleanerTest.java @@ -18,8 +18,8 @@ import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.test.context.junit4.SpringRunner; -import java.time.LocalDateTime; -import java.time.ZoneOffset; +import java.time.Instant; +import java.time.temporal.ChronoUnit; import java.util.UUID; import static org.junit.Assert.assertEquals; @@ -58,10 +58,10 @@ private void cleanDB() { @Test public void test() { - LocalDateTime now = LocalDateTime.now(ZoneOffset.UTC); - LocalDateTime yesterday = now.minusDays(1); - CaseMetadataEntity shouldNotExpireEntity = new CaseMetadataEntity(UUID.randomUUID(), now.plusHours(1)); - CaseMetadataEntity shouldExpireEntity = new CaseMetadataEntity(UUID.randomUUID(), yesterday.plusHours(1)); + Instant now = Instant.now(); + Instant yesterday = now.minus(1, ChronoUnit.DAYS); + CaseMetadataEntity shouldNotExpireEntity = new CaseMetadataEntity(UUID.randomUUID(), now.plus(1, ChronoUnit.HOURS)); + CaseMetadataEntity shouldExpireEntity = new CaseMetadataEntity(UUID.randomUUID(), yesterday.plus(1, ChronoUnit.HOURS)); CaseMetadataEntity noExpireDateEntity = new CaseMetadataEntity(UUID.randomUUID(), null); caseMetadataRepository.save(shouldExpireEntity); caseMetadataRepository.save(shouldNotExpireEntity); diff --git a/src/test/resources/application-default.yaml b/src/test/resources/application-default.yaml index e045393..9dba455 100644 --- a/src/test/resources/application-default.yaml +++ b/src/test/resources/application-default.yaml @@ -4,10 +4,12 @@ spring: properties: dialect: org.hibernate.dialect.H2Dialect hibernate.format_sql: true - hibernate: - format_sql: true - generate_statistics: true - dialect: org.hibernate.dialect.H2Dialect + hibernate: + format_sql: true + generate_statistics: true + dialect: org.hibernate.dialect.H2Dialect + #to turn off schema validation that fails (because of timestampz types) and blocks tests even if the schema is compatible + ddl-auto: none logging: level: