Persist repo ref hashes in db
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
func (db *Database) GetPipelines() ([]Pipeline, error) {
|
||||
@@ -344,3 +345,53 @@ WHERE pipeline=$1;`
|
||||
|
||||
return runs, nil
|
||||
}
|
||||
|
||||
func (db *Database) GetPipelineRefs(pipelineId uuid.UUID) (map[string]string, error) {
|
||||
query := `
|
||||
SELECT name, hash
|
||||
FROM pipeline_refs
|
||||
WHERE pipeline_id=$1;`
|
||||
|
||||
refsMap := make(map[string]string)
|
||||
|
||||
refs, err := db.Conn.Query(context.Background(), query, pipelineId)
|
||||
if err != nil {
|
||||
return refsMap, fmt.Errorf("Could not get pipeline refs for pipeline with id \"%v\": %w", pipelineId, err)
|
||||
}
|
||||
defer refs.Close()
|
||||
|
||||
for refs.Next() {
|
||||
var name string
|
||||
var hash string
|
||||
if err := refs.Scan(
|
||||
&name,
|
||||
&hash,
|
||||
); err != nil {
|
||||
return refsMap, err
|
||||
}
|
||||
|
||||
refsMap[name] = hash
|
||||
}
|
||||
|
||||
return refsMap, nil
|
||||
}
|
||||
|
||||
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})
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
log.Debugf("copyCount: %v", copyCount)
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user