23 lines
690 B
Go
23 lines
690 B
Go
package models
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
)
|
|
|
|
func GetOrCreateCommit(ctx context.Context, pool *pgxpool.Pool, repositoryID, sha string) (*Commit, error) {
|
|
c := &Commit{}
|
|
err := pool.QueryRow(ctx, `
|
|
INSERT INTO commits (repository_id, sha)
|
|
VALUES ($1, $2)
|
|
ON CONFLICT (repository_id, sha) DO UPDATE SET repository_id = EXCLUDED.repository_id
|
|
RETURNING id, repository_id, sha, author, message, branch, committed_at, created_at
|
|
`, repositoryID, sha).Scan(&c.ID, &c.RepositoryID, &c.SHA, &c.Author, &c.Message, &c.Branch, &c.CommittedAt, &c.CreatedAt)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("get or create commit: %w", err)
|
|
}
|
|
return c, nil
|
|
}
|