|
|
@ -67,22 +67,18 @@ Some actions should allow for rollback when database record insertion/update/del |
|
|
|
So services must be allowed to create a database transaction. Here is some example, |
|
|
|
So services must be allowed to create a database transaction. Here is some example, |
|
|
|
|
|
|
|
|
|
|
|
```go |
|
|
|
```go |
|
|
|
// servcies/repository/repo.go |
|
|
|
// services/repository/repository.go |
|
|
|
func CreateXXXX() error {\ |
|
|
|
func CreateXXXX() error { |
|
|
|
ctx, committer, err := db.TxContext() |
|
|
|
return db.WithTx(func(ctx context.Context) error { |
|
|
|
if err != nil { |
|
|
|
e := db.GetEngine(ctx) |
|
|
|
return err |
|
|
|
// do something, if err is returned, it will rollback automatically |
|
|
|
} |
|
|
|
|
|
|
|
defer committer.Close() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// do something, if return err, it will rollback automatically when `committer.Close()` is invoked. |
|
|
|
|
|
|
|
if err := issues.UpdateIssue(ctx, repoID); err != nil { |
|
|
|
if err := issues.UpdateIssue(ctx, repoID); err != nil { |
|
|
|
// ... |
|
|
|
// ... |
|
|
|
|
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// ... |
|
|
|
// ...... |
|
|
|
return nil |
|
|
|
|
|
|
|
}) |
|
|
|
return committer.Commit() |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
``` |
|
|
|
``` |
|
|
|
|
|
|
|
|
|
|
@ -94,14 +90,14 @@ If the function will be used in the transaction, just let `context.Context` as t |
|
|
|
func UpdateIssue(ctx context.Context, repoID int64) error { |
|
|
|
func UpdateIssue(ctx context.Context, repoID int64) error { |
|
|
|
e := db.GetEngine(ctx) |
|
|
|
e := db.GetEngine(ctx) |
|
|
|
|
|
|
|
|
|
|
|
// ...... |
|
|
|
// ... |
|
|
|
} |
|
|
|
} |
|
|
|
``` |
|
|
|
``` |
|
|
|
|
|
|
|
|
|
|
|
### Package Name |
|
|
|
### Package Name |
|
|
|
|
|
|
|
|
|
|
|
For the top level package, use a plural as package name, i.e. `services`, `models`, for sub packages, use singular, |
|
|
|
For the top level package, use a plural as package name, i.e. `services`, `models`, for sub packages, use singular, |
|
|
|
i.e. `servcies/user`, `models/repository`. |
|
|
|
i.e. `services/user`, `models/repository`. |
|
|
|
|
|
|
|
|
|
|
|
### Import Alias |
|
|
|
### Import Alias |
|
|
|
|
|
|
|
|
|
|
|