mirror of
https://github.com/processone/ejabberd.git
synced 2024-06-04 21:27:16 +02:00
Fix upgrade of PubSub mnesia tables from older ejabberd version.
This commit is contained in:
parent
8781c8346b
commit
cc958f7787
|
@ -4,13 +4,13 @@
|
||||||
%%% compliance with the License. You should have received a copy of the
|
%%% compliance with the License. You should have received a copy of the
|
||||||
%%% Erlang Public License along with this software. If not, it can be
|
%%% Erlang Public License along with this software. If not, it can be
|
||||||
%%% retrieved via the world wide web at http://www.erlang.org/.
|
%%% retrieved via the world wide web at http://www.erlang.org/.
|
||||||
%%%
|
%%%
|
||||||
%%%
|
%%%
|
||||||
%%% Software distributed under the License is distributed on an "AS IS"
|
%%% Software distributed under the License is distributed on an "AS IS"
|
||||||
%%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
|
%%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
|
||||||
%%% the License for the specific language governing rights and limitations
|
%%% the License for the specific language governing rights and limitations
|
||||||
%%% under the License.
|
%%% under the License.
|
||||||
%%%
|
%%%
|
||||||
%%%
|
%%%
|
||||||
%%% The Initial Developer of the Original Code is ProcessOne.
|
%%% The Initial Developer of the Original Code is ProcessOne.
|
||||||
%%% Portions created by ProcessOne are Copyright 2006-2014, ProcessOne
|
%%% Portions created by ProcessOne are Copyright 2006-2014, ProcessOne
|
||||||
|
@ -357,6 +357,7 @@ init([ServerHost, Opts]) ->
|
||||||
ejabberd_router:register_route(Host),
|
ejabberd_router:register_route(Host),
|
||||||
update_node_database(Host, ServerHost),
|
update_node_database(Host, ServerHost),
|
||||||
update_state_database(Host, ServerHost),
|
update_state_database(Host, ServerHost),
|
||||||
|
update_item_database_binary(),
|
||||||
put(server_host, ServerHost),
|
put(server_host, ServerHost),
|
||||||
init_nodes(Host, ServerHost, NodeTree, Plugins),
|
init_nodes(Host, ServerHost, NodeTree, Plugins),
|
||||||
State = #state{host = Host, server_host = ServerHost,
|
State = #state{host = Host, server_host = ServerHost,
|
||||||
|
@ -437,12 +438,93 @@ init_nodes(Host, ServerHost, _NodeTree, Plugins) ->
|
||||||
false -> ok
|
false -> ok
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
|
||||||
|
update_item_database_binary() ->
|
||||||
|
F = fun () ->
|
||||||
|
case catch mnesia:read({pubsub_last_item, mnesia:first(pubsub_last_item)}) of
|
||||||
|
[First] when is_list(First#pubsub_last_item.itemid) ->
|
||||||
|
?INFO_MSG("Binarization of pubsub items table...", []),
|
||||||
|
lists:foreach(fun (Id) ->
|
||||||
|
[Node] = mnesia:read({pubsub_last_item, Id}),
|
||||||
|
|
||||||
|
ItemId = iolist_to_binary(Node#pubsub_last_item.itemid),
|
||||||
|
|
||||||
|
ok = mnesia:delete({pubsub_last_item, Id}),
|
||||||
|
ok = mnesia:write(Node#pubsub_last_item{itemid=ItemId})
|
||||||
|
end,
|
||||||
|
mnesia:all_keys(pubsub_last_item));
|
||||||
|
_-> no_need
|
||||||
|
end
|
||||||
|
end,
|
||||||
|
case mnesia:transaction(F) of
|
||||||
|
{aborted, Reason} ->
|
||||||
|
?ERROR_MSG("Failed to binarize pubsub items table: ~p", [Reason]);
|
||||||
|
{atomic, no_need} ->
|
||||||
|
ok;
|
||||||
|
{atomic, Result} ->
|
||||||
|
?INFO_MSG("Pubsub items table has been binarized: ~p", [Result])
|
||||||
|
end.
|
||||||
|
|
||||||
|
|
||||||
|
update_node_database_binary() ->
|
||||||
|
F = fun () ->
|
||||||
|
case catch mnesia:read({pubsub_node, mnesia:first(pubsub_node)}) of
|
||||||
|
[First] when is_list(First#pubsub_node.type) ->
|
||||||
|
?INFO_MSG("Binarization of pubsub nodes table...", []),
|
||||||
|
lists:foreach(fun ({H, N}) ->
|
||||||
|
[Node] = mnesia:read({pubsub_node, {H, N}}),
|
||||||
|
|
||||||
|
Type = iolist_to_binary(Node#pubsub_node.type),
|
||||||
|
BN = case N of
|
||||||
|
Binary when is_binary(Binary) ->
|
||||||
|
N;
|
||||||
|
_ ->
|
||||||
|
{result, BN1} = node_call(Type, path_to_node, [N]),
|
||||||
|
BN1
|
||||||
|
end,
|
||||||
|
BP = case [case P of
|
||||||
|
Binary2 when is_binary(Binary2) -> P;
|
||||||
|
_ -> element(2, node_call(Type, path_to_node, [P]))
|
||||||
|
end
|
||||||
|
|| P <- Node#pubsub_node.parents] of
|
||||||
|
[<<>>] -> [];
|
||||||
|
Parents -> Parents
|
||||||
|
end,
|
||||||
|
|
||||||
|
BH = case H of
|
||||||
|
{U, S, R} -> {iolist_to_binary(U), iolist_to_binary(S), iolist_to_binary(R)};
|
||||||
|
String -> iolist_to_binary(String)
|
||||||
|
end,
|
||||||
|
|
||||||
|
Owners = [{iolist_to_binary(U), iolist_to_binary(S), iolist_to_binary(R)} ||
|
||||||
|
{U, S, R} <- Node#pubsub_node.owners],
|
||||||
|
|
||||||
|
ok = mnesia:delete({pubsub_node, {H, N}}),
|
||||||
|
ok = mnesia:write(Node#pubsub_node{nodeid = {BH, BN},
|
||||||
|
parents = BP,
|
||||||
|
type = Type,
|
||||||
|
owners = Owners});
|
||||||
|
(_) -> ok
|
||||||
|
end,
|
||||||
|
mnesia:all_keys(pubsub_node));
|
||||||
|
_-> no_need
|
||||||
|
end
|
||||||
|
end,
|
||||||
|
case mnesia:transaction(F) of
|
||||||
|
{aborted, Reason} ->
|
||||||
|
?ERROR_MSG("Failed to binarize pubsub node table: ~p", [Reason]);
|
||||||
|
{atomic, no_need} ->
|
||||||
|
ok;
|
||||||
|
{atomic, Result} ->
|
||||||
|
?INFO_MSG("Pubsub nodes table has been binarized: ~p", [Result])
|
||||||
|
end.
|
||||||
|
|
||||||
update_node_database(Host, ServerHost) ->
|
update_node_database(Host, ServerHost) ->
|
||||||
mnesia:del_table_index(pubsub_node, type),
|
mnesia:del_table_index(pubsub_node, type),
|
||||||
mnesia:del_table_index(pubsub_node, parentid),
|
mnesia:del_table_index(pubsub_node, parentid),
|
||||||
case catch mnesia:table_info(pubsub_node, attributes) of
|
case catch mnesia:table_info(pubsub_node, attributes) of
|
||||||
[host_node, host_parent, info] ->
|
[host_node, host_parent, info] ->
|
||||||
?INFO_MSG("upgrade node pubsub tables", []),
|
?INFO_MSG("Upgrading pubsub nodes table...", []),
|
||||||
F = fun () ->
|
F = fun () ->
|
||||||
{Result, LastIdx} = lists:foldl(fun ({pubsub_node,
|
{Result, LastIdx} = lists:foldl(fun ({pubsub_node,
|
||||||
NodeId, ParentId,
|
NodeId, ParentId,
|
||||||
|
@ -569,10 +651,10 @@ update_node_database(Host, ServerHost) ->
|
||||||
end,
|
end,
|
||||||
case mnesia:transaction(FNew) of
|
case mnesia:transaction(FNew) of
|
||||||
{atomic, Result} ->
|
{atomic, Result} ->
|
||||||
?INFO_MSG("Pubsub node tables updated correctly: ~p",
|
?INFO_MSG("Pubsub nodes table upgraded: ~p",
|
||||||
[Result]);
|
[Result]);
|
||||||
{aborted, Reason} ->
|
{aborted, Reason} ->
|
||||||
?ERROR_MSG("Problem updating Pubsub node tables:~n~p",
|
?ERROR_MSG("Problem upgrading Pubsub nodes table:~n~p",
|
||||||
[Reason])
|
[Reason])
|
||||||
end;
|
end;
|
||||||
[nodeid, parentid, type, owners, options] ->
|
[nodeid, parentid, type, owners, options] ->
|
||||||
|
@ -673,10 +755,10 @@ update_node_database(Host, ServerHost) ->
|
||||||
case mnesia:transaction(FNew) of
|
case mnesia:transaction(FNew) of
|
||||||
{atomic, Result} ->
|
{atomic, Result} ->
|
||||||
rename_default_nodeplugin(),
|
rename_default_nodeplugin(),
|
||||||
?INFO_MSG("Pubsub node tables updated correctly: ~p",
|
?INFO_MSG("Pubsub nodes table upgraded: ~p",
|
||||||
[Result]);
|
[Result]);
|
||||||
{aborted, Reason} ->
|
{aborted, Reason} ->
|
||||||
?ERROR_MSG("Problem updating Pubsub node tables:~n~p",
|
?ERROR_MSG("Problem upgrading Pubsub nodes table:~n~p",
|
||||||
[Reason])
|
[Reason])
|
||||||
end;
|
end;
|
||||||
[nodeid, id, parent, type, owners, options] ->
|
[nodeid, id, parent, type, owners, options] ->
|
||||||
|
@ -691,48 +773,7 @@ update_node_database(Host, ServerHost) ->
|
||||||
rename_default_nodeplugin();
|
rename_default_nodeplugin();
|
||||||
_ -> ok
|
_ -> ok
|
||||||
end,
|
end,
|
||||||
mnesia:transaction(fun () ->
|
update_node_database_binary().
|
||||||
case catch mnesia:first(pubsub_node) of
|
|
||||||
{_, L} when is_list(L) ->
|
|
||||||
lists:foreach(fun ({H, N})
|
|
||||||
when is_list(N) ->
|
|
||||||
[Node] =
|
|
||||||
mnesia:read({pubsub_node,
|
|
||||||
{H,
|
|
||||||
N}}),
|
|
||||||
Type =
|
|
||||||
Node#pubsub_node.type,
|
|
||||||
BN = element(2,
|
|
||||||
node_call(Type,
|
|
||||||
path_to_node,
|
|
||||||
[N])),
|
|
||||||
BP = case [element(2,
|
|
||||||
node_call(Type,
|
|
||||||
path_to_node,
|
|
||||||
[P]))
|
|
||||||
|| P
|
|
||||||
<- Node#pubsub_node.parents]
|
|
||||||
of
|
|
||||||
[<<>>] -> [];
|
|
||||||
Parents ->
|
|
||||||
Parents
|
|
||||||
end,
|
|
||||||
mnesia:write(Node#pubsub_node{nodeid
|
|
||||||
=
|
|
||||||
{H,
|
|
||||||
BN},
|
|
||||||
parents
|
|
||||||
=
|
|
||||||
BP}),
|
|
||||||
mnesia:delete({pubsub_node,
|
|
||||||
{H,
|
|
||||||
N}});
|
|
||||||
(_) -> ok
|
|
||||||
end,
|
|
||||||
mnesia:all_keys(pubsub_node));
|
|
||||||
_ -> ok
|
|
||||||
end
|
|
||||||
end).
|
|
||||||
|
|
||||||
rename_default_nodeplugin() ->
|
rename_default_nodeplugin() ->
|
||||||
lists:foreach(fun (Node) ->
|
lists:foreach(fun (Node) ->
|
||||||
|
@ -745,12 +786,15 @@ rename_default_nodeplugin() ->
|
||||||
|
|
||||||
update_state_database(_Host, _ServerHost) ->
|
update_state_database(_Host, _ServerHost) ->
|
||||||
case catch mnesia:table_info(pubsub_state, attributes) of
|
case catch mnesia:table_info(pubsub_state, attributes) of
|
||||||
[stateid, items, affiliation, subscription] ->
|
[stateid, nodeidx, items, affiliation, subscriptions] ->
|
||||||
?INFO_MSG("upgrade state pubsub tables", []),
|
?INFO_MSG("Upgrading pubsub states table...", []),
|
||||||
F = fun ({pubsub_state, {JID, NodeID}, Items, Aff, Sub}, Acc) ->
|
F = fun ({pubsub_state, {{U,S,R}, NodeID}, _NodeIdx, Items, Aff, Sub}, Acc) ->
|
||||||
|
JID = {iolist_to_binary(U), iolist_to_binary(S), iolist_to_binary(R)},
|
||||||
Subs = case Sub of
|
Subs = case Sub of
|
||||||
none ->
|
none ->
|
||||||
[];
|
[];
|
||||||
|
[] ->
|
||||||
|
[];
|
||||||
_ ->
|
_ ->
|
||||||
{result, SubID} = pubsub_subscription:subscribe_node(JID, NodeID, []),
|
{result, SubID} = pubsub_subscription:subscribe_node(JID, NodeID, []),
|
||||||
[{Sub, SubID}]
|
[{Sub, SubID}]
|
||||||
|
@ -772,10 +816,10 @@ update_state_database(_Host, _ServerHost) ->
|
||||||
end,
|
end,
|
||||||
case mnesia:transaction(FNew) of
|
case mnesia:transaction(FNew) of
|
||||||
{atomic, Result} ->
|
{atomic, Result} ->
|
||||||
?INFO_MSG("Pubsub state tables updated correctly: ~p",
|
?INFO_MSG("Pubsub states table upgraded: ~p",
|
||||||
[Result]);
|
[Result]);
|
||||||
{aborted, Reason} ->
|
{aborted, Reason} ->
|
||||||
?ERROR_MSG("Problem updating Pubsub state tables:~n~p",
|
?ERROR_MSG("Problem upgrading Pubsub states table:~n~p",
|
||||||
[Reason])
|
[Reason])
|
||||||
end;
|
end;
|
||||||
_ ->
|
_ ->
|
||||||
|
@ -4353,7 +4397,7 @@ broadcast_purge_node(Host, Node, NodeId, Type, NodeOptions) ->
|
||||||
broadcast_stanza(Host, Node, NodeId, Type,
|
broadcast_stanza(Host, Node, NodeId, Type,
|
||||||
NodeOptions, SubsByDepth, nodes, Stanza, false),
|
NodeOptions, SubsByDepth, nodes, Stanza, false),
|
||||||
{result, true};
|
{result, true};
|
||||||
_ ->
|
_ ->
|
||||||
{result, false}
|
{result, false}
|
||||||
end;
|
end;
|
||||||
_ ->
|
_ ->
|
||||||
|
@ -5500,4 +5544,3 @@ parse_subscriptions([{State, Item}]) ->
|
||||||
subscribed -> "s"
|
subscribed -> "s"
|
||||||
end,
|
end,
|
||||||
string:join([STATE, Item],":").
|
string:join([STATE, Item],":").
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user