From 77a8d0840aedba73eb2fbd79b5e521b679e21205 Mon Sep 17 00:00:00 2001 From: restitux Date: Fri, 24 Feb 2023 22:28:50 -0700 Subject: [PATCH] Fix inserting new repeated refs into db failing #20 --- database/func.go | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/database/func.go b/database/func.go index b196b64..ec588de 100644 --- a/database/func.go +++ b/database/func.go @@ -6,7 +6,6 @@ import ( "regexp" "github.com/google/uuid" - "github.com/jackc/pgx/v5" ) func (db *Database) GetPipelines() ([]Pipeline, error) { @@ -301,11 +300,11 @@ func (db *Database) UpdateRunResult(r Run) error { query := ` UPDATE runs SET in_progress=$1, result=$2, stdout=$3, stderr=$4 -WHERE id=$3;` +WHERE id=$5;` // TODO: does r.Result need a pointer derefrence? _, err := db.Conn.Exec(context.Background(), - query, r.InProgress, r.Result, r.Stdout, r.Stderr) + query, r.InProgress, r.Result, r.Stdout, r.Stderr, r.Id) return err } @@ -379,21 +378,18 @@ WHERE pipeline_id=$1;` func (db *Database) UpdatePipelineRefs(pipelineId uuid.UUID, refsMap map[string]string) error { - refsSlice := make([][]interface{}, 0) - for name, ref := range refsMap { - refsSlice = append(refsSlice, []interface{}{name, pipelineId, ref}) - } + query := ` +INSERT INTO pipeline_refs(name, pipeline_id, hash) +VALUES($1, $2, $3) +ON CONFLICT (name) +DO + UPDATE SET hash=$3;` - copyCount, err := db.Conn.CopyFrom( - context.Background(), - pgx.Identifier{"pipeline_refs"}, - []string{"name", "pipeline_id", "hash"}, - pgx.CopyFromRows(refsSlice), - ) - if err != nil { - return fmt.Errorf("could not insert updated pipeline refs: %w", err) + for name, hash := range refsMap { + _, err := db.Conn.Exec(context.Background(), query, name, pipelineId, hash) + + return err } - log.Debugf("copyCount: %v", copyCount) return nil }