~netlandish/gobwebs

6c9ac3bb339f410eeca62163a7ff477bd636bdb2 — Peter Sanchez 1 year, 9 months ago fe8ba15
Renaming database context getter helper
3 files changed, 5 insertions(+), 5 deletions(-)

M database/middleware.go
M database/sql.go
M server/server.go
M database/middleware.go => database/middleware.go +1 -1
@@ 9,7 9,7 @@ import (
func DBTransactionMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
	return func(c echo.Context) error {
		res := next(c)
		db := DBFromContext(c.Request().Context())
		db := ForContext(c.Request().Context())
		db.EnableCommit()
		if err := db.CommitTx(); err != nil {
			return err

M database/sql.go => database/sql.go +3 -3
@@ 29,8 29,8 @@ func Context(ctx context.Context, db DBI) context.Context {
	return context.WithValue(ctx, dbCtxKey, db)
}

// DBFromContext pulls db pool from context
func DBFromContext(ctx context.Context) DBI {
// ForContext pulls DBI obj for context
func ForContext(ctx context.Context) DBI {
	db, ok := ctx.Value(dbCtxKey).(DBI)
	if !ok {
		panic(errors.New("Invalid database context"))


@@ 40,7 40,7 @@ func DBFromContext(ctx context.Context) DBI {

// WithTx calls a function wrapped in a database transaction
func WithTx(ctx context.Context, opts *sql.TxOptions, fn func(tx *sql.Tx) error) error {
	db := DBFromContext(ctx)
	db := ForContext(ctx)
	tx, err := db.BeginTx(ctx, opts)
	if err != nil {
		return err

M server/server.go => server/server.go +1 -1
@@ 294,7 294,7 @@ func (s *Server) WithDefaultMiddleware() *Server {
				),
			)
			c.Response().Before(func() {
				db := database.DBFromContext(c.Request().Context())
				db := database.ForContext(c.Request().Context())
				db.EnableCommit()
				if err := db.CommitTx(); err != nil {
					panic(err)