From 1aa85a8ad1d9146cd8bb4cc29be1c81721a1592f Mon Sep 17 00:00:00 2001 From: Benny Siegert Date: Thu, 22 Aug 2024 15:08:21 +0200 Subject: [PATCH] Add a handler for deleting a build from the DB. Careful! For #40. --- ddao/manual_additions.go | 20 ++++++++++++++++++++ ddao/queries.sql.go | 10 ++++++++++ pages/bulktracker.go | 6 +++++- queries.sql | 4 ++++ 4 files changed, 39 insertions(+), 1 deletion(-) diff --git a/ddao/manual_additions.go b/ddao/manual_additions.go index 4b645d8..0f1bdc4 100644 --- a/ddao/manual_additions.go +++ b/ddao/manual_additions.go @@ -95,6 +95,26 @@ func (d *DB) BeginReadOnlyTransaction(ctx context.Context) (*DB, func(), error) }, func() { tx.Rollback() }, nil } +// DeleteBuild deletes an entire build entry, the metadata and the detailed results. +func (d *DB) DeleteBuild(ctx context.Context, buildID int64) error { + tx, err := d.BeginTransaction(ctx, nil) + if err != nil { + return err + } + defer tx.Rollback() + + q := d.WithTx(tx) + err = q.DeleteAllForBuild(ctx, NullInt64(buildID)) + if err != nil { + return err + } + err = q.deleteBuild(ctx, buildID) + if err != nil { + return err + } + return tx.Commit() +} + // PutResults writes the results for the given build ID to the database. func (d *DB) PutResults(ctx context.Context, results []PkgResult, buildID int64) error { tx, err := d.BeginTransaction(ctx, nil) diff --git a/ddao/queries.sql.go b/ddao/queries.sql.go index e3e424a..3507ccb 100644 --- a/ddao/queries.sql.go +++ b/ddao/queries.sql.go @@ -609,6 +609,16 @@ func (q *Queries) SetBuildLastError(ctx context.Context, arg SetBuildLastErrorPa return err } +const deleteBuild = `-- name: deleteBuild :exec +DELETE from builds +WHERE build_id = ? +` + +func (q *Queries) deleteBuild(ctx context.Context, buildID int64) error { + _, err := q.db.ExecContext(ctx, deleteBuild, buildID) + return err +} + const getAllPkgsMatching = `-- name: getAllPkgsMatching :many SELECT pkgpath FROM pkgpaths diff --git a/pages/bulktracker.go b/pages/bulktracker.go index e9c3264..154901f 100644 --- a/pages/bulktracker.go +++ b/pages/bulktracker.go @@ -132,7 +132,11 @@ func (b *BuildDetails) ServeHTTP(w http.ResponseWriter, r *http.Request) { templates.ReindexOK(w) return case "delete": - // delete.DeleteBuildDetails.Call(ctx, key) + err = b.DB.DeleteBuild(ctx, buildID) + if err != nil { + log.Errorf(ctx, "DeleteBuild: %v", err) + } + return } if len(paths) > 1 { diff --git a/queries.sql b/queries.sql index e5e874c..30b2369 100644 --- a/queries.sql +++ b/queries.sql @@ -2,6 +2,10 @@ DELETE from results WHERE build_id = ?; +-- name: deleteBuild :exec +DELETE from builds +WHERE build_id = ?; + -- name: GetBuild :one SELECT * FROM builds WHERE build_id = ?;