25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-12-24 17:29:28 +01:00

Improve handling of mnesia schema

This commit is contained in:
Christophe Romain 2016-11-30 11:09:17 +01:00
parent 95a4b1b266
commit 92db9ff105
41 changed files with 222 additions and 53 deletions

View File

@ -76,11 +76,11 @@
-export_type([acl/0]).
start() ->
mnesia:create_table(acl,
ejabberd_mnesia:create(?MODULE, acl,
[{ram_copies, [node()]}, {type, bag},
{local_content, true},
{attributes, record_info(fields, acl)}]),
mnesia:create_table(access,
ejabberd_mnesia:create(?MODULE, access,
[{ram_copies, [node()]},
{local_content, true},
{attributes, record_info(fields, access)}]),

View File

@ -59,7 +59,7 @@
start(Host) ->
%% TODO: Check cluster mode
mnesia:create_table(anonymous, [{ram_copies, [node()]},
ejabberd_mnesia:create(?MODULE, anonymous, [{ram_copies, [node()]},
{type, bag},
{attributes, record_info(fields, anonymous)}]),
%% The hooks are needed to add / remove users from the anonymous tables

View File

@ -66,10 +66,10 @@ start(Host) ->
ok.
init_db() ->
mnesia:create_table(passwd,
ejabberd_mnesia:create(?MODULE, passwd,
[{disc_copies, [node()]},
{attributes, record_info(fields, passwd)}]),
mnesia:create_table(reg_users_counter,
ejabberd_mnesia:create(?MODULE, reg_users_counter,
[{ram_copies, [node()]},
{attributes, record_info(fields, reg_users_counter)}]).

View File

@ -281,7 +281,7 @@ init() ->
record_info(fields, ejabberd_commands))
catch exit:{aborted, {no_exists, _}} -> ok
end,
mnesia:create_table(ejabberd_commands,
ejabberd_mnesia:create(?MODULE, ejabberd_commands,
[{ram_copies, [node()]},
{local_content, true},
{attributes, record_info(fields, ejabberd_commands)},

View File

@ -104,7 +104,7 @@ mnesia_init() ->
_ ->
ok
end,
mnesia:create_table(local_config,
ejabberd_mnesia:create(?MODULE, local_config,
[{ram_copies, [node()]},
{local_content, true},
{attributes, record_info(fields, local_config)}]),
@ -1300,7 +1300,7 @@ convert_table_to_binary(Tab, Fields, Type, DetectFun, ConvertFun) ->
?INFO_MSG("Converting '~s' table from strings to binaries.", [Tab]),
TmpTab = list_to_atom(atom_to_list(Tab) ++ "_tmp_table"),
catch mnesia:delete_table(TmpTab),
case mnesia:create_table(TmpTab,
case ejabberd_mnesia:create(?MODULE, TmpTab,
[{disc_only_copies, [node()]},
{type, Type},
{local_content, true},

View File

@ -205,7 +205,7 @@ init([]) ->
?MYHOSTS),
catch ets:new(?IQTABLE, [named_table, public]),
update_table(),
mnesia:create_table(iq_response,
ejabberd_mnesia:create(?MODULE, iq_response,
[{ram_copies, [node()]},
{attributes, record_info(fields, iq_response)}]),
mnesia:add_table_copy(iq_response, node(), ram_copies),

169
src/ejabberd_mnesia.erl Normal file
View File

@ -0,0 +1,169 @@
%%%----------------------------------------------------------------------
%%% File : mnesia_mnesia.erl
%%% Author : Christophe Romain <christophe.romain@process-one.net>
%%% Purpose : Handle configurable mnesia schema
%%% Created : 17 Nov 2016 by Christophe Romain <christophe.romain@process-one.net>
%%%
%%%
%%% ejabberd, Copyright (C) 2002-2016 ProcessOne
%%%
%%% This program is free software; you can redistribute it and/or
%%% modify it under the terms of the GNU General Public License as
%%% published by the Free Software Foundation; either version 2 of the
%%% License, or (at your option) any later version.
%%%
%%% This program is distributed in the hope that it will be useful,
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
%%% General Public License for more details.
%%%
%%% You should have received a copy of the GNU General Public License along
%%% with this program; if not, write to the Free Software Foundation, Inc.,
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
%%%
%%%----------------------------------------------------------------------
%%% This module should be used everywhere ejabberd creates a mnesia table
%%% to make the schema customizable without code change
%%% Just apply this change in ejabberd modules
%%% s/ejabberd_mnesia:create(?MODULE, /ejabberd_mnesia:create(?MODULE, /
-module(ejabberd_mnesia).
-author('christophe.romain@process-one.net').
-export([create/3, reset/2, update/2]).
-define(STORAGE_TYPES, [disc_copies, disc_only_copies, ram_copies]).
-define(NEED_RESET, [local_content, type]).
create(Module, Name, TabDef) ->
Schema = schema(Module, Name, TabDef),
{attributes, Attrs} = lists:keyfind(attributes, 1, Schema),
case catch mnesia:table_info(Name, attributes) of
{'EXIT', _} ->
ejabberd_mnesia:create(?MODULE, Name, Schema);
Attrs ->
case need_reset(TabDef, Schema) of
true -> reset(Name, Schema);
false -> update(Name, Schema)
end;
OldAttrs ->
Fun = case lists:member({transform,1}, Module:module_info(exports)) of
true -> fun(Old) -> Module:transform(Old) end;
false -> fun(Old) -> transform(OldAttrs, Attrs, Old) end
end,
mnesia:transform_table(Name, Fun, Attrs)
end.
reset(Name, TabDef) ->
mnesia:delete_table(Name),
ejabberd_mnesia:create(?MODULE, Name, TabDef).
update(Name, TabDef) ->
Storage = mnesia:table_info(Name, storage_type),
NewStorage = lists:foldl(
fun({Key, _}, Acc) ->
case lists:member(Key, ?STORAGE_TYPES) of
true -> Key;
false -> Acc
end
end, Storage, TabDef),
R1 = if Storage=/=NewStorage ->
mnesia:change_table_copy_type(Name, node(), NewStorage);
true ->
{atomic, ok}
end,
Indexes = mnesia:table_info(Name, index),
NewIndexes = proplists:get_value(index, TabDef, []),
[mnesia:del_table_index(Name, Attr)
|| Attr <- Indexes--NewIndexes],
R2 = [mnesia:add_table_index(Name, Attr)
|| Attr <- NewIndexes--Indexes],
lists:foldl(
fun({atomic, ok}, Acc) -> Acc;
(Error, _Acc) -> Error
end, {atomic, ok}, [R1|R2]).
%
% utilities
%
schema(Module, Name, TabDef) ->
case parse(Module) of
{ok, CustomDefs} ->
case lists:keyfind(Name, 1, CustomDefs) of
{Name, CustomDef} -> merge(TabDef, CustomDef);
_ -> TabDef
end;
_ ->
TabDef
end.
merge(TabDef, CustomDef) ->
{CustomKeys, _} = lists:unzip(CustomDef),
CleanDef = lists:foldl(
fun(Elem, Acc) ->
case lists:member(Elem, ?STORAGE_TYPES) of
true ->
lists:foldl(
fun(Key, CleanAcc) ->
lists:keydelete(Key, 1, CleanAcc)
end, Acc, ?STORAGE_TYPES);
false ->
Acc
end
end, TabDef, CustomKeys),
lists:ukeymerge(1,
lists:ukeysort(1, CustomDef),
lists:ukeysort(1, CleanDef)).
parse(Module) ->
Path = case os:getenv("EJABBERD_SCHEMA_PATH") of
false ->
case code:priv_dir(ejabberd) of
{error, _} -> "schema"; % $SPOOL_DIR/schema
Priv -> filename:join(Priv, "schema")
end;
CustomDir ->
CustomDir
end,
File = filename:join(Path, atom_to_list(Module)++".mnesia"),
case file:consult(File) of
{ok, Terms} -> parse(Terms, []);
Error -> Error
end.
parse([], Acc) ->
{ok, lists:reverse(Acc)};
parse([{Name, Storage, TabDef}|Tail], Acc)
when is_atom(Name),
is_atom(Storage),
is_list(TabDef) ->
NewDef = case lists:member(Storage, ?STORAGE_TYPES) of
true -> [{Storage, [node()]} | TabDef];
false -> TabDef
end,
parse(Tail, [{Name, NewDef} | Acc]);
parse([Other|_], _) ->
{error, {invalid, Other}}.
need_reset(FromDef, ToDef) ->
ValuesF = [lists:keyfind(Key, 1, FromDef) || Key <- ?NEED_RESET],
ValuesT = [lists:keyfind(Key, 1, ToDef) || Key <- ?NEED_RESET],
lists:foldl(
fun({Val, Val}, Acc) -> Acc;
({_, false}, Acc) -> Acc;
({_, _}, _) -> true
end, false, lists:zip(ValuesF, ValuesT)).
transform(OldAttrs, Attrs, Old) ->
[Name|OldValues] = tuple_to_list(Old),
Before = lists:zip(OldAttrs, OldValues),
After = lists:foldl(
fun(Attr, Acc) ->
case lists:keyfind(Attr, 1, Before) of
false -> [{Attr, undefined}|Acc];
Value -> [Value|Acc]
end
end, [], lists:reverse(Attrs)),
{Attrs, NewRecord} = lists:unzip(After),
list_to_tuple([Name|NewRecord]).

View File

@ -34,7 +34,7 @@
-include("ejabberd_oauth.hrl").
init() ->
mnesia:create_table(oauth_token,
ejabberd_mnesia:create(?MODULE, oauth_token,
[{disc_copies, [node()]},
{attributes,
record_info(fields, oauth_token)}]),

View File

@ -280,7 +280,7 @@ process_iq(From, To, El) ->
%%--------------------------------------------------------------------
init([]) ->
update_tables(),
mnesia:create_table(route,
ejabberd_mnesia:create(?MODULE, route,
[{ram_copies, [node()]},
{type, bag},
{attributes, record_info(fields, route)}]),

View File

@ -115,7 +115,7 @@ unregister_route(Domain) ->
%% Description: Initiates the server
%%--------------------------------------------------------------------
init([]) ->
mnesia:create_table(route_multicast,
ejabberd_mnesia:create(?MODULE, route_multicast,
[{ram_copies, [node()]},
{type, bag},
{attributes,

View File

@ -236,14 +236,14 @@ make_key({From, To}, StreamID) ->
init([]) ->
update_tables(),
mnesia:create_table(s2s,
ejabberd_mnesia:create(?MODULE, s2s,
[{ram_copies, [node()]},
{type, bag},
{attributes, record_info(fields, s2s)}]),
mnesia:add_table_copy(s2s, node(), ram_copies),
mnesia:subscribe(system),
ejabberd_commands:register_commands(get_commands_spec()),
mnesia:create_table(temporarily_blocked,
ejabberd_mnesia:create(?MODULE, temporarily_blocked,
[{ram_copies, [node()]},
{attributes, record_info(fields, temporarily_blocked)}]),
{ok, #state{}}.

View File

@ -80,10 +80,10 @@ get_sessions(LUser, LServer, LResource) ->
%%%===================================================================
init([]) ->
update_tables(),
mnesia:create_table(session,
ejabberd_mnesia:create(?MODULE, session,
[{ram_copies, [node()]},
{attributes, record_info(fields, session)}]),
mnesia:create_table(session_counter,
ejabberd_mnesia:create(?MODULE, session_counter,
[{ram_copies, [node()]},
{attributes, record_info(fields, session_counter)}]),
mnesia:add_table_index(session, usr),

View File

@ -49,7 +49,7 @@
-record(sql_pool, {host, pid}).
start_link(Host) ->
mnesia:create_table(sql_pool,
ejabberd_mnesia:create(?MODULE, sql_pool,
[{ram_copies, [node()]}, {type, bag},
{local_content, true},
{attributes, record_info(fields, sql_pool)}]),

View File

@ -21,11 +21,11 @@
%%% API
%%%===================================================================
init(_Host, _Opts) ->
mnesia:create_table(motd,
ejabberd_mnesia:create(?MODULE, motd,
[{disc_copies, [node()]},
{attributes,
record_info(fields, motd)}]),
mnesia:create_table(motd_users,
ejabberd_mnesia:create(?MODULE, motd_users,
[{disc_copies, [node()]},
{attributes,
record_info(fields, motd_users)}]),

View File

@ -240,7 +240,7 @@ setup_database() ->
_ ->
ok
end,
mnesia:create_table(bosh,
ejabberd_mnesia:create(?MODULE, bosh,
[{ram_copies, [node()]}, {local_content, true},
{attributes, record_info(fields, bosh)}]),
mnesia:add_table_copy(bosh, node(), ram_copies).

View File

@ -27,7 +27,7 @@ init(_Host, _Opts) ->
_ ->
mnesia:delete_table(caps_features)
end,
mnesia:create_table(caps_features,
ejabberd_mnesia:create(?MODULE, caps_features,
[{disc_only_copies, [node()]},
{local_content, true},
{attributes,

View File

@ -30,7 +30,7 @@ init(_Host, _Opts) ->
%% probably table don't exist
ok
end,
mnesia:create_table(carboncopy,
ejabberd_mnesia:create(?MODULE, carboncopy,
[{ram_copies, [node()]},
{attributes, record_info(fields, carboncopy)},
{type, bag}]),

View File

@ -89,7 +89,7 @@ stop(_Host) ->
setup_database() ->
migrate_database(),
mnesia:create_table(http_bind,
ejabberd_mnesia:create(?MODULE, http_bind,
[{ram_copies, [node()]},
{attributes, record_info(fields, http_bind)}]).

View File

@ -21,7 +21,7 @@
%%% API
%%%===================================================================
init(_Host, _Opts) ->
mnesia:create_table(irc_custom,
ejabberd_mnesia:create(?MODULE, irc_custom,
[{disc_copies, [node()]},
{attributes, record_info(fields, irc_custom)}]),
update_table().

View File

@ -19,7 +19,7 @@
%%% API
%%%===================================================================
init(_Host, _Opts) ->
mnesia:create_table(last_activity,
ejabberd_mnesia:create(?MODULE, last_activity,
[{disc_copies, [node()]},
{attributes,
record_info(fields, last_activity)}]),

View File

@ -32,11 +32,11 @@
%%% API
%%%===================================================================
init(_Host, _Opts) ->
mnesia:create_table(archive_msg,
ejabberd_mnesia:create(?MODULE, archive_msg,
[{disc_only_copies, [node()]},
{type, bag},
{attributes, record_info(fields, archive_msg)}]),
mnesia:create_table(archive_prefs,
ejabberd_mnesia:create(?MODULE, archive_prefs,
[{disc_only_copies, [node()]},
{attributes, record_info(fields, archive_prefs)}]).

View File

@ -177,7 +177,7 @@ init([Host, Opts]) ->
Mod = gen_mod:db_mod(Host, Opts, ?MODULE),
Mod:init(Host, [{host, MyHost}|Opts]),
update_tables(),
mnesia:create_table(muc_online_room,
ejabberd_mnesia:create(?MODULE, muc_online_room,
[{ram_copies, [node()]},
{type, ordered_set},
{attributes, record_info(fields, muc_online_room)}]),

View File

@ -26,11 +26,11 @@
%%%===================================================================
init(_Host, Opts) ->
MyHost = proplists:get_value(host, Opts),
mnesia:create_table(muc_room,
ejabberd_mnesia:create(?MODULE, muc_room,
[{disc_copies, [node()]},
{attributes,
record_info(fields, muc_room)}]),
mnesia:create_table(muc_registered,
ejabberd_mnesia:create(?MODULE, muc_registered,
[{disc_copies, [node()]},
{attributes,
record_info(fields, muc_registered)}]),

View File

@ -840,7 +840,7 @@ received_awaiter(JID, Waiter, LServiceS) ->
%%%-------------------------
create_cache() ->
mnesia:create_table(multicastc,
ejabberd_mnesia:create(?MODULE, multicastc,
[{ram_copies, [node()]},
{attributes, record_info(fields, multicastc)}]).

View File

@ -25,7 +25,7 @@
%%% API
%%%===================================================================
init(_Host, _Opts) ->
mnesia:create_table(offline_msg,
ejabberd_mnesia:create(?MODULE, offline_msg,
[{disc_only_copies, [node()]}, {type, bag},
{attributes, record_info(fields, offline_msg)}]),
update_table().

View File

@ -25,7 +25,7 @@
%%% API
%%%===================================================================
init(_Host, _Opts) ->
mnesia:create_table(privacy,
ejabberd_mnesia:create(?MODULE, privacy,
[{disc_copies, [node()]},
{attributes, record_info(fields, privacy)}]),
update_table().

View File

@ -21,7 +21,7 @@
%%% API
%%%===================================================================
init(_Host, _Opts) ->
mnesia:create_table(private_storage,
ejabberd_mnesia:create(?MODULE, private_storage,
[{disc_only_copies, [node()]},
{attributes,
record_info(fields, private_storage)}]),

View File

@ -62,7 +62,7 @@ start_link(Host, Opts) ->
[]).
init([Opts]) ->
mnesia:create_table(bytestream,
ejabberd_mnesia:create(?MODULE, bytestream,
[{ram_copies, [node()]},
{attributes, record_info(fields, bytestream)}]),
mnesia:add_table_copy(bytestream, node(), ram_copies),

View File

@ -268,7 +268,7 @@ init([ServerHost, Opts]) ->
BaseOptions = DefaultModule:options(),
DefaultNodeCfg = gen_mod:get_opt(default_node_config, Opts,
fun(A) when is_list(A) -> filter_node_options(A, BaseOptions) end, []),
mnesia:create_table(pubsub_last_item,
ejabberd_mnesia:create(?MODULE, pubsub_last_item,
[{ram_copies, [node()]},
{attributes, record_info(fields, pubsub_last_item)}]),
mod_disco:register_feature(ServerHost, ?NS_PUBSUB),

View File

@ -54,7 +54,7 @@ start(Host, Opts) ->
stream_feature_register, 50),
ejabberd_hooks:add(c2s_unauthenticated_iq, Host,
?MODULE, unauthenticated_iq_register, 50),
mnesia:create_table(mod_register_ip,
ejabberd_mnesia:create(?MODULE, mod_register_ip,
[{ram_copies, [node()]}, {local_content, true},
{attributes, [key, value]}]),
mnesia:add_table_copy(mod_register_ip, node(),

View File

@ -24,10 +24,10 @@
%%% API
%%%===================================================================
init(_Host, _Opts) ->
mnesia:create_table(roster,
ejabberd_mnesia:create(?MODULE, roster,
[{disc_copies, [node()]},
{attributes, record_info(fields, roster)}]),
mnesia:create_table(roster_version,
ejabberd_mnesia:create(?MODULE, roster_version,
[{disc_copies, [node()]},
{attributes,
record_info(fields, roster_version)}]),

View File

@ -26,10 +26,10 @@
%%% API
%%%===================================================================
init(_Host, _Opts) ->
mnesia:create_table(sr_group,
ejabberd_mnesia:create(?MODULE, sr_group,
[{disc_copies, [node()]},
{attributes, record_info(fields, sr_group)}]),
mnesia:create_table(sr_user,
ejabberd_mnesia:create(?MODULE, sr_user,
[{disc_copies, [node()]}, {type, bag},
{attributes, record_info(fields, sr_user)}]),
update_tables(),

View File

@ -179,7 +179,7 @@ ping(SIPSocket) ->
%%%===================================================================
init([]) ->
update_table(),
mnesia:create_table(sip_session,
ejabberd_mnesia:create(?MODULE, sip_session,
[{ram_copies, [node()]},
{type, bag},
{attributes, record_info(fields, sip_session)}]),

View File

@ -24,10 +24,10 @@
%%% API
%%%===================================================================
init(_Host, _Opts) ->
mnesia:create_table(vcard,
ejabberd_mnesia:create(?MODULE, vcard,
[{disc_only_copies, [node()]},
{attributes, record_info(fields, vcard)}]),
mnesia:create_table(vcard_search,
ejabberd_mnesia:create(?MODULE, vcard_search,
[{disc_copies, [node()]},
{attributes,
record_info(fields, vcard_search)}]),

View File

@ -19,7 +19,7 @@
%%% API
%%%===================================================================
init(_Host, _Opts) ->
mnesia:create_table(vcard_xupdate,
ejabberd_mnesia:create(?MODULE, vcard_xupdate,
[{disc_copies, [node()]},
{attributes,
record_info(fields, vcard_xupdate)}]),

View File

@ -51,11 +51,11 @@
init(_Host, _ServerHost, _Opts) ->
%pubsub_subscription:init(Host, ServerHost, Opts),
mnesia:create_table(pubsub_state,
ejabberd_mnesia:create(?MODULE, pubsub_state,
[{disc_copies, [node()]},
{type, ordered_set},
{attributes, record_info(fields, pubsub_state)}]),
mnesia:create_table(pubsub_item,
ejabberd_mnesia:create(?MODULE, pubsub_item,
[{disc_only_copies, [node()]},
{attributes, record_info(fields, pubsub_item)}]),
ItemsFields = record_info(fields, pubsub_item),

View File

@ -49,7 +49,7 @@
delete_node/2]).
init(_Host, _ServerHost, _Options) ->
mnesia:create_table(pubsub_node,
ejabberd_mnesia:create(?MODULE, pubsub_node,
[{disc_copies, [node()]},
{attributes, record_info(fields, pubsub_node)}]),
mnesia:add_table_index(pubsub_node, id),

View File

@ -34,7 +34,7 @@
-export([init/3, new/1, free/2]).
init(_Host, _ServerHost, _Opts) ->
mnesia:create_table(pubsub_index,
ejabberd_mnesia:create(?MODULE, pubsub_index,
[{disc_copies, [node()]},
{attributes, record_info(fields, pubsub_index)}]).

View File

@ -253,7 +253,7 @@ update_node_database(Host, ServerHost) ->
end,
{atomic, NewRecords} = mnesia:transaction(F),
{atomic, ok} = mnesia:delete_table(pubsub_node),
{atomic, ok} = mnesia:create_table(pubsub_node,
{atomic, ok} = ejabberd_mnesia:create(?MODULE, pubsub_node,
[{disc_copies, [node()]},
{attributes,
record_info(fields,
@ -421,7 +421,7 @@ update_state_database(_Host, _ServerHost) ->
{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,
{atomic, ok} = ejabberd_mnesia:create(?MODULE, pubsub_state,
[{disc_copies, [node()]},
{attributes, record_info(fields, pubsub_state)}]),
FNew = fun () ->

View File

@ -126,7 +126,7 @@ parse_options_xform(XFields) ->
%% Internal functions
%%====================================================================
create_table() ->
case mnesia:create_table(pubsub_subscription,
case ejabberd_mnesia:create(?MODULE, pubsub_subscription,
[{disc_copies, [node()]},
{attributes,
record_info(fields, pubsub_subscription)},

View File

@ -50,7 +50,7 @@
-spec start() -> ok.
start() ->
mnesia:create_table(shaper,
ejabberd_mnesia:create(?MODULE, shaper,
[{ram_copies, [node()]},
{local_content, true},
{attributes, record_info(fields, shaper)}]),