From a02f7548b08561e4a9f4a1ddec629a90092c618b Mon Sep 17 00:00:00 2001 From: Michelangelo Mori Date: Mon, 6 Jan 2025 11:09:19 +0100 Subject: [PATCH] Fix type hint in delete history by ids statement. (#5249) The missing `::uuid[]` caused SQLC to generate code splicing the slice into the statement rather than passing it as-is, i.e. generating `ANY(?, ?, ?, ?, ...)` rather than `ANY({ ... })`, causing the statement compilation to fail. --- cmd/server/app/history_purge.go | 10 +++++----- cmd/server/app/history_purge_test.go | 2 +- database/query/eval_history.sql | 2 +- internal/db/eval_history.sql.go | 15 ++------------- 4 files changed, 9 insertions(+), 20 deletions(-) diff --git a/cmd/server/app/history_purge.go b/cmd/server/app/history_purge.go index ff177c76f0..53e4744492 100644 --- a/cmd/server/app/history_purge.go +++ b/cmd/server/app/history_purge.go @@ -11,6 +11,7 @@ import ( _ "github.com/golang-migrate/migrate/v4/database/postgres" // nolint _ "github.com/golang-migrate/migrate/v4/source/file" // nolint "github.com/google/uuid" + "github.com/rs/zerolog" "github.com/spf13/cobra" "github.com/spf13/viper" @@ -53,9 +54,9 @@ func historyPurgeCommand(cmd *cobra.Command, _ []string) error { // We maintain up to 30 days of history, plus any record // that's the latest for any entity/rule pair. threshold := time.Now().UTC().AddDate(0, 0, -30) - cmd.Printf("Calculated threshold is %s", threshold) + zerolog.Ctx(ctx).Info().Msgf("Calculated threshold is %s", threshold) - if err := purgeLoop(ctx, store, threshold, batchSize, dryRun, cmd.Printf); err != nil { + if err := purgeLoop(ctx, store, threshold, batchSize, dryRun); err != nil { cliErrorf(cmd, "failed purging evaluation log: %s", err) } @@ -88,7 +89,6 @@ func purgeLoop( threshold time.Time, batchSize uint, dryRun bool, - printf func(format string, a ...any), ) error { deleted := 0 @@ -107,7 +107,7 @@ func purgeLoop( } if len(records) == 0 { - printf("No records to delete\n") + zerolog.Ctx(ctx).Info().Msg("No records to delete\n") return nil } @@ -124,7 +124,7 @@ func purgeLoop( } } - printf("Done purging history, deleted %d records\n", + zerolog.Ctx(ctx).Info().Msgf("Done purging history, deleted %d records", deleted, ) diff --git a/cmd/server/app/history_purge_test.go b/cmd/server/app/history_purge_test.go index 903be2fc7a..909f05cde3 100644 --- a/cmd/server/app/history_purge_test.go +++ b/cmd/server/app/history_purge_test.go @@ -220,7 +220,7 @@ func TestPurgeLoop(t *testing.T) { store = tt.dbSetup(ctrl) } - err := purgeLoop(ctx, store, tt.threshold, tt.size, tt.dryRun, t.Logf) + err := purgeLoop(ctx, store, tt.threshold, tt.size, tt.dryRun) if tt.err { require.Error(t, err) return diff --git a/database/query/eval_history.sql b/database/query/eval_history.sql index 25637b3f65..6b8a889eab 100644 --- a/database/query/eval_history.sql +++ b/database/query/eval_history.sql @@ -193,4 +193,4 @@ SELECT s.evaluation_time, -- name: DeleteEvaluationHistoryByIDs :execrows DELETE FROM evaluation_statuses s - WHERE s.id = ANY(sqlc.slice(evaluationIds)); + WHERE s.id = ANY(sqlc.slice(evaluationIds)::uuid[]); diff --git a/internal/db/eval_history.sql.go b/internal/db/eval_history.sql.go index 9291eb6784..1733d67698 100644 --- a/internal/db/eval_history.sql.go +++ b/internal/db/eval_history.sql.go @@ -9,7 +9,6 @@ import ( "context" "database/sql" "encoding/json" - "strings" "time" "github.com/google/uuid" @@ -18,21 +17,11 @@ import ( const deleteEvaluationHistoryByIDs = `-- name: DeleteEvaluationHistoryByIDs :execrows DELETE FROM evaluation_statuses s - WHERE s.id = ANY($1) + WHERE s.id = ANY($1::uuid[]) ` func (q *Queries) DeleteEvaluationHistoryByIDs(ctx context.Context, evaluationids []uuid.UUID) (int64, error) { - query := deleteEvaluationHistoryByIDs - var queryParams []interface{} - if len(evaluationids) > 0 { - for _, v := range evaluationids { - queryParams = append(queryParams, v) - } - query = strings.Replace(query, "/*SLICE:evaluationids*/?", strings.Repeat(",?", len(evaluationids))[1:], 1) - } else { - query = strings.Replace(query, "/*SLICE:evaluationids*/?", "NULL", 1) - } - result, err := q.db.ExecContext(ctx, query, queryParams...) + result, err := q.db.ExecContext(ctx, deleteEvaluationHistoryByIDs, pq.Array(evaluationids)) if err != nil { return 0, err }