Add Build.get_previous database lookup

This commit is contained in:
Reynir Björnsson 2021-04-23 12:06:39 +02:00
parent 9974510787
commit b965b3ca7c
3 changed files with 36 additions and 0 deletions

View file

@ -382,6 +382,21 @@ module Build = struct
LIMIT 1 LIMIT 1
|} |}
let get_previous =
Caqti_request.find_opt
id
Caqti_type.(tup2 id Meta.t)
{| SELECT b.id,
b.uuid, b.start_d, b.start_ps, b.finish_d, b.finish_ps,
b.result_kind, b.result_code, b.result_msg,
b.main_binary, b.job
FROM build b, build b0
WHERE b0.id = ? AND b0.job = b.job AND
(b0.start_d > b.start_d OR b0.start_d = b.start_d AND b0.start_ps > b.start_ps)
ORDER BY b.start_d DESC, b.start_ps DESC
LIMIT 1
|}
let add = let add =
Caqti_request.exec Caqti_request.exec
t t

View file

@ -146,6 +146,9 @@ sig
val get_latest : val get_latest :
(id, id * Meta.t * file option, [< `Many | `One | `Zero > `One `Zero ]) (id, id * Meta.t * file option, [< `Many | `One | `Zero > `One `Zero ])
Caqti_request.t Caqti_request.t
val get_previous :
(id, id * Meta.t, [< `Many | `One | `Zero > `One `Zero ])
Caqti_request.t
val add : (t, unit, [< `Many | `One | `Zero > `Zero ]) Caqti_request.t val add : (t, unit, [< `Many | `One | `Zero > `Zero ]) Caqti_request.t
val get_by_hash : val get_by_hash :
(Cstruct.t, string * t, [< `Many | `One | `Zero > `One `Zero]) Caqti_request.t (Cstruct.t, string * t, [< `Many | `One | `Zero > `One `Zero]) Caqti_request.t

View file

@ -233,6 +233,22 @@ let test_build_get_latest (module Db : CONN) =
Alcotest.(check (option Testable.file)) "same main binary" main_binary' (Some main_binary); Alcotest.(check (option Testable.file)) "same main binary" main_binary' (Some main_binary);
Alcotest.(check Testable.uuid) "same uuid" meta.uuid uuid' Alcotest.(check Testable.uuid) "same uuid" meta.uuid uuid'
let test_build_get_previous (module Db : CONN) =
add_second_build (module Db) >>= fun () ->
Db.find_opt Builder_db.Build.get_by_uuid uuid'
>>| get_opt "no build" >>= fun (id, _build) ->
Db.find_opt Builder_db.Build.get_previous id
>>| get_opt "no previous build" >>| fun (_id, meta) ->
Alcotest.(check Testable.uuid) "same uuid" meta.uuid uuid
let test_build_get_previous_none (module Db : CONN) =
Db.find_opt Builder_db.Build.get_by_uuid uuid
>>| get_opt "no build" >>= fun (id, _build) ->
Db.find_opt Builder_db.Build.get_previous id >>| function
| None -> ()
| Some (_id, meta) ->
Alcotest.failf "Got unexpected result %a" Uuidm.pp meta.uuid
let test_build_get_by_hash (module Db : CONN) = let test_build_get_by_hash (module Db : CONN) =
add_second_build (module Db) >>= fun () -> add_second_build (module Db) >>= fun () ->
Db.find_opt Builder_db.Build.get_by_hash main_binary.sha256 Db.find_opt Builder_db.Build.get_by_hash main_binary.sha256
@ -293,6 +309,8 @@ let () =
test_case "One build (meta data)" `Quick (with_build_db test_build_get_all_meta); test_case "One build (meta data)" `Quick (with_build_db test_build_get_all_meta);
test_case "Get latest build" `Quick (with_build_db test_build_get_latest); test_case "Get latest build" `Quick (with_build_db test_build_get_latest);
test_case "Get build by hash" `Quick (with_build_db test_build_get_by_hash); test_case "Get build by hash" `Quick (with_build_db test_build_get_by_hash);
test_case "Get previous build" `Quick (with_build_db test_build_get_previous);
test_case "Get previous build when first" `Quick (with_build_db test_build_get_previous_none);
]; ];
"build-artifact", [ "build-artifact", [
test_case "Get all by build" `Quick (with_build_db test_artifact_get_all_by_build); test_case "Get all by build" `Quick (with_build_db test_artifact_get_all_by_build);