Skip to content

Commit

Permalink
NodeName is converted to an atom automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
jacekwegr committed Dec 13, 2022
1 parent 429d930 commit e6b93f3
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 13 deletions.
5 changes: 5 additions & 0 deletions big_tests/tests/graphql_mnesia_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ admin_mnesia_tests() ->
get_all_info_test,
install_fallback_error_test,
set_master_test,
set_master_self_test,
set_master_bad_name_test,
set_master_empty_name_test].

Expand Down Expand Up @@ -267,6 +268,10 @@ set_master_test(Config) ->
ParsedRes = get_ok_value([data, mnesia, setMaster], set_master(mim(), Config)),
?assertEqual(<<"Master node set">>, ParsedRes).

set_master_self_test(Config) ->
ParsedRes = get_ok_value([data, mnesia, setMaster], set_master(#{node => self}, Config)),
?assertEqual(<<"Master node set">>, ParsedRes).

set_master_bad_name_test(Config) ->
Res = set_master(#{node => incorrect_name}, Config),
get_coercion_err_msg(Res).
Expand Down
4 changes: 2 additions & 2 deletions src/graphql/admin/mongoose_graphql_mnesia_admin_mutation.erl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
-include("../mongoose_graphql_types.hrl").

execute(_Ctx, mnesia, <<"setMaster">>, #{<<"node">> := Node}) ->
case mnesia_api:set_master(binary_to_list(Node)) of
case mnesia_api:set_master(Node) of
{ok, _} -> {ok, "Master node set"};
Error -> make_error(Error, #{node => Node})
end;
Expand All @@ -21,7 +21,7 @@ execute(_Ctx, mnesia, <<"backup">>, #{<<"path">> := Path}) ->
end;
execute(_Ctx, mnesia, <<"changeNodename">>, #{<<"fromString">> := FromString,
<<"toString">> := ToString, <<"source">> := Source, <<"target">> := Target}) ->
case mnesia_api:mnesia_change_nodename(binary_to_list(FromString), binary_to_list(ToString),
case mnesia_api:mnesia_change_nodename(FromString, ToString,
binary_to_list(Source), binary_to_list(Target)) of
{ok, _} -> {ok, "Name of the node in the backup was successfully changed"};
Error -> make_error(Error, #{fromString => FromString, toString => ToString,
Expand Down
4 changes: 3 additions & 1 deletion src/graphql/mongoose_graphql_scalar.erl
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ node_from_binary(<<>>) ->
node_from_binary(NodeName) ->
case string:lexemes(binary_to_list(NodeName), "@") of
[_Name, _Host] ->
{ok, NodeName};
{ok, binary_to_atom(NodeName)};
["self"] ->
{ok, node()};
_ ->
{error, incorrect_node_name}
end.
Expand Down
14 changes: 4 additions & 10 deletions src/mnesia_api.erl
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,9 @@ load_mnesia(Path) ->
{cannot_load, String}
end.

-spec mnesia_change_nodename(string(), string(), _, _) ->
-spec mnesia_change_nodename(atom(), atom(), _, _) ->
{ok, _} | {change_error(), io_lib:chars()}.
mnesia_change_nodename(FromString, ToString, Source, Target) ->
From = list_to_atom(FromString),
To = list_to_atom(ToString),
mnesia_change_nodename(From, To, Source, Target) ->
Switch =
fun
(Node) when Node == From ->
Expand Down Expand Up @@ -175,12 +173,8 @@ install_fallback_mnesia(Path) ->
{cannot_fallback, String}
end.

-spec set_master(Node :: atom() | string()) -> {cannot_set, io_lib:chars()} | {ok, []}.
set_master("self") ->
set_master(node());
set_master(NodeString) when is_list(NodeString) ->
set_master(list_to_atom(NodeString));
set_master(Node) when is_atom(Node) ->
-spec set_master(Node :: atom()) -> {cannot_set, io_lib:chars()} | {ok, []}.
set_master(Node) ->
case mnesia:set_master_nodes([Node]) of
ok ->
{ok, ""};
Expand Down

0 comments on commit e6b93f3

Please sign in to comment.