From 52da2cf588720f3f9d8ed80127232f4fbf85bb7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Reynir=20Bj=C3=B6rnsson?= Date: Fri, 12 Mar 2021 09:47:28 +0100 Subject: [PATCH] Refactor list result iter --- bin/migrations/grej.ml | 6 +++++- bin/migrations/m20210126.ml | 4 +--- bin/migrations/m20210218.ml | 12 ++++-------- bin/migrations/m20210308.ml | 6 ++---- 4 files changed, 12 insertions(+), 16 deletions(-) diff --git a/bin/migrations/grej.ml b/bin/migrations/grej.ml index 5f23c09..614599e 100644 --- a/bin/migrations/grej.ml +++ b/bin/migrations/grej.ml @@ -16,4 +16,8 @@ let check_version then Error (`Wrong_version (application_id, user_version)) else Ok () - +let list_iter_result f xs = + List.fold_left + (fun r x -> r >>= fun () -> f x) + (Ok ()) + xs diff --git a/bin/migrations/m20210126.ml b/bin/migrations/m20210126.ml index 9b8ab70..211da1c 100644 --- a/bin/migrations/m20210126.ml +++ b/bin/migrations/m20210126.ml @@ -33,8 +33,7 @@ let migrate (module Db : Caqti_blocking.CONNECTION) = Grej.check_version ~application_id:0l ~user_version:0L (module Db) >>= fun () -> Db.exec alter_build () >>= fun () -> Db.collect_list all_builds () >>= fun builds -> - List.fold_left (fun r build -> - r >>= fun () -> + Grej.list_iter_result (fun build -> Db.collect_list bin_artifact build >>= function | [_id, 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) (List.map snd binaries)); Ok ()) - (Ok ()) builds >>= fun () -> Db.exec Builder_db.set_application_id () >>= fun () -> Db.exec (Grej.set_version new_user_version) () diff --git a/bin/migrations/m20210218.ml b/bin/migrations/m20210218.ml index 0115f79..4c05bc2 100644 --- a/bin/migrations/m20210218.ml +++ b/bin/migrations/m20210218.ml @@ -84,26 +84,22 @@ let migrate (module Db : Caqti_blocking.CONNECTION) = Grej.check_version ~user_version:old_user_version (module Db) >>= fun () -> Db.exec new_build_artifact () >>= fun () -> Db.rev_collect_list collect_build_artifact () >>= fun build_artifacts -> - List.fold_left - (fun r (id, (filepath, localpath, sha256), build) -> - r >>= fun () -> + Grej.list_iter_result + (fun (id, (filepath, localpath, sha256), build) -> let stats = Unix.stat localpath in Db.exec insert_new_build_artifact (id, (filepath, localpath, sha256, Int64.of_int stats.st_size), build)) - (Ok ()) build_artifacts >>= fun () -> Db.exec drop_build_artifact () >>= fun () -> Db.exec rename_build_artifact () >>= fun () -> Db.exec new_build_file () >>= fun () -> Db.rev_collect_list collect_build_file () >>= fun build_files -> - List.fold_left - (fun r (id, (filepath, localpath, sha256), build) -> - r >>= fun () -> + Grej.list_iter_result + (fun (id, (filepath, localpath, sha256), build) -> let stats = Unix.stat localpath in Db.exec insert_new_build_file (id, (filepath, localpath, sha256, Int64.of_int stats.st_size), build)) - (Ok ()) build_files >>= fun () -> Db.exec drop_build_file () >>= fun () -> Db.exec rename_build_file () >>= fun () -> diff --git a/bin/migrations/m20210308.ml b/bin/migrations/m20210308.ml index b928a5e..d8f037c 100644 --- a/bin/migrations/m20210308.ml +++ b/bin/migrations/m20210308.ml @@ -14,11 +14,9 @@ let fixup (module Db : Caqti_blocking.CONNECTION) = let open Rresult.R.Infix in Grej.check_version ~user_version:3L (module Db) >>= fun () -> Db.rev_collect_list broken_builds () >>= fun broken_builds -> - List.fold_left - (fun r ((build, uuid, job_name) : Rep.id * Uuidm.t * string) -> - r >>= fun () -> + Grej.list_iter_result + (fun ((build, uuid, job_name) : Rep.id * Uuidm.t * string) -> 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; Db.exec Builder_db.Build.remove build) - (Ok ()) broken_builds