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

refactor(database): move log logic into separate package #722

Merged
merged 66 commits into from
Feb 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
57e5162
Merge branch 'master' of github.com:go-vela/server
jbrockopp Feb 23, 2022
b6b840c
Merge branch 'master' of github.com:go-vela/server
jbrockopp Mar 10, 2022
858c818
Merge branch 'master' of github.com:go-vela/server
jbrockopp Mar 16, 2022
ceddb11
Merge branch 'master' of github.com:go-vela/server
jbrockopp Mar 28, 2022
331b2a2
Merge branch 'master' of github.com:go-vela/server
jbrockopp Apr 11, 2022
c4ec5fb
Merge branches 'master' and 'master' of github.com:go-vela/server
jbrockopp Apr 18, 2022
30c1151
Merge branch 'master' of github.com:go-vela/server
jbrockopp Apr 20, 2022
0ddc31c
Merge branch 'master' of github.com:go-vela/server
jbrockopp Apr 27, 2022
890f9fa
Merge branch 'master' of github.com:go-vela/server
jbrockopp May 9, 2022
cf4ca0e
Merge branch 'master' of github.com:go-vela/server
jbrockopp May 20, 2022
175e5c5
Merge branch 'master' of github.com:go-vela/server
jbrockopp May 31, 2022
1728dce
Merge branch 'master' of github.com:go-vela/server
jbrockopp Jun 12, 2022
88d3974
Merge branch 'master' of github.com:go-vela/server
jbrockopp Jun 22, 2022
de5ac75
feat(database): add user engine
jbrockopp Jun 22, 2022
8f2fce6
chore(database): remove old user logic
jbrockopp Jun 22, 2022
736a0e9
refactor(database): restructure DDL functions
jbrockopp Jun 22, 2022
c28ca23
chore: updates for database user engine
jbrockopp Jun 22, 2022
a4d8bbc
Merge branch 'master' of github.com:go-vela/server
jbrockopp Jun 26, 2022
c719ab0
Merge branch 'refactor/database/user' of github.com:go-vela/server in…
jbrockopp Jun 26, 2022
a147ce8
feat(database): add repo engine
jbrockopp Jun 27, 2022
b7e267f
Merge branch 'master' of github.com:go-vela/server into refactor/data…
jbrockopp Aug 20, 2022
ff5b8e1
chore: save work
jbrockopp Aug 27, 2022
ef70014
Merge branch 'master' of github.com:go-vela/server
jbrockopp Aug 27, 2022
ca230da
Merge branch 'master' of github.com:go-vela/server into refactor/data…
jbrockopp Aug 27, 2022
ab2f213
fix: Scan() -> Take()
jbrockopp Aug 27, 2022
68cf328
fix: sort by for listing repos
jbrockopp Sep 10, 2022
55862b2
Merge branch 'master' into refactor/database/repo
jbrockopp Sep 10, 2022
443c5d0
Merge branches 'master' and 'master' of github.com:go-vela/server
jbrockopp Sep 11, 2022
a47cf36
Merge branch 'refactor/database/repo' of github.com:go-vela/server in…
jbrockopp Sep 11, 2022
8e628be
feat(database): add worker engine
jbrockopp Sep 11, 2022
a827790
chore(database): remove old worker logic
jbrockopp Sep 11, 2022
a960f5d
chore: updates for database worker engine
jbrockopp Sep 11, 2022
3b7918f
Merge branch 'main' into refactor/database/worker
jbrockopp Sep 30, 2022
535b2bb
Merge branch 'refactor/database/worker' of github.com:go-vela/server …
jbrockopp Sep 30, 2022
acad4fd
fix: issues from merge conflict
jbrockopp Sep 30, 2022
72bc1f1
Merge branch 'main' into refactor/database/worker
jbrockopp Sep 30, 2022
3745fdf
Merge branch 'refactor/database/worker' of github.com:go-vela/server …
jbrockopp Sep 30, 2022
6fcc762
chore: address linter feedback
jbrockopp Sep 30, 2022
57206f9
chore: address linter feedback v2
jbrockopp Sep 30, 2022
af3cf83
Merge branch 'main' into refactor/database/worker
jbrockopp Oct 5, 2022
6cf8508
Merge branch 'main' into refactor/database/worker
jbrockopp Oct 7, 2022
b5254d7
Merge branch 'refactor/database/worker' of github.com:go-vela/server …
jbrockopp Oct 7, 2022
0648d24
Merge branch 'main' into refactor/database/worker
jbrockopp Oct 7, 2022
4ac392a
Merge branch 'refactor/database/worker' of github.com:go-vela/server …
jbrockopp Oct 7, 2022
a7994ec
Merge branch 'main' into refactor/database/worker
jbrockopp Oct 7, 2022
7c2b5b7
Merge branch 'refactor/database/worker' of github.com:go-vela/server …
jbrockopp Oct 7, 2022
c4d188d
Merge branch 'refactor/database/worker' of github.com:go-vela/server …
jbrockopp Oct 7, 2022
1171ce1
feat(database): add hook engine
jbrockopp Oct 7, 2022
286e06e
chore: remove Limit(1) with Take()
jbrockopp Oct 7, 2022
a28899c
Merge branch 'refactor/database/worker' of github.com:go-vela/server …
jbrockopp Oct 7, 2022
d9d2749
chore(database): remove old hook logic
jbrockopp Oct 7, 2022
6086c0a
chore: updates for database hook engine
jbrockopp Oct 7, 2022
7991f03
refactor(database/worker): name of index
jbrockopp Oct 8, 2022
5b90666
Merge branch 'refactor/database/worker' of github.com:go-vela/server …
jbrockopp Oct 8, 2022
feb5118
Merge branch 'refactor/database/hook' of github.com:go-vela/server
jbrockopp Oct 8, 2022
dc7187e
feat(database): add log engine
jbrockopp Oct 10, 2022
877639d
chore(database): remove old log logic
jbrockopp Oct 10, 2022
0ffb84b
chore: updates for database log engine
jbrockopp Oct 10, 2022
11c8518
Merge branch 'main' of github.com:go-vela/server into refactor/databa…
jbrockopp Feb 14, 2023
6bf3448
chore: update copyright year
jbrockopp Feb 14, 2023
b42ca0e
chore: address linter feedback
jbrockopp Feb 14, 2023
a6652af
chore: address linter feedback
jbrockopp Feb 14, 2023
bb3ee60
Merge branch 'main' into refactor/database/log
jbrockopp Feb 21, 2023
36bef65
Merge branch 'main' into refactor/database/log
wass3r Feb 23, 2023
54983cc
Merge branch 'main' into refactor/database/log
wass3r Feb 24, 2023
9ae0b99
Merge branch 'main' into refactor/database/log
plyr4 Feb 24, 2023
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
57 changes: 42 additions & 15 deletions api/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ func GetBuildLogs(c *gin.Context) {
}).Infof("reading logs for build %s", entry)

// send API call to capture the list of logs for the build
l, err := database.FromContext(c).GetBuildLogs(b.GetID())
//
// TODO: add page and per_page query parameters
l, t, err := database.FromContext(c).ListLogsForBuild(b, 1, 100)
if err != nil {
retErr := fmt.Errorf("unable to get logs for build %s: %w", entry, err)

Expand All @@ -92,6 +94,15 @@ func GetBuildLogs(c *gin.Context) {
return
}

// create pagination object
pagination := Pagination{
Page: 1,
PerPage: 100,
Total: t,
}
// set pagination headers
pagination.SetHeaderLink(c)

c.JSON(http.StatusOK, l)
}

Expand Down Expand Up @@ -200,7 +211,7 @@ func CreateServiceLog(c *gin.Context) {
}

// send API call to capture the created log
l, _ := database.FromContext(c).GetServiceLog(s.GetID())
l, _ := database.FromContext(c).GetLogForService(s)

c.JSON(http.StatusCreated, l)
}
Expand Down Expand Up @@ -270,7 +281,7 @@ func GetServiceLog(c *gin.Context) {
}).Infof("reading logs for service %s", entry)

// send API call to capture the service logs
l, err := database.FromContext(c).GetServiceLog(s.GetID())
l, err := database.FromContext(c).GetLogForService(s)
if err != nil {
retErr := fmt.Errorf("unable to get logs for service %s: %w", entry, err)

Expand Down Expand Up @@ -358,7 +369,7 @@ func UpdateServiceLog(c *gin.Context) {
}).Infof("updating logs for service %s", entry)

// send API call to capture the service logs
l, err := database.FromContext(c).GetServiceLog(s.GetID())
l, err := database.FromContext(c).GetLogForService(s)
if err != nil {
retErr := fmt.Errorf("unable to get logs for service %s: %w", entry, err)

Expand Down Expand Up @@ -396,7 +407,7 @@ func UpdateServiceLog(c *gin.Context) {
}

// send API call to capture the updated log
l, _ = database.FromContext(c).GetServiceLog(s.GetID())
l, _ = database.FromContext(c).GetLogForService(s)

c.JSON(http.StatusOK, l)
}
Expand Down Expand Up @@ -444,8 +455,6 @@ func UpdateServiceLog(c *gin.Context) {

// DeleteServiceLog represents the API handler to remove
// the logs for a service from the configured backend.
//
//nolint:dupl // ignore similar code with step
func DeleteServiceLog(c *gin.Context) {
// capture middleware values
b := build.Retrieve(c)
Expand All @@ -467,8 +476,18 @@ func DeleteServiceLog(c *gin.Context) {
"user": u.GetName(),
}).Infof("deleting logs for service %s", entry)

// send API call to capture the service logs
l, err := database.FromContext(c).GetLogForService(s)
if err != nil {
retErr := fmt.Errorf("unable to get logs for service %s: %w", entry, err)

util.HandleError(c, http.StatusInternalServerError, retErr)

return
}

// send API call to remove the log
err := database.FromContext(c).DeleteLog(s.GetID())
err = database.FromContext(c).DeleteLog(l)
if err != nil {
retErr := fmt.Errorf("unable to delete logs for service %s: %w", entry, err)

Expand Down Expand Up @@ -585,7 +604,7 @@ func CreateStepLog(c *gin.Context) {
}

// send API call to capture the created log
l, _ := database.FromContext(c).GetStepLog(s.GetID())
l, _ := database.FromContext(c).GetLogForStep(s)

c.JSON(http.StatusCreated, l)
}
Expand Down Expand Up @@ -656,7 +675,7 @@ func GetStepLog(c *gin.Context) {
}).Infof("reading logs for step %s", entry)

// send API call to capture the step logs
l, err := database.FromContext(c).GetStepLog(s.GetID())
l, err := database.FromContext(c).GetLogForStep(s)
if err != nil {
retErr := fmt.Errorf("unable to get logs for step %s: %w", entry, err)

Expand Down Expand Up @@ -744,7 +763,7 @@ func UpdateStepLog(c *gin.Context) {
}).Infof("updating logs for step %s", entry)

// send API call to capture the step logs
l, err := database.FromContext(c).GetStepLog(s.GetID())
l, err := database.FromContext(c).GetLogForStep(s)
if err != nil {
retErr := fmt.Errorf("unable to get logs for step %s: %w", entry, err)

Expand Down Expand Up @@ -782,7 +801,7 @@ func UpdateStepLog(c *gin.Context) {
}

// send API call to capture the updated log
l, _ = database.FromContext(c).GetStepLog(s.GetID())
l, _ = database.FromContext(c).GetLogForStep(s)

c.JSON(http.StatusOK, l)
}
Expand Down Expand Up @@ -830,8 +849,6 @@ func UpdateStepLog(c *gin.Context) {

// DeleteStepLog represents the API handler to remove
// the logs for a step from the configured backend.
//
//nolint:dupl // ignore similar code with service
func DeleteStepLog(c *gin.Context) {
// capture middleware values
b := build.Retrieve(c)
Expand All @@ -853,8 +870,18 @@ func DeleteStepLog(c *gin.Context) {
"user": u.GetName(),
}).Infof("deleting logs for step %s", entry)

// send API call to capture the step logs
l, err := database.FromContext(c).GetLogForStep(s)
if err != nil {
retErr := fmt.Errorf("unable to get logs for step %s: %w", entry, err)

util.HandleError(c, http.StatusInternalServerError, retErr)

return
}

// send API call to remove the log
err := database.FromContext(c).DeleteLog(s.GetID())
err = database.FromContext(c).DeleteLog(l)
if err != nil {
retErr := fmt.Errorf("unable to delete logs for step %s: %w", entry, err)

Expand Down
4 changes: 2 additions & 2 deletions api/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func PostServiceStream(c *gin.Context) {
defer close(done)

// send API call to capture the service logs
_log, err := database.FromContext(c).GetServiceLog(s.GetID())
_log, err := database.FromContext(c).GetLogForService(s)
if err != nil {
retErr := fmt.Errorf("unable to get logs for service %s/%d: %w", entry, s.GetNumber(), err)

Expand Down Expand Up @@ -269,7 +269,7 @@ func PostStepStream(c *gin.Context) {
defer close(done)

// send API call to capture the step logs
_log, err := database.FromContext(c).GetStepLog(s.GetID())
_log, err := database.FromContext(c).GetLogForStep(s)
if err != nil {
retErr := fmt.Errorf("unable to get logs for step %s/%d: %w", entry, s.GetNumber(), err)

Expand Down
25 changes: 25 additions & 0 deletions database/log/count.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) 2023 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

package log

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

// CountLogs gets the count of all logs from the database.
func (e *engine) CountLogs() (int64, error) {
e.logger.Tracef("getting count of all logs from the database")

// variable to store query results
var l int64

// send query to the database and store result in variable
err := e.client.
Table(constants.TableLog).
Count(&l).
Error

return l, err
}
27 changes: 27 additions & 0 deletions database/log/count_build.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) 2023 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

package log

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

// CountLogsForBuild gets the count of logs by build ID from the database.
func (e *engine) CountLogsForBuild(b *library.Build) (int64, error) {
e.logger.Tracef("getting count of logs for build %d from the database", b.GetID())

// variable to store query results
var l int64

// send query to the database and store result in variable
err := e.client.
Table(constants.TableLog).
Where("build_id = ?", b.GetID()).
Count(&l).
Error

return l, err
}
99 changes: 99 additions & 0 deletions database/log/count_build_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Copyright (c) 2023 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

package log

import (
"reflect"
"testing"

"github.com/DATA-DOG/go-sqlmock"
)

func TestLog_Engine_CountLogsForBuild(t *testing.T) {
// setup types
_service := testLog()
_service.SetID(1)
_service.SetRepoID(1)
_service.SetBuildID(1)
_service.SetServiceID(1)

_step := testLog()
_step.SetID(2)
_step.SetRepoID(1)
_step.SetBuildID(1)
_step.SetStepID(1)

_build := testBuild()
_build.SetID(1)
_build.SetID(1)
_build.SetRepoID(1)
_build.SetNumber(1)

_postgres, _mock := testPostgres(t)
defer func() { _sql, _ := _postgres.client.DB(); _sql.Close() }()

// create expected result in mock
_rows := sqlmock.NewRows([]string{"count"}).AddRow(2)

// ensure the mock expects the query
_mock.ExpectQuery(`SELECT count(*) FROM "logs" WHERE build_id = $1`).WithArgs(1).WillReturnRows(_rows)

_sqlite := testSqlite(t)
defer func() { _sql, _ := _sqlite.client.DB(); _sql.Close() }()

err := _sqlite.CreateLog(_service)
if err != nil {
t.Errorf("unable to create test service log for sqlite: %v", err)
}

err = _sqlite.CreateLog(_step)
if err != nil {
t.Errorf("unable to create test step log for sqlite: %v", err)
}

// setup tests
tests := []struct {
failure bool
name string
database *engine
want int64
}{
{
failure: false,
name: "postgres",
database: _postgres,
want: 2,
},
{
failure: false,
name: "sqlite3",
database: _sqlite,
want: 2,
},
}

// run tests
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got, err := test.database.CountLogsForBuild(_build)

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

return
}

if err != nil {
t.Errorf("CountLogsForBuild for %s returned err: %v", test.name, err)
}

if !reflect.DeepEqual(got, test.want) {
t.Errorf("CountLogsForBuild for %s is %v, want %v", test.name, got, test.want)
}
})
}
}
Loading