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

Commit

Permalink
Merge pull request #49 from admin-ch/feature/db-push-cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
gstoehld authored Mar 27, 2023
2 parents 77b66da + 953ae3b commit 90bd457
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.5.1</version>
<version>42.5.4</version>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public JdbcDeliveryDataServiceImpl(DataSource dataSource, int pushBatchSize) {
this.pushRegistrationInsert =
new SimpleJdbcInsert(dataSource)
.withTableName("t_push_registration")
.usingGeneratedKeyColumns("pk_push_registration_id", "created_at");
.usingGeneratedKeyColumns("pk_push_registration_id", "created_at", "updated_at");
this.covidCertInsert =
new SimpleJdbcInsert(dataSource)
.withTableName("t_covidcert")
Expand Down Expand Up @@ -165,7 +165,7 @@ public void upsertPushRegistration(PushRegistration registration) {
} else {
var sql =
"update t_push_registration "
+ "set push_token = :push_token, register_id = :register_id "
+ "set push_token = :push_token, register_id = :register_id, updated_at = now() "
+ "where push_token = :push_token or register_id = :register_id";
jt.update(sql, createPushRegistrationParams(registration));
}
Expand Down Expand Up @@ -241,10 +241,14 @@ public void insertCovidCert(DbCovidCert covidCert) {
@Override
@Transactional(readOnly = false)
public void cleanDB(Duration retentionPeriod) {
var sql = "delete from t_transfer where created_at < :retention_time";
var transferSql = "delete from t_transfer where created_at < :retention_time";
var pushSql = "delete from t_push_registration where created_at < :retention_time";

var retentionTime = Instant.now().minus(retentionPeriod);
var params = new MapSqlParameterSource("retention_time", Date.from(retentionTime));
jt.update(sql, params);

jt.update(transferSql, params);
jt.update(pushSql, params);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/*
* Remove all existing push registrations since we don't know how old they are.
* The apps will re-register themselves periodically.
*/

alter table t_push_registration add column updated_at timestamp with time zone NOT NULL DEFAULT now();
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/*
* Remove all existing push registrations since we don't know how old they are.
* The apps will re-register themselves periodically.
*/

alter table t_push_registration add column updated_at timestamp with time zone NOT NULL DEFAULT now();
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,12 @@ void testCleanDB() throws Exception {
// init transfer
DeliveryRegistration registration = getDeliveryRegistration(CODE);
deliveryDataService.initTransfer(registration);
// insert push registration
PushRegistration pushRegistration = new PushRegistration();
pushRegistration.setPushToken("push_token");
pushRegistration.setPushType(PushType.IOS);
pushRegistration.setRegisterId("register_id");
deliveryDataService.upsertPushRegistration(pushRegistration);
// insert covid cert
var dbCovidCert = new DbCovidCert();
dbCovidCert.setFkTransfer(deliveryDataService.findPkTransferId(CODE));
Expand All @@ -256,6 +262,7 @@ void testCleanDB() throws Exception {
deliveryDataService.cleanDB(Duration.ofDays(-1));
assertThrows(CodeNotFoundException.class, () -> deliveryDataService.findTransfer(CODE));
assertThrows(CodeNotFoundException.class, () -> deliveryDataService.findCovidCerts(CODE));
assertEquals(0, deliveryDataService.countRegistrations());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class PushRegistration {
@NotNull private String registerId;
@JsonIgnore private int id;
@JsonIgnore private Instant lastPush;
@JsonIgnore private Instant updatedAt;

public String getPushToken() {
return pushToken;
Expand Down
6 changes: 3 additions & 3 deletions ch-covidcertificate-backend-delivery/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

<spring-boot-version>2.7.8</spring-boot-version>
<spring-boot-version>2.7.9</spring-boot-version>
<jackson-version>2.11.1</jackson-version>
<jsonwebtoken-version>0.11.5</jsonwebtoken-version>
<testcontainers-version>1.17.6</testcontainers-version>
<shedlock.version>4.44.0</shedlock.version>
<spring-cloud-sleuth-version>3.1.6</spring-cloud-sleuth-version>
<spring-cloud-sleuth-version>3.1.7</spring-cloud-sleuth-version>

<itCoverageAgent/>

Expand Down Expand Up @@ -221,7 +221,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<version>3.11.0</version>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
Expand Down

0 comments on commit 90bd457

Please sign in to comment.