go-transaction-manager
Описание
Transaction manager for GoLang
Языки
- Go98,3%
- Shell1,3%
- Makefile0,4%
Go transaction manager
Transaction manager is an abstraction to coordinate database transaction boundaries.
Easiest way to get the perfect repository.
Supported implementations
- database/sql, docs ( Go 1.13)
- jmoiron/sqlx, docs ( Go 1.13)
- gorm, docs ( Go 1.18)
- mongo-go-driver, docs ( Go 1.13)
- go-redis/redis, docs ( Go 1.17)
- pgx_v4, docs ( Go 1.16)
- pgx_v5, docs ( Go 1.19)
Installation
To install some support database use .
For example .
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.
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
ormake go.mod.tidy.make go.mod.vendor - To run all tests use
ormake testfor integration tests.make test.with_real_db
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 .
Use to prevent modification.
To run tests
go1.16 test -race -mod=readonly ./...
How to bump up Golang version in CI/CD
- Changes in .github/workflows/main.yaml.
- Add all old version of Go in
forgo-version:job.tests-units - Update
on current version of Go forgo-version:andlintjobs.tests-integration
- Add all old version of Go in
- Update build tags by replacing
on new version.build go1.xx
Resolve problems with old version of dependencies
To build compatible for old version use (docs).
However, doesn't always work correct and we need to set some library versions manually.
in trm, sql, sqlx.go get go.uber.org/multierr@v1.9.0in trm, sql, sqlx.go get github.com/mattn/go-sqlite3@v1.14.14in trm, sql, sqlx, goredis8, mongo.go get github.com/stretchr/testify@v1.8.2in pgxv4. Golang version was bumped up from 1.12 to 1.17 in pgconn v1.14.3.go get github.com/jackc/pgconn@v1.14.2in pgxv4.go get golang.org/x/text@v0.13.0