From 3785b3e951a7d52b93dd1020d7450c0d133f62eb Mon Sep 17 00:00:00 2001 From: Christophe Romain Date: Mon, 16 May 2011 17:19:42 +0200 Subject: [PATCH] add index migration code --- src/mod_pubsub/mod_pubsub.erl | 49 +++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/src/mod_pubsub/mod_pubsub.erl b/src/mod_pubsub/mod_pubsub.erl index 42f8d7a79..42c0efadf 100644 --- a/src/mod_pubsub/mod_pubsub.erl +++ b/src/mod_pubsub/mod_pubsub.erl @@ -479,6 +479,55 @@ update_state_database(_Host, _ServerHost) -> ?ERROR_MSG("Problem updating Pubsub state tables:~n~p", [Reason]) end; + [stateid, items, affiliation, subscriptions] -> + ?INFO_MSG("upgrade state pubsub table", []), + F = fun ({pubsub_state, {JID, Nidx}, Items, Aff, Subs}, Acc) -> + NewState = #pubsub_state{stateid = {JID, Nidx}, + nodeidx = Nidx, + items = Items, + affiliation = Aff, + subscriptions = Subs}, + [NewState | Acc] + end, + {atomic, NewRecs} = mnesia:transaction(fun mnesia:foldl/3, + [F, [], pubsub_state]), + {atomic, ok} = mnesia:delete_table(pubsub_state), + {atomic, ok} = mnesia:create_table(pubsub_state, + [{disc_copies, [node()]}, + {attributes, record_info(fields, pubsub_state)}]), + FNew = fun () -> + lists:foreach(fun mnesia:write/1, NewRecs) + end, + case mnesia:transaction(FNew) of + {atomic, Res1} -> + ?INFO_MSG("Pubsub state tables updated correctly: ~p", [Res1]); + {aborted, Rea1} -> + ?ERROR_MSG("Problem updating Pubsub state table:~n~p", [Rea1]) + end, + ?INFO_MSG("upgrade item pubsub table", []), + F = fun ({pubsub_item, {ItemId, Nidx}, C, M, P}, Acc) -> + NewItem = #pubsub_item{itemid = {ItemId, Nidx}, + nodeidx = Nidx, + creation = C, + modification = M, + payload = P}, + [NewItem | Acc] + end, + {atomic, NewRecs} = mnesia:transaction(fun mnesia:foldl/3, + [F, [], pubsub_item]), + {atomic, ok} = mnesia:delete_table(pubsub_item), + {atomic, ok} = mnesia:create_table(pubsub_item, + [{disc_copies, [node()]}, + {attributes, record_info(fields, pubsub_item)}]), + FNew = fun () -> + lists:foreach(fun mnesia:write/1, NewRecs) + end, + case mnesia:transaction(FNew) of + {atomic, Res2} -> + ?INFO_MSG("Pubsub item tables updated correctly: ~p", [Res2]); + {aborted, Rea2} -> + ?ERROR_MSG("Problem updating Pubsub item table:~n~p", [Rea2]) + end; _ -> ok end.