Job_tag: try to retrieve the tag value - insert if none, update if some

This commit is contained in:
Robur 2021-06-30 10:55:00 +00:00
parent 1e190e42c7
commit b4996939af
3 changed files with 18 additions and 9 deletions

View file

@ -157,13 +157,15 @@ module Job_tag = struct
let add = let add =
Caqti_request.exec Caqti_request.exec
Caqti_type.(tup3 id string id) Caqti_type.(tup3 id string id)
{| INSERT INTO job_tag (tag, value, job) "INSERT INTO job_tag (tag, value, job) VALUES (?1, ?2, ?3)"
VALUES (?1, ?2, ?3)
ON CONFLICT(x) DO UPDATE SET value = ?2 WHERE tag = ?1 AND job = ?3 let update =
|} Caqti_request.exec
Caqti_type.(tup3 id string id)
"UPDATE job_tag SET value = ?2 WHERE tag = ?1 AND job = ?3"
let get_value = let get_value =
Caqti_request.find Caqti_request.find_opt
Caqti_type.(tup2 id id) Caqti_type.(tup2 id id)
Caqti_type.string Caqti_type.string
"SELECT value FROM job_tag WHERE tag = ? AND job = ?" "SELECT value FROM job_tag WHERE tag = ? AND job = ?"

View file

@ -85,8 +85,10 @@ module Job_tag : sig
(unit, unit, [< `Many | `One | `Zero > `Zero ]) Caqti_request.t (unit, unit, [< `Many | `One | `Zero > `Zero ]) Caqti_request.t
val add : val add :
(id * string * id, unit, [< `Many | `One | `Zero > `Zero ]) Caqti_request.t (id * string * id, unit, [< `Many | `One | `Zero > `Zero ]) Caqti_request.t
val update :
(id * string * id, unit, [< `Many | `One | `Zero > `Zero ]) Caqti_request.t
val get_value : val get_value :
(id * id, string, [< `Many | `One | `Zero > `One ]) Caqti_request.t (id * id, string, [< `Many | `One | `Zero > `Zero `One ]) Caqti_request.t
end end
module Build_artifact : sig module Build_artifact : sig

View file

@ -270,15 +270,20 @@ let add_build
main_binary = None; user_id; job_id } >>= fun () -> main_binary = None; user_id; job_id } >>= fun () ->
Db.find last_insert_rowid () >>= fun id -> Db.find last_insert_rowid () >>= fun id ->
let sec_syn = infer_section_and_synopsis raw_artifacts in let sec_syn = infer_section_and_synopsis raw_artifacts in
let add_or_update tag_id tag_value =
Db.find_opt Job_tag.get_value (tag_id, job_id) >>= function
| None -> Db.exec Job_tag.add (tag_id, tag_value, job_id)
| Some _ -> Db.exec Job_tag.update (tag_id, tag_value, job_id)
in
(match fst sec_syn with (match fst sec_syn with
| None -> Lwt_result.return () | None -> Lwt_result.return ()
| Some section_v -> Db.exec Job_tag.add (section_id, section_v, id)) >>= fun () -> | Some section_v -> add_or_update section_id section_v) >>= fun () ->
(match snd sec_syn with (match snd sec_syn with
| None, _-> Lwt_result.return () | None, _-> Lwt_result.return ()
| Some synopsis_v, _ -> Db.exec Job_tag.add (synopsis_id, synopsis_v, id)) >>= fun () -> | Some synopsis_v, _ -> add_or_update synopsis_id synopsis_v) >>= fun () ->
(match snd sec_syn with (match snd sec_syn with
| _, None -> Lwt_result.return () | _, None -> Lwt_result.return ()
| _, Some descr_v -> Db.exec Job_tag.add (descr_id, descr_v, id)) >>= fun () -> | _, Some descr_v -> add_or_update descr_id descr_v) >>= fun () ->
List.fold_left List.fold_left
(fun r file -> (fun r file ->
r >>= fun () -> r >>= fun () ->