From 4a342262a5064e7ee0759c02c41e8929d5c5433d Mon Sep 17 00:00:00 2001 From: gak Date: Wed, 24 Jul 2024 13:50:38 +1000 Subject: [PATCH] fix: remove unused savepoints and just use begin/commit (#2143) Fixes: #2125 --- backend/controller/sql/conn.go | 34 ++++++++++++++-------------------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/backend/controller/sql/conn.go b/backend/controller/sql/conn.go index 3b69110ec9..3d2d9933cb 100644 --- a/backend/controller/sql/conn.go +++ b/backend/controller/sql/conn.go @@ -4,7 +4,6 @@ import ( "context" "errors" "fmt" - "github.com/jackc/pgx/v5" ) @@ -39,8 +38,7 @@ func (d *DB) Begin(ctx context.Context) (*Tx, error) { } type Tx struct { - tx pgx.Tx - savepoints []string + tx pgx.Tx *Queries } @@ -49,33 +47,29 @@ func (t *Tx) Conn() ConnI { return t.tx } func (t *Tx) Tx() pgx.Tx { return t.tx } func (t *Tx) Begin(ctx context.Context) (*Tx, error) { - savepoint := fmt.Sprintf("savepoint_%d", len(t.savepoints)) - t.savepoints = append(t.savepoints, savepoint) - _, err := t.tx.Exec(ctx, `SAVEPOINT `+savepoint) + _, err := t.tx.Begin(ctx) if err != nil { - return nil, err + return nil, fmt.Errorf("beginning transaction: %w", err) } - return &Tx{tx: t.tx, savepoints: t.savepoints, Queries: t.Queries}, nil + return &Tx{tx: t.tx, Queries: t.Queries}, nil } func (t *Tx) Commit(ctx context.Context) error { - if len(t.savepoints) == 0 { - return t.tx.Commit(ctx) + err := t.tx.Commit(ctx) + if err != nil { + return fmt.Errorf("committing transaction: %w", err) } - savepoint := t.savepoints[len(t.savepoints)-1] - t.savepoints = t.savepoints[:len(t.savepoints)-1] - _, err := t.tx.Exec(ctx, `RELEASE SAVEPOINT `+savepoint) - return err + + return nil } func (t *Tx) Rollback(ctx context.Context) error { - if len(t.savepoints) == 0 { - return t.tx.Rollback(ctx) + err := t.tx.Rollback(ctx) + if err != nil { + return fmt.Errorf("rolling back transaction: %w", err) } - savepoint := t.savepoints[len(t.savepoints)-1] - t.savepoints = t.savepoints[:len(t.savepoints)-1] - _, err := t.tx.Exec(ctx, `ROLLBACK TO SAVEPOINT `+savepoint) - return err + + return nil } // CommitOrRollback can be used in a defer statement to commit or rollback a