Skip to content

Commit

Permalink
enhance: supply parent context to all db calls
Browse files Browse the repository at this point in the history
  • Loading branch information
plyr4 committed Aug 13, 2024
1 parent df7bf4a commit da924d5
Show file tree
Hide file tree
Showing 173 changed files with 404 additions and 86 deletions.
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
Expand Up @@ -27,7 +27,10 @@ func (e *engine) CreateDeployment(ctx context.Context, d *library.Deployment) (*
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 @@ func (e *engine) ListDeployments(ctx context.Context) ([]*library.Deployment, er

// send query to the database and store result in variable
err := e.client.
WithContext(ctx).
Table(constants.TableDeployment).
Find(&d).
Error
Expand All @@ -45,6 +46,7 @@ func (e *engine) ListDeployments(ctx context.Context) ([]*library.Deployment, er

// 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 @@ func (e *engine) ListDeploymentsForRepo(ctx context.Context, r *api.Repo, page,

// 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 Down Expand Up @@ -58,6 +59,7 @@ func (e *engine) ListDeploymentsForRepo(ctx context.Context, r *api.Repo, page,

// 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

0 comments on commit da924d5

Please sign in to comment.