From 4dd4d9755d38881ae1086f05759c21eab2b3a116 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Mon, 16 Sep 2024 14:20:47 +0200 Subject: [PATCH] Use session for ServerConfigSet to make it atomic (#4116) --- server/store/datastore/server_config.go | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/server/store/datastore/server_config.go b/server/store/datastore/server_config.go index 2dd52f406..a18517edb 100644 --- a/server/store/datastore/server_config.go +++ b/server/store/datastore/server_config.go @@ -31,7 +31,13 @@ func (s storage) ServerConfigSet(key, value string) error { Key: key, } - count, err := s.engine.Count(config) + sess := s.engine.NewSession() + defer sess.Close() + if err := sess.Begin(); err != nil { + return err + } + + count, err := sess.Count(config) if err != nil { return err } @@ -39,12 +45,15 @@ func (s storage) ServerConfigSet(key, value string) error { config.Value = value if count == 0 { - _, err := s.engine.Insert(config) + _, err = sess.Insert(config) + } else { + _, err = sess.Where("`key` = ?", config.Key).Cols("value").Update(config) + } + if err != nil { return err } - _, err = s.engine.Where("`key` = ?", config.Key).Cols("value").Update(config) - return err + return sess.Commit() } func (s storage) ServerConfigDelete(key string) error {