package database import ( "context" "fmt" "github.com/google/uuid" ) func (db *Database) GetPipelines() ([]Pipeline, error) { query := ` SELECT id, name, url, poll_interval FROM pipelines;` pipelines := make([]Pipeline, 0) rows, err := db.Conn.Query(context.Background(), query) if err != nil { return pipelines, fmt.Errorf("Could not query database for pipelines: %w", err) } defer rows.Close() for rows.Next() { var pipeline Pipeline var idStr string if err := rows.Scan(&idStr, &pipeline.Name, &pipeline.Url, &pipeline.PollInterval); err != nil { return pipelines, err } pipeline.Id, err = uuid.Parse(idStr) if err != nil { return pipelines, err } pipelines = append(pipelines, pipeline) } return pipelines, nil } func (db *Database) GetPipelineById(id uuid.UUID) (Pipeline, error) { query := ` SELECT name, url, poll_interval FROM pipelines WHERE id=$1;` pipeline := Pipeline{ Id: id, } err := db.Conn.QueryRow(context.Background(), query, id).Scan(&pipeline.Name, &pipeline.Url, &pipeline.PollInterval) if err != nil { return pipeline, fmt.Errorf("Could not query database for pipeline with id %v: %w", id.String(), err) } return pipeline, nil } func (db *Database) CreatePipeline(name string, url string, pollInterval int) (Pipeline, error) { query := ` INSERT INTO pipelines (id, name, url, poll_interval) VALUES (uuid_generate_v4(), $1, $2, $3) RETURNING id, name, url, poll_interval;` pipeline := Pipeline{} var idStr string err := db.Conn.QueryRow(context.Background(), query, name, url, pollInterval).Scan(&idStr, &pipeline.Name, &pipeline.Url, &pipeline.PollInterval) if err != nil { return pipeline, fmt.Errorf("Could not create pipeline: %w", err) } id, err := uuid.Parse(idStr) if err != nil { return pipeline, fmt.Errorf("Could not parse UUID generated by DB: %w", err) } pipeline.Id = id return pipeline, nil } func (db *Database) GetWebhooksForPipeline(id uuid.UUID) ([]Webhook, error) { query := ` SELECT id, server_type, secret FROM webhooks WHERE pipeline=$1;` webhooks := make([]Webhook, 0) rows, err := db.Conn.Query(context.Background(), query, id) if err != nil { return webhooks, fmt.Errorf("Could not get webhooks for pipeline with id \"%v\": %w", id, err) } defer rows.Close() for rows.Next() { var webhook Webhook var idStr string if err := rows.Scan(&idStr, &webhook.ServerType, &webhook.Secret); err != nil { return webhooks, err } webhook.Id, err = uuid.Parse(idStr) if err != nil { return webhooks, err } webhooks = append(webhooks, webhook) } return webhooks, nil } func (db *Database) GetWebhookById(id uuid.UUID) (Webhook, error) { query := ` SELECT server_type, secret, pipeline FROM webhooks WHERE id=$1;` webhook := Webhook{ Id: id, } err := db.Conn.QueryRow(context.Background(), query, id).Scan(&webhook.ServerType, &webhook.Secret, &webhook.Pipeline) if err != nil { return webhook, fmt.Errorf("Could not query database for webhook with id %v: %w", id.String(), err) } return webhook, nil } func (db *Database) CreateWebhook(serverType WebhookSender, pipelineId uuid.UUID) (Webhook, error) { query := ` INSERT INTO webhooks (id, server_type, secret, pipeline) VALUES (uuid_generate_v4(), $1, (select substr(md5(random()::text), 0, 50)), $2) RETURNING id, server_type, secret, pipeline;` webhook := Webhook{} var idStr string err := db.Conn.QueryRow(context.Background(), query, string(serverType), pipelineId).Scan(&idStr, &webhook.ServerType, &webhook.Secret, &webhook.Pipeline) if err != nil { return webhook, err } id, err := uuid.Parse(idStr) if err != nil { return webhook, err } webhook.Id = id return webhook, nil } func (db *Database) CreateRun(pipelineId uuid.UUID) (Run, error) { query := ` INSERT INTO runs (id, pipeline, in_progress) VALUES(uuid_generate_v4(), $1, true) RETURNING id, pipeline, in_progress;` run := Run{} var idStr string err := db.Conn.QueryRow(context.Background(), query, pipelineId).Scan(&idStr, &run.Pipeline, &run.InProgress) if err != nil { return run, err } run.Id, err = uuid.Parse(idStr) if err != nil { return run, err } return run, nil } func (db *Database) UpdateRunResult(r Run) error { query := ` UPDATE runs SET in_progress=$1, result=$2, stdout=$3, stderr=$4 WHERE id=$3;` // TODO: does r.Result need a pointer derefrence? _, err := db.Conn.Exec(context.Background(), query, r.InProgress, r.Result, r.Stdout, r.Stderr) return err } func (db *Database) GetRunsForPipeline(pipelineId uuid.UUID) ([]Run, error) { query := ` SELECT id, in_progress, result, stdout, stderr FROM runs WHERE pipeline=$1;` runs := make([]Run, 0) rows, err := db.Conn.Query(context.Background(), query, pipelineId) if err != nil { return runs, fmt.Errorf("Could not get runs for pipeline with id \"%v\": %w", pipelineId, err) } defer rows.Close() for rows.Next() { var run Run var idStr string if err := rows.Scan( &idStr, &run.InProgress, &run.Result, &run.Stdout, &run.Stderr, ); err != nil { return runs, err } run.Id, err = uuid.Parse(idStr) if err != nil { return runs, err } runs = append(runs, run) } return runs, nil }