Skip to content

Commit

Permalink
fix: add ctx to base database
Browse files Browse the repository at this point in the history
  • Loading branch information
plyr4 committed Aug 4, 2023
1 parent 5bdb2e9 commit 032fb08
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
6 changes: 5 additions & 1 deletion database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package database

import (
"context"
"fmt"
"time"

Expand Down Expand Up @@ -54,6 +55,8 @@ type (
client *gorm.DB
// engine configuration settings used in database functions
config *config
// engine context used in database functions
ctx context.Context
// sirupsen/logrus logger used in database functions
logger *logrus.Entry

Expand Down Expand Up @@ -85,6 +88,7 @@ func New(opts ...EngineOpt) (Interface, error) {
e.client = new(gorm.DB)
e.config = new(config)
e.logger = new(logrus.Entry)
e.ctx = context.TODO()

// apply all provided configuration options
for _, opt := range opts {
Expand Down Expand Up @@ -143,7 +147,7 @@ func New(opts ...EngineOpt) (Interface, error) {
}

// create database agnostic engines for resources
err = e.NewResources()
err = e.NewResources(e.ctx)
if err != nil {
return nil, err
}
Expand Down
5 changes: 4 additions & 1 deletion database/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
package database

import (
"context"

"github.com/go-vela/server/database/build"
"github.com/go-vela/server/database/hook"
"github.com/go-vela/server/database/log"
Expand All @@ -19,11 +21,12 @@ import (
)

// NewResources creates and returns the database agnostic engines for resources.
func (e *engine) NewResources() error {
func (e *engine) NewResources(ctx context.Context) error {
var err error

// create the database agnostic engine for builds
e.BuildInterface, err = build.New(
build.WithContext(e.ctx),
build.WithClient(e.client),
build.WithLogger(e.logger),
build.WithSkipCreation(e.config.SkipCreation),
Expand Down
3 changes: 2 additions & 1 deletion database/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package database

import (
"context"
"testing"

"github.com/DATA-DOG/go-sqlmock"
Expand Down Expand Up @@ -94,7 +95,7 @@ func TestDatabase_Engine_NewResources(t *testing.T) {
// run tests
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
err := test.database.NewResources()
err := test.database.NewResources(context.TODO())

if test.failure {
if err == nil {
Expand Down

0 comments on commit 032fb08

Please sign in to comment.