|
| 1 | +package sql |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "database/sql" |
| 6 | + "log/slog" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/jmoiron/sqlx" |
| 10 | + "github.com/maragudk/goqite" |
| 11 | + _ "github.com/mattn/go-sqlite3" |
| 12 | + "maragu.dev/errors" |
| 13 | +) |
| 14 | + |
| 15 | +type Helper struct { |
| 16 | + DB *sqlx.DB |
| 17 | + JobsQ *goqite.Queue |
| 18 | + log *slog.Logger |
| 19 | + path string |
| 20 | +} |
| 21 | + |
| 22 | +type NewHelperOptions struct { |
| 23 | + Log *slog.Logger |
| 24 | + Path string |
| 25 | +} |
| 26 | + |
| 27 | +// NewHelper with the given options. |
| 28 | +// If no logger is provided, logs are discarded. |
| 29 | +func NewHelper(opts NewHelperOptions) *Helper { |
| 30 | + if opts.Log == nil { |
| 31 | + opts.Log = slog.New(slog.DiscardHandler) |
| 32 | + } |
| 33 | + |
| 34 | + // - Set WAL mode (not strictly necessary each time because it's persisted in the database, but good for first run) |
| 35 | + // - Set busy timeout, so concurrent writers wait on each other instead of erroring immediately |
| 36 | + // - Enable foreign key checks |
| 37 | + opts.Path += "?_journal=WAL&_timeout=5000&_fk=true" |
| 38 | + |
| 39 | + return &Helper{ |
| 40 | + log: opts.Log, |
| 41 | + path: opts.Path, |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +func (h *Helper) Connect() error { |
| 46 | + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 47 | + defer cancel() |
| 48 | + |
| 49 | + h.log.Info("Starting database", "path", h.path) |
| 50 | + |
| 51 | + var err error |
| 52 | + h.DB, err = sqlx.ConnectContext(ctx, "sqlite3", h.path) |
| 53 | + if err != nil { |
| 54 | + return err |
| 55 | + } |
| 56 | + |
| 57 | + return nil |
| 58 | +} |
| 59 | + |
| 60 | +// InTransaction runs callback in a transaction, and makes sure to handle rollbacks, commits etc. |
| 61 | +func (h *Helper) InTransaction(ctx context.Context, callback func(tx *Tx) error) (err error) { |
| 62 | + tx, err := h.DB.BeginTxx(ctx, &sql.TxOptions{Isolation: sql.LevelSerializable}) |
| 63 | + if err != nil { |
| 64 | + return errors.Wrap(err, "error beginning transaction") |
| 65 | + } |
| 66 | + defer func() { |
| 67 | + if rec := recover(); rec != nil { |
| 68 | + err = rollback(tx, errors.Newf("panic: %v", rec)) |
| 69 | + } |
| 70 | + }() |
| 71 | + if err := callback(&Tx{Tx: tx}); err != nil { |
| 72 | + return rollback(tx, err) |
| 73 | + } |
| 74 | + if err := tx.Commit(); err != nil { |
| 75 | + return errors.Wrap(err, "error committing transaction") |
| 76 | + } |
| 77 | + |
| 78 | + return nil |
| 79 | +} |
| 80 | + |
| 81 | +// rollback a transaction, handling both the original error and any transaction rollback errors. |
| 82 | +func rollback(tx *sqlx.Tx, err error) error { |
| 83 | + if txErr := tx.Rollback(); txErr != nil { |
| 84 | + return errors.Wrap(err, "error rolling back transaction after error (transaction error: %v), original error", txErr) |
| 85 | + } |
| 86 | + return err |
| 87 | +} |
| 88 | + |
| 89 | +func (h *Helper) Ping(ctx context.Context) error { |
| 90 | + return h.InTransaction(ctx, func(tx *Tx) error { |
| 91 | + return tx.Exec(ctx, `select 1`) |
| 92 | + }) |
| 93 | +} |
| 94 | + |
| 95 | +func (h *Helper) Select(ctx context.Context, dest any, query string, args ...any) error { |
| 96 | + return h.DB.SelectContext(ctx, dest, query, args...) |
| 97 | +} |
| 98 | + |
| 99 | +func (h *Helper) Get(ctx context.Context, dest any, query string, args ...any) error { |
| 100 | + return h.DB.GetContext(ctx, dest, query, args...) |
| 101 | +} |
| 102 | + |
| 103 | +func (h *Helper) Exec(ctx context.Context, query string, args ...any) error { |
| 104 | + _, err := h.DB.ExecContext(ctx, query, args...) |
| 105 | + return err |
| 106 | +} |
| 107 | + |
| 108 | +type Tx struct { |
| 109 | + Tx *sqlx.Tx |
| 110 | +} |
| 111 | + |
| 112 | +func (t *Tx) Select(ctx context.Context, dest any, query string, args ...any) error { |
| 113 | + return t.Tx.SelectContext(ctx, dest, query, args...) |
| 114 | +} |
| 115 | + |
| 116 | +func (t *Tx) Get(ctx context.Context, dest any, query string, args ...any) error { |
| 117 | + return t.Tx.GetContext(ctx, dest, query, args...) |
| 118 | +} |
| 119 | + |
| 120 | +func (t *Tx) Exec(ctx context.Context, query string, args ...any) error { |
| 121 | + _, err := t.Tx.ExecContext(ctx, query, args...) |
| 122 | + return err |
| 123 | +} |
| 124 | + |
| 125 | +var ErrNoRows = sql.ErrNoRows |
0 commit comments