From 6c450c6f3b85540cede8d45fc2726658e722b7c1 Mon Sep 17 00:00:00 2001 From: Thomas Miceli <27960254+thomiceli@users.noreply.github.com> Date: Mon, 25 Sep 2023 18:43:36 +0200 Subject: [PATCH] Delete gists when user is deleted (#108) --- internal/db/user.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/internal/db/user.go b/internal/db/user.go index 9c385e8..d5ec34a 100644 --- a/internal/db/user.go +++ b/internal/db/user.go @@ -39,7 +39,7 @@ func (user *User) BeforeDelete(tx *gorm.DB) error { } // Decrement forks counter for all gists forked by this user - return tx.Model(&Gist{}). + err = tx.Model(&Gist{}). Omit("updated_at"). Where("id IN (?)", tx. Select("forked_id"). @@ -48,6 +48,12 @@ func (user *User) BeforeDelete(tx *gorm.DB) error { ). UpdateColumn("nb_forks", gorm.Expr("nb_forks - 1")). Error + if err != nil { + return err + } + + // Delete all gists created by this user + return tx.Where("user_id = ?", user.ID).Delete(&Gist{}).Error } func UserExists(username string) (bool, error) {