Switch to storing Git repositories in a table

Rather than just storing the URL in the guix_revisions and
load_new_guix_revision_jobs tables. This will help when storing more
information like tags and branches in the future.
This commit is contained in:
Christopher Baines 2019-05-05 13:35:48 +01:00
parent 051962b54d
commit ce4c3c6ed3
12 changed files with 246 additions and 77 deletions

View file

@ -0,0 +1,58 @@
(define-module (guix-data-service model git-repository)
#:use-module (ice-9 match)
#:use-module (squee)
#:export (all-git-repositories
git-repository-id->url
git-repository-url->git-repository-id
guix-revisions-and-jobs-for-git-repository))
(define (all-git-repositories conn)
(exec-query
conn
(string-append
"SELECT id, label, url FROM git_repositories")))
(define (git-repository-id->url conn id)
(match
(exec-query
conn
(string-append
"SELECT url FROM git_repositories WHERE id = $1;")
(list id))
(((url)) url)))
(define (git-repository-url->git-repository-id conn url)
(let ((existing-id
(exec-query
conn
(string-append
"SELECT id FROM git_repositories WHERE url = '" url "'"))))
(match existing-id
(((id)) id)
(()
(caar
(exec-query conn
(string-append
"INSERT INTO git_repositories "
"(url) "
"VALUES "
"('" url "') "
"RETURNING id")))))))
(define (guix-revisions-and-jobs-for-git-repository conn git-repository-id)
(define query
"
SELECT NULL AS id, load_new_guix_revision_jobs.id AS job_id, commit, source
FROM load_new_guix_revision_jobs
WHERE git_repository_id = $1
UNION
SELECT id, NULL, commit, NULL
FROM guix_revisions
WHERE git_repository_id = $1
ORDER BY 1 DESC NULLS FIRST, 2 DESC LIMIT 10;")
(exec-query
conn
query
(list git-repository-id)))