touched up the database code to use transactions where necessary

This commit is contained in:
2025-11-28 23:01:56 -07:00
parent d73070f9b7
commit 7ce3bed15b
10 changed files with 376 additions and 132 deletions
+5 -3
View File
@@ -9,6 +9,8 @@
// The database package contains database management and storage logic.
package database
import "github.com/jmoiron/sqlx"
type Sidebox struct {
Uid int32 `db:"uid"`
Boxid int32 `db:"boxid"`
@@ -17,12 +19,12 @@ type Sidebox struct {
}
// copySideboxes copies sideboxes from one user to another.
func copySideboxes(toUid int32, fromUid int32) error {
func copySideboxes(tx *sqlx.Tx, toUid int32, fromUid int32) error {
sbox := make([]Sidebox, 0, 3)
err := amdb.Select(&sbox, "SELECT * from sideboxes WHERE uid = ?", fromUid)
err := tx.Select(&sbox, "SELECT * from sideboxes WHERE uid = ?", fromUid)
if err == nil {
for _, sb := range sbox {
_, err := amdb.Exec("INSERT INTO sideboxes (uid, boxid, sequence, param) VALUES (?, ?, ?, ?)", toUid, sb.Boxid, sb.Sequence, sb.Param)
_, err := tx.Exec("INSERT INTO sideboxes (uid, boxid, sequence, param) VALUES (?, ?, ?, ?)", toUid, sb.Boxid, sb.Sequence, sb.Param)
if err != nil {
break
}