package pipeline import ( "context" "database/sql" "fmt" "strconv" "git.a-wan.net/alan/pipelines/internal/protocol" ) // examplesSeededSetting is the server_settings key recording that the example // pipelines were considered for this database. Once present, SeedExamples never // adds anything again, so deleting the examples does not bring them back. const examplesSeededSetting = "examples_seeded" // SeedExamples publishes the given example definitions as ordinary pipelines, // but only into a brand-new database: one whose schema this Open created, that // has no pipelines and has never been seeded. Every call that gets past the // marker check records the marker, so an existing install that upgrades is // marked without gaining or rewriting anything, and a later start never seeds. // Sources must already be bound to the server's default model and harness. It // returns the number of pipelines created. func (s *Store) SeedExamples(ctx context.Context, items []protocol.Template) (int, error) { var marker string err := s.db.QueryRowContext(ctx, `SELECT value FROM server_settings WHERE key=?`, examplesSeededSetting).Scan(&marker) if err == nil { return 0, nil } if err != sql.ErrNoRows { return 0, err } seeded := 0 if s.created { var count int if err := s.db.QueryRowContext(ctx, `SELECT COUNT(*) FROM pipelines`).Scan(&count); err != nil { return 0, err } if count == 0 { for _, item := range items { // Create-only: a seed never replaces or versions an existing pipeline. if _, err := s.Submit(ctx, item.Name, item.Source, true); err != nil { return seeded, fmt.Errorf("seed example %q: %w", item.Name, err) } seeded++ } } } _, err = s.db.ExecContext(ctx, `INSERT INTO server_settings(key,value) VALUES(?,?) ON CONFLICT(key) DO NOTHING`, examplesSeededSetting, strconv.Itoa(seeded)) return seeded, err }