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