Go · 6132 bytes Raw Blame History
1 // Code generated by sqlc. DO NOT EDIT.
2 // versions:
3 // sqlc v1.31.1
4 // source: workflow_caches.sql
5
6 package actionsdb
7
8 import (
9 "context"
10
11 "github.com/jackc/pgx/v5/pgtype"
12 )
13
14 const countWorkflowCachesForRepo = `-- name: CountWorkflowCachesForRepo :one
15 SELECT COUNT(*) FROM workflow_caches
16 WHERE repo_id = $1
17 AND ($2::text IS NULL OR git_ref = $2::text)
18 AND ($3::text IS NULL OR cache_key = $3::text)
19 `
20
21 type CountWorkflowCachesForRepoParams struct {
22 RepoID int64
23 GitRef pgtype.Text
24 CacheKey pgtype.Text
25 }
26
27 func (q *Queries) CountWorkflowCachesForRepo(ctx context.Context, db DBTX, arg CountWorkflowCachesForRepoParams) (int64, error) {
28 row := db.QueryRow(ctx, countWorkflowCachesForRepo, arg.RepoID, arg.GitRef, arg.CacheKey)
29 var count int64
30 err := row.Scan(&count)
31 return count, err
32 }
33
34 const deleteWorkflowCacheByID = `-- name: DeleteWorkflowCacheByID :execrows
35 DELETE FROM workflow_caches WHERE id = $1 AND repo_id = $2
36 `
37
38 type DeleteWorkflowCacheByIDParams struct {
39 ID int64
40 RepoID int64
41 }
42
43 func (q *Queries) DeleteWorkflowCacheByID(ctx context.Context, db DBTX, arg DeleteWorkflowCacheByIDParams) (int64, error) {
44 result, err := db.Exec(ctx, deleteWorkflowCacheByID, arg.ID, arg.RepoID)
45 if err != nil {
46 return 0, err
47 }
48 return result.RowsAffected(), nil
49 }
50
51 const deleteWorkflowCachesByKey = `-- name: DeleteWorkflowCachesByKey :many
52 WITH deleted AS (
53 DELETE FROM workflow_caches
54 WHERE repo_id = $1
55 AND cache_key = $2
56 AND ($3::text IS NULL OR git_ref = $3::text)
57 RETURNING object_key
58 )
59 SELECT object_key FROM deleted
60 `
61
62 type DeleteWorkflowCachesByKeyParams struct {
63 RepoID int64
64 CacheKey string
65 GitRef pgtype.Text
66 }
67
68 // Returns the deleted rows' object_keys so the handler can purge the
69 // backing tarballs from object storage.
70 func (q *Queries) DeleteWorkflowCachesByKey(ctx context.Context, db DBTX, arg DeleteWorkflowCachesByKeyParams) ([]string, error) {
71 rows, err := db.Query(ctx, deleteWorkflowCachesByKey, arg.RepoID, arg.CacheKey, arg.GitRef)
72 if err != nil {
73 return nil, err
74 }
75 defer rows.Close()
76 items := []string{}
77 for rows.Next() {
78 var object_key string
79 if err := rows.Scan(&object_key); err != nil {
80 return nil, err
81 }
82 items = append(items, object_key)
83 }
84 if err := rows.Err(); err != nil {
85 return nil, err
86 }
87 return items, nil
88 }
89
90 const getWorkflowCacheByID = `-- name: GetWorkflowCacheByID :one
91 SELECT id, repo_id, cache_key, cache_version, git_ref, object_key,
92 size_bytes, last_accessed_at, created_at
93 FROM workflow_caches
94 WHERE id = $1
95 `
96
97 func (q *Queries) GetWorkflowCacheByID(ctx context.Context, db DBTX, id int64) (WorkflowCache, error) {
98 row := db.QueryRow(ctx, getWorkflowCacheByID, id)
99 var i WorkflowCache
100 err := row.Scan(
101 &i.ID,
102 &i.RepoID,
103 &i.CacheKey,
104 &i.CacheVersion,
105 &i.GitRef,
106 &i.ObjectKey,
107 &i.SizeBytes,
108 &i.LastAccessedAt,
109 &i.CreatedAt,
110 )
111 return i, err
112 }
113
114 const insertWorkflowCache = `-- name: InsertWorkflowCache :one
115
116 INSERT INTO workflow_caches (
117 repo_id, cache_key, cache_version, git_ref, object_key, size_bytes
118 ) VALUES ($1, $2, $3, $4, $5, $6)
119 ON CONFLICT (repo_id, cache_key, cache_version, git_ref) DO UPDATE
120 SET object_key = EXCLUDED.object_key,
121 size_bytes = EXCLUDED.size_bytes,
122 last_accessed_at = now()
123 RETURNING id, repo_id, cache_key, cache_version, git_ref, object_key,
124 size_bytes, last_accessed_at, created_at
125 `
126
127 type InsertWorkflowCacheParams struct {
128 RepoID int64
129 CacheKey string
130 CacheVersion string
131 GitRef string
132 ObjectKey string
133 SizeBytes int64
134 }
135
136 // SPDX-License-Identifier: AGPL-3.0-or-later
137 // Called by the future runner-side upload handler when an
138 // actions/cache step completes its tarball upload. Idempotent on
139 // (repo_id, cache_key, cache_version, git_ref): a re-upload with the
140 // same coordinates updates size + last_accessed_at + object_key.
141 func (q *Queries) InsertWorkflowCache(ctx context.Context, db DBTX, arg InsertWorkflowCacheParams) (WorkflowCache, error) {
142 row := db.QueryRow(ctx, insertWorkflowCache,
143 arg.RepoID,
144 arg.CacheKey,
145 arg.CacheVersion,
146 arg.GitRef,
147 arg.ObjectKey,
148 arg.SizeBytes,
149 )
150 var i WorkflowCache
151 err := row.Scan(
152 &i.ID,
153 &i.RepoID,
154 &i.CacheKey,
155 &i.CacheVersion,
156 &i.GitRef,
157 &i.ObjectKey,
158 &i.SizeBytes,
159 &i.LastAccessedAt,
160 &i.CreatedAt,
161 )
162 return i, err
163 }
164
165 const listWorkflowCachesForRepo = `-- name: ListWorkflowCachesForRepo :many
166 SELECT id, repo_id, cache_key, cache_version, git_ref, object_key,
167 size_bytes, last_accessed_at, created_at
168 FROM workflow_caches
169 WHERE repo_id = $1
170 AND ($4::text IS NULL OR git_ref = $4::text)
171 AND ($5::text IS NULL OR cache_key = $5::text)
172 ORDER BY last_accessed_at DESC, id DESC
173 LIMIT $2 OFFSET $3
174 `
175
176 type ListWorkflowCachesForRepoParams struct {
177 RepoID int64
178 Limit int32
179 Offset int32
180 GitRef pgtype.Text
181 CacheKey pgtype.Text
182 }
183
184 // Paginated list filtered optionally by ref + key. NULL params skip
185 // the filter. Sorted by last_accessed_at DESC so an operator sees the
186 // live caches first.
187 func (q *Queries) ListWorkflowCachesForRepo(ctx context.Context, db DBTX, arg ListWorkflowCachesForRepoParams) ([]WorkflowCache, error) {
188 rows, err := db.Query(ctx, listWorkflowCachesForRepo,
189 arg.RepoID,
190 arg.Limit,
191 arg.Offset,
192 arg.GitRef,
193 arg.CacheKey,
194 )
195 if err != nil {
196 return nil, err
197 }
198 defer rows.Close()
199 items := []WorkflowCache{}
200 for rows.Next() {
201 var i WorkflowCache
202 if err := rows.Scan(
203 &i.ID,
204 &i.RepoID,
205 &i.CacheKey,
206 &i.CacheVersion,
207 &i.GitRef,
208 &i.ObjectKey,
209 &i.SizeBytes,
210 &i.LastAccessedAt,
211 &i.CreatedAt,
212 ); err != nil {
213 return nil, err
214 }
215 items = append(items, i)
216 }
217 if err := rows.Err(); err != nil {
218 return nil, err
219 }
220 return items, nil
221 }
222
223 const touchWorkflowCache = `-- name: TouchWorkflowCache :exec
224 UPDATE workflow_caches
225 SET last_accessed_at = now()
226 WHERE id = $1
227 `
228
229 // Bumps last_accessed_at on cache hit. Called by the future
230 // restore-side handler.
231 func (q *Queries) TouchWorkflowCache(ctx context.Context, db DBTX, id int64) error {
232 _, err := db.Exec(ctx, touchWorkflowCache, id)
233 return err
234 }
235