From e6b93f328da025e77fddf944a665ccb08485f8b0 Mon Sep 17 00:00:00 2001 From: jacekwegr Date: Tue, 13 Dec 2022 12:48:07 +0100 Subject: [PATCH] NodeName is converted to an atom automatically --- big_tests/tests/graphql_mnesia_SUITE.erl | 5 +++++ .../mongoose_graphql_mnesia_admin_mutation.erl | 4 ++-- src/graphql/mongoose_graphql_scalar.erl | 4 +++- src/mnesia_api.erl | 14 ++++---------- 4 files changed, 14 insertions(+), 13 deletions(-) diff --git a/big_tests/tests/graphql_mnesia_SUITE.erl b/big_tests/tests/graphql_mnesia_SUITE.erl index 488602e24d..7c93551f8b 100644 --- a/big_tests/tests/graphql_mnesia_SUITE.erl +++ b/big_tests/tests/graphql_mnesia_SUITE.erl @@ -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]. @@ -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). diff --git a/src/graphql/admin/mongoose_graphql_mnesia_admin_mutation.erl b/src/graphql/admin/mongoose_graphql_mnesia_admin_mutation.erl index 9fef39acec..542640a946 100644 --- a/src/graphql/admin/mongoose_graphql_mnesia_admin_mutation.erl +++ b/src/graphql/admin/mongoose_graphql_mnesia_admin_mutation.erl @@ -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; @@ -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, diff --git a/src/graphql/mongoose_graphql_scalar.erl b/src/graphql/mongoose_graphql_scalar.erl index 05af0b99bf..d8d0f30fe1 100644 --- a/src/graphql/mongoose_graphql_scalar.erl +++ b/src/graphql/mongoose_graphql_scalar.erl @@ -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. diff --git a/src/mnesia_api.erl b/src/mnesia_api.erl index 6cf4643faf..603f87e937 100644 --- a/src/mnesia_api.erl +++ b/src/mnesia_api.erl @@ -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 -> @@ -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, ""};