go-transaction-manager

0
README.md

Go transaction manager

Go Reference Test Status Coverage Status Go Report Card License

Transaction manager is an abstraction to coordinate database transaction boundaries.

Easiest way to get the perfect repository.

Supported implementations

Installation

To install some support database use

go get github.com/avito-tech/go-transaction-manager/drivers/{name}
.

For example

go get github.com/avito-tech/go-transaction-manager/drivers/sqlx/v2
.

Backwards Compatibility

The library is compatible with the most recent two versions of Go. Compatibility beyond that is not guaranteed.

The critical bugs are firstly solved for the most recent two Golang versions and then for older ones if it is simple.

Disclaimer: Keep your dependencies up to date, even indirect ones.

go get -u && go mod tidy
helps you.

Note: The go-transaction-manager uses some old dependencies to support backwards compatibility for old versions of Go.

Usage

To use multiple transactions from different databases, you need to set CtxKey in Settings by WithCtxKey (docs).

For nested transactions with different transaction managers, you need to use ChainedMW (docs).

To skip a transaction rollback due to an error, use ErrSkip or Skippable

Explanation of the approach English, Russian article and youtube.

Examples with an ideal repository and nested transactions.

Below is an example how to start usage.

Benchmark

Comparing examples with and without trm.

Contribution

Requirements

Local Running

  • To install all dependencies use
    make go.mod.tidy
    or
    make go.mod.vendor
    .
  • To run all tests use
    make test
    or
    make test.with_real_db
    for integration tests.

To run database by docker, there is docker-compose.yaml.

For full GitHub Actions run, you can use act.

Running old go versions

To stop Golang upgrading set environment variable

GOTOOLCHAIN=local
.

Use

-mod=readonly
to prevent
go.mod
modification.

To run tests

go1.16 test -race -mod=readonly ./...

How to bump up Golang version in CI/CD

  1. Changes in .github/workflows/main.yaml.
    1. Add all old version of Go in
      go-version:
      for
      tests-units
      job.
    2. Update
      go-version:
      on current version of Go for
      lint
      and
      tests-integration
      jobs.
  2. Update build tags by replacing
    build go1.xx
    on new version.

Resolve problems with old version of dependencies

To build

go.mod
compatible for old version use
go mod tidy -compat=1.13
(docs).

However,

--compat
doesn't always work correct and we need to set some library versions manually.

  1. go get go.uber.org/multierr@v1.9.0
    in trm, sql, sqlx.
  2. go get github.com/mattn/go-sqlite3@v1.14.14
    in trm, sql, sqlx.
  3. go get github.com/stretchr/testify@v1.8.2
    in trm, sql, sqlx, goredis8, mongo.
  4. go get github.com/jackc/pgconn@v1.14.2
    in pgxv4. Golang version was bumped up from 1.12 to 1.17 in pgconn v1.14.3.
  5. go get golang.org/x/text@v0.13.0
    in pgxv4.