Skip to content

Commit

Permalink
Allow init_backends to be called multiple times
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikaka27 committed Dec 5, 2024
1 parent eb9cbfb commit 29cb353
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions lib/mnesia/src/mnesia_schema.erl
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@
info/1,
init/1,
init_backends/0,
init_backends/1,
get_backends_to_initialize/1,
insert_cstruct/3,
is_remote_member/1,
list2cs/1,
Expand Down Expand Up @@ -161,13 +163,22 @@ init(IgnoreFallback) ->
mnesia_controller:add_active_replica(schema, node()),
init_backends().


init_backends() ->
Backends = lists:foldl(fun({Alias, Mod}, Acc) ->
orddict:append(Mod, Alias, Acc)
end, orddict:new(), get_ext_types()),
[init_backend(Mod, Aliases) || {Mod, Aliases} <- Backends],
ok.
Backends = get_backends_to_initialize(get_ext_types()),
init_backends(Backends).
init_backends([]) ->
ok;
init_backends([{Mod, Aliases} = Backend | Rest]) ->
Mods = mnesia_lib:val(ext_backends),
case lists:member(Mod, Mods) of
true ->
dbg_out("Backend ~p is already initialized~n", [Backend]);
false ->
init_backend(Mod, Aliases),
mnesia_lib:set(ext_backends, [Mod | Mods]),
dbg_out("Backend ~p initialized~n", [Backend])
end,
init_backends(Rest).

init_backend(Mod, [_|_] = Aliases) ->
case Mod:init_backend() of
Expand All @@ -177,6 +188,10 @@ init_backend(Mod, [_|_] = Aliases) ->
mnesia:abort({backend_init_error, Error})
end.

get_backends_to_initialize(Ext) ->
lists:foldl(fun({Alias, Mod}, Acc) ->
orddict:append(Mod, Alias, Acc) end, orddict:new(), Ext).

exit_on_error({error, Reason}) ->
exit(Reason);
exit_on_error(GoodRes) ->
Expand Down

0 comments on commit 29cb353

Please sign in to comment.