Skip to content

Commit

Permalink
check for sql.ErrNoRows on delete with returning statements
Browse files Browse the repository at this point in the history
Signed-off-by: kim <[email protected]>
  • Loading branch information
NyaaaWhatsUpDoc committed Aug 3, 2023
1 parent 2397ac3 commit 600525e
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions internal/db/bundb/statusfave.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package bundb

import (
"context"
"database/sql"
"errors"
"fmt"

Expand Down Expand Up @@ -217,12 +218,17 @@ func (s *statusFaveDB) DeleteStatusFaveByID(ctx context.Context, id string) erro
Where("id = ?", id).
Returning("status_id").
Exec(ctx, &statusID); err != nil {
if err == sql.ErrNoRows {
// Not an issue, only due
// to us doing a RETURNING.
err = nil
}
return s.db.ProcessError(err)
}

if statusID != "" {
// Invalidate any cached status faves for this status.
s.state.Caches.GTS.StatusFave().Invalidate("ID", statusID)
s.state.Caches.GTS.StatusFave().Invalidate("ID", id)

// Invalidate any cached status fave IDs for this status.
s.state.Caches.GTS.StatusFaveIDs().Invalidate(statusID)
Expand Down Expand Up @@ -254,11 +260,18 @@ func (s *statusFaveDB) DeleteStatusFaves(ctx context.Context, targetAccountID st

// Execute query, store favourited status IDs.
if _, err := q.Exec(ctx, &statusIDs); err != nil {
if err == sql.ErrNoRows {
// Not an issue, only due
// to us doing a RETURNING.
err = nil
}
return s.db.ProcessError(err)
}

// Collate (deduplicating) status IDs.
statusIDs = collate(func(i int) string { return statusIDs[i] }, len(statusIDs))
statusIDs = collate(func(i int) string {
return statusIDs[i]
}, len(statusIDs))

for _, id := range statusIDs {
// Invalidate any cached status faves for this status.
Expand Down

0 comments on commit 600525e

Please sign in to comment.