simplify some tx code
Ted Unangst tedu@tedunangst.com
Wed, 06 Mar 2024 16:33:34 -0500
1 files changed,
8 insertions(+),
9 deletions(-)
jump to
M
database.go
→
database.go
@@ -650,6 +650,7 @@ if err != nil {
elog.Printf("can't begin tx: %s", err) return err } + defer tx.Rollback() res, err := tx.Stmt(stmtSaveChonk).Exec(ch.UserID, ch.XID, ch.Who, ch.Target, dt, ch.Noise, ch.Format) if err == nil {@@ -663,8 +664,6 @@ }
} chatplusone(tx, ch.UserID) err = tx.Commit() - } else { - tx.Rollback() } return err }@@ -849,6 +848,7 @@ if err != nil {
elog.Printf("can't begin tx: %s", err) return err } + defer tx.Rollback() plain := h.Plain() res, err := tx.Stmt(stmtSaveHonk).Exec(h.UserID, h.What, h.Honker, h.XID, h.RID, dt, h.URL,@@ -864,8 +864,6 @@ dlog.Printf("another one for me: %s", h.XID)
meplusone(tx, h.UserID) } err = tx.Commit() - } else { - tx.Rollback() } if err != nil { elog.Printf("error saving honk: %s", err)@@ -885,6 +883,7 @@ if err != nil {
elog.Printf("can't begin tx: %s", err) return err } + defer tx.Rollback() plain := h.Plain() err = deleteextras(tx, h.ID, false)@@ -906,8 +905,6 @@ }
} if err == nil { err = tx.Commit() - } else { - tx.Rollback() } if err != nil { elog.Printf("error updating honk %d: %s", h.ID, err)@@ -922,6 +919,7 @@ if err != nil {
elog.Printf("can't begin tx: %s", err) return err } + defer tx.Rollback() err = deleteextras(tx, honkid, true) if err == nil {@@ -929,8 +927,6 @@ _, err = tx.Stmt(stmtDeleteHonk).Exec(honkid)
} if err == nil { err = tx.Commit() - } else { - tx.Rollback() } if err != nil { elog.Printf("error deleting honk %d: %s", honkid, err)@@ -1026,7 +1022,10 @@ }
h.Badonks = append(h.Badonks, Badonk{Who: who, What: react}) j, _ := jsonify(h.Badonks) db := opendatabase() - tx, _ := db.Begin() + tx, err := db.Begin() + if err != nil { + return + } _, _ = tx.Stmt(stmtDeleteOneMeta).Exec(h.ID, "badonks") _, _ = tx.Stmt(stmtSaveMeta).Exec(h.ID, "badonks", j) tx.Commit()