Files
pipelines/internal/pipeline/archive.go
T
alan 06bf580154 Archive pipelines: hide unused ones while keeping their history
archived_at on pipelines (migrated in place). archive_pipeline and
unarchive_pipeline by id or name with an expected-version guard;
list_pipelines hides archived unless include_archived. Archived
pipelines refuse runs, retries, new versions and name reuse, and
cannot be archived while an enabled schedule points at them.
CLI archive/unarchive and list --archived/--all, docs and skill note.
2026-11-04 23:25:55 +00:00

141 lines
4.4 KiB
Go

package pipeline
import (
"context"
"database/sql"
"fmt"
"strconv"
"strings"
"time"
)
// Archiving retires a pipeline from everyday lists without deleting anything:
// its versions, runs, artifacts and search history stay as they are, and its
// name stays reserved. Only the pipelines row carries the archived_at stamp.
func (s *Store) initArchive() error {
return s.addColumnIfMissing("pipelines", "archived_at", "TEXT")
}
// ArchivedError reports that a name or ID belongs to an archived pipeline.
type ArchivedError struct{ Name string }
func (e *ArchivedError) Error() string {
return fmt.Sprintf("pipeline %q is archived; unarchive it first (pipelines unarchive %s)", e.Name, quoteArg(e.Name))
}
func quoteArg(name string) string {
if strings.ContainsAny(name, " \t'\"\\$`") {
return "'" + strings.ReplaceAll(name, "'", `'\''`) + "'"
}
return name
}
// ResolvePipelineID finds a pipeline by exact name first, then by numeric ID,
// archived or not, matching the CLI's name-over-ID rule.
func (s *Store) ResolvePipelineID(ctx context.Context, id int64, name string) (int64, error) {
name = strings.TrimSpace(name)
if name != "" {
var found int64
err := s.db.QueryRowContext(ctx, `SELECT id FROM pipelines WHERE name=?`, name).Scan(&found)
if err == nil {
return found, nil
}
if err != sql.ErrNoRows {
return 0, err
}
if id == 0 {
id, _ = strconv.ParseInt(name, 10, 64)
}
if id <= 0 {
return 0, fmt.Errorf("pipeline %q not found", name)
}
}
if id <= 0 {
return 0, fmt.Errorf("a pipeline name or ID is required")
}
var found int64
err := s.db.QueryRowContext(ctx, `SELECT id FROM pipelines WHERE id=?`, id).Scan(&found)
if err == sql.ErrNoRows {
return 0, fmt.Errorf("pipeline not found")
}
return found, err
}
// SetArchived archives or unarchives one pipeline. A positive expectedVersion
// refuses the change when a newer version was published meanwhile. Archiving
// is refused while an enabled schedule still points at the pipeline, naming
// the schedules, so nothing silently stops firing; repeating a request that is
// already in effect is a no-op.
func (s *Store) SetArchived(ctx context.Context, id int64, archived bool, expectedVersion int, now time.Time) error {
if expectedVersion < 0 {
return fmt.Errorf("expected version must be nonnegative")
}
s.mu.Lock()
defer s.mu.Unlock()
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return err
}
defer tx.Rollback()
var name string
var stamp sql.NullString
var latest int
err = tx.QueryRowContext(ctx, `SELECT p.name,p.archived_at,COALESCE((SELECT MAX(version) FROM pipeline_versions WHERE pipeline_id=p.id),0) FROM pipelines p WHERE p.id=?`, id).Scan(&name, &stamp, &latest)
if err == sql.ErrNoRows {
return fmt.Errorf("pipeline not found")
}
if err != nil {
return err
}
if expectedVersion > 0 && latest != expectedVersion {
return fmt.Errorf("pipeline %q changed: expected latest version %d, found %d; reload first", name, expectedVersion, latest)
}
if stamp.Valid == archived {
return tx.Commit()
}
if !archived {
if _, err = tx.ExecContext(ctx, `UPDATE pipelines SET archived_at=NULL WHERE id=?`, id); err != nil {
return err
}
return tx.Commit()
}
rows, err := tx.QueryContext(ctx, savedScheduleSelect+` WHERE pipeline_id=? AND deleted=0 AND enabled=1 ORDER BY id`, id)
if err != nil {
return err
}
var enabled []string
for rows.Next() {
item, err := scanSavedSchedule(rows)
if err != nil {
rows.Close()
return err
}
enabled = append(enabled, fmt.Sprintf("%q (#%d)", item.Name, item.ID))
}
err = rows.Err()
rows.Close()
if err != nil {
return err
}
if len(enabled) > 0 {
return fmt.Errorf("pipeline %q has enabled schedule(s) %s; pause or repoint them before archiving", name, strings.Join(enabled, ", "))
}
if _, err = tx.ExecContext(ctx, `UPDATE pipelines SET archived_at=? WHERE id=?`, now.UTC().Format(time.RFC3339), id); err != nil {
return err
}
return tx.Commit()
}
// archivedName reports whether the pipeline row is archived inside a
// transaction, so a submission or schedule edit cannot race an archive.
func archivedName(ctx context.Context, tx *sql.Tx, query string, arg any) (string, bool, error) {
var name string
var stamp sql.NullString
err := tx.QueryRowContext(ctx, `SELECT name,archived_at FROM pipelines WHERE `+query, arg).Scan(&name, &stamp)
if err == sql.ErrNoRows {
return "", false, nil
}
return name, stamp.Valid, err
}