Merge: archive and unarchive pipelines
# Conflicts: # internal/cli/commands.go # internal/pipeline/store.go
This commit is contained in:
@@ -61,6 +61,10 @@ CREATE TABLE IF NOT EXISTS submissions (id INTEGER PRIMARY KEY, name TEXT NOT NU
|
||||
return nil, err
|
||||
}
|
||||
s := &Store{db: db, dir: filepath.Dir(path), path: path, done: make(chan struct{}), created: existing == 0}
|
||||
if err := s.initArchive(); err != nil {
|
||||
db.Close()
|
||||
return nil, err
|
||||
}
|
||||
if err := s.initRuns(); err != nil {
|
||||
db.Close()
|
||||
return nil, err
|
||||
@@ -173,8 +177,15 @@ func OpenMaintenance(path string) (*Store, error) {
|
||||
// adjacent server state such as stage artifacts.
|
||||
func (s *Store) Directory() string { return s.dir }
|
||||
|
||||
// List returns the active (unarchived) pipelines.
|
||||
func (s *Store) List(ctx context.Context) ([]protocol.PipelineSummary, error) {
|
||||
rows, err := s.db.QueryContext(ctx, `SELECT p.id, p.name, v.version,
|
||||
return s.ListPipelines(ctx, false)
|
||||
}
|
||||
|
||||
// ListPipelines returns the latest version of every pipeline; archived ones
|
||||
// are included, marked by ArchivedAt, only when includeArchived is set.
|
||||
func (s *Store) ListPipelines(ctx context.Context, includeArchived bool) ([]protocol.PipelineSummary, error) {
|
||||
rows, err := s.db.QueryContext(ctx, `SELECT p.id, p.name, v.version, COALESCE(p.archived_at, ''),
|
||||
json_array_length(v.graph_json, '$.stages'),
|
||||
strftime('%Y-%m-%dT%H:%M:%SZ', p.created_at), strftime('%Y-%m-%dT%H:%M:%SZ', v.created_at),
|
||||
v.graph_json,
|
||||
@@ -182,7 +193,8 @@ v.graph_json,
|
||||
(SELECT COUNT(*) FROM saved_schedules WHERE pipeline_id=p.id AND deleted=0 AND enabled=1)
|
||||
FROM pipelines p JOIN pipeline_versions v ON v.pipeline_id=p.id
|
||||
WHERE v.version=(SELECT MAX(version) FROM pipeline_versions WHERE pipeline_id=p.id)
|
||||
ORDER BY p.name, p.id`)
|
||||
AND (? OR p.archived_at IS NULL)
|
||||
ORDER BY p.name, p.id`, includeArchived)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -191,7 +203,7 @@ ORDER BY p.name, p.id`)
|
||||
for rows.Next() {
|
||||
var item protocol.PipelineSummary
|
||||
var graphJSON string
|
||||
if err := rows.Scan(&item.ID, &item.Name, &item.Version, &item.Stages, &item.CreatedAt, &item.UpdatedAt, &graphJSON, &item.ScheduleCount, &item.EnabledScheduleCount); err != nil {
|
||||
if err := rows.Scan(&item.ID, &item.Name, &item.Version, &item.ArchivedAt, &item.Stages, &item.CreatedAt, &item.UpdatedAt, &graphJSON, &item.ScheduleCount, &item.EnabledScheduleCount); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var shape protocol.Graph
|
||||
@@ -231,10 +243,10 @@ func (s *Store) Get(ctx context.Context, id int64, version int) (*protocol.Pipel
|
||||
defer tx.Rollback()
|
||||
detail := &protocol.PipelineDetail{}
|
||||
err = tx.QueryRowContext(ctx, `SELECT p.id,p.name,strftime('%Y-%m-%dT%H:%M:%SZ',p.created_at),
|
||||
v.version,strftime('%Y-%m-%dT%H:%M:%SZ',v.created_at)
|
||||
v.version,strftime('%Y-%m-%dT%H:%M:%SZ',v.created_at),COALESCE(p.archived_at,'')
|
||||
FROM pipelines p JOIN pipeline_versions v ON v.pipeline_id=p.id
|
||||
WHERE p.id=? ORDER BY v.version DESC LIMIT 1`, id).
|
||||
Scan(&detail.ID, &detail.Name, &detail.CreatedAt, &detail.LatestVersion, &detail.UpdatedAt)
|
||||
Scan(&detail.ID, &detail.Name, &detail.CreatedAt, &detail.LatestVersion, &detail.UpdatedAt, &detail.ArchivedAt)
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, fmt.Errorf("pipeline not found")
|
||||
}
|
||||
@@ -337,6 +349,15 @@ func (s *Store) submit(ctx context.Context, name, source string, createOnly bool
|
||||
var version int
|
||||
var versionID any
|
||||
hash := fmt.Sprintf("%x", sha256.Sum256([]byte(source)))
|
||||
if validation == nil {
|
||||
// An archived name stays reserved: neither a fresh pipeline nor a new
|
||||
// version may take it until the operator unarchives it.
|
||||
if _, archived, err := archivedName(ctx, tx, "name=?", name); err != nil {
|
||||
return 0, err
|
||||
} else if archived {
|
||||
validation = &ArchivedError{Name: name}
|
||||
}
|
||||
}
|
||||
if validation == nil {
|
||||
_, err = tx.ExecContext(ctx, "INSERT INTO pipelines(name) VALUES (?) ON CONFLICT(name) DO NOTHING", name)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user