-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1. Bump Go version to 1.23 2. Fix skipping code error in test 3. Fix dependency
- Loading branch information
Showing
36 changed files
with
756 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,6 +42,14 @@ For example `go get github.com/avito-tech/go-transaction-manager/drivers/sqlx/v2 | |
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](trm/settings.go) | ||
|
@@ -162,8 +170,55 @@ func (r *repo) Save(ctx context.Context, u *user) error { | |
|
||
## Contribution | ||
|
||
1. To local development sync dependencies use `make go.work.sync`. | ||
2. After finalizing of changes bump up version in all drivers. | ||
### Requirements | ||
|
||
- [golangci-lint](https://golangci-lint.run/welcome/install/) | ||
- [make](https://www.gnu.org/software/make/#download) | ||
|
||
### Local Running | ||
|
||
* To install all dependencies use `make go.mod.tidy` or `make go.mod.vendor`. | ||
* To run all tests use `make go.test` or `make go.test.with_real_db` for integration tests. | ||
* 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](trm/drivers/test/docker-compose.yaml). | ||
```bash | ||
docker compose -f trm/drivers/test/docker-compose.yaml up | ||
``` | ||
|
||
For full GitHub Actions run, you can use [act](https://github.com/nektos/act). | ||
|
||
#### Running old go versions | ||
|
||
To stop Golang upgrading set environment variable `GOTOOLCHAIN=local` . | ||
|
||
```sh | ||
go install go1.16 # or older version | ||
go1.16 install | ||
``` | ||
|
||
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](.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](https://go.dev/ref/mod#go-mod-tidy)). | ||
|
||
However, `--compat` doesn't always work correct and we need to set some library versions manually. | ||
|
||
1. `go get go.uber.org/[email protected]` in [trm](trm), [sql](drivers/sql), [sqlx](drivers/sqlx). | ||
2. `go get github.com/mattn/[email protected]` in [trm](trm), [sql](drivers/sql), [sqlx](drivers/sqlx). | ||
3. `go get github.com/stretchr/[email protected]` in [trm](trm), [sql](drivers/sql), [sqlx](drivers/sqlx), [goredis8](drivers/goredis8), [mongo](drivers/mongo). | ||
4. `go get github.com/jackc/[email protected]` in [pgxv4](drivers/pgxv4). Golang version was bumped up from 1.12 to 1.17 in pgconn v1.14.3. | ||
5. `go get golang.org/x/[email protected]` in [pgxv4](drivers/pgxv4). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
//go:build go1.20 | ||
// +build go1.20 | ||
//go:build go1.23 | ||
// +build go1.23 | ||
|
||
package goredis8 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
//go:build go1.20 | ||
// +build go1.20 | ||
//go:build go1.23 | ||
// +build go1.23 | ||
|
||
package gorm | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package mongo | ||
|
||
import ( | ||
"context" | ||
|
||
"go.mongodb.org/mongo-driver/mongo" | ||
|
||
"github.com/avito-tech/go-transaction-manager/trm/v2" | ||
trmcontext "github.com/avito-tech/go-transaction-manager/trm/v2/context" | ||
) | ||
|
||
// DefaultCtxGetter is the CtxGetter with settings.DefaultCtxKey. | ||
var DefaultCtxGetter = NewCtxGetter(trmcontext.DefaultManager) | ||
|
||
// CtxGetter gets Tr from trm.СtxManager by casting trm.Transaction to Tr. | ||
type CtxGetter struct { | ||
ctxManager trm.СtxManager | ||
} | ||
|
||
// NewCtxGetter returns *CtxGetter to get Tr from context.Context. | ||
func NewCtxGetter(c trm.СtxManager) *CtxGetter { | ||
return &CtxGetter{ctxManager: c} | ||
} | ||
|
||
// DefaultTrOrDB returns mongo.Session from context.Context or DB(mongo.Session) otherwise. | ||
func (c *CtxGetter) DefaultTrOrDB(ctx context.Context, db mongo.Session) mongo.Session { | ||
if tr := c.ctxManager.Default(ctx); tr != nil { | ||
return c.convert(tr) | ||
} | ||
|
||
return db | ||
} | ||
|
||
// TrOrDB returns mongo.Session from context.Context by trm.CtxKey or DB(mongo.Session) otherwise. | ||
func (c *CtxGetter) TrOrDB(ctx context.Context, key trm.CtxKey, db mongo.Session) mongo.Session { | ||
if tr := c.ctxManager.ByKey(ctx, key); tr != nil { | ||
return c.convert(tr) | ||
} | ||
|
||
return db | ||
} | ||
|
||
func (c *CtxGetter) convert(tr trm.Transaction) mongo.Session { | ||
if tx, ok := tr.Transaction().(mongo.Session); ok { | ||
return tx | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.