Files
pipelines/internal/pipeline/store.go
T

233 lines
7.9 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package pipeline
import (
"context"
"crypto/sha256"
"database/sql"
"encoding/json"
"fmt"
"pipelines/internal/protocol"
"pipelines/internal/stage"
"strings"
"sync"
"time"
_ "modernc.org/sqlite"
)
type Store struct {
db *sql.DB
mu sync.Mutex
}
func Open(path string) (*Store, error) {
db, err := sql.Open("sqlite", path)
if err != nil {
return nil, err
}
db.SetMaxOpenConns(1)
_, err = db.Exec(`PRAGMA foreign_keys=ON; PRAGMA journal_mode=WAL; PRAGMA busy_timeout=5000;
CREATE TABLE IF NOT EXISTS pipelines (id INTEGER PRIMARY KEY, name TEXT NOT NULL UNIQUE, created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP);
CREATE TABLE IF NOT EXISTS pipeline_versions (id INTEGER PRIMARY KEY, pipeline_id INTEGER NOT NULL REFERENCES pipelines(id), version INTEGER NOT NULL, source TEXT NOT NULL, content_hash TEXT NOT NULL, graph_json TEXT NOT NULL, created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP, UNIQUE(pipeline_id, version));
CREATE TABLE IF NOT EXISTS submissions (id INTEGER PRIMARY KEY, name TEXT NOT NULL, source TEXT NOT NULL, diagnostic TEXT NOT NULL, version_id INTEGER REFERENCES pipeline_versions(id), created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP);`)
if err != nil {
db.Close()
return nil, err
}
s := &Store{db: db}
if err := s.initRuns(); err != nil {
db.Close()
return nil, err
}
if err := s.initSchedules(); err != nil {
db.Close()
return nil, err
}
return s, nil
}
func (s *Store) Close() error { return s.db.Close() }
func (s *Store) List(ctx context.Context) ([]protocol.PipelineSummary, error) {
rows, err := s.db.QueryContext(ctx, `SELECT p.id, p.name, v.version,
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)
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`)
if err != nil {
return nil, err
}
defer rows.Close()
result := []protocol.PipelineSummary{}
for rows.Next() {
var item protocol.PipelineSummary
if err := rows.Scan(&item.ID, &item.Name, &item.Version, &item.Stages, &item.CreatedAt, &item.UpdatedAt); err != nil {
return nil, err
}
result = append(result, item)
}
return result, rows.Err()
}
// Get returns a consistent immutable version and the available version history.
// Version zero selects the latest accepted version.
func (s *Store) Get(ctx context.Context, id int64, version int) (*protocol.PipelineDetail, error) {
if id <= 0 || version < 0 {
return nil, fmt.Errorf("invalid pipeline ID or version")
}
tx, err := s.db.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
if err != nil {
return nil, err
}
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)
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)
if err == sql.ErrNoRows {
return nil, fmt.Errorf("pipeline not found")
}
if err != nil {
return nil, err
}
if version == 0 {
version = detail.LatestVersion
}
var graph string
err = tx.QueryRowContext(ctx, `SELECT version,source,content_hash,graph_json,strftime('%Y-%m-%dT%H:%M:%SZ',created_at)
FROM pipeline_versions WHERE pipeline_id=? AND version=?`, id, version).
Scan(&detail.Version, &detail.Source, &detail.ContentHash, &graph, &detail.VersionCreatedAt)
if err == sql.ErrNoRows {
return nil, fmt.Errorf("pipeline version not found")
}
if err != nil {
return nil, err
}
if err := json.Unmarshal([]byte(graph), &detail.Graph); err != nil {
return nil, err
}
// Normalize legacy terminal aliases for clients without rewriting history.
for i := range detail.Graph.Stages {
s := &detail.Graph.Stages[i]
s.Type = stage.EffectiveType(s.Type)
for outcome, target := range s.Edges {
s.Edges[outcome] = protocol.ResolveTarget(outcome, target)
}
}
detail.Stages = len(detail.Graph.Stages)
rows, err := tx.QueryContext(ctx, `SELECT version,json_array_length(graph_json,'$.stages'),strftime('%Y-%m-%dT%H:%M:%SZ',created_at)
FROM pipeline_versions WHERE pipeline_id=? ORDER BY version DESC`, id)
if err != nil {
return nil, err
}
detail.Versions = []protocol.VersionSummary{}
for rows.Next() {
var item protocol.VersionSummary
if err := rows.Scan(&item.Version, &item.Stages, &item.CreatedAt); err != nil {
rows.Close()
return nil, err
}
detail.Versions = append(detail.Versions, item)
}
err = rows.Err()
rows.Close()
if err != nil {
return nil, err
}
if err := tx.Commit(); err != nil {
return nil, err
}
return detail, nil
}
// Submit persists both accepted and rejected submissions. Equality is source-byte equality.
func (s *Store) Submit(ctx context.Context, name, source string, createOnly ...bool) (int, error) {
fresh := len(createOnly) > 0 && createOnly[0]
return s.SubmitChecked(ctx, name, source, fresh, 0)
}
// SubmitChecked guards draft edits against concurrent accepted versions atomically.
func (s *Store) SubmitChecked(ctx context.Context, name, source string, createOnly bool, expectedVersion int) (int, error) {
s.mu.Lock()
defer s.mu.Unlock()
name = strings.TrimSpace(name)
g, validation := Parse(source)
if expectedVersion < 0 {
validation = fmt.Errorf("expected version must be nonnegative")
}
if name == "" || len(name) > 200 || strings.ContainsAny(name, "\r\n\x00") {
validation = fmt.Errorf("pipeline name must be 1–200 bytes without line breaks or NUL")
}
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return 0, err
}
defer tx.Rollback()
var pid int64
var version int
var versionID any
hash := fmt.Sprintf("%x", sha256.Sum256([]byte(source)))
if validation == nil {
_, err = tx.ExecContext(ctx, "INSERT INTO pipelines(name) VALUES (?) ON CONFLICT(name) DO NOTHING", name)
if err != nil {
return 0, err
}
if err = tx.QueryRowContext(ctx, "SELECT id FROM pipelines WHERE name=?", name).Scan(&pid); err != nil {
return 0, err
}
var latestHash string
err = tx.QueryRowContext(ctx, "SELECT version, content_hash FROM pipeline_versions WHERE pipeline_id=? ORDER BY version DESC LIMIT 1", pid).Scan(&version, &latestHash)
if err != nil && err != sql.ErrNoRows {
return 0, err
}
if createOnly && version > 0 {
validation = fmt.Errorf("pipeline name already exists; choose a new name")
} else if expectedVersion > 0 && version != expectedVersion {
validation = fmt.Errorf("draft conflict: expected latest version %d, found %d; review changes before submitting", expectedVersion, version)
} else if hash == latestHash {
validation = fmt.Errorf("no changes")
} else {
version++
encoded, err := json.Marshal(g)
if err != nil {
return 0, err
}
result, err := tx.ExecContext(ctx, "INSERT INTO pipeline_versions(pipeline_id,version,source,content_hash,graph_json) VALUES (?,?,?,?,?)", pid, version, source, hash, string(encoded))
if err != nil {
return 0, err
}
id, err := result.LastInsertId()
if err != nil {
return 0, err
}
versionID = id
if g.Settings != nil {
activated := time.Now()
for slot, rule := range g.Settings.Schedules {
next, err := NextSchedule(rule, activated)
if err != nil {
return 0, err
}
if _, err := tx.ExecContext(ctx, `INSERT INTO schedule_state(pipeline_id,version,slot,next_due) VALUES(?,?,?,?)`, pid, version, slot, next.UTC().Format(time.RFC3339Nano)); err != nil {
return 0, err
}
}
}
}
}
diagnostic := ""
if validation != nil {
diagnostic = validation.Error()
}
if _, err := tx.ExecContext(ctx, "INSERT INTO submissions(name,source,diagnostic,version_id) VALUES (?,?,?,?)", name, source, diagnostic, versionID); err != nil {
return 0, err
}
if err := tx.Commit(); err != nil {
return 0, err
}
return version, validation
}