From 7b995044b48e0c18a6b345f6d784191ac97ad264 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Guillot?= Date: Sun, 17 Nov 2019 22:13:16 -0800 Subject: [PATCH] Make sure to remove integration settings when removing a user --- storage/user.go | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/storage/user.go b/storage/user.go index 39b50ef6..5c146442 100644 --- a/storage/user.go +++ b/storage/user.go @@ -6,7 +6,6 @@ package storage // import "miniflux.app/storage" import ( "database/sql" - "errors" "fmt" "strings" @@ -288,18 +287,23 @@ func (s *Storage) fetchUser(query string, args ...interface{}) (*model.User, err // RemoveUser deletes a user. func (s *Storage) RemoveUser(userID int64) error { - result, err := s.db.Exec("DELETE FROM users WHERE id = $1", userID) + ts, err := s.db.Begin() if err != nil { - return fmt.Errorf(`store: unable to remove this user: %v`, err) + return fmt.Errorf(`store: unable to start transaction: %v`, err) } - count, err := result.RowsAffected() - if err != nil { - return fmt.Errorf(`store: unable to remove this user: %v`, err) + if _, err := ts.Exec(`DELETE FROM users WHERE id=$1`, userID); err != nil { + ts.Rollback() + return fmt.Errorf(`store: unable to remove user #%d: %v`, userID, err) } - if count == 0 { - return errors.New(`store: nothing has been removed`) + if _, err := ts.Exec(`DELETE FROM integrations WHERE user_id=$1`, userID); err != nil { + ts.Rollback() + return fmt.Errorf(`store: unable to remove integration settings for user #%d: %v`, userID, err) + } + + if err := ts.Commit(); err != nil { + return fmt.Errorf(`store: unable to commit transaction: %v`, err) } return nil