Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion api/build/executable.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func GetBuildExecutable(c *gin.Context) {
o := org.Retrieve(c)
r := repo.Retrieve(c)
cl := claims.Retrieve(c)
ctx := c.Request.Context()

// update engine logger with API metadata
//
Expand All @@ -81,7 +82,7 @@ func GetBuildExecutable(c *gin.Context) {
"subject": cl.Subject,
}).Infof("reading build executable %s/%d", r.GetFullName(), b.GetNumber())

bExecutable, err := database.FromContext(c).PopBuildExecutable(b.GetID())
bExecutable, err := database.FromContext(c).PopBuildExecutable(ctx, b.GetID())
if err != nil {
retErr := fmt.Errorf("unable to pop build executable: %w", err)
util.HandleError(c, http.StatusInternalServerError, retErr)
Expand Down
2 changes: 1 addition & 1 deletion api/build/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func PublishToQueue(ctx context.Context, queue queue.Service, db database.Interf
bExecutable.SetBuildID(b.GetID())
bExecutable.SetData(byteExecutable)

err = db.CreateBuildExecutable(bExecutable)
err = db.CreateBuildExecutable(ctx, bExecutable)
if err != nil {
logrus.Errorf("Failed to publish build executable to database %d for %s: %v", b.GetNumber(), r.GetFullName(), err)

Expand Down
3 changes: 2 additions & 1 deletion database/executable/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package executable

import (
"context"
"fmt"

"github.com/go-vela/types/constants"
Expand All @@ -14,7 +15,7 @@ import (
)

// CreateBuildExecutable creates a new build executable in the database.
func (e *engine) CreateBuildExecutable(b *library.BuildExecutable) error {
func (e *engine) CreateBuildExecutable(ctx context.Context, b *library.BuildExecutable) error {
e.logger.WithFields(logrus.Fields{
"build": b.GetBuildID(),
}).Tracef("creating build executable for build %d in the database", b.GetBuildID())
Expand Down
3 changes: 2 additions & 1 deletion database/executable/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package executable

import (
"context"
"testing"

"github.com/DATA-DOG/go-sqlmock"
Expand Down Expand Up @@ -53,7 +54,7 @@ VALUES ($1,$2,$3) RETURNING "id"`).
// run tests
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
err := test.database.CreateBuildExecutable(_bExecutable)
err := test.database.CreateBuildExecutable(context.TODO(), _bExecutable)

if test.failure {
if err == nil {
Expand Down
5 changes: 4 additions & 1 deletion database/executable/executable.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package executable

import (
"context"
"fmt"

"github.com/go-vela/types/constants"
Expand All @@ -31,6 +32,8 @@ type (
// engine configuration settings used in build executable functions
config *config

ctx context.Context

// gorm.io/gorm database client used in build executable functions
//
// https://pkg.go.dev/gorm.io/gorm#DB
Expand Down Expand Up @@ -71,7 +74,7 @@ func New(opts ...EngineOpt) (*engine, error) {
}

// create the build executables table
err := e.CreateBuildExecutableTable(e.client.Config.Dialector.Name())
err := e.CreateBuildExecutableTable(e.ctx, e.client.Config.Dialector.Name())
if err != nil {
return nil, fmt.Errorf("unable to create %s table: %w", constants.TableBuildExecutable, err)
}
Expand Down
12 changes: 8 additions & 4 deletions database/executable/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,26 @@

package executable

import "github.com/go-vela/types/library"
import (
"context"

"github.com/go-vela/types/library"
)

// BuildExecutableInterface represents the Vela interface for build executable
// functions with the supported Database backends.
type BuildExecutableInterface interface {
// BuildExecutable Data Definition Language Functions
//
// https://en.wikipedia.org/wiki/Data_definition_language
CreateBuildExecutableTable(string) error
CreateBuildExecutableTable(context.Context, string) error

// BuildExecutable Data Manipulation Language Functions
//
// https://en.wikipedia.org/wiki/Data_manipulation_language

// CreateBuildExecutable defines a function that creates a build executable.
CreateBuildExecutable(*library.BuildExecutable) error
CreateBuildExecutable(context.Context, *library.BuildExecutable) error
// PopBuildExecutable defines a function that gets and deletes a build executable.
PopBuildExecutable(int64) (*library.BuildExecutable, error)
PopBuildExecutable(context.Context, int64) (*library.BuildExecutable, error)
}
11 changes: 11 additions & 0 deletions database/executable/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
package executable

import (
"context"

"github.com/sirupsen/logrus"

"gorm.io/gorm"
Expand Down Expand Up @@ -72,3 +74,12 @@ func WithSkipCreation(skipCreation bool) EngineOpt {
return nil
}
}

// WithContext sets the context in the database engine for build executables.
func WithContext(ctx context.Context) EngineOpt {
return func(e *engine) error {
e.ctx = ctx

return nil
}
}
50 changes: 50 additions & 0 deletions database/executable/opts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package executable

import (
"context"
"reflect"
"testing"

Expand Down Expand Up @@ -263,3 +264,52 @@ func TestExecutable_EngineOpt_WithSkipCreation(t *testing.T) {
})
}
}

func TestExecutable_EngineOpt_WithContext(t *testing.T) {
// setup types
e := &engine{config: new(config)}

// setup tests
tests := []struct {
failure bool
name string
ctx context.Context
want context.Context
}{
{
failure: false,
name: "context set to TODO",
ctx: context.TODO(),
want: context.TODO(),
},
{
failure: false,
name: "context set to nil",
ctx: nil,
want: nil,
},
}

// run tests
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
err := WithContext(test.ctx)(e)

if test.failure {
if err == nil {
t.Errorf("WithContext for %s should have returned err", test.name)
}

return
}

if err != nil {
t.Errorf("WithContext returned err: %v", err)
}

if !reflect.DeepEqual(e.ctx, test.want) {
t.Errorf("WithContext is %v, want %v", e.ctx, test.want)
}
})
}
}
4 changes: 3 additions & 1 deletion database/executable/pop.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
package executable

import (
"context"

"github.com/go-vela/types/constants"
"github.com/go-vela/types/database"
"github.com/go-vela/types/library"
"gorm.io/gorm/clause"
)

// PopBuildExecutable pops a build executable by build_id from the database.
func (e *engine) PopBuildExecutable(id int64) (*library.BuildExecutable, error) {
func (e *engine) PopBuildExecutable(ctx context.Context, id int64) (*library.BuildExecutable, error) {
e.logger.Tracef("popping build executable for build %d from the database", id)

// variable to store query results
Expand Down
5 changes: 3 additions & 2 deletions database/executable/pop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package executable

import (
"context"
"reflect"
"testing"

Expand Down Expand Up @@ -33,7 +34,7 @@ func TestExecutable_Engine_PopBuildExecutable(t *testing.T) {
_sqlite := testSqlite(t)
defer func() { _sql, _ := _sqlite.client.DB(); _sql.Close() }()

err := _sqlite.CreateBuildExecutable(_bExecutable)
err := _sqlite.CreateBuildExecutable(context.TODO(), _bExecutable)
if err != nil {
t.Errorf("unable to create test build executable for sqlite: %v", err)
}
Expand Down Expand Up @@ -62,7 +63,7 @@ func TestExecutable_Engine_PopBuildExecutable(t *testing.T) {
// run tests
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got, err := test.database.PopBuildExecutable(1)
got, err := test.database.PopBuildExecutable(context.TODO(), 1)

if test.failure {
if err == nil {
Expand Down
8 changes: 6 additions & 2 deletions database/executable/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

package executable

import "github.com/go-vela/types/constants"
import (
"context"

"github.com/go-vela/types/constants"
)

const (
// CreatePostgresTable represents a query to create the Postgres build_executables table.
Expand Down Expand Up @@ -33,7 +37,7 @@ build_executables (
)

// CreateBuildExecutableTable creates the build executables table in the database.
func (e *engine) CreateBuildExecutableTable(driver string) error {
func (e *engine) CreateBuildExecutableTable(ctx context.Context, driver string) error {
e.logger.Tracef("creating build_executables table in the database")

// handle the driver provided to create the table
Expand Down
3 changes: 2 additions & 1 deletion database/executable/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package executable

import (
"context"
"testing"

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

if test.failure {
if err == nil {
Expand Down
4 changes: 2 additions & 2 deletions database/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ func testExecutables(t *testing.T, db Interface, resources *Resources) {

// create the pipelines
for _, executable := range resources.Executables {
err := db.CreateBuildExecutable(executable)
err := db.CreateBuildExecutable(context.TODO(), executable)
if err != nil {
t.Errorf("unable to create executable %d: %v", executable.GetID(), err)
}
Expand All @@ -414,7 +414,7 @@ func testExecutables(t *testing.T, db Interface, resources *Resources) {

// pop executables for builds
for _, executable := range resources.Executables {
got, err := db.PopBuildExecutable(executable.GetBuildID())
got, err := db.PopBuildExecutable(context.TODO(), executable.GetBuildID())
if err != nil {
t.Errorf("unable to get executable %d for build %d: %v", executable.GetID(), executable.GetBuildID(), err)
}
Expand Down
1 change: 1 addition & 0 deletions database/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func (e *engine) NewResources(ctx context.Context) error {

// create the database agnostic engine for build_executables
e.BuildExecutableInterface, err = executable.New(
executable.WithContext(e.ctx),
executable.WithClient(e.client),
executable.WithLogger(e.logger),
executable.WithSkipCreation(e.config.SkipCreation),
Expand Down