-
-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
r/golang Is it an anti-pattern to pass *sql.Tx in a context value?
- Loading branch information
1 parent
68b5172
commit 5dbfad3
Showing
6 changed files
with
195 additions
and
36 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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"log" | ||
"os" | ||
|
||
"github.com/google/uuid" | ||
"github.com/jackc/pgx/v5" | ||
|
||
cmdinternal "github.com/MarioCarrion/videos/2023/transaction-in-context/cmd/internal" | ||
"github.com/MarioCarrion/videos/2023/transaction-in-context/internal/postgresql" | ||
) | ||
|
||
func main() { | ||
var userID, name string | ||
|
||
flag.StringVar(&userID, "id", "", "id of user to clone") | ||
flag.StringVar(&name, "name", "", "name of the new user") | ||
flag.Parse() | ||
|
||
if userID == "" { | ||
flag.PrintDefaults() | ||
os.Exit(0) | ||
} | ||
|
||
id, err := uuid.Parse(userID) | ||
if err != nil { | ||
log.Fatalln("UUID Parsing error:", err) | ||
} | ||
|
||
//- | ||
|
||
ctx := context.Background() | ||
|
||
conn, err := pgx.Connect(ctx, cmdinternal.NewConnString()) | ||
if err != nil { | ||
log.Fatalln("Connection error:", err) | ||
} | ||
|
||
userClonerRepo := postgresql.NewUserCloner(conn) | ||
|
||
user, err := userClonerRepo.Clone(ctx, id, name) | ||
if err != nil { | ||
log.Fatalln("userClonerRepo.Clone", err) | ||
} | ||
|
||
cmdinternal.Print(&user) | ||
} |
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
57 changes: 57 additions & 0 deletions
57
2023/transaction-in-context/internal/postgresql/user_cloner.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,57 @@ | ||
package postgresql | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/MarioCarrion/videos/2023/transaction-in-context/internal" | ||
"github.com/google/uuid" | ||
"github.com/jackc/pgx/v5" | ||
) | ||
|
||
type UserCloner struct { | ||
conn *pgx.Conn | ||
} | ||
|
||
func NewUserCloner(conn *pgx.Conn) *UserCloner { | ||
return &UserCloner{ | ||
conn: conn, | ||
} | ||
} | ||
|
||
func (u *UserCloner) Clone(ctx context.Context, id uuid.UUID, name string) (internal.User, error) { | ||
var user internal.User | ||
|
||
transaction(ctx, u.conn, func(tx pgx.Tx) error { | ||
urq := userRoleQueries{conn: tx} | ||
|
||
userFound, err := urq.Select(ctx, id) | ||
if err != nil { | ||
return fmt.Errorf("urq.Select(1) %w", err) | ||
} | ||
|
||
uq := userQueries{conn: tx} | ||
|
||
userNew, err := uq.Insert(ctx, name) | ||
if err != nil { | ||
return fmt.Errorf("uq.Insert %w", err) | ||
} | ||
|
||
for _, role := range userFound.Roles { | ||
if err := urq.Insert(ctx, userNew.ID, role.ID); err != nil { | ||
return fmt.Errorf("urq.Insert %w", err) | ||
} | ||
} | ||
|
||
userFound, err = urq.Select(ctx, userNew.ID) | ||
if err != nil { | ||
return fmt.Errorf("urq.Select(2) %w", err) | ||
} | ||
|
||
user = userFound | ||
|
||
return nil | ||
}) | ||
|
||
return user, nil | ||
} |
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