-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1. show how old dependency vulnerabilities can be solved
2. solved toolchain golan problem 3. explain how to generate go.mod with compatible with old versions
- Loading branch information
Showing
11 changed files
with
352 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ on: | |
name: Test | ||
env: | ||
OS_TARGET: ubuntu-latest | ||
GOTOOLCHAIN: local | ||
jobs: | ||
tests-units: | ||
needs: [lint] | ||
|
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
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
13 changes: 13 additions & 0 deletions
13
trm/internal/prove_receiving_updates_without_vulnerabilities/README.md
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,13 @@ | ||
# Proving that TRM doesn't affect updates to newer version. | ||
|
||
1. Install pgxv5 driver with old pgx version `github.com/jackc/pgx/[email protected]` | ||
|
||
`GOWORK=off go get github.com/avito-tech/go-transaction-manager/drivers/pgxv5/[email protected]` | ||
2. In `go.mod` we see `github.com/jackc/pgx/v5 v5.5.1 // indirect`. | ||
3. Call `GOWORK=off go mod tidy && GOWORK=off go mod vendor` to install the old versions. | ||
4. Then, we can update `pgx` manually and see in `go.mod` the last version `github.com/jackc/pgx/v5 v5.6.0 // indirect`. | ||
|
||
`go get github.com/jackc/pgx/v5` | ||
or | ||
`go mod tidy` | ||
5. `go test ./...` to run [example_test.go](example_test.go) |
122 changes: 122 additions & 0 deletions
122
trm/internal/prove_receiving_updates_without_vulnerabilities/example_test.go
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,122 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/jackc/pgx/v5/pgxpool" | ||
|
||
trmpgx "github.com/avito-tech/go-transaction-manager/drivers/pgxv5/v2" | ||
|
||
"github.com/avito-tech/go-transaction-manager/trm/v2/manager" | ||
) | ||
|
||
// Example demonstrates the implementation of the Repository pattern by trm.Manager. | ||
func Example() { | ||
ctx := context.Background() | ||
|
||
uri := fmt.Sprintf("postgres://%s:%s@%s:%d/%s", | ||
"user", "pass", "localhost", 5432, "db", | ||
) | ||
|
||
pool, err := pgxpool.New(ctx, uri) | ||
checkErr(err) | ||
|
||
defer pool.Close() | ||
|
||
sqlStmt := `CREATE TABLE IF NOT EXISTS users_v5 (user_id SERIAL, username TEXT)` | ||
_, err = pool.Exec(ctx, sqlStmt) | ||
checkErr(err, sqlStmt) | ||
|
||
r := newRepo(pool, trmpgx.DefaultCtxGetter) | ||
trManager := manager.Must(trmpgx.NewDefaultFactory(pool)) | ||
|
||
u := &user{ | ||
Username: "username", | ||
} | ||
|
||
err = trManager.Do(ctx, func(ctx context.Context) error { | ||
if err := r.Save(ctx, u); err != nil { | ||
return err | ||
} | ||
|
||
return trManager.Do(ctx, func(ctx context.Context) error { | ||
u.Username = "new_username" | ||
|
||
return r.Save(ctx, u) | ||
}) | ||
}) | ||
checkErr(err) | ||
|
||
userFromDB, err := r.GetByID(ctx, u.ID) | ||
checkErr(err) | ||
|
||
fmt.Println(userFromDB) | ||
|
||
// Output: &{1 new_username} | ||
} | ||
|
||
type repo struct { | ||
db *pgxpool.Pool | ||
getter *trmpgx.CtxGetter | ||
} | ||
|
||
func newRepo(db *pgxpool.Pool, c *trmpgx.CtxGetter) *repo { | ||
repo := &repo{ | ||
db: db, | ||
getter: c, | ||
} | ||
|
||
return repo | ||
} | ||
|
||
type user struct { | ||
ID int64 | ||
Username string | ||
} | ||
|
||
func (r *repo) GetByID(ctx context.Context, id int64) (*user, error) { | ||
query := `SELECT * FROM users_v5 WHERE user_id=$1` | ||
|
||
conn := r.getter.DefaultTrOrDB(ctx, r.db) | ||
row := conn.QueryRow(ctx, query, id) | ||
|
||
user := &user{} | ||
|
||
err := row.Scan(&user.ID, &user.Username) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return user, nil | ||
} | ||
|
||
func (r *repo) Save(ctx context.Context, u *user) error { | ||
isNew := u.ID == 0 | ||
conn := r.getter.DefaultTrOrDB(ctx, r.db) | ||
|
||
if !isNew { | ||
query := `UPDATE users_v5 SET username = $1 WHERE user_id = $2` | ||
|
||
if _, err := conn.Exec(ctx, query, u.Username, u.ID); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
query := `INSERT INTO users_v5 (username) VALUES ($1) RETURNING user_id` | ||
|
||
err := conn.QueryRow(ctx, query, u.Username).Scan(&u.ID) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func checkErr(err error, args ...interface{}) { | ||
if err != nil { | ||
panic(fmt.Sprint(append([]interface{}{err}, args...)...)) | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
trm/internal/prove_receiving_updates_without_vulnerabilities/go.mod
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,20 @@ | ||
module prove_receiving_updates_without_vulnerabilities | ||
|
||
go 1.22 | ||
|
||
require ( | ||
github.com/avito-tech/go-transaction-manager/drivers/pgxv5/v2 v2.0.0-rc9.2 | ||
github.com/avito-tech/go-transaction-manager/trm/v2 v2.0.0-rc10 | ||
github.com/jackc/pgx/v5 v5.5.1 | ||
) | ||
|
||
require ( | ||
github.com/jackc/pgpassfile v1.0.0 // indirect | ||
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect | ||
github.com/jackc/puddle/v2 v2.2.1 // indirect | ||
go.uber.org/atomic v1.7.0 // indirect | ||
go.uber.org/multierr v1.9.0 // indirect | ||
golang.org/x/crypto v0.17.0 // indirect | ||
golang.org/x/sync v0.1.0 // indirect | ||
golang.org/x/text v0.14.0 // indirect | ||
) |
Oops, something went wrong.