mirror of
https://github.com/ddworken/hishtory.git
synced 2024-11-22 16:24:00 +01:00
Refactor deletion request creation to happen in a transaction to avoid 1-n DB queries
This commit is contained in:
parent
151dfb4009
commit
9f32ae5d2e
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user