-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: ♻️ some refactoring in dbcontext
- Loading branch information
1 parent
e6a5a51
commit 6c00afc
Showing
38 changed files
with
1,107 additions
and
443 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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package contracts | ||
|
||
import ( | ||
"context" | ||
|
||
"gorm.io/gorm" | ||
) | ||
|
||
type IGormDBContext interface { | ||
WithTx(ctx context.Context) (IGormDBContext, error) | ||
WithTxIfExists(ctx context.Context) IGormDBContext | ||
RunInTx(ctx context.Context, action ActionFunc) error | ||
DB() *gorm.DB | ||
} | ||
|
||
type ActionFunc func(ctx context.Context, gormContext IGormDBContext) error |
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 |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package gormdbcontext | ||
|
||
import ( | ||
"context" | ||
|
||
defaultlogger "github.com/mehdihadeli/go-ecommerce-microservices/internal/pkg/logger/defaultlogger" | ||
"github.com/mehdihadeli/go-ecommerce-microservices/internal/pkg/postgresgorm/contracts" | ||
"github.com/mehdihadeli/go-ecommerce-microservices/internal/pkg/postgresgorm/helpers/gormextensions" | ||
|
||
"gorm.io/gorm" | ||
) | ||
|
||
type gormDBContext struct { | ||
db *gorm.DB | ||
} | ||
|
||
func NewGormDBContext(db *gorm.DB) contracts.IGormDBContext { | ||
c := &gormDBContext{db: db} | ||
|
||
return c | ||
} | ||
|
||
func (c *gormDBContext) DB() *gorm.DB { | ||
return c.db | ||
} | ||
|
||
// WithTx creates a transactional DBContext with getting tx-gorm from the ctx. This will throw an error if the transaction does not exist. | ||
func (c *gormDBContext) WithTx( | ||
ctx context.Context, | ||
) (contracts.IGormDBContext, error) { | ||
tx, err := gormextensions.GetTxFromContext(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return NewGormDBContext(tx), nil | ||
} | ||
|
||
// WithTxIfExists creates a transactional DBContext with getting tx-gorm from the ctx. not throw an error if the transaction is not existing and returns an existing database. | ||
func (c *gormDBContext) WithTxIfExists( | ||
ctx context.Context, | ||
) contracts.IGormDBContext { | ||
tx := gormextensions.GetTxFromContextIfExists(ctx) | ||
if tx == nil { | ||
return c | ||
} | ||
|
||
return NewGormDBContext(tx) | ||
} | ||
|
||
func (c *gormDBContext) RunInTx( | ||
ctx context.Context, | ||
action contracts.ActionFunc, | ||
) error { | ||
// https://gorm.io/docs/transactions.html#Transaction | ||
tx := c.DB().WithContext(ctx).Begin() | ||
|
||
defaultlogger.GetLogger().Info("beginning database transaction") | ||
|
||
gormContext := gormextensions.SetTxToContext(ctx, tx) | ||
ctx = gormContext | ||
|
||
defer func() { | ||
if r := recover(); r != nil { | ||
tx.WithContext(ctx).Rollback() | ||
|
||
if err, _ := r.(error); err != nil { | ||
defaultlogger.GetLogger().Errorf( | ||
"panic tn the transaction, rolling back transaction with panic err: %+v", | ||
err, | ||
) | ||
} else { | ||
defaultlogger.GetLogger().Errorf("panic tn the transaction, rolling back transaction with panic message: %+v", r) | ||
} | ||
} | ||
}() | ||
|
||
err := action(ctx, c) | ||
if err != nil { | ||
defaultlogger.GetLogger().Error("rolling back transaction") | ||
tx.WithContext(ctx).Rollback() | ||
|
||
return err | ||
} | ||
|
||
defaultlogger.GetLogger().Info("committing transaction") | ||
|
||
if err = tx.WithContext(ctx).Commit().Error; err != nil { | ||
defaultlogger.GetLogger().Errorf("transaction commit error: %+v", err) | ||
} | ||
|
||
return err | ||
} |
Oops, something went wrong.