Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhance: supply parent context to all db calls #1168

Merged
merged 5 commits into from
Aug 21, 2024
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
1 change: 1 addition & 0 deletions database/build/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func (e *engine) CleanBuilds(ctx context.Context, msg string, before int64) (int

// send query to the database
result := e.client.
WithContext(ctx).
Table(constants.TableBuild).
Where("created < ?", before).
Where("status = 'running' OR status = 'pending'").
Expand Down
1 change: 1 addition & 0 deletions database/build/count.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func (e *engine) CountBuilds(ctx context.Context) (int64, error) {

// send query to the database and store result in variable
err := e.client.
WithContext(ctx).
Table(constants.TableBuild).
Count(&b).
Error
Expand Down
1 change: 1 addition & 0 deletions database/build/count_deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func (e *engine) CountBuildsForDeployment(ctx context.Context, d *library.Deploy

// send query to the database and store result in variable
err := e.client.
WithContext(ctx).
Table(constants.TableBuild).
Where("source = ?", d.GetURL()).
Where(filters).
Expand Down
1 change: 1 addition & 0 deletions database/build/count_org.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func (e *engine) CountBuildsForOrg(ctx context.Context, org string, filters map[

// send query to the database and store result in variable
err := e.client.
WithContext(ctx).
Table(constants.TableBuild).
Joins("JOIN repos ON builds.repo_id = repos.id").
Where("repos.org = ?", org).
Expand Down
1 change: 1 addition & 0 deletions database/build/count_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func (e *engine) CountBuildsForRepo(ctx context.Context, r *api.Repo, filters ma

// send query to the database and store result in variable
err := e.client.
WithContext(ctx).
Table(constants.TableBuild).
Where("repo_id = ?", r.GetID()).
Where(filters).
Expand Down
1 change: 1 addition & 0 deletions database/build/count_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func (e *engine) CountBuildsForStatus(ctx context.Context, status string, filter

// send query to the database and store result in variable
err := e.client.
WithContext(ctx).
Table(constants.TableBuild).
Where("status = ?", status).
Where(filters).
Expand Down
5 changes: 4 additions & 1 deletion database/build/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ func (e *engine) CreateBuild(ctx context.Context, b *api.Build) (*api.Build, err
build = build.Crop()

// send query to the database
err = e.client.Table(constants.TableBuild).Create(build).Error
err = e.client.
WithContext(ctx).
Table(constants.TableBuild).
Create(build).Error
if err != nil {
return nil, err
}
Expand Down
1 change: 1 addition & 0 deletions database/build/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func (e *engine) DeleteBuild(ctx context.Context, b *api.Build) error {

// send query to the database
return e.client.
WithContext(ctx).
Table(constants.TableBuild).
Delete(build).
Error
Expand Down
1 change: 1 addition & 0 deletions database/build/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func (e *engine) GetBuild(ctx context.Context, id int64) (*api.Build, error) {

// send query to the database and store result in variable
err := e.client.
WithContext(ctx).
Table(constants.TableBuild).
Preload("Repo").
Preload("Repo.Owner").
Expand Down
1 change: 1 addition & 0 deletions database/build/get_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func (e *engine) GetBuildForRepo(ctx context.Context, r *api.Repo, number int) (

// send query to the database and store result in variable
err := e.client.
WithContext(ctx).
Table(constants.TableBuild).
Preload("Repo").
Preload("Repo.Owner").
Expand Down
16 changes: 12 additions & 4 deletions database/build/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,31 @@ func (e *engine) CreateBuildIndexes(ctx context.Context) error {
e.logger.Tracef("creating indexes for builds table")

// create the created column index for the builds table
err := e.client.Exec(CreateCreatedIndex).Error
err := e.client.
WithContext(ctx).
Exec(CreateCreatedIndex).Error
if err != nil {
return err
}

// create the repo_id column index for the builds table
err = e.client.Exec(CreateRepoIDIndex).Error
err = e.client.
WithContext(ctx).
Exec(CreateRepoIDIndex).Error
if err != nil {
return err
}

// create the source column index for the builds table
err = e.client.Exec(CreateSourceIndex).Error
err = e.client.
WithContext(ctx).
Exec(CreateSourceIndex).Error
if err != nil {
return err
}

// create the status column index for the builds table
return e.client.Exec(CreateStatusIndex).Error
return e.client.
WithContext(ctx).
Exec(CreateStatusIndex).Error
}
1 change: 1 addition & 0 deletions database/build/last_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func (e *engine) LastBuildForRepo(ctx context.Context, r *api.Repo, branch strin

// send query to the database and store result in variable
err := e.client.
WithContext(ctx).
Table(constants.TableBuild).
Preload("Repo").
Preload("Repo.Owner").
Expand Down
1 change: 1 addition & 0 deletions database/build/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func (e *engine) ListBuilds(ctx context.Context) ([]*api.Build, error) {

// send query to the database and store result in variable
err = e.client.
WithContext(ctx).
Preload("Repo").
Preload("Repo.Owner").
Table(constants.TableBuild).
Expand Down
5 changes: 4 additions & 1 deletion database/build/list_dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ func (e *engine) ListBuildsForDashboardRepo(ctx context.Context, r *api.Repo, br
b := new([]types.Build)
builds := []*api.Build{}

query := e.client.Table(constants.TableBuild).Where("repo_id = ?", r.GetID())
query := e.client.
WithContext(ctx).
Table(constants.TableBuild).
Where("repo_id = ?", r.GetID())

if len(branches) > 0 {
query = query.Where("branch IN (?)", branches)
Expand Down
1 change: 1 addition & 0 deletions database/build/list_org.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func (e *engine) ListBuildsForOrg(ctx context.Context, org string, filters map[s
offset := perPage * (page - 1)

err = e.client.
WithContext(ctx).
Table(constants.TableBuild).
Preload("Repo").
Preload("Repo.Owner").
Expand Down
1 change: 1 addition & 0 deletions database/build/list_pending_running.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func (e *engine) ListPendingAndRunningBuilds(ctx context.Context, after string)

// send query to the database and store result in variable
err := e.client.
WithContext(ctx).
Table(constants.TableBuild).
Select("builds.created, builds.number, builds.status, repos.full_name").
InnerJoins("INNER JOIN repos ON builds.repo_id = repos.id").
Expand Down
1 change: 1 addition & 0 deletions database/build/list_pending_running_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func (e *engine) ListPendingAndRunningBuildsForRepo(ctx context.Context, repo *a

// send query to the database and store result in variable
err := e.client.
WithContext(ctx).
Table(constants.TableBuild).
Preload("Repo").
Preload("Repo.Owner").
Expand Down
1 change: 1 addition & 0 deletions database/build/list_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func (e *engine) ListBuildsForRepo(ctx context.Context, r *api.Repo, filters map
offset := perPage * (page - 1)

err = e.client.
WithContext(ctx).
Table(constants.TableBuild).
Preload("Repo").
Preload("Repo.Owner").
Expand Down
8 changes: 6 additions & 2 deletions database/build/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,15 @@ func (e *engine) CreateBuildTable(ctx context.Context, driver string) error {
switch driver {
case constants.DriverPostgres:
// create the builds table for Postgres
return e.client.Exec(CreatePostgresTable).Error
return e.client.
WithContext(ctx).
Exec(CreatePostgresTable).Error
case constants.DriverSqlite:
fallthrough
default:
// create the builds table for Sqlite
return e.client.Exec(CreateSqliteTable).Error
return e.client.
WithContext(ctx).
Exec(CreateSqliteTable).Error
}
}
5 changes: 4 additions & 1 deletion database/build/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ func (e *engine) UpdateBuild(ctx context.Context, b *api.Build) (*api.Build, err
build = build.Crop()

// send query to the database
err = e.client.Table(constants.TableBuild).Save(build).Error
err = e.client.
WithContext(ctx).
Table(constants.TableBuild).
Save(build).Error
if err != nil {
return nil, err
}
Expand Down
5 changes: 4 additions & 1 deletion database/dashboard/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ func (e *engine) CreateDashboard(ctx context.Context, d *api.Dashboard) (*api.Da
}

// send query to the database
result := e.client.Table(constants.TableDashboard).Create(dashboard)
result := e.client.
WithContext(ctx).
Table(constants.TableDashboard).
Create(dashboard)

return dashboard.ToAPI(), result.Error
}
1 change: 1 addition & 0 deletions database/dashboard/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func (e *engine) DeleteDashboard(ctx context.Context, d *api.Dashboard) error {

// send query to the database
return e.client.
WithContext(ctx).
Table(constants.TableDashboard).
Delete(dashboard).
Error
Expand Down
1 change: 1 addition & 0 deletions database/dashboard/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func (e *engine) GetDashboard(ctx context.Context, id string) (*api.Dashboard, e

// send query to the database and store result in variable
err := e.client.
WithContext(ctx).
Table(constants.TableDashboard).
Where("id = ?", id).
Take(d).
Expand Down
8 changes: 6 additions & 2 deletions database/dashboard/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,15 @@ func (e *engine) CreateDashboardTable(ctx context.Context, driver string) error
switch driver {
case constants.DriverPostgres:
// create the dashboards table for Postgres
return e.client.Exec(CreatePostgresTable).Error
return e.client.
WithContext(ctx).
Exec(CreatePostgresTable).Error
case constants.DriverSqlite:
fallthrough
default:
// create the dashboards table for Sqlite
return e.client.Exec(CreateSqliteTable).Error
return e.client.
WithContext(ctx).
Exec(CreateSqliteTable).Error
}
}
5 changes: 4 additions & 1 deletion database/dashboard/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ func (e *engine) UpdateDashboard(ctx context.Context, d *api.Dashboard) (*api.Da
}

// send query to the database
err = e.client.Table(constants.TableDashboard).Save(dashboard).Error
err = e.client.
WithContext(ctx).
Table(constants.TableDashboard).
Save(dashboard).Error
if err != nil {
return nil, err
}
Expand Down
1 change: 1 addition & 0 deletions database/deployment/count.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func (e *engine) CountDeployments(ctx context.Context) (int64, error) {

// send query to the database and store result in variable
err := e.client.
WithContext(ctx).
Table(constants.TableDeployment).
Count(&d).
Error
Expand Down
1 change: 1 addition & 0 deletions database/deployment/count_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func (e *engine) CountDeploymentsForRepo(ctx context.Context, r *api.Repo) (int6

// send query to the database and store result in variable
err := e.client.
WithContext(ctx).
Table(constants.TableDeployment).
Where("repo_id = ?", r.GetID()).
Count(&d).
Expand Down
5 changes: 4 additions & 1 deletion database/deployment/create.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: Apache-2.0

package deployment

Check failure on line 3 in database/deployment/create.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] database/deployment/create.go#L3

3-37 lines are duplicate of `database/deployment/update.go:3-37` (dupl)
Raw output
database/deployment/create.go:3: 3-37 lines are duplicate of `database/deployment/update.go:3-37` (dupl)
package deployment

import (
	"context"

	"github.com/sirupsen/logrus"

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

// CreateDeployment creates a new deployment in the database.
func (e *engine) CreateDeployment(ctx context.Context, d *library.Deployment) (*library.Deployment, error) {
	e.logger.WithFields(logrus.Fields{
		"deployment": d.GetID(),
	}).Tracef("creating deployment %d", d.GetID())

	// cast the library type to database type
	deployment := database.DeploymentFromLibrary(d)

	// validate the necessary fields are populated
	err := deployment.Validate()
	if err != nil {
		return nil, err
	}

	result := e.client.
		WithContext(ctx).
		Table(constants.TableDeployment).
		Create(deployment)

	// send query to the database
	return deployment.ToLibrary(d.Builds), result.Error
}

import (
"context"
Expand All @@ -27,7 +27,10 @@
return nil, err
}

result := e.client.Table(constants.TableDeployment).Create(deployment)
result := e.client.
WithContext(ctx).
Table(constants.TableDeployment).
Create(deployment)

// send query to the database
return deployment.ToLibrary(d.Builds), result.Error
Expand Down
1 change: 1 addition & 0 deletions database/deployment/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func (e *engine) DeleteDeployment(ctx context.Context, d *library.Deployment) er

// send query to the database
return e.client.
WithContext(ctx).
Table(constants.TableDeployment).
Delete(deployment).
Error
Expand Down
2 changes: 2 additions & 0 deletions database/deployment/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func (e *engine) GetDeployment(ctx context.Context, id int64) (*library.Deployme

// send query to the database and store result in variable
err := e.client.
WithContext(ctx).
Table(constants.TableDeployment).
Where("id = ?", id).
Take(d).
Expand All @@ -40,6 +41,7 @@ func (e *engine) GetDeployment(ctx context.Context, id int64) (*library.Deployme

// send query to the database and store result in variable
err = e.client.
WithContext(ctx).
Table(constants.TableBuild).
Where("id = ?", bID).
Take(b).
Expand Down
2 changes: 2 additions & 0 deletions database/deployment/get_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func (e *engine) GetDeploymentForRepo(ctx context.Context, r *api.Repo, number i

// send query to the database and store result in variable
err := e.client.
WithContext(ctx).
Table(constants.TableDeployment).
Where("repo_id = ?", r.GetID()).
Where("number = ?", number).
Expand All @@ -48,6 +49,7 @@ func (e *engine) GetDeploymentForRepo(ctx context.Context, r *api.Repo, number i

// send query to the database and store result in variable
err = e.client.
WithContext(ctx).
Table(constants.TableBuild).
Where("id = ?", bID).
Take(b).
Expand Down
4 changes: 3 additions & 1 deletion database/deployment/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@ func (e *engine) CreateDeploymentIndexes(ctx context.Context) error {
e.logger.Tracef("creating indexes for deployments table")

// create the repo_id column index for the deployments table
return e.client.Exec(CreateRepoIDIndex).Error
return e.client.
WithContext(ctx).
Exec(CreateRepoIDIndex).Error
}
2 changes: 2 additions & 0 deletions database/deployment/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

// send query to the database and store result in variable
err := e.client.
WithContext(ctx).
Table(constants.TableDeployment).
Find(&d).
Error
Expand All @@ -29,7 +30,7 @@
}

// iterate through all query results
for _, deployment := range *d {

Check failure on line 33 in database/deployment/list.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] database/deployment/list.go#L33

33-63 lines are duplicate of `database/deployment/list_repo.go:46-76` (dupl)
Raw output
database/deployment/list.go:33: 33-63 lines are duplicate of `database/deployment/list_repo.go:46-76` (dupl)
	for _, deployment := range *d {
		// https://golang.org/doc/faq#closures_and_goroutines
		tmp := deployment

		builds := []*library.Build{}

		for _, a := range tmp.Builds {
			bID, err := strconv.ParseInt(a, 10, 64)
			if err != nil {
				return nil, err
			}
			// variable to store query results
			b := new(database.Build)

			// send query to the database and store result in variable
			err = e.client.
				WithContext(ctx).
				Table(constants.TableBuild).
				Where("id = ?", bID).
				Take(b).
				Error
			if err != nil {
				return nil, err
			}

			builds = append(builds, b.ToLibrary())
		}

		// convert query result to library type
		deployments = append(deployments, tmp.ToLibrary(builds))
	}
// https://golang.org/doc/faq#closures_and_goroutines
tmp := deployment

Expand All @@ -45,6 +46,7 @@

// send query to the database and store result in variable
err = e.client.
WithContext(ctx).
Table(constants.TableBuild).
Where("id = ?", bID).
Take(b).
Expand Down
2 changes: 2 additions & 0 deletions database/deployment/list_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

// send query to the database and store result in variable
err := e.client.
WithContext(ctx).
Table(constants.TableDeployment).
Where("repo_id = ?", r.GetID()).
Order("number DESC").
Expand All @@ -42,7 +43,7 @@
}

// iterate through all query results
for _, deployment := range *d {

Check failure on line 46 in database/deployment/list_repo.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] database/deployment/list_repo.go#L46

46-76 lines are duplicate of `database/deployment/list.go:33-63` (dupl)
Raw output
database/deployment/list_repo.go:46: 46-76 lines are duplicate of `database/deployment/list.go:33-63` (dupl)
	for _, deployment := range *d {
		// https://golang.org/doc/faq#closures_and_goroutines
		tmp := deployment

		builds := []*library.Build{}

		for _, a := range tmp.Builds {
			bID, err := strconv.ParseInt(a, 10, 64)
			if err != nil {
				return nil, err
			}
			// variable to store query results
			b := new(database.Build)

			// send query to the database and store result in variable
			err = e.client.
				WithContext(ctx).
				Table(constants.TableBuild).
				Where("id = ?", bID).
				Take(b).
				Error
			if err != nil {
				return nil, err
			}

			builds = append(builds, b.ToLibrary())
		}

		// convert query result to library type
		deployments = append(deployments, tmp.ToLibrary(builds))
	}
// https://golang.org/doc/faq#closures_and_goroutines
tmp := deployment

Expand All @@ -58,6 +59,7 @@

// send query to the database and store result in variable
err = e.client.
WithContext(ctx).
Table(constants.TableBuild).
Where("id = ?", bID).
Take(b).
Expand Down
8 changes: 6 additions & 2 deletions database/deployment/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,15 @@ func (e *engine) CreateDeploymentTable(ctx context.Context, driver string) error
switch driver {
case constants.DriverPostgres:
// create the deployments table for Postgres
return e.client.Exec(CreatePostgresTable).Error
return e.client.
WithContext(ctx).
Exec(CreatePostgresTable).Error
case constants.DriverSqlite:
fallthrough
default:
// create the deployments table for Sqlite
return e.client.Exec(CreateSqliteTable).Error
return e.client.
WithContext(ctx).
Exec(CreateSqliteTable).Error
}
}
Loading
Loading