Refactor deletion request creation to happen in a transaction to avoid 1-n DB queries

This commit is contained in:
David Dworken 2023-10-15 10:29:47 -07:00
parent 151dfb4009
commit 9f32ae5d2e
No known key found for this signature in database

View File

@ -243,22 +243,22 @@ func (db *DB) DeletionRequestCreate(ctx context.Context, request *shared.Deletio
fmt.Printf("db.DeletionRequestCreate: Found %d devices\n", len(devices))
// TODO: maybe this should be a transaction?
for _, device := range devices {
request.DestinationDeviceId = device.DeviceId
tx := db.WithContext(ctx).Create(&request)
if tx.Error != nil {
return fmt.Errorf("tx.Error: %w", tx.Error)
return db.Transaction(func(tx *gorm.DB) error {
for _, device := range devices {
request.DestinationDeviceId = device.DeviceId
tx := db.WithContext(ctx).Create(&request)
if tx.Error != nil {
return fmt.Errorf("create: tx.Error: %w", tx.Error)
}
}
}
numDeleted, err := db.DeleteMessagesFromBackend(ctx, userID, request.Messages.Ids)
if err != nil {
return fmt.Errorf("db.DeleteMessagesFromBackend: %w", err)
}
fmt.Printf("addDeletionRequestHandler: Deleted %d rows in the backend\n", numDeleted)
return nil
numDeleted, err := db.DeleteMessagesFromBackend(ctx, userID, request.Messages.Ids)
if err != nil {
return fmt.Errorf("db.DeleteMessagesFromBackend: %w", err)
}
fmt.Printf("addDeletionRequestHandler: Deleted %d rows in the backend\n", numDeleted)
return nil
})
}
func (db *DB) FeedbackCreate(ctx context.Context, feedback *shared.Feedback) error {