Refactor list result iter

This commit is contained in:
Reynir Björnsson 2021-03-12 09:47:28 +01:00
parent e2a489a74d
commit 52da2cf588
4 changed files with 12 additions and 16 deletions

View file

@ -16,4 +16,8 @@ let check_version
then Error (`Wrong_version (application_id, user_version)) then Error (`Wrong_version (application_id, user_version))
else Ok () else Ok ()
let list_iter_result f xs =
List.fold_left
(fun r x -> r >>= fun () -> f x)
(Ok ())
xs

View file

@ -33,8 +33,7 @@ let migrate (module Db : Caqti_blocking.CONNECTION) =
Grej.check_version ~application_id:0l ~user_version:0L (module Db) >>= fun () -> Grej.check_version ~application_id:0l ~user_version:0L (module Db) >>= fun () ->
Db.exec alter_build () >>= fun () -> Db.exec alter_build () >>= fun () ->
Db.collect_list all_builds () >>= fun builds -> Db.collect_list all_builds () >>= fun builds ->
List.fold_left (fun r build -> Grej.list_iter_result (fun build ->
r >>= fun () ->
Db.collect_list bin_artifact build >>= function Db.collect_list bin_artifact build >>= function
| [_id, main_binary] -> | [_id, main_binary] ->
Db.exec set_main_binary (build, Some main_binary) Db.exec set_main_binary (build, Some main_binary)
@ -46,7 +45,6 @@ let migrate (module Db : Caqti_blocking.CONNECTION) =
Logs.debug (fun m -> m "binaries: [%a]" Fmt.(list ~sep:(any ";") string) Logs.debug (fun m -> m "binaries: [%a]" Fmt.(list ~sep:(any ";") string)
(List.map snd binaries)); (List.map snd binaries));
Ok ()) Ok ())
(Ok ())
builds >>= fun () -> builds >>= fun () ->
Db.exec Builder_db.set_application_id () >>= fun () -> Db.exec Builder_db.set_application_id () >>= fun () ->
Db.exec (Grej.set_version new_user_version) () Db.exec (Grej.set_version new_user_version) ()

View file

@ -84,26 +84,22 @@ let migrate (module Db : Caqti_blocking.CONNECTION) =
Grej.check_version ~user_version:old_user_version (module Db) >>= fun () -> Grej.check_version ~user_version:old_user_version (module Db) >>= fun () ->
Db.exec new_build_artifact () >>= fun () -> Db.exec new_build_artifact () >>= fun () ->
Db.rev_collect_list collect_build_artifact () >>= fun build_artifacts -> Db.rev_collect_list collect_build_artifact () >>= fun build_artifacts ->
List.fold_left Grej.list_iter_result
(fun r (id, (filepath, localpath, sha256), build) -> (fun (id, (filepath, localpath, sha256), build) ->
r >>= fun () ->
let stats = Unix.stat localpath in let stats = Unix.stat localpath in
Db.exec insert_new_build_artifact Db.exec insert_new_build_artifact
(id, (filepath, localpath, sha256, Int64.of_int stats.st_size), build)) (id, (filepath, localpath, sha256, Int64.of_int stats.st_size), build))
(Ok ())
build_artifacts >>= fun () -> build_artifacts >>= fun () ->
Db.exec drop_build_artifact () >>= fun () -> Db.exec drop_build_artifact () >>= fun () ->
Db.exec rename_build_artifact () >>= fun () -> Db.exec rename_build_artifact () >>= fun () ->
Db.exec new_build_file () >>= fun () -> Db.exec new_build_file () >>= fun () ->
Db.rev_collect_list collect_build_file () >>= fun build_files -> Db.rev_collect_list collect_build_file () >>= fun build_files ->
List.fold_left Grej.list_iter_result
(fun r (id, (filepath, localpath, sha256), build) -> (fun (id, (filepath, localpath, sha256), build) ->
r >>= fun () ->
let stats = Unix.stat localpath in let stats = Unix.stat localpath in
Db.exec insert_new_build_file Db.exec insert_new_build_file
(id, (filepath, localpath, sha256, Int64.of_int stats.st_size), build)) (id, (filepath, localpath, sha256, Int64.of_int stats.st_size), build))
(Ok ())
build_files >>= fun () -> build_files >>= fun () ->
Db.exec drop_build_file () >>= fun () -> Db.exec drop_build_file () >>= fun () ->
Db.exec rename_build_file () >>= fun () -> Db.exec rename_build_file () >>= fun () ->

View file

@ -14,11 +14,9 @@ let fixup (module Db : Caqti_blocking.CONNECTION) =
let open Rresult.R.Infix in let open Rresult.R.Infix in
Grej.check_version ~user_version:3L (module Db) >>= fun () -> Grej.check_version ~user_version:3L (module Db) >>= fun () ->
Db.rev_collect_list broken_builds () >>= fun broken_builds -> Db.rev_collect_list broken_builds () >>= fun broken_builds ->
List.fold_left Grej.list_iter_result
(fun r ((build, uuid, job_name) : Rep.id * Uuidm.t * string) -> (fun ((build, uuid, job_name) : Rep.id * Uuidm.t * string) ->
r >>= fun () ->
Format.printf "Removing job %a.\nPlease clean up data files in /var/db/builder-web/%s/%a\n" Format.printf "Removing job %a.\nPlease clean up data files in /var/db/builder-web/%s/%a\n"
Uuidm.pp uuid job_name Uuidm.pp uuid; Uuidm.pp uuid job_name Uuidm.pp uuid;
Db.exec Builder_db.Build.remove build) Db.exec Builder_db.Build.remove build)
(Ok ())
broken_builds broken_builds