-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstore.go
More file actions
113 lines (103 loc) · 3.29 KB
/
Copy pathstore.go
File metadata and controls
113 lines (103 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package artifact
import (
"context"
"database/sql"
"fmt"
"time"
)
// Artifact represents metadata for a persisted artifact.
type Artifact struct {
ID string
ExecutionID string
StepName string
Name string
URL string
Size int64
CreatedAt time.Time
}
// Store manages artifact metadata in Postgres.
type Store struct {
DB *sql.DB
}
// Create inserts artifact metadata into the database.
func (s *Store) Create(ctx context.Context, a *Artifact) error {
_, err := s.DB.ExecContext(ctx, `
INSERT INTO execution_artifacts (execution_id, step_name, name, url, size)
VALUES ($1, $2, $3, $4, $5)
`, a.ExecutionID, a.StepName, a.Name, a.URL, a.Size)
if err != nil {
return fmt.Errorf("artifact create: %w", err)
}
return nil
}
// GetByName retrieves a single artifact by execution ID and artifact name.
func (s *Store) GetByName(ctx context.Context, executionID, name string) (*Artifact, error) {
var a Artifact
err := s.DB.QueryRowContext(ctx, `
SELECT id, execution_id, step_name, name, url, size, created_at
FROM execution_artifacts
WHERE execution_id = $1 AND name = $2
`, executionID, name).Scan(&a.ID, &a.ExecutionID, &a.StepName, &a.Name, &a.URL, &a.Size, &a.CreatedAt)
if err == sql.ErrNoRows {
return nil, fmt.Errorf("artifact %q not found for execution %s", name, executionID)
}
if err != nil {
return nil, fmt.Errorf("artifact get: %w", err)
}
return &a, nil
}
// ListByExecution returns all artifacts associated with the given execution, ordered by creation time.
func (s *Store) ListByExecution(ctx context.Context, executionID string) ([]Artifact, error) {
rows, err := s.DB.QueryContext(ctx, `
SELECT id, execution_id, step_name, name, url, size, created_at
FROM execution_artifacts
WHERE execution_id = $1
ORDER BY created_at ASC
`, executionID)
if err != nil {
return nil, fmt.Errorf("artifact list: %w", err)
}
defer rows.Close()
var artifacts []Artifact
for rows.Next() {
var a Artifact
if err := rows.Scan(&a.ID, &a.ExecutionID, &a.StepName, &a.Name, &a.URL, &a.Size, &a.CreatedAt); err != nil {
return nil, fmt.Errorf("artifact scan: %w", err)
}
artifacts = append(artifacts, a)
}
return artifacts, rows.Err()
}
// DeleteByExecution removes all artifact metadata for an execution.
func (s *Store) DeleteByExecution(ctx context.Context, executionID string) error {
_, err := s.DB.ExecContext(ctx, `
DELETE FROM execution_artifacts WHERE execution_id = $1
`, executionID)
if err != nil {
return fmt.Errorf("artifact delete: %w", err)
}
return nil
}
// ListExpired returns artifacts older than the given duration.
func (s *Store) ListExpired(ctx context.Context, olderThan time.Duration) ([]Artifact, error) {
cutoff := time.Now().Add(-olderThan)
rows, err := s.DB.QueryContext(ctx, `
SELECT id, execution_id, step_name, name, url, size, created_at
FROM execution_artifacts
WHERE created_at < $1
ORDER BY created_at ASC
`, cutoff)
if err != nil {
return nil, fmt.Errorf("artifact list expired: %w", err)
}
defer rows.Close()
var artifacts []Artifact
for rows.Next() {
var a Artifact
if err := rows.Scan(&a.ID, &a.ExecutionID, &a.StepName, &a.Name, &a.URL, &a.Size, &a.CreatedAt); err != nil {
return nil, fmt.Errorf("artifact scan: %w", err)
}
artifacts = append(artifacts, a)
}
return artifacts, rows.Err()
}