tenseleyflow/shithub / 0264232

Browse files

actions/sqlc: queries for run delete + artifact object keys + artifact delete

Authored by mfwolffe <wolffemf@dukes.jmu.edu>
SHA
02642329902d4e02b2007d87360c5531ce51b8af
Parents
aafdb74
Tree
f47aa6d

5 changed files

StatusFile+-
M internal/actions/queries/workflow_artifacts.sql 11 0
M internal/actions/queries/workflow_runs.sql 8 0
M internal/actions/sqlc/querier.go 11 0
M internal/actions/sqlc/workflow_artifacts.sql.go 41 0
M internal/actions/sqlc/workflow_runs.sql.go 17 0
internal/actions/queries/workflow_artifacts.sqlmodified
@@ -27,3 +27,14 @@ LIMIT $2;
2727
 -- name: DeleteWorkflowArtifactsByIDs :execrows
2828
 DELETE FROM workflow_artifacts
2929
 WHERE id = ANY($1::bigint[]);
30
+
31
+-- name: ListArtifactObjectKeysForRun :many
32
+-- Used by the run-delete REST handler to drive a best-effort S3
33
+-- cleanup after the workflow_runs row (and its cascaded
34
+-- workflow_artifacts rows) have been removed.
35
+SELECT object_key
36
+FROM workflow_artifacts
37
+WHERE run_id = $1;
38
+
39
+-- name: DeleteWorkflowArtifactByID :execrows
40
+DELETE FROM workflow_artifacts WHERE id = $1;
internal/actions/queries/workflow_runs.sqlmodified
@@ -53,6 +53,14 @@ SELECT id, repo_id, run_index, workflow_file, workflow_name,
5353
 FROM workflow_runs
5454
 WHERE repo_id = $1 AND workflow_file = $2 AND trigger_event_id = $3;
5555
 
56
+-- name: DeleteWorkflowRunByID :execrows
57
+-- Cascades to workflow_jobs → workflow_steps → workflow_step_log_chunks
58
+-- and workflow_artifacts via the on-delete-cascade FK chain. The
59
+-- S3-side artifact blobs are NOT removed here; the handler queries
60
+-- the artifact object_keys first and deletes them best-effort after
61
+-- the row is gone.
62
+DELETE FROM workflow_runs WHERE id = $1;
63
+
5664
 -- name: GetWorkflowRunByID :one
5765
 SELECT id, repo_id, run_index, workflow_file, workflow_name,
5866
        head_sha, head_ref, event, event_payload,
internal/actions/sqlc/querier.gomodified
@@ -26,7 +26,14 @@ type Querier interface {
2626
 	DeleteRepoVariable(ctx context.Context, db DBTX, arg DeleteRepoVariableParams) error
2727
 	DeleteStaleStepLogChunksForCleanup(ctx context.Context, db DBTX, completedAt pgtype.Timestamptz) (int64, error)
2828
 	DeleteStepLogChunks(ctx context.Context, db DBTX, stepID int64) error
29
+	DeleteWorkflowArtifactByID(ctx context.Context, db DBTX, id int64) (int64, error)
2930
 	DeleteWorkflowArtifactsByIDs(ctx context.Context, db DBTX, dollar_1 []int64) (int64, error)
31
+	// Cascades to workflow_jobs → workflow_steps → workflow_step_log_chunks
32
+	// and workflow_artifacts via the on-delete-cascade FK chain. The
33
+	// S3-side artifact blobs are NOT removed here; the handler queries
34
+	// the artifact object_keys first and deletes them best-effort after
35
+	// the row is gone.
36
+	DeleteWorkflowRunByID(ctx context.Context, db DBTX, id int64) (int64, error)
3037
 	// Idempotent: re-disabling an already-disabled workflow is a no-op
3138
 	// and does not bump disabled_at.
3239
 	DisableWorkflow(ctx context.Context, db DBTX, arg DisableWorkflowParams) error
@@ -76,6 +83,10 @@ type Querier interface {
7683
 	IsWorkflowDisabled(ctx context.Context, db DBTX, arg IsWorkflowDisabledParams) (bool, error)
7784
 	ListActiveWorkflowRunsForAdmin(ctx context.Context, db DBTX, arg ListActiveWorkflowRunsForAdminParams) ([]WorkflowRun, error)
7885
 	ListAllStepLogChunksForStep(ctx context.Context, db DBTX, stepID int64) ([]WorkflowStepLogChunk, error)
86
+	// Used by the run-delete REST handler to drive a best-effort S3
87
+	// cleanup after the workflow_runs row (and its cascaded
88
+	// workflow_artifacts rows) have been removed.
89
+	ListArtifactObjectKeysForRun(ctx context.Context, db DBTX, runID int64) ([]string, error)
7990
 	ListArtifactsForRun(ctx context.Context, db DBTX, runID int64) ([]ListArtifactsForRunRow, error)
8091
 	// Older queued/running runs with the same group block the new run while they
8192
 	// still have at least one queued/running job that has not already received a
internal/actions/sqlc/workflow_artifacts.sql.gomodified
@@ -11,6 +11,18 @@ import (
1111
 	"github.com/jackc/pgx/v5/pgtype"
1212
 )
1313
 
14
+const deleteWorkflowArtifactByID = `-- name: DeleteWorkflowArtifactByID :execrows
15
+DELETE FROM workflow_artifacts WHERE id = $1
16
+`
17
+
18
+func (q *Queries) DeleteWorkflowArtifactByID(ctx context.Context, db DBTX, id int64) (int64, error) {
19
+	result, err := db.Exec(ctx, deleteWorkflowArtifactByID, id)
20
+	if err != nil {
21
+		return 0, err
22
+	}
23
+	return result.RowsAffected(), nil
24
+}
25
+
1426
 const deleteWorkflowArtifactsByIDs = `-- name: DeleteWorkflowArtifactsByIDs :execrows
1527
 DELETE FROM workflow_artifacts
1628
 WHERE id = ANY($1::bigint[])
@@ -82,6 +94,35 @@ func (q *Queries) InsertArtifact(ctx context.Context, db DBTX, arg InsertArtifac
8294
 	return i, err
8395
 }
8496
 
97
+const listArtifactObjectKeysForRun = `-- name: ListArtifactObjectKeysForRun :many
98
+SELECT object_key
99
+FROM workflow_artifacts
100
+WHERE run_id = $1
101
+`
102
+
103
+// Used by the run-delete REST handler to drive a best-effort S3
104
+// cleanup after the workflow_runs row (and its cascaded
105
+// workflow_artifacts rows) have been removed.
106
+func (q *Queries) ListArtifactObjectKeysForRun(ctx context.Context, db DBTX, runID int64) ([]string, error) {
107
+	rows, err := db.Query(ctx, listArtifactObjectKeysForRun, runID)
108
+	if err != nil {
109
+		return nil, err
110
+	}
111
+	defer rows.Close()
112
+	items := []string{}
113
+	for rows.Next() {
114
+		var object_key string
115
+		if err := rows.Scan(&object_key); err != nil {
116
+			return nil, err
117
+		}
118
+		items = append(items, object_key)
119
+	}
120
+	if err := rows.Err(); err != nil {
121
+		return nil, err
122
+	}
123
+	return items, nil
124
+}
125
+
85126
 const listArtifactsForRun = `-- name: ListArtifactsForRun :many
86127
 SELECT id, name, object_key, byte_count, expires_at, created_at
87128
 FROM workflow_artifacts
internal/actions/sqlc/workflow_runs.sql.gomodified
@@ -117,6 +117,23 @@ func (q *Queries) DeleteOldWorkflowRunsForCleanup(ctx context.Context, db DBTX,
117117
 	return result.RowsAffected(), nil
118118
 }
119119
 
120
+const deleteWorkflowRunByID = `-- name: DeleteWorkflowRunByID :execrows
121
+DELETE FROM workflow_runs WHERE id = $1
122
+`
123
+
124
+// Cascades to workflow_jobs → workflow_steps → workflow_step_log_chunks
125
+// and workflow_artifacts via the on-delete-cascade FK chain. The
126
+// S3-side artifact blobs are NOT removed here; the handler queries
127
+// the artifact object_keys first and deletes them best-effort after
128
+// the row is gone.
129
+func (q *Queries) DeleteWorkflowRunByID(ctx context.Context, db DBTX, id int64) (int64, error) {
130
+	result, err := db.Exec(ctx, deleteWorkflowRunByID, id)
131
+	if err != nil {
132
+		return 0, err
133
+	}
134
+	return result.RowsAffected(), nil
135
+}
136
+
120137
 const enqueueWorkflowRun = `-- name: EnqueueWorkflowRun :one
121138
 INSERT INTO workflow_runs (
122139
     repo_id, run_index, workflow_file, workflow_name,