From 55b69380ebd704477c4a37ff9b70fcb3cbdc63e5 Mon Sep 17 00:00:00 2001 From: xengineering Date: Sun, 3 Mar 2024 08:49:46 +0100 Subject: model: Assert one affected row on .Update() A missing or wrong .Id field for example otherwise results in a silent error because nothing is actually updated. --- model/recipe.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'model') diff --git a/model/recipe.go b/model/recipe.go index e5cf0a6..760fe79 100644 --- a/model/recipe.go +++ b/model/recipe.go @@ -90,11 +90,18 @@ SET WHERE id = ?` - _, err := db.Exec(query, r.Title, r.Portions, r.Url, r.Notes, + res, err := db.Exec(query, r.Title, r.Portions, r.Url, r.Notes, r.Created, r.LastChanged, r.Id) if err != nil { return err } + affected, err := res.RowsAffected() + if err != nil { + return err + } + if affected != 1 { + return fmt.Errorf("Recipe update affected %d rows instead of 1", affected) + } return nil } -- cgit v1.2.3-70-g09d2