diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 922a380..cbd2a4e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -12,7 +12,7 @@ jobs: steps: - uses: actions/checkout@v7.0.1 - - uses: jdx/mise-action@v5.1.2 + - uses: jdx/mise-action@v5.2.7 - name: mise run check run: mise run check diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 524dc32..bc0c9c4 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -35,7 +35,7 @@ jobs: fi - name: Create GitHub Release - uses: taiki-e/create-gh-release-action@v1.11.0 + uses: taiki-e/create-gh-release-action@v1.12.0 with: token: ${{ secrets.GITHUB_TOKEN }} changelog: ${{ steps.module.outputs.changelog }} diff --git a/CHANGELOG.md b/CHANGELOG.md index 246a05e..5942571 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,71 @@ module only; the `postgres` and `sqlint` sub-modules each keep their own. ## [Unreleased] +## [v0.7.0] - 2026-11-11 + +A transaction's configuration is the standard library's `*sql.TxOptions`, passed as a trailing +argument, not variadic option functions: `DB.Begin`, `DB.Transact`, and `Transact` take it, and +nil keeps a default transaction. + +### Changed + +- **Breaking:** `DB.Begin(ctx, opts)` takes a `*sql.TxOptions` in place of variadic `TxOption` + values. Nil opens a default transaction; a non-nil value's `Isolation` and `ReadOnly` reach + the driver. +- **Breaking:** `DB.Transact(ctx, fn, opts)` and `Transact(ctx, b, fn, opts)` take a required + trailing `*sql.TxOptions` in place of variadic `TxOption` values. A call with no option + passes nil. +- **Breaking:** `Beginner.Begin` has `DB.Begin`'s new signature, + `Begin(ctx context.Context, opts *sql.TxOptions) (*Tx, error)`, so a type that implements + `Beginner` with its own `Begin` changes its signature. A type embedding `*DB` is unaffected. + +### Removed + +- **Breaking:** `TxOption` and the `Isolation` and `ReadOnly` option functions, with no + aliases. Set `sql.TxOptions.Isolation` and `sql.TxOptions.ReadOnly`. + +### Migrating from v0.6.x + +- **Options become one `*sql.TxOptions` value.** Pass nil for a default transaction. + + ```go + // v0.6.x + tx, err := db.Begin(ctx) + tx, err := db.Begin(ctx, sqlate.Isolation(sql.LevelSerializable)) + tx, err := db.Begin(ctx, sqlate.Isolation(sql.LevelRepeatableRead), sqlate.ReadOnly()) + + // v0.7.0 + tx, err := db.Begin(ctx, nil) + tx, err := db.Begin(ctx, &sql.TxOptions{Isolation: sql.LevelSerializable}) + tx, err := db.Begin(ctx, &sql.TxOptions{Isolation: sql.LevelRepeatableRead, ReadOnly: true}) + ``` + +- **`Transact` takes the options as its last argument.** A call that passed none passes nil. + + ```go + // v0.6.x + n, err := db.Transact(ctx, fn) + n, err := sqlate.Transact(ctx, b, fn, sqlate.ReadOnly()) + + // v0.7.0 + n, err := db.Transact(ctx, fn, nil) + n, err := sqlate.Transact(ctx, b, fn, &sql.TxOptions{ReadOnly: true}) + ``` + +- **A `Beginner` implementer's `Begin` takes `*sql.TxOptions`.** Pass the value on unchanged. + + ```go + // v0.6.x + func (a *AppDB) Begin(ctx context.Context, opts ...sqlate.TxOption) (*sqlate.Tx, error) { + return a.DB.Begin(ctx, opts...) + } + + // v0.7.0 + func (a *AppDB) Begin(ctx context.Context, opts *sql.TxOptions) (*sqlate.Tx, error) { + return a.DB.Begin(ctx, opts) + } + ``` + ## [v0.6.1] - 2026-10-15 ### Fixed @@ -217,7 +282,8 @@ experiment's library packages. - `sqltest`, the scripted `database/sql` driver every consumer's unit tier runs over: `Open`, `Recorder`, `Response`, and the stub `Dialect`. -[Unreleased]: https://github.com/standards-lab/sqlate/compare/v0.6.1...HEAD +[Unreleased]: https://github.com/standards-lab/sqlate/compare/v0.7.0...HEAD +[v0.7.0]: https://github.com/standards-lab/sqlate/compare/v0.6.1...v0.7.0 [v0.6.1]: https://github.com/standards-lab/sqlate/compare/v0.6.0...v0.6.1 [v0.6.0]: https://github.com/standards-lab/sqlate/compare/v0.5.0...v0.6.0 [v0.5.0]: https://github.com/standards-lab/sqlate/compare/v0.4.1...v0.5.0 diff --git a/STANDARDS.md b/STANDARDS.md index 1b00c07..b124f20 100644 --- a/STANDARDS.md +++ b/STANDARDS.md @@ -10,6 +10,7 @@ The judgement calls the standards-reviewer applies to sqlate, beyond what `mise - `architecture/standards/go-elemental/principles/dsl-driven-services.md`: `query`, `header`, and every authored `.sql` file, the library's patterns included. - `architecture/standards/go-elemental/principles/baseline-standards.md`: `query`'s guards and projections, which take every name and bound from their caller. - `architecture/standards/go-elemental/principles/utc-times.md`: `query.Scanner` and `query.Scalar`, which return every time in UTC, a cursor's keyed time, and the `postgres` dialect's `timestamp with time zone` history. +- `architecture/standards/go-elemental/principles/construction-and-execution.md`: `DB.Begin`, `DB.Transact`, and `Transact` take their transaction's configuration as a trailing `*sql.TxOptions` whose nil keeps a default transaction, and a new runner that takes configuration does the same. - `architecture/principles/service-tiers.md`: the `Dialect` boundary between the base module and each engine sub-module. - `architecture/principles/tool-beside-library.md`: the `sqlint` package and its command. - `architecture/principles/validation-first.md`: authored SQL, `sqlint.Load` and `migrate.New`. diff --git a/doc.go b/doc.go index c29339d..7bc1f53 100644 --- a/doc.go +++ b/doc.go @@ -23,7 +23,8 @@ // [Session] is the method set the query and migrate packages take, // implemented by both [DB] and [Tx], so the same statement runs against the // pool or inside a transaction. The consumer owns the transaction boundary: -// [DB.Begin] opens a [Tx] with [TxOption] values applied, and [DB.Transact] +// [DB.Begin] opens a [Tx] with the standard library's +// [database/sql.TxOptions], nil for a default transaction, and [DB.Transact] // runs one unit of work with a result: commit on success, rollback on the // unit's error or panic; [Transact] is the same runner over any [Beginner]. // [Tx.Commit] routes its error through the dialect's MapError, the one place diff --git a/docs/features.md b/docs/features.md index 9560d48..5b36fb7 100644 --- a/docs/features.md +++ b/docs/features.md @@ -23,17 +23,19 @@ dialect before returning it. `ErrorMapper` is the capability both expose for err after a call returns, from `rows.Err` and `Scan`; the typed values use it, and a consumer running raw SQL through a session can too. -**Transactions.** `DB.Begin(ctx, opts...)` opens a `*Tx` with `TxOption` values applied: -`Isolation(level)` and `ReadOnly()`. `Tx.Commit` maps its error, the one place a violation -deferred to `COMMIT` can be classified; `Tx.Rollback` does not. `DB.Transact(ctx, fn, opts...)` -is the runner: it begins, calls `fn(tx)`, commits on success, and returns `fn`'s result. On +**Transactions.** `DB.Begin(ctx, opts)` opens a `*Tx` with the standard library's +`*sql.TxOptions`: nil is a default transaction, and a non-nil value's `Isolation` and `ReadOnly` +reach the driver, as in `&sql.TxOptions{Isolation: sql.LevelSerializable, ReadOnly: true}`. +`Tx.Commit` maps its error, the one place a violation deferred to `COMMIT` can be classified; +`Tx.Rollback` does not. `DB.Transact(ctx, fn, opts)` is the runner, taking the same trailing +options: it begins, calls `fn(tx)`, commits on success, and returns `fn`'s result. On `fn`'s error it rolls back and returns that error with a rollback failure joined onto it. A panic in `fn` rolls back and re-panics, so no transaction leaks to the pool. `Beginner` is the -interface of a session that can open a transaction, satisfied by `*DB` and any type embedding -it. A protocol handed a session rather than a transaction asserts `Beginner` to open its own. -`sqlate.Transact(ctx, b, fn, opts...)` is the same runner over any `Beginner`, so a protocol, or -an application's own pool type embedding `*DB`, runs its unit exactly as `DB.Transact` does; -`DB.Transact` is `Transact` over the `*DB`. +interface of a session that can open a transaction. Its `Begin` has `DB.Begin`'s signature, and +`*DB` and any type embedding it satisfy it. A protocol handed a session rather than a +transaction asserts `Beginner` to open its own. `sqlate.Transact(ctx, b, fn, opts)` is the same +runner over any `Beginner`, so a protocol, or an application's own pool type embedding `*DB`, +runs its unit exactly as `DB.Transact` does; `DB.Transact` is `Transact` over the `*DB`. **Pinned connections.** `DB.Conn(ctx)` pins one connection for a protocol that needs session scope: a session-level lock, or DDL an engine refuses inside a transaction. The caller closes diff --git a/docs/quick-start.md b/docs/quick-start.md index 0c54507..8585682 100644 --- a/docs/quick-start.md +++ b/docs/quick-start.md @@ -572,7 +572,7 @@ func (s *Store) Seed(ctx context.Context, rows []SeedTeam) (int64, error) { inserted += n } return inserted, nil - }) + }, nil) } // Create validates the command and runs it; the result is the new row's @@ -619,7 +619,7 @@ func (s *Store) Replace(ctx context.Context, id string, version int64, t NewTeam return Identity{}, err } return s.create.One(ctx, tx, query.ArgsOf(t)) - }) + }, nil) } ``` @@ -638,7 +638,8 @@ func (s *Store) Replace(ctx context.Context, id string, version int64, t NewTeam - `Rename` validates, then runs the guarded command with the version the caller read and returns the new one; `With` adds the id the command needs beside the command's own fields. - `Replace` runs two commands as one unit of work: `Transact` commits on success and rolls back - on error or panic. + on error or panic. Its last argument is the transaction's `*sql.TxOptions`; nil is a default + transaction, and `&sql.TxOptions{Isolation: sql.LevelSerializable}` would run it serializable. ## 9. Test without a database diff --git a/mise.toml b/mise.toml index 17b3f18..f79efc0 100644 --- a/mise.toml +++ b/mise.toml @@ -1,6 +1,6 @@ [tools] -go = "1.27.2" -golangci-lint = "2.14.0" +go = "1.27.4" +golangci-lint = "2.15.0" jq = "1.8.2" "aqua:google/go-containerregistry" = "0.22.1" diff --git a/postgres/CHANGELOG.md b/postgres/CHANGELOG.md index 89f81b9..52fa7d1 100644 --- a/postgres/CHANGELOG.md +++ b/postgres/CHANGELOG.md @@ -7,6 +7,21 @@ changelog covers this sub-module only; the base module keeps its own. ## [Unreleased] +## [v0.7.0] - 2026-11-11 + +### Changed + +- **Breaking:** The `sqlate` requirement is v0.7.0, whose `DB.Begin`, `DB.Transact`, and + `Transact` take a `*sql.TxOptions` (nil for a default transaction) in place of the variadic + `TxOption`, and whose `Beginner` has `DB.Begin`'s signature. A module that requires this + release builds against it, so code calling those methods moves to the new form: drop + `sqlate.Isolation(level)` and `sqlate.ReadOnly()`, pass + `&sql.TxOptions{Isolation: level, ReadOnly: true}` or nil instead, and give `Transact` that + trailing argument. The dialect's own API is unchanged. +- The live proofs' counting session implements the new `Beginner`. + +Requires `github.com/standards-lab/sqlate v0.7.0`. + ## [v0.6.0] - 2026-10-15 ### Added @@ -112,7 +127,8 @@ The first release of the PostgreSQL dialect, against `github.com/standards-lab/s - The integration tier: the migrate and query proofs against a live engine, behind the `integration` build tag. -[Unreleased]: https://github.com/standards-lab/sqlate/compare/postgres/v0.6.0...HEAD +[Unreleased]: https://github.com/standards-lab/sqlate/compare/postgres/v0.7.0...HEAD +[v0.7.0]: https://github.com/standards-lab/sqlate/compare/postgres/v0.6.0...postgres/v0.7.0 [v0.6.0]: https://github.com/standards-lab/sqlate/compare/postgres/v0.5.0...postgres/v0.6.0 [v0.5.0]: https://github.com/standards-lab/sqlate/compare/postgres/v0.4.0...postgres/v0.5.0 [v0.4.0]: https://github.com/standards-lab/sqlate/compare/postgres/v0.3.0...postgres/v0.4.0 diff --git a/postgres/go.mod b/postgres/go.mod index 798f3a1..88f28e7 100644 --- a/postgres/go.mod +++ b/postgres/go.mod @@ -3,14 +3,14 @@ module github.com/standards-lab/sqlate/postgres go 1.27 require ( - github.com/jackc/pgx/v5 v5.11.0 - github.com/standards-lab/sqlate v0.6.0 + github.com/jackc/pgx/v5 v5.13.0 + github.com/standards-lab/sqlate v0.6.1 ) require ( github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect - github.com/jackc/puddle/v2 v2.2.2 // indirect - golang.org/x/sync v0.17.0 // indirect - golang.org/x/text v0.29.0 // indirect + github.com/jackc/puddle/v2 v2.2.3 // indirect + golang.org/x/sync v0.22.0 // indirect + golang.org/x/text v0.41.0 // indirect ) diff --git a/postgres/go.sum b/postgres/go.sum index d42d026..7891b8b 100644 --- a/postgres/go.sum +++ b/postgres/go.sum @@ -5,23 +5,23 @@ github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsI github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo= github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= -github.com/jackc/pgx/v5 v5.11.0 h1:IzBBtyK9AHqf98cctWFifYSci2hgQR/cd56wB4p+ogg= -github.com/jackc/pgx/v5 v5.11.0/go.mod h1:mal1tBGAFfLHvZzaYh77YS/eC6IX9OWbRV1QIIM0Jn4= -github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo= -github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= +github.com/jackc/pgx/v5 v5.13.0 h1:Hl08k64maU0nrRyVUEbwCnK6/gT0EhMrtAqBHz9/gA4= +github.com/jackc/pgx/v5 v5.13.0/go.mod h1:u2Fi6no03HCSBYE+ZVdLxbwdu3xyi2vU+K/YNJioSds= +github.com/jackc/puddle/v2 v2.2.3 h1:f8HoREISb3l5eO9coPgK7U3hKLhWGokOFVcThl5jrao= +github.com/jackc/puddle/v2 v2.2.3/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/standards-lab/sqlate v0.6.0 h1:QdSuEo7sPamge38X3vocTRc6mvMthJWJhKtDcpyYyLw= -github.com/standards-lab/sqlate v0.6.0/go.mod h1:pikNmGf/z3x46SBWFXgt/xKIDZuquSgCoXy5OWBFFR4= +github.com/standards-lab/sqlate v0.6.1 h1:Kgp8freMX9Gs2KweBwG4IPzENgUmSJq8+r4tq02cDNw= +github.com/standards-lab/sqlate v0.6.1/go.mod h1:pikNmGf/z3x46SBWFXgt/xKIDZuquSgCoXy5OWBFFR4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U= github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= -golang.org/x/sync v0.17.0 h1:l60nONMj9l5drqw6jlhIELNv9I0A4OFgRsG9k2oT9Ug= -golang.org/x/sync v0.17.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI= -golang.org/x/text v0.29.0 h1:1neNs90w9YzJ9BocxfsQNHKuAT4pkghyXc4nhZ6sJvk= -golang.org/x/text v0.29.0/go.mod h1:7MhJOA9CD2qZyOKYazxdYMF85OwPdEr9jTtBpO7ydH4= +golang.org/x/sync v0.22.0 h1:SZjpbeLmrCk4xhRSZFNZW5gFUeCeFgjekvI/+gfScek= +golang.org/x/sync v0.22.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0= +golang.org/x/text v0.41.0 h1:vz/seA0lnX87Othu2f/0L24RcgrXD9/YFTSuGjj3rH8= +golang.org/x/text v0.41.0/go.mod h1:jvf1O8ajNzZqhSrQBPbutR/EB83Cc0CFrezNQIwbb5M= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/postgres/returning_live_test.go b/postgres/returning_live_test.go index d8f2b49..3016325 100644 --- a/postgres/returning_live_test.go +++ b/postgres/returning_live_test.go @@ -124,9 +124,9 @@ func (c *counted) QueryContext(ctx context.Context, q string, args ...any) (*sql return c.DB.QueryContext(ctx, q, args...) } -func (c *counted) Begin(ctx context.Context, opts ...sqlate.TxOption) (*sqlate.Tx, error) { +func (c *counted) Begin(ctx context.Context, opts *sql.TxOptions) (*sqlate.Tx, error) { c.begin++ - return c.DB.Begin(ctx, opts...) + return c.DB.Begin(ctx, opts) } // calls is what one scenario cost: the session's calls and the engine's @@ -465,7 +465,7 @@ func TestLive_ReturningInsideACallersTransaction(t *testing.T) { return row, fmt.Errorf("retire again: %+v, changed %v: %w", row, changed, err) } return row, nil - }) + }, nil) sent := tr.take() if err != nil { t.Errorf("%s: Transact: %v", f.name, err) diff --git a/query/returning.go b/query/returning.go index d4cdc8e..5761828 100644 --- a/query/returning.go +++ b/query/returning.go @@ -304,7 +304,7 @@ func (r Returning[T]) One(ctx context.Context, s sqlate.Session, args Args) (T, out, err := sqlate.Transact(ctx, s, func(tx *sqlate.Tx) (changedRow[T], error) { row, changed, err := r.fallback(ctx, tx, args) return changedRow[T]{row: row, changed: changed}, err - }) + }, nil) return out.row, out.changed, err } return zero, false, ErrTransactionRequired diff --git a/query/returning_test.go b/query/returning_test.go index 9af3e4e..6c526eb 100644 --- a/query/returning_test.go +++ b/query/returning_test.go @@ -415,7 +415,7 @@ func TestReturningOne_InsideATransactionOpensNone(t *testing.T) { pool, rec := sqltest.Open(t, script...) db := sqlate.Wrap(pool, f.dialect) r := rename(t, f.dialect) - tx, err := db.Begin(ctx) + tx, err := db.Begin(ctx, nil) if err != nil { t.Fatal(err) } diff --git a/query/rows_test.go b/query/rows_test.go index f3acf49..2448f51 100644 --- a/query/rows_test.go +++ b/query/rows_test.go @@ -163,7 +163,7 @@ func TestExec_TransactionRequired(t *testing.T) { _, err := db.Transact(ctx, func(tx *sqlate.Tx) (struct{}, error) { _, err := lock.Exec(ctx, tx, query.Args{"key": int64(1)}) return struct{}{}, err - }) + }, nil) if err != nil { t.Errorf("Exec in a transaction: %v", err) } diff --git a/session.go b/session.go index 9ce52c0..a89524e 100644 --- a/session.go +++ b/session.go @@ -70,8 +70,9 @@ type TryLocker interface { // Beginner is a session that can open a transaction: *DB, and any type that // embeds it. A protocol that must run several statements as one unit, and // is handed a session rather than a transaction, asserts it to open its own. +// Begin has DB.Begin's signature: nil opts opens a default transaction. type Beginner interface { - Begin(ctx context.Context, opts ...TxOption) (*Tx, error) + Begin(ctx context.Context, opts *sql.TxOptions) (*Tx, error) } var _ Beginner = (*DB)(nil) diff --git a/sqlate_test.go b/sqlate_test.go index 658f473..adc275a 100644 --- a/sqlate_test.go +++ b/sqlate_test.go @@ -97,7 +97,7 @@ func TestTx_EveryMethodMapsIncludingCommit(t *testing.T) { rec.FailPrepare = func(string) error { return errDriver } rec.FailCommit = errDriver - tx, err := db.Begin(ctx) + tx, err := db.Begin(ctx, nil) if err != nil { t.Fatalf("begin: %v", err) } @@ -110,11 +110,24 @@ func TestTx_EveryMethodMapsIncludingCommit(t *testing.T) { mapped(t, tx.Commit()) } +func TestBegin_NilOpensADefaultTransaction(t *testing.T) { + db, rec := wrap(t) + tx, err := db.Begin(context.Background(), nil) + if err != nil { + t.Fatalf("begin: %v", err) + } + _ = tx.Rollback() + begin := rec.Calls()[0] + if begin.Op != sqltest.OpBegin || begin.TxOptions != (driver.TxOptions{}) { + t.Errorf("begin recorded %+v, want default options", begin) + } +} + func TestBegin_OptionsReachTheDriverAndFailureWrapsConnectionFailed(t *testing.T) { ctx := context.Background() db, rec := wrap(t) - tx, err := db.Begin(ctx, sqlate.Isolation(sql.LevelSerializable), sqlate.ReadOnly()) + tx, err := db.Begin(ctx, &sql.TxOptions{Isolation: sql.LevelSerializable, ReadOnly: true}) if err != nil { t.Fatalf("begin: %v", err) } @@ -127,7 +140,7 @@ func TestBegin_OptionsReachTheDriverAndFailureWrapsConnectionFailed(t *testing.T } rec.FailBegin = errDriver - _, err = db.Begin(ctx) + _, err = db.Begin(ctx, nil) if !errors.Is(err, sqlate.ErrConnectionFailed) || !errors.Is(err, errDriver) { t.Errorf("begin failure = %v, want ErrConnectionFailed wrapping the driver error", err) } @@ -141,7 +154,7 @@ func TestTransact_CommitsAndReturnsTheResult(t *testing.T) { return 0, err } return res.RowsAffected() - }) + }, nil) if err != nil || n != 1 { t.Fatalf("Transact = %d, %v", n, err) } @@ -149,13 +162,29 @@ func TestTransact_CommitsAndReturnsTheResult(t *testing.T) { if got := rec.Ops(); !slices.Equal(got, want) { t.Errorf("ops = %v, want %v", got, want) } + if got := rec.Calls()[0].TxOptions; got != (driver.TxOptions{}) { + t.Errorf("begin options = %+v, want default options for nil", got) + } +} + +func TestTransact_PassesItsOptionsToBegin(t *testing.T) { + db, rec := wrap(t) + _, err := db.Transact(context.Background(), func(*sqlate.Tx) (int, error) { return 1, nil }, + &sql.TxOptions{Isolation: sql.LevelRepeatableRead, ReadOnly: true}) + if err != nil { + t.Fatalf("Transact: %v", err) + } + got := rec.Calls()[0].TxOptions + if got.Isolation != driver.IsolationLevel(sql.LevelRepeatableRead) || !got.ReadOnly { + t.Errorf("begin options = %+v, want repeatable read, read-only", got) + } } func TestTransact_RollsBackOnErrorAndJoinsRollbackFailure(t *testing.T) { db, rec := wrap(t) unit := errors.New("unit failed") - _, err := db.Transact(context.Background(), func(*sqlate.Tx) (int, error) { return 0, unit }) + _, err := db.Transact(context.Background(), func(*sqlate.Tx) (int, error) { return 0, unit }, nil) if !errors.Is(err, unit) { t.Fatalf("err = %v, want the unit's error", err) } @@ -164,7 +193,7 @@ func TestTransact_RollsBackOnErrorAndJoinsRollbackFailure(t *testing.T) { } rec.FailRollback = errDriver - _, err = db.Transact(context.Background(), func(*sqlate.Tx) (int, error) { return 0, unit }) + _, err = db.Transact(context.Background(), func(*sqlate.Tx) (int, error) { return 0, unit }, nil) if !errors.Is(err, unit) || !errors.Is(err, errDriver) { t.Errorf("err = %v, want the unit error joined with the rollback error", err) } @@ -173,7 +202,7 @@ func TestTransact_RollsBackOnErrorAndJoinsRollbackFailure(t *testing.T) { func TestTransact_CommitFailureIsMapped(t *testing.T) { db, rec := wrap(t) rec.FailCommit = errDriver - _, err := db.Transact(context.Background(), func(*sqlate.Tx) (int, error) { return 1, nil }) + _, err := db.Transact(context.Background(), func(*sqlate.Tx) (int, error) { return 1, nil }, nil) mapped(t, err) } @@ -187,7 +216,7 @@ func TestTransact_PanicRollsBackAndRepanics(t *testing.T) { t.Errorf("ops = %v, want begin then rollback", got) } }() - _, _ = db.Transact(context.Background(), func(*sqlate.Tx) (int, error) { panic("boom") }) + _, _ = db.Transact(context.Background(), func(*sqlate.Tx) (int, error) { panic("boom") }, nil) } // appDB is an application's own pool type, a sqlate.Beginner through the @@ -203,26 +232,26 @@ func TestTransact_RunsTheUnitOnAnEmbeddingBeginner(t *testing.T) { return 0, err } return res.RowsAffected() - }, sqlate.ReadOnly()) + }, &sql.TxOptions{Isolation: sql.LevelSerializable, ReadOnly: true}) if err != nil || n != 1 { t.Fatalf("Transact = %d, %v", n, err) } if got, want := rec.Ops(), []sqltest.Op{sqltest.OpBegin, sqltest.OpExec, sqltest.OpCommit}; !slices.Equal(got, want) { t.Errorf("ops = %v, want %v", got, want) } - if !rec.Calls()[0].TxOptions.ReadOnly { - t.Errorf("begin options = %+v, want the options applied", rec.Calls()[0].TxOptions) + if got := rec.Calls()[0].TxOptions; got.Isolation != driver.IsolationLevel(sql.LevelSerializable) || !got.ReadOnly { + t.Errorf("begin options = %+v, want the options as given", got) } unit := errors.New("unit failed") rec.FailRollback = errDriver - if _, err := sqlate.Transact(ctx, appDB{db}, func(*sqlate.Tx) (int, error) { return 0, unit }); !errors.Is(err, unit) || !errors.Is(err, errDriver) { + if _, err := sqlate.Transact(ctx, appDB{db}, func(*sqlate.Tx) (int, error) { return 0, unit }, nil); !errors.Is(err, unit) || !errors.Is(err, errDriver) { t.Errorf("err = %v, want the unit error joined with the rollback error", err) } rec.FailBegin = errDriver called := false - if _, err := sqlate.Transact(ctx, appDB{db}, func(*sqlate.Tx) (int, error) { called = true; return 0, nil }); !errors.Is(err, sqlate.ErrConnectionFailed) || called { + if _, err := sqlate.Transact(ctx, appDB{db}, func(*sqlate.Tx) (int, error) { called = true; return 0, nil }, nil); !errors.Is(err, sqlate.ErrConnectionFailed) || called { t.Errorf("begin failure = %v, unit called %v; want ErrConnectionFailed and no unit", err, called) } } @@ -288,7 +317,7 @@ func TestMapError_ClassifiesConnectivityBeforeTheDialect(t *testing.T) { // it, so it is asserted through the mapper rather than the driver. connectionFailed(db.MapError(fmt.Errorf("query: %w", driver.ErrBadConn))) - tx, err := db.Begin(ctx) + tx, err := db.Begin(ctx, nil) if err != nil { t.Fatalf("begin: %v", err) } diff --git a/sqlint/CHANGELOG.md b/sqlint/CHANGELOG.md index 4da22d4..9ec8b4f 100644 --- a/sqlint/CHANGELOG.md +++ b/sqlint/CHANGELOG.md @@ -7,6 +7,18 @@ changelog covers this sub-module only; the base module keeps its own. ## [Unreleased] +## [v0.4.0] - 2026-11-11 + +### Changed + +- **Breaking:** The `sqlate` requirement is v0.7.0, whose `DB.Begin`, `DB.Transact`, and + `Transact` take a `*sql.TxOptions` (nil for a default transaction) in place of the variadic + `TxOption`, and whose `Beginner` has `DB.Begin`'s signature. A module that requires the + linter, through a `tool` directive included, builds against that release. The linter's API + and findings are unchanged. + +Requires `github.com/standards-lab/sqlate v0.7.0`. + ## [v0.3.0] - 2026-10-09 ### Changed @@ -79,7 +91,8 @@ The first release of the linter, against `github.com/standards-lab/sqlate v0.1.0 - `cmd/sqlint`, the command: one root argument, findings as `path:line: message`, exit 1 on any finding. -[Unreleased]: https://github.com/standards-lab/sqlate/compare/sqlint/v0.3.0...HEAD +[Unreleased]: https://github.com/standards-lab/sqlate/compare/sqlint/v0.4.0...HEAD +[v0.4.0]: https://github.com/standards-lab/sqlate/compare/sqlint/v0.3.0...sqlint/v0.4.0 [v0.3.0]: https://github.com/standards-lab/sqlate/compare/sqlint/v0.2.1...sqlint/v0.3.0 [v0.2.1]: https://github.com/standards-lab/sqlate/compare/sqlint/v0.2.0...sqlint/v0.2.1 [v0.2.0]: https://github.com/standards-lab/sqlate/compare/sqlint/v0.1.2...sqlint/v0.2.0 diff --git a/sqlint/go.mod b/sqlint/go.mod index c7f10bf..6a72a19 100644 --- a/sqlint/go.mod +++ b/sqlint/go.mod @@ -4,5 +4,5 @@ go 1.27 require ( github.com/BurntSushi/toml v1.6.0 - github.com/standards-lab/sqlate v0.5.0 + github.com/standards-lab/sqlate v0.6.1 ) diff --git a/sqlint/go.sum b/sqlint/go.sum index e1898fc..73a70ec 100644 --- a/sqlint/go.sum +++ b/sqlint/go.sum @@ -1,4 +1,4 @@ github.com/BurntSushi/toml v1.6.0 h1:dRaEfpa2VI55EwlIW72hMRHdWouJeRF7TPYhI+AUQjk= github.com/BurntSushi/toml v1.6.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho= -github.com/standards-lab/sqlate v0.5.0 h1:2QEbhIzy2mtMxY2XMfqqqzOlCZs2I3vZNN7fB9daBjw= -github.com/standards-lab/sqlate v0.5.0/go.mod h1:pikNmGf/z3x46SBWFXgt/xKIDZuquSgCoXy5OWBFFR4= +github.com/standards-lab/sqlate v0.6.1 h1:Kgp8freMX9Gs2KweBwG4IPzENgUmSJq8+r4tq02cDNw= +github.com/standards-lab/sqlate v0.6.1/go.mod h1:pikNmGf/z3x46SBWFXgt/xKIDZuquSgCoXy5OWBFFR4= diff --git a/tx.go b/tx.go index 3aed34c..cfe303d 100644 --- a/tx.go +++ b/tx.go @@ -7,20 +7,6 @@ import ( "fmt" ) -// TxOption sets a field of the sql.TxOptions Begin opens with. -type TxOption func(*sql.TxOptions) - -// Isolation requests an isolation level; the guard's serializable claim has -// a code path through it. -func Isolation(level sql.IsolationLevel) TxOption { - return func(o *sql.TxOptions) { o.Isolation = level } -} - -// ReadOnly opens the transaction read-only. -func ReadOnly() TxOption { - return func(o *sql.TxOptions) { o.ReadOnly = true } -} - // Tx is one transaction, a Session whose errors are classified and mapped // on every call and on commit. type Tx struct { @@ -28,14 +14,11 @@ type Tx struct { dialect Dialect } -// Begin opens a transaction with the options applied; a failure to begin -// wraps ErrConnectionFailed. -func (d *DB) Begin(ctx context.Context, opts ...TxOption) (*Tx, error) { - var o sql.TxOptions - for _, opt := range opts { - opt(&o) - } - tx, err := d.pool.BeginTx(ctx, &o) +// Begin opens a transaction with opts, which it passes to the driver as +// given; nil opens a default transaction. A failure to begin wraps +// ErrConnectionFailed. +func (d *DB) Begin(ctx context.Context, opts *sql.TxOptions) (*Tx, error) { + tx, err := d.pool.BeginTx(ctx, opts) if err != nil { return nil, fmt.Errorf("%w: %w", ErrConnectionFailed, err) } @@ -69,20 +52,22 @@ func (t *Tx) Commit() error { return t.MapError(t.tx.Commit()) } func (t *Tx) Rollback() error { return t.tx.Rollback() } // Transact runs fn as one unit of work with a result on the pool: it is -// the root Transact over d. -func (d *DB) Transact[T any](ctx context.Context, fn func(*Tx) (T, error), opts ...TxOption) (T, error) { - return Transact(ctx, d, fn, opts...) +// the root Transact over d, opening the transaction with opts (nil for a +// default transaction). +func (d *DB) Transact[T any](ctx context.Context, fn func(*Tx) (T, error), opts *sql.TxOptions) (T, error) { + return Transact(ctx, d, fn, opts) } // Transact runs fn as one unit of work with a result in a transaction b -// opens: begin with opts, fn, commit on success. On fn's error it rolls back -// and returns that error, with a rollback failure joined onto it. A panic in -// fn rolls back and re-panics, so no transaction leaks to the pool's reaper. -// b is *DB or any type that embeds it, so a protocol handed a session it -// asserts to Beginner runs its unit the way DB.Transact does. -func Transact[T any](ctx context.Context, b Beginner, fn func(*Tx) (T, error), opts ...TxOption) (T, error) { +// opens: begin with opts (nil for a default transaction), fn, commit on +// success. On fn's error it rolls back and returns that error, with a +// rollback failure joined onto it. A panic in fn rolls back and re-panics, +// so no transaction leaks to the pool's reaper. b is *DB or any type that +// embeds it, so a protocol handed a session it asserts to Beginner runs its +// unit the way DB.Transact does. +func Transact[T any](ctx context.Context, b Beginner, fn func(*Tx) (T, error), opts *sql.TxOptions) (T, error) { var zero T - tx, err := b.Begin(ctx, opts...) + tx, err := b.Begin(ctx, opts) if err != nil { return zero, err }