Skip to content

Commit

Permalink
use mutable list to register migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
timohuber committed May 29, 2024
1 parent d1250a2 commit bff64e4
Showing 1 changed file with 7 additions and 12 deletions.
19 changes: 7 additions & 12 deletions sihl/src/database_migration.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ let log_src = Logs.Src.create ("sihl.service." ^ Contract_migration.name)
module Logs = (val Logs.src_log log_src : Logs.LOG)
module Map = Map.Make (String)

let registered_migrations : Contract_migration.steps Map.t ref = ref Map.empty
let registered_migrations = ref []

module Make (Repo : Database_migration_repo.Sig) : Contract_migration.Sig =
struct
Expand Down Expand Up @@ -74,14 +74,12 @@ struct

let register_migration migration =
let label, _ = migration in
let found = Map.find_opt label !registered_migrations in
let found = List.assoc_opt label !registered_migrations in
match found with
| Some _ ->
Logs.debug (fun m ->
m "Found duplicate migration '%s', ignoring it" label)
| None ->
registered_migrations
:= Map.add label (snd migration) !registered_migrations
| None -> registered_migrations := !registered_migrations @ [ migration ]
;;

let register_migrations migrations = List.iter register_migration migrations
Expand Down Expand Up @@ -208,15 +206,12 @@ struct
run migrations
;;

let run_all ?ctx () =
let steps = !registered_migrations |> Map.to_seq |> List.of_seq in
execute ?ctx steps
;;
let run_all ?ctx () = execute ?ctx !registered_migrations

let migrations_status ?ctx ?migrations () =
let migrations_to_check =
match migrations with
| Some migrations -> migrations |> List.to_seq |> Map.of_seq
| Some migrations -> migrations
| None -> !registered_migrations
in
let%lwt migrations_states = Repo.get_all ?ctx (table ()) in
Expand All @@ -226,7 +221,7 @@ struct
migration_state.Database_migration_repo.Migration.namespace)
in
let registered_migrations_namespaces =
Map.to_seq migrations_to_check |> List.of_seq |> List.map fst
migrations_to_check |> List.map fst
in
let namespaces_to_check =
List.concat
Expand All @@ -236,7 +231,7 @@ struct
Lwt.return
@@ List.map
(fun namespace ->
let migrations = Map.find_opt namespace migrations_to_check in
let migrations = List.assoc_opt namespace migrations_to_check in
let migration_state =
List.find_opt
(fun migration_state ->
Expand Down

0 comments on commit bff64e4

Please sign in to comment.