Skip to content

Commit ac493a6

Browse files
authored
V2 (#96)
Separated drivers
1 parent f1e5dc8 commit ac493a6

File tree

162 files changed

+1550
-2084
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

162 files changed

+1550
-2084
lines changed

.github/workflows/main.yaml

+25-15
Original file line numberDiff line numberDiff line change
@@ -112,30 +112,40 @@ jobs:
112112
- name: Tests with real databases
113113
run: make test.coverage.with_real_db
114114

115-
- name: Code coverage data
116-
run: |
117-
set -x
118-
COVERAGE_TOTAL=`go tool cover -func=coverage.out | grep total | grep -Eo '[0-9]+\.[0-9]+'`
119-
echo "COVERAGE_TOTAL=$COVERAGE_TOTAL" >> $GITHUB_ENV
120-
- uses: jandelgado/[email protected]
121-
with:
122-
outfile: ./coverage.lcov
123-
124115
- name: Coveralls
125-
uses: coverallsapp/github-action@master
116+
uses: coverallsapp/github-action@v2
126117
with:
127-
path-to-lcov: ./coverage.lcov
118+
format: golang
119+
file: ./coverage.out
128120
github-token: ${{ secrets.GITHUB_TOKEN }}
129121

130-
golangci:
131-
name: lint
122+
# https://github.com/golangci/golangci-lint/issues/828
123+
generate-pkgs-matrix:
132124
runs-on: ubuntu-latest
125+
outputs:
126+
matrix: ${{ steps.set-matrix.outputs.matrix }}
127+
steps:
128+
- name: Fetch Repository
129+
uses: actions/checkout@v4
130+
- id: set-matrix
131+
run: |
132+
DIRECTORIES=$(find . -not -path "*/vendor/*" -type f -name go.mod -exec sh -c 'echo $(dirname {})' \; | jq -R -s -c 'split("\n")[:-1]')
133+
echo "matrix=${DIRECTORIES}" >> $GITHUB_OUTPUT
134+
135+
lint:
136+
needs: generate-pkgs-matrix
137+
runs-on: ubuntu-latest
138+
strategy:
139+
matrix:
140+
modules: ${{fromJson(needs.generate-pkgs-matrix.outputs.matrix)}}
133141
steps:
134-
- uses: actions/setup-go@v3
142+
- name: Fetch Repository
143+
uses: actions/checkout@v4
144+
- uses: actions/setup-go@v4
135145
with:
136146
go-version: ${{ env.GO_TARGET_VERSION }}
137-
- uses: actions/checkout@v3
138147
- name: golangci-lint
139148
uses: golangci/golangci-lint-action@v3
140149
with:
141150
args: --timeout=3m -v
151+
working-directory: ${{ matrix.modules }}

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,4 @@ vendor/
2828

2929
# Code coverage
3030
coverage.*
31+
coverage/

.golangci.yml

+5-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
linters-settings:
2-
enable-all: true
3-
41
run:
52
skip-dirs:
63
- trm/mock
@@ -68,9 +65,6 @@ linters:
6865

6966
issues:
7067
exclude-use-default: false
71-
errcheck:
72-
check-type-assertions: true
73-
check-blank: true
7468
exclude:
7569
- ST1000 # ST1000: at least one file in a package should have a package comment
7670
exclude-rules:
@@ -95,28 +89,28 @@ issues:
9589
linters:
9690
- staticcheck
9791
text: "SA1029: should not use built-in type string as key for value; define your own type to avoid collisions"
98-
- path: .+/context\.go
92+
- path: (.+/)?context\.go
9993
linters:
10094
- ireturn
101-
- path: .+/transaction\.go
95+
- path: (.+/)?transaction\.go
10296
linters:
10397
- ireturn
10498
source: \) Begin\(ctx
105-
- path: .+/settings\.go
99+
- path: (.+/)?settings\.go
106100
linters:
107101
- ireturn
108102
source: "(?:EnrichBy|SetPropagation|SetCtxKey|CtxKey|SetCancelable|SetTimeout)"
109103
- linters:
110104
- gochecknoglobals
111105
source: "DefaultCtxGetter"
112106
- &internal_text
113-
path: ^internal
107+
path: (^trm/internal|^internal)
114108
text: "var-naming: don't use an underscore in package name"
115109
- <<: *internal_text
116110
text: "package-comments: should have a package comment"
117111
- <<: *internal_text
118112
text: "ST1003: should not use underscores in package names"
119-
- path: (_test\.go|^internal)
113+
- path: (_test\.go|^trm/internal|^internal)
120114
linters:
121115
- funlen
122116
- gochecknoglobals

Makefile

+42-9
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,58 @@
1-
CVPKG=go list ./... | grep -v mocks | grep -v internal/
2-
GO_TEST=go test `$(CVPKG)` -race
3-
GO_TEST_WITH_REAL_DB=$(GO_TEST) --tags=with_real_db
4-
COVERAGE_FILE="coverage.out"
1+
DIR=$(PWD)
2+
3+
GO_TEST=cd ./sh && bash ./go.test.sh
4+
GO_TEST_COVERAGE=cd ./sh && bash ./go.test.coverage.sh
5+
6+
GO_TEST_WITH_REAL_DB=--tags=with_real_db
57

68
test:
79
$(GO_TEST)
810

911
test.with_real_db:
10-
$(GO_TEST_WITH_REAL_DB)
12+
$(GO_TEST) $(GO_TEST_WITH_REAL_DB)
1113

1214
test.coverage:
13-
$(GO_TEST) -covermode=atomic -coverprofile=$(COVERAGE_FILE)
15+
$(GO_TEST_COVERAGE)
1416

1517
test.coverage.with_real_db:
16-
$(GO_TEST_WITH_REAL_DB) -covermode=atomic -coverprofile=$(COVERAGE_FILE)
18+
$(GO_TEST_COVERAGE) $(GO_TEST_WITH_REAL_DB)
1719

1820
fmt:
19-
go fmt ./...
21+
cd sh && sh ./go.fmt.sh
2022

2123
lint:
22-
golangci-lint run -v --timeout=2m
24+
cd sh && sh ./lint.sh
25+
26+
lint.verbose:
27+
cd sh && sh ./lint.sh -v
28+
29+
lint.cache.clean:
30+
golangci-lint cache clean
2331

2432
generate:
2533
go generate ./...
34+
35+
go.mod.tidy:
36+
cd sh && sh ./go.mod.tidy.sh
37+
38+
go.mod.vendor:
39+
cd sh && sh ./go.mod.vendor.sh
40+
41+
go.work.sync:
42+
go work sync
43+
44+
45+
tag: git.tag tag.pkg
46+
47+
tag.pkg:
48+
cd sh && sh ./tag.pkg.sh $(version)
49+
50+
git.tag: git.tag.create git.tag.push
51+
52+
# 1.0, "v2." added automatically
53+
# make git.tag version="0.0-rc1"
54+
git.tag.create:
55+
cd sh && sh ./git.tag.sh $(version)
56+
57+
git.tag.push:
58+
git push origin --tags

README.md

+45-25
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# Go transaction manager
22

3-
[![Go Reference](https://pkg.go.dev/badge/github.com/avito-tech/go-transaction-manager.svg)](https://pkg.go.dev/github.com/avito-tech/go-transaction-manager)
3+
[![Go Reference](https://pkg.go.dev/badge/github.com/avito-tech/go-transaction-manager.svg)](https://pkg.go.dev/github.com/avito-tech/go-transaction-manager/trm/v2)
44
[![Test Status](https://github.com/avito-tech/go-transaction-manager/actions/workflows/main.yaml/badge.svg)](https://github.com/avito-tech/go-transaction-manager/actions?query=branch%3Amain)
55
[![Coverage Status](https://coveralls.io/repos/github/avito-tech/go-transaction-manager/badge.svg?branch=main)](https://coveralls.io/github/avito-tech/go-transaction-manager?branch=main)
6-
[![Go Report Card](https://goreportcard.com/badge/github.com/avito-tech/go-transaction-manager)](https://goreportcard.com/report/github.com/avito-tech/go-transaction-manager)
6+
[![Go Report Card](https://goreportcard.com/badge/github.com/avito-tech/go-transaction-manager)](https://goreportcard.com/report/github.com/avito-tech/go-transaction-manager/)
77
[![License](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)
88

99
Transaction manager is an abstraction to coordinate database transaction boundaries.
@@ -12,45 +12,56 @@ Easiest way to get the perfect repository.
1212

1313
## Supported implementations
1414

15-
* [database/sql](https://pkg.go.dev/database/sql), [docs](https://pkg.go.dev/github.com/avito-tech/go-transaction-manager/sql) (Go 1.13)
16-
* [jmoiron/sqlx](https://github.com/jmoiron/sqlx), [docs](https://pkg.go.dev/github.com/avito-tech/go-transaction-manager/sqlx) (Go 1.13)
17-
* [gorm](https://github.com/go-gorm/gorm), [docs](https://pkg.go.dev/github.com/avito-tech/go-transaction-manager/gorm) (Go 1.16)
18-
* [mongo-go-driver](https://github.com/mongodb/mongo-go-driver), [docs](https://pkg.go.dev/github.com/avito-tech/go-transaction-manager/mongo) (Go 1.13)
19-
* [go-redis/redis](https://github.com/go-redis/redis), [docs](https://pkg.go.dev/github.com/avito-tech/go-transaction-manager/redis) (Go 1.17)
20-
* [pgx_v4](https://github.com/jackc/pgx/tree/v4), [docs](https://pkg.go.dev/github.com/avito-tech/go-transaction-manager/pgxv4) (Go 1.16)
21-
* [pgx_v5](https://github.com/jackc/pgx), [docs](https://pkg.go.dev/github.com/avito-tech/go-transaction-manager/pgxv5) (Go 1.19)
15+
* [database/sql](https://pkg.go.dev/database/sql), [docs](https://pkg.go.dev/github.com/avito-tech/go-transaction-manager/drivers/sql/v2) (
16+
Go 1.13)
17+
* [jmoiron/sqlx](https://github.com/jmoiron/sqlx), [docs](https://pkg.go.dev/github.com/avito-tech/go-transaction-manager/drivers/sqlx/v2) (
18+
Go 1.13)
19+
* [gorm](https://github.com/go-gorm/gorm), [docs](https://pkg.go.dev/github.com/avito-tech/go-transaction-manager/drivers/gorm/v2) (
20+
Go 1.18)
21+
* [mongo-go-driver](https://github.com/mongodb/mongo-go-driver), [docs](https://pkg.go.dev/github.com/avito-tech/go-transaction-manager/drivers/mongo/v2) (
22+
Go 1.13)
23+
* [go-redis/redis](https://github.com/go-redis/redis), [docs](https://pkg.go.dev/github.com/avito-tech/go-transaction-manager/drivers/goredis8/v2) (
24+
Go 1.17)
25+
* [pgx_v4](https://github.com/jackc/pgx/tree/v4), [docs](https://pkg.go.dev/github.com/avito-tech/go-transaction-manager/drivers/pgxv4/v2) (
26+
Go 1.16)
27+
* [pgx_v5](https://github.com/jackc/pgx), [docs](https://pkg.go.dev/github.com/avito-tech/go-transaction-manager/drivers/pgxv5/v2) (
28+
Go 1.19)
2229

2330
## Installation
2431

2532
```bash
26-
go get github.com/avito-tech/go-transaction-manager
33+
go get github.com/avito-tech/go-transaction-manager/trm/v2
2734
```
2835

36+
To install some support database use `go get github.com/avito-tech/go-transaction-manager/drivers/{name}`.
37+
38+
For example `go get github.com/avito-tech/go-transaction-manager/drivers/sqlx/v2`.
39+
2940
### Backwards Compatibility
3041

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

3445
## Usage
3546

36-
**To use multiple transactions from different databases**, you need to set CtxKey in [Settings](trm/settings.go) by [WithCtxKey](trm/settings/option.go).
47+
**To use multiple transactions from different databases**, you need to set CtxKey in [Settings](trm/settings.go)
48+
by [WithCtxKey](trm/settings/option.go) ([docs](https://pkg.go.dev/github.com/avito-tech/go-transaction-manager/trm/v2)).
3749

38-
**For nested transactions with different transaction managers**, you need to use [ChainedMW](trm/manager/chain.go) ([docs](https://pkg.go.dev/github.com/avito-tech/go-transaction-manager/trm/manager)).
50+
**For nested transactions with different transaction managers**, you need to use [ChainedMW](trm/manager/chain.go) ([docs](https://pkg.go.dev/github.com/avito-tech/go-transaction-manager/trm/v2/manager)).
3951

40-
**To skip a transaction rollback due to an error, use [ErrSkip](trm/manager.go#L20) or [Skippable](trm/manager.go#L24)**
52+
**To skip a transaction rollback due to an error, use [ErrSkip](manager.go#L20) or [Skippable](manager.go#L24)**
4153

42-
### Explanation of the approach ([English](https://www.youtube.com/watch?v=aRsea6FFAyA), [Russian](https://habr.com/ru/companies/avito/articles/727168/))
54+
### Explanation of the approach [English](https://www.youtube.com/watch?v=aRsea6FFAyA), Russian [article](https://habr.com/ru/companies/avito/articles/727168/) and [youtube](https://www.youtube.com/watch?v=fcdckM5sUxA).
4355

4456
### Examples with an ideal *repository* and nested transactions.
4557

46-
* [database/sql](sql/example_test.go)
47-
* [jmoiron/sqlx](sqlx/example_test.go)
48-
* [gorm](gorm/example_test.go)
49-
* [mongo-go-driver](mongo/example_test.go)
50-
* [go-redis/redis](redis/example_test.go)
51-
* [pgx_v4](pgxv4/example_test.go)
52-
* [pgx_v5](pgxv5/example_test.go)
53-
58+
* [database/sql](drivers/sql/example_test.go)
59+
* [jmoiron/sqlx](drivers/sqlx/example_test.go)
60+
* [gorm](drivers/gorm/example_test.go)
61+
* [mongo-go-driver](drivers/mongo/example_test.go)
62+
* [go-redis/redis](drivers/goredis8/example_test.go)
63+
* [pgx_v4](drivers/pgxv4/example_test.go)
64+
* [pgx_v5](drivers/pgxv5/example_test.go)
5465

5566
Below is an example how to start usage.
5667

@@ -64,8 +75,8 @@ import (
6475
"github.com/jmoiron/sqlx"
6576
_ "github.com/mattn/go-sqlite3"
6677

67-
trmsqlx "github.com/avito-tech/go-transaction-manager/sqlx"
68-
"github.com/avito-tech/go-transaction-manager/trm/manager"
78+
trmsqlx "github.com/avito-tech/go-transaction-manager/drivers/sqlx/v2"
79+
"github.com/avito-tech/go-transaction-manager/trm/v2/manager"
6980
)
7081

7182
func main() {
@@ -85,6 +96,7 @@ func main() {
8596
err = trManager.Do(ctx, func(ctx context.Context) error {
8697
checkErr(r.Save(ctx, u))
8798

99+
// example of nested transactions
88100
return trManager.Do(ctx, func(ctx context.Context) error {
89101
u.Username = "new_username"
90102
return r.Save(ctx, u)
@@ -146,4 +158,12 @@ func (r *repo) Save(ctx context.Context, u *user) error {
146158

147159
## Benchmark
148160

149-
[Comparing](internal/benchmark/with_or_without_trm/README.md) examples with and without trm.
161+
[Comparing](trm/internal/benchmark/with_or_without_trm/README.md) examples with and without trm.
162+
163+
## Contribution
164+
165+
1. To local development sync dependencies use `make go.work.sync`.
166+
2. After finalizing of changes bump up version in all drivers.
167+
168+
* To install all dependencies use `make go.mod.tidy` or `make go.mod.vendor`.
169+
* To run all tests use `make go.test` or `make go.test.with_real_db` for integration tests.

redis/context.go drivers/goredis8/context.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
package redis
1+
package goredis8
22

33
import (
44
"context"
55

66
"github.com/go-redis/redis/v8"
77

8-
"github.com/avito-tech/go-transaction-manager/trm"
9-
trmcontext "github.com/avito-tech/go-transaction-manager/trm/context"
8+
"github.com/avito-tech/go-transaction-manager/trm/v2"
9+
trmcontext "github.com/avito-tech/go-transaction-manager/trm/v2/context"
1010
)
1111

1212
// DefaultCtxGetter is the CtxGetter with settings.DefaultCtxKey.
1313
var DefaultCtxGetter = NewCtxGetter(trmcontext.DefaultManager)
1414

15-
// CtxGetter gets redis.Pipeliner from trm.СtxManager by casting trm.Transaction to redis.UniversalClient.
15+
// CtxGetter gets goredis8.Pipeliner from trm.СtxManager by casting trm.Transaction to redis.UniversalClient.
1616
type CtxGetter struct {
1717
ctxManager trm.СtxManager
1818
}
@@ -22,7 +22,7 @@ func NewCtxGetter(c trm.СtxManager) *CtxGetter {
2222
return &CtxGetter{ctxManager: c}
2323
}
2424

25-
// DefaultTrOrDB returns Cmdable from context.Context or DB(redis.Cmdable) otherwise.
25+
// DefaultTrOrDB returns Cmdable from context.Context or DB(goredis8.Cmdable) otherwise.
2626
func (c *CtxGetter) DefaultTrOrDB(ctx context.Context, db redis.Cmdable) redis.Cmdable {
2727
if tr := c.ctxManager.Default(ctx); tr != nil {
2828
return c.convert(tr)
@@ -31,7 +31,7 @@ func (c *CtxGetter) DefaultTrOrDB(ctx context.Context, db redis.Cmdable) redis.C
3131
return db
3232
}
3333

34-
// TrOrDB returns Cmdable from context.Context by trm.CtxKey or DB(redis.Cmdable) otherwise.
34+
// TrOrDB returns Cmdable from context.Context by trm.CtxKey or DB(goredis8.Cmdable) otherwise.
3535
func (c *CtxGetter) TrOrDB(ctx context.Context, key trm.CtxKey, db redis.Cmdable) redis.Cmdable {
3636
if tr := c.ctxManager.ByKey(ctx, key); tr != nil {
3737
return c.convert(tr)

redis/example_test.go drivers/goredis8/example_test.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//go:build with_real_db
22
// +build with_real_db
33

4-
package redis_test
4+
package goredis8_test
55

66
import (
77
"context"
@@ -10,10 +10,11 @@ import (
1010

1111
"github.com/go-redis/redis/v8"
1212

13-
trmredis "github.com/avito-tech/go-transaction-manager/redis"
14-
"github.com/avito-tech/go-transaction-manager/trm"
15-
"github.com/avito-tech/go-transaction-manager/trm/manager"
16-
"github.com/avito-tech/go-transaction-manager/trm/settings"
13+
trmredis "github.com/avito-tech/go-transaction-manager/drivers/goredis8/v2"
14+
15+
"github.com/avito-tech/go-transaction-manager/trm/v2"
16+
"github.com/avito-tech/go-transaction-manager/trm/v2/manager"
17+
"github.com/avito-tech/go-transaction-manager/trm/v2/settings"
1718
)
1819

1920
// Example demonstrates the implementation of the Repository pattern by trm.Manager.

redis/factory.go drivers/goredis8/factory.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
package redis
1+
package goredis8
22

33
import (
44
"context"
55

66
"github.com/go-redis/redis/v8"
77

8-
"github.com/avito-tech/go-transaction-manager/trm"
8+
trm "github.com/avito-tech/go-transaction-manager/trm/v2"
99
)
1010

1111
// NewDefaultFactory creates default trm.Transaction(redis.UniversalClient).

drivers/goredis8/go.mod

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module github.com/avito-tech/go-transaction-manager/drivers/goredis8/v2
2+
3+
go 1.14
4+
5+
require (
6+
github.com/avito-tech/go-transaction-manager/trm/v2 v2.0.0-rc6
7+
github.com/go-redis/redis/v8 v8.11.5
8+
github.com/go-redis/redismock/v8 v8.11.5
9+
github.com/kr/pretty v0.3.0 // indirect
10+
github.com/rogpeppe/go-internal v1.12.0 // indirect
11+
github.com/stretchr/testify v1.8.2
12+
golang.org/x/net v0.10.0 // indirect
13+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
14+
)

0 commit comments

Comments
 (0)