-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)}]),
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
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)}]).
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)},
_ ->
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)}]),
?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},
?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),
--- /dev/null
+%%%----------------------------------------------------------------------
+%%% 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]).
-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)}]),
%%--------------------------------------------------------------------
init([]) ->
update_tables(),
- mnesia:create_table(route,
+ ejabberd_mnesia:create(?MODULE, route,
[{ram_copies, [node()]},
{type, bag},
{attributes, record_info(fields, route)}]),
%% Description: Initiates the server
%%--------------------------------------------------------------------
init([]) ->
- mnesia:create_table(route_multicast,
+ ejabberd_mnesia:create(?MODULE, route_multicast,
[{ram_copies, [node()]},
{type, bag},
{attributes,
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{}}.
%%%===================================================================
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),
-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)}]),
%%% 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)}]),
_ ->
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).
_ ->
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,
%% 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}]),
setup_database() ->
migrate_database(),
- mnesia:create_table(http_bind,
+ ejabberd_mnesia:create(?MODULE, http_bind,
[{ram_copies, [node()]},
{attributes, record_info(fields, http_bind)}]).
%%% 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().
%%% API
%%%===================================================================
init(_Host, _Opts) ->
- mnesia:create_table(last_activity,
+ ejabberd_mnesia:create(?MODULE, last_activity,
[{disc_copies, [node()]},
{attributes,
record_info(fields, last_activity)}]),
%%% 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)}]).
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)}]),
%%%===================================================================
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)}]),
%%%-------------------------
create_cache() ->
- mnesia:create_table(multicastc,
+ ejabberd_mnesia:create(?MODULE, multicastc,
[{ram_copies, [node()]},
{attributes, record_info(fields, multicastc)}]).
%%% 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().
%%% API
%%%===================================================================
init(_Host, _Opts) ->
- mnesia:create_table(privacy,
+ ejabberd_mnesia:create(?MODULE, privacy,
[{disc_copies, [node()]},
{attributes, record_info(fields, privacy)}]),
update_table().
%%% 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)}]),
[]).
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),
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),
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(),
%%% 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)}]),
%%% 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(),
%%%===================================================================
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)}]),
%%% 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)}]),
%%% API
%%%===================================================================
init(_Host, _Opts) ->
- mnesia:create_table(vcard_xupdate,
+ ejabberd_mnesia:create(?MODULE, vcard_xupdate,
[{disc_copies, [node()]},
{attributes,
record_info(fields, vcard_xupdate)}]),
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),
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),
-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)}]).
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,
{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 () ->
%% 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)},
-spec start() -> ok.
start() ->
- mnesia:create_table(shaper,
+ ejabberd_mnesia:create(?MODULE, shaper,
[{ram_copies, [node()]},
{local_content, true},
{attributes, record_info(fields, shaper)}]),