Persist repo ref hashes in db

This commit is contained in:
2023-02-08 18:44:54 -07:00
parent e4043ae3be
commit 6d2936393b
3 changed files with 79 additions and 7 deletions
+10
View File
@@ -159,6 +159,16 @@ CREATE TABLE runners (
name TEXT,
secret TEXT
);
CREATE TABLE pipeline_refs (
name TEXT PRIMARY KEY NOT NULL,
pipeline_id UUID NOT NULL,
hash TEXT NOT NULL,
CONSTRAINT fk_pipeline_id
FOREIGN KEY(pipeline_id)
REFERENCES pipelines(id)
);
`
_, err := conn.Exec(context.Background(), createTablesQuery)
+51
View File
@@ -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
}