Make deletion request auto-retry SQLITE_LOCKED errors since deletion requests will now be much more common with pre-saving support

This commit is contained in:
David Dworken 2023-09-22 19:47:10 -07:00
parent 8bb02ea88c
commit e3d54e43e8
No known key found for this signature in database

View File

@ -605,12 +605,14 @@ func HandleDeletionRequests(ctx context.Context, deletionRequests []*shared.Dele
db := hctx.GetDb(ctx)
for _, request := range deletionRequests {
for _, entry := range request.Messages.Ids {
// Note that entry.EndTime is not always present (for pre-saved entries). And likewise,
// entry.EntryId is not always present for older entries. So we just check that one of them matches.
tx := db.Where("device_id = ? AND (end_time = ? OR entry_id = ?)", entry.DeviceId, entry.EndTime, entry.EntryId)
res := tx.Delete(&data.HistoryEntry{})
if res.Error != nil {
return fmt.Errorf("DB error: %w", res.Error)
err := RetryingDbFunction(func() error {
// Note that entry.EndTime is not always present (for pre-saved entries). And likewise,
// entry.EntryId is not always present for older entries. So we just check that one of them matches.
tx := db.Where("device_id = ? AND (end_time = ? OR entry_id = ?)", entry.DeviceId, entry.EndTime, entry.EntryId)
return tx.Delete(&data.HistoryEntry{}).Error
})
if err != nil {
return fmt.Errorf("DB error when deleting entries: %w", err)
}
}
}