Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
46 changes: 30 additions & 16 deletions database/postgres/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,26 @@ import (
)

// GetBuild gets a build by number and repo ID from the database.
//
// nolint: dupl // ignore false positive of duplicate code
func (c *client) GetBuild(number int, r *library.Repo) (*library.Build, error) {
logrus.Tracef("getting build %s/%d from the database", r.GetFullName(), number)

// variable to store query results
b := new(database.Build)

// send query to the database and store result in variable
err := c.Postgres.
result := c.Postgres.
Table(constants.TableBuild).
Raw(dml.SelectRepoBuild, r.GetID(), number).
Scan(b).Error
Scan(b)

// check if the query returned a record not found error or no rows were returned
if errors.Is(result.Error, gorm.ErrRecordNotFound) || result.RowsAffected == 0 {
return nil, gorm.ErrRecordNotFound
}

return b.ToLibrary(), err
return b.ToLibrary(), result.Error
}

// GetLastBuild gets the last build by repo ID from the database.
Expand All @@ -41,17 +48,18 @@ func (c *client) GetLastBuild(r *library.Repo) (*library.Build, error) {
b := new(database.Build)

// send query to the database and store result in variable
err := c.Postgres.
result := c.Postgres.
Table(constants.TableBuild).
Raw(dml.SelectLastRepoBuild, r.GetID()).
Scan(b).Error
Scan(b)

// the record will not exist if it's a new repo
if errors.Is(err, gorm.ErrRecordNotFound) {
// check if the query returned a record not found error or no rows were returned
if errors.Is(result.Error, gorm.ErrRecordNotFound) || result.RowsAffected == 0 {
// the record will not exist if it's a new repo
return nil, nil
}

return b.ToLibrary(), err
return b.ToLibrary(), result.Error
}

// GetLastBuildByBranch gets the last build by repo ID and branch from the database.
Expand All @@ -63,17 +71,18 @@ func (c *client) GetLastBuildByBranch(r *library.Repo, branch string) (*library.
b := new(database.Build)

// send query to the database and store result in variable
err := c.Postgres.
result := c.Postgres.
Table(constants.TableBuild).
Raw(dml.SelectLastRepoBuildByBranch, r.GetID(), branch).
Scan(b).Error
Scan(b)

// the record will not exist if it's a new repo
if errors.Is(err, gorm.ErrRecordNotFound) {
// check if the query returned a record not found error or no rows were returned
if errors.Is(result.Error, gorm.ErrRecordNotFound) || result.RowsAffected == 0 {
// the record will not exist if it's a new repo
return nil, nil
}

return b.ToLibrary(), err
return b.ToLibrary(), result.Error
}

// GetPendingAndRunningBuilds returns the list of pending
Expand All @@ -85,10 +94,15 @@ func (c *client) GetPendingAndRunningBuilds(after string) ([]*library.BuildQueue
b := new([]database.BuildQueue)

// send query to the database and store result in variable
err := c.Postgres.
result := c.Postgres.
Table(constants.TableBuild).
Raw(dml.SelectPendingAndRunningBuilds, after).
Scan(b).Error
Scan(b)

// check if the query returned a record not found error or no rows were returned
if errors.Is(result.Error, gorm.ErrRecordNotFound) || result.RowsAffected == 0 {
return nil, gorm.ErrRecordNotFound
}

// variable we want to return
builds := []*library.BuildQueue{}
Expand All @@ -102,7 +116,7 @@ func (c *client) GetPendingAndRunningBuilds(after string) ([]*library.BuildQueue
builds = append(builds, tmp.ToLibrary())
}

return builds, err
return builds, result.Error
}

// CreateBuild creates a new build in the database.
Expand Down
35 changes: 29 additions & 6 deletions database/postgres/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ func TestPostgres_Client_GetBuild(t *testing.T) {
[]string{"id", "repo_id", "number", "parent", "event", "status", "error", "enqueued", "created", "started", "finished", "deploy", "deploy_payload", "clone", "source", "title", "message", "commit", "sender", "author", "email", "link", "branch", "ref", "base_ref", "head_ref", "host", "runtime", "distribution", "timestamp"},
).AddRow(1, 1, 1, 0, "", "", "", 0, 0, 0, 0, "", nil, "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 0)

// ensure the mock expects the query
// ensure the mock expects the query for test case 1
_mock.ExpectQuery(_query.SQL.String()).WillReturnRows(_rows)
// ensure the mock expects the error for test case 2
_mock.ExpectQuery(_query.SQL.String()).WillReturnError(gorm.ErrRecordNotFound)

// setup tests
tests := []struct {
Expand All @@ -62,6 +64,10 @@ func TestPostgres_Client_GetBuild(t *testing.T) {
failure: false,
want: _build,
},
{
failure: true,
want: nil,
},
}

// run tests
Expand Down Expand Up @@ -120,8 +126,10 @@ func TestPostgres_Client_GetLastBuild(t *testing.T) {
[]string{"id", "repo_id", "number", "parent", "event", "status", "error", "enqueued", "created", "started", "finished", "deploy", "deploy_payload", "clone", "source", "title", "message", "commit", "sender", "author", "email", "link", "branch", "ref", "base_ref", "head_ref", "host", "runtime", "distribution", "timestamp"},
).AddRow(1, 1, 1, 0, "", "", "", 0, 0, 0, 0, "", nil, "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 0)

// ensure the mock expects the query
// ensure the mock expects the query for test case 1
_mock.ExpectQuery(_query.SQL.String()).WillReturnRows(_rows)
// ensure the mock expects the error for test case 2
_mock.ExpectQuery(_query.SQL.String()).WillReturnError(gorm.ErrRecordNotFound)

// setup tests
tests := []struct {
Expand All @@ -132,6 +140,10 @@ func TestPostgres_Client_GetLastBuild(t *testing.T) {
failure: false,
want: _build,
},
{
failure: false,
want: nil,
},
}

// run tests
Expand Down Expand Up @@ -190,8 +202,10 @@ func TestPostgres_Client_GetLastBuildByBranch(t *testing.T) {
[]string{"id", "repo_id", "number", "parent", "event", "status", "error", "enqueued", "created", "started", "finished", "deploy", "deploy_payload", "clone", "source", "title", "message", "commit", "sender", "author", "email", "link", "branch", "ref", "base_ref", "head_ref", "host", "runtime", "distribution", "timestamp"},
).AddRow(1, 1, 1, 0, "", "", "", 0, 0, 0, 0, "", nil, "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 0)

// ensure the mock expects the query
// ensure the mock expects the query for test case 1
_mock.ExpectQuery(_query.SQL.String()).WillReturnRows(_rows)
// ensure the mock expects the error for test case 2
_mock.ExpectQuery(_query.SQL.String()).WillReturnError(gorm.ErrRecordNotFound)

// setup tests
tests := []struct {
Expand All @@ -202,6 +216,10 @@ func TestPostgres_Client_GetLastBuildByBranch(t *testing.T) {
failure: false,
want: _build,
},
{
failure: false,
want: nil,
},
}

// run tests
Expand Down Expand Up @@ -254,11 +272,12 @@ func TestPostgres_Client_GetPendingAndRunningBuilds(t *testing.T) {

// create expected return in mock
_rows := sqlmock.NewRows([]string{"created", "full_name", "number", "status"}).
AddRow(0, "", 1, "").
AddRow(0, "", 2, "")
AddRow(0, "", 1, "").AddRow(0, "", 2, "")

// ensure the mock expects the query
// ensure the mock expects the query for test case 1
_mock.ExpectQuery(_query.SQL.String()).WillReturnRows(_rows)
// ensure the mock expects the error for test case 2
_mock.ExpectQuery(_query.SQL.String()).WillReturnError(gorm.ErrRecordNotFound)

// setup tests
tests := []struct {
Expand All @@ -269,6 +288,10 @@ func TestPostgres_Client_GetPendingAndRunningBuilds(t *testing.T) {
failure: false,
want: []*library.BuildQueue{_buildOne, _buildTwo},
},
{
failure: true,
want: nil,
},
}

// run tests
Expand Down
24 changes: 16 additions & 8 deletions database/postgres/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,26 @@ import (
)

// GetHook gets a hook by number and repo ID from the database.
//
// nolint: dupl // ignore false positive of duplicate code
func (c *client) GetHook(number int, r *library.Repo) (*library.Hook, error) {
logrus.Tracef("getting hook %s/%d from the database", r.GetFullName(), number)

// variable to store query results
h := new(database.Hook)

// send query to the database and store result in variable
err := c.Postgres.
result := c.Postgres.
Table(constants.TableHook).
Raw(dml.SelectRepoHook, r.GetID(), number).
Scan(h).Error
Scan(h)

// check if the query returned a record not found error or no rows were returned
if errors.Is(result.Error, gorm.ErrRecordNotFound) || result.RowsAffected == 0 {
return nil, gorm.ErrRecordNotFound
}

return h.ToLibrary(), err
return h.ToLibrary(), result.Error
}

// GetLastHook gets the last hook by repo ID from the database.
Expand All @@ -41,17 +48,18 @@ func (c *client) GetLastHook(r *library.Repo) (*library.Hook, error) {
h := new(database.Hook)

// send query to the database and store result in variable
err := c.Postgres.
result := c.Postgres.
Table(constants.TableHook).
Raw(dml.SelectLastRepoHook, r.GetID()).
Scan(h).Error
Scan(h)

// the record will not exist if it's a new repo
if errors.Is(err, gorm.ErrRecordNotFound) {
// check if the query returned a record not found error or no rows were returned
if errors.Is(result.Error, gorm.ErrRecordNotFound) || result.RowsAffected == 0 {
// the record will not exist if it's a new repo
return nil, nil
}

return h.ToLibrary(), err
return h.ToLibrary(), result.Error
}

// CreateHook creates a new hook in the database.
Expand Down
16 changes: 14 additions & 2 deletions database/postgres/hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ func TestPostgres_Client_GetHook(t *testing.T) {
[]string{"id", "repo_id", "build_id", "number", "source_id", "created", "host", "event", "branch", "error", "status", "link"},
).AddRow(1, 1, 1, 1, "c8da1302-07d6-11ea-882f-4893bca275b8", 0, "", "", "", "", "", "")

// ensure the mock expects the query
// ensure the mock expects the query for test case 1
_mock.ExpectQuery(_query.SQL.String()).WillReturnRows(_rows)
// ensure the mock expects the error for test case 2
_mock.ExpectQuery(_query.SQL.String()).WillReturnError(gorm.ErrRecordNotFound)

// setup tests
tests := []struct {
Expand All @@ -61,6 +63,10 @@ func TestPostgres_Client_GetHook(t *testing.T) {
failure: false,
want: _hook,
},
{
failure: true,
want: nil,
},
}

// run tests
Expand Down Expand Up @@ -118,8 +124,10 @@ func TestPostgres_Client_GetLastHook(t *testing.T) {
[]string{"id", "repo_id", "build_id", "number", "source_id", "created", "host", "event", "branch", "error", "status", "link"},
).AddRow(1, 1, 1, 1, "c8da1302-07d6-11ea-882f-4893bca275b8", 0, "", "", "", "", "", "")

// ensure the mock expects the query
// ensure the mock expects the query for test case 1
_mock.ExpectQuery(_query.SQL.String()).WillReturnRows(_rows)
// ensure the mock expects the error for test case 2
_mock.ExpectQuery(_query.SQL.String()).WillReturnError(gorm.ErrRecordNotFound)

// setup tests
tests := []struct {
Expand All @@ -130,6 +138,10 @@ func TestPostgres_Client_GetLastHook(t *testing.T) {
failure: false,
want: _hook,
},
{
failure: false,
want: nil,
},
}

// run tests
Expand Down
34 changes: 20 additions & 14 deletions database/postgres/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
package postgres

import (
"errors"
"fmt"

"github.com/go-vela/server/database/postgres/dml"
"github.com/go-vela/types/constants"
"github.com/go-vela/types/database"
"github.com/go-vela/types/library"
"gorm.io/gorm"

"github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -66,30 +68,32 @@ func (c *client) GetStepLog(id int64) (*library.Log, error) {
l := new(database.Log)

// send query to the database and store result in variable
err := c.Postgres.
result := c.Postgres.
Table(constants.TableLog).
Raw(dml.SelectStepLog, id).
Scan(l).Error
if err != nil {
return l.ToLibrary(), err
Scan(l)

// check if the query returned a record not found error or no rows were returned
if errors.Is(result.Error, gorm.ErrRecordNotFound) || result.RowsAffected == 0 {
return nil, gorm.ErrRecordNotFound
}

// decompress log data for the step
//
// https://pkg.go.dev/github.com/go-vela/types/database#Log.Decompress
err = l.Decompress()
err := l.Decompress()
if err != nil {
// ensures that the change is backwards compatible
// by logging the error instead of returning it
// which allows us to fetch uncompressed logs
logrus.Errorf("unable to decompress logs for step %d: %v", id, err)

// return the uncompressed log
return l.ToLibrary(), nil
return l.ToLibrary(), result.Error
}

// return the decompressed log
return l.ToLibrary(), nil
return l.ToLibrary(), result.Error
}

// GetServiceLog gets a log by unique ID from the database.
Expand All @@ -102,30 +106,32 @@ func (c *client) GetServiceLog(id int64) (*library.Log, error) {
l := new(database.Log)

// send query to the database and store result in variable
err := c.Postgres.
result := c.Postgres.
Table(constants.TableLog).
Raw(dml.SelectServiceLog, id).
Scan(l).Error
if err != nil {
return l.ToLibrary(), err
Scan(l)

// check if the query returned a record not found error or no rows were returned
if errors.Is(result.Error, gorm.ErrRecordNotFound) || result.RowsAffected == 0 {
return nil, gorm.ErrRecordNotFound
}

// decompress log data for the service
//
// https://pkg.go.dev/github.com/go-vela/types/database#Log.Decompress
err = l.Decompress()
err := l.Decompress()
if err != nil {
// ensures that the change is backwards compatible
// by logging the error instead of returning it
// which allowing us to fetch uncompressed logs
logrus.Errorf("unable to decompress logs for service %d: %v", id, err)

// return the uncompressed log
return l.ToLibrary(), nil
return l.ToLibrary(), result.Error
}

// return the decompressed log
return l.ToLibrary(), nil
return l.ToLibrary(), result.Error
}

// CreateLog creates a new log in the database.
Expand Down
Loading