Skip to content

Commit

Permalink
PP-11205 Change the order of deleting user data (#2240)
Browse files Browse the repository at this point in the history
- Delete forgotten_passwords & invites records first which have foreign key relation to the users table.
- Deleting historical users first could result in a `foreign key constraint violation` if there are references in forgotten_passwords & invites
  • Loading branch information
kbottla authored Nov 10, 2023
1 parent 86dfc67 commit a078aa3
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public class ExpungeAndArchiveHistoricalDataResource {
public ExpungeAndArchiveHistoricalDataResource(ExpungeAndArchiveHistoricalDataService expungeAndArchiveHistoricalDataService) {
this.expungeAndArchiveHistoricalDataService = expungeAndArchiveHistoricalDataService;
}

@POST
@Produces(APPLICATION_JSON)
@Operation(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ public void expungeAndArchiveHistoricalData() {
if (expungeAndArchiveDataConfig.isExpungeAndArchiveHistoricalDataEnabled()) {
ZonedDateTime deleteUsersAndRelatedDataBeforeDate = getDeleteUsersAndRelatedDataBeforeDate();

int noOfUsersDeleted = userDao.deleteUsersNotAssociatedWithAnyService(deleteUsersAndRelatedDataBeforeDate.toInstant());
int noOfInvitesDeleted = inviteDao.deleteInvites(deleteUsersAndRelatedDataBeforeDate);
int noOfForgottenPasswordsDeleted = forgottenPasswordDao.deleteForgottenPasswords(deleteUsersAndRelatedDataBeforeDate);
int noOfUsersDeleted = userDao.deleteUsersNotAssociatedWithAnyService(deleteUsersAndRelatedDataBeforeDate.toInstant());

int noOfServicesArchived = archiveServices();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,17 @@ void shouldDeleteHistoricalUsers() {

@Test
void shouldDeleteHistoricalInvitesAndForgottenPasswords() {
User user = userDbFixture(databaseHelper).insertUser();
User userToBeDeleted = userDbFixture(databaseHelper)
.withCreatedAt(now.minusYears(5))
.insertUser();
User userThatWontBeDeleted = userDbFixture(databaseHelper)
.withCreatedAt(now)
.insertUser();

ForgottenPasswordDbFixture forgottenPasswordDbFixture = insertForgottenPassword(user.getId(), now.minusYears(5));
ForgottenPasswordDbFixture forgottenPasswordThatShouldNotBeDeleted = insertForgottenPassword(user.getId(), now);
String code = insertInvite(now.minusYears(5));
String codeThatShouldNotBeDeleted = insertInvite(now);
ForgottenPasswordDbFixture forgottenPasswordDbFixture = insertForgottenPassword(userToBeDeleted.getId(), now.minusYears(5));
ForgottenPasswordDbFixture forgottenPasswordThatShouldNotBeDeleted = insertForgottenPassword(userThatWontBeDeleted.getId(), now);
String code = insertInvite(userToBeDeleted.getId(), now.minusYears(5));
String codeThatShouldNotBeDeleted = insertInvite(userThatWontBeDeleted.getId(), now);

assertForgottenPasswordsExist(forgottenPasswordDbFixture.getId(), forgottenPasswordThatShouldNotBeDeleted.getId());
assertInvitesExist(code, codeThatShouldNotBeDeleted);
Expand All @@ -106,6 +111,9 @@ void shouldDeleteHistoricalInvitesAndForgottenPasswords() {

Optional<Map<String, Object>> inviteByCode = databaseHelper.findInviteByCode(code);
assertTrue(inviteByCode.isEmpty());

assertUserNotExists(userToBeDeleted.getId());
assertUserExists(userThatWontBeDeleted.getId());
}

@Test
Expand Down Expand Up @@ -221,9 +229,10 @@ private void assertInvitesExist(String... codes) {
});
}

private String insertInvite(ZonedDateTime date) {
private String insertInvite(Integer userId, ZonedDateTime date) {
return inviteDbFixture(databaseHelper)
.withDate(date)
.withSenderId(userId)
.insertInviteToAddUserToService();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class InviteDbFixture {
private Integer loginCounter = 0;
private String externalServiceId = randomUuid();
private Integer serviceId = randomInt();
private Integer senderId;

private InviteDbFixture(DatabaseTestHelper databaseTestHelper) {
this.databaseTestHelper = databaseTestHelper;
Expand All @@ -40,13 +41,13 @@ public String insertInviteToAddUserToService() {
int roleId = RoleDbFixture.roleDbFixture(databaseTestHelper).insertRole().getId();
String userUsername = randomUuid();
String userEmail = userUsername + "@example.com";
int invitingUserId =
senderId =
UserDbFixture.userDbFixture(databaseTestHelper)
.withEmail(userEmail)
.insertUser()
.getId();
databaseTestHelper.addInvite(
nextInt(), invitingUserId, serviceId, roleId,
nextInt(), senderId, serviceId, roleId,
email, code, otpKey, date, expiryDate, telephoneNumber, password,
disabled, loginCounter
);
Expand Down Expand Up @@ -124,4 +125,9 @@ public InviteDbFixture withDate(ZonedDateTime date) {
this.date = date;
return this;
}

public InviteDbFixture withSenderId(Integer senderId) {
this.senderId = senderId;
return this;
}
}

0 comments on commit a078aa3

Please sign in to comment.