Skip to content

Commit

Permalink
report number of rows affected
Browse files Browse the repository at this point in the history
  • Loading branch information
ecrupper committed Oct 17, 2023
1 parent 255b90e commit 9c5af21
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 10 deletions.
8 changes: 4 additions & 4 deletions api/admin/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func CleanResources(c *gin.Context) {
logrus.Infof("platform admin %s: cleaned %d builds in database", u.GetName(), builds)

// clean executables
err = database.FromContext(c).CleanBuildExecutables(ctx)
executables, err := database.FromContext(c).CleanBuildExecutables(ctx)
if err != nil {
retErr := fmt.Errorf("%d builds cleaned. unable to clean build executables: %w", builds, err)

Expand All @@ -121,7 +121,7 @@ func CleanResources(c *gin.Context) {
// clean services
services, err := database.FromContext(c).CleanServices(ctx, msg, before)
if err != nil {
retErr := fmt.Errorf("%d builds cleaned. unable to update services: %w", builds, err)
retErr := fmt.Errorf("%d builds cleaned. %d executables cleaned. unable to update services: %w", builds, executables, err)

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

Expand All @@ -133,7 +133,7 @@ func CleanResources(c *gin.Context) {
// clean steps
steps, err := database.FromContext(c).CleanSteps(msg, before)
if err != nil {
retErr := fmt.Errorf("%d builds cleaned. %d services cleaned. unable to update steps: %w", builds, services, err)
retErr := fmt.Errorf("%d builds cleaned. %d executables cleaned. %d services cleaned. unable to update steps: %w", builds, executables, services, err)

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

Expand All @@ -142,5 +142,5 @@ func CleanResources(c *gin.Context) {

logrus.Infof("platform admin %s: cleaned %d steps in database", u.GetName(), steps)

c.JSON(http.StatusOK, fmt.Sprintf("%d builds cleaned. %d services cleaned. %d steps cleaned.", builds, services, steps))
c.JSON(http.StatusOK, fmt.Sprintf("%d builds cleaned. %d executables cleaned. %d services cleaned. %d steps cleaned.", builds, executables, services, steps))
}
9 changes: 5 additions & 4 deletions database/executable/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@ const CleanExecutablesSqlite = `
`

// CleanBuildExecutables pops executables which have a corresponding build that was cleaned.
func (e *engine) CleanBuildExecutables(ctx context.Context) error {
func (e *engine) CleanBuildExecutables(ctx context.Context) (int64, error) {
logrus.Trace("clearing build executables in the database")

switch e.config.Driver {
case constants.DriverPostgres:
return e.client.Exec(CleanExecutablesPostgres).Error

res := e.client.Exec(CleanExecutablesPostgres)
return res.RowsAffected, res.Error
default:
return e.client.Exec(CleanExecutablesSqlite).Error
res := e.client.Exec(CleanExecutablesSqlite)
return res.RowsAffected, res.Error
}
}
6 changes: 5 additions & 1 deletion database/executable/clean_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func TestExecutable_Engine_CleanExecutables(t *testing.T) {
// run tests
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
err := test.database.CleanBuildExecutables(context.TODO())
got, err := test.database.CleanBuildExecutables(context.TODO())

if test.failure {
if err == nil {
Expand All @@ -112,6 +112,10 @@ func TestExecutable_Engine_CleanExecutables(t *testing.T) {
t.Errorf("CleanExecutables for %s returned err: %v", test.name, err)
}

if got != 1 {
t.Errorf("CleanExecutables for %s should have affected 1 row, affected %d", test.name, got)
}

_, err = test.database.PopBuildExecutable(context.TODO(), 2)
if err == nil {
t.Errorf("CleanExecutables for %s should have returned an error", test.name)
Expand Down
2 changes: 1 addition & 1 deletion database/executable/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type BuildExecutableInterface interface {
// https://en.wikipedia.org/wiki/Data_manipulation_language

// CleanBuildExecutables defines a function that deletes errored builds' corresponding executables.
CleanBuildExecutables(context.Context) error
CleanBuildExecutables(context.Context) (int64, error)
// CreateBuildExecutable defines a function that creates a build executable.
CreateBuildExecutable(context.Context, *library.BuildExecutable) error
// PopBuildExecutable defines a function that gets and deletes a build executable.
Expand Down

0 comments on commit 9c5af21

Please sign in to comment.