# HG changeset patch # User Peter Sanchez # Date 1645036158 21600 # Wed Feb 16 12:29:18 2022 -0600 # Node ID f13793291c383d91d7b62da23b3eaf906cd210ff # Parent 81c70aad9b83c1262f26e5ec94eedda82338e9ef Unexporting some functions that aren't needed outside the module diff --git a/migrate.go b/migrate.go --- a/migrate.go +++ b/migrate.go @@ -70,7 +70,7 @@ } } - ctx = Context(ctx, me.db) + ctx = dbContext(ctx, me.db) err = me.createMigrationTable(ctx) if err != nil { return err @@ -128,7 +128,7 @@ } } - ctx = Context(ctx, me.db) + ctx = dbContext(ctx, me.db) err = me.createMigrationTable(ctx) if err != nil { return err @@ -212,7 +212,7 @@ var cancel context.CancelFunc if m.Timeout > 0 { - ctx, cancel = ContextAddTimeout(ctx, m.Timeout) + ctx, cancel = contextAddTimeout(ctx, m.Timeout) defer cancel() } @@ -240,7 +240,7 @@ var cancel context.CancelFunc if m.Timeout > 0 { - ctx, cancel = ContextAddTimeout(ctx, m.Timeout) + ctx, cancel = contextAddTimeout(ctx, m.Timeout) defer cancel() } diff --git a/sql.go b/sql.go --- a/sql.go +++ b/sql.go @@ -64,18 +64,15 @@ var txOptionsRO *sql.TxOptions = &sql.TxOptions{Isolation: 0, ReadOnly: true} -// ContextAddTimeout returns a context with query timeout -func ContextAddTimeout(ctx context.Context, timeout int) (context.Context, context.CancelFunc) { +func contextAddTimeout(ctx context.Context, timeout int) (context.Context, context.CancelFunc) { return context.WithTimeout(ctx, time.Duration(timeout)*time.Second) } -// Context adds db connection to context for immediate use -func Context(ctx context.Context, db *sql.DB) context.Context { +func dbContext(ctx context.Context, db *sql.DB) context.Context { return context.WithValue(ctx, dbCtxKey, db) } -// DBFromContext pulls db pool from context -func DBFromContext(ctx context.Context) *sql.DB { +func dbFromContext(ctx context.Context) *sql.DB { db, ok := ctx.Value(dbCtxKey).(*sql.DB) if !ok { panic(errors.New("Invalid database context")) @@ -85,7 +82,7 @@ // 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 := dbFromContext(ctx) tx, err := db.BeginTx(ctx, opts) if err != nil { return err