Skip to content

Commit

Permalink
Fix query that creates mam pm delete domain statements
Browse files Browse the repository at this point in the history
  • Loading branch information
NelsonVides committed Jan 9, 2023
1 parent 2911371 commit 9feb307
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 23 deletions.
53 changes: 38 additions & 15 deletions src/mam/mod_mam_rdbms_arch.erl
Original file line number Diff line number Diff line change
Expand Up @@ -132,20 +132,7 @@ register_prepared_queries(Opts) ->
"WHERE user_id = ?">>),

%% Domain Removal
{MaybeLimitSQL, MaybeLimitMSSQL} = mod_mam_utils:batch_delete_limits(Opts),
IdTable = <<"(SELECT ", MaybeLimitMSSQL/binary,
" id from mam_server_user WHERE server = ? ", MaybeLimitSQL/binary, ")">>,
ServerTable = <<"(SELECT * FROM (SELECT", MaybeLimitMSSQL/binary,
" server FROM mam_server_user WHERE server = ? ", MaybeLimitSQL/binary, ") as t)">>,
mongoose_rdbms:prepare(mam_remove_domain, mam_message, ['mam_server_user.server'],
<<"DELETE FROM mam_message "
"WHERE user_id IN ", IdTable/binary>>),
mongoose_rdbms:prepare(mam_remove_domain_prefs, mam_config, ['mam_server_user.server'],
<<"DELETE FROM mam_config "
"WHERE user_id IN ", IdTable/binary>>),
mongoose_rdbms:prepare(mam_remove_domain_users, mam_server_user, [server],
<<"DELETE ", MaybeLimitMSSQL/binary,
" FROM mam_server_user WHERE server IN ", ServerTable/binary>>),
prepare_remove_domain(Opts),

mongoose_rdbms:prepare(mam_make_tombstone, mam_message, [message, user_id, id],
<<"UPDATE mam_message SET message = ?, search_body = '' "
Expand All @@ -166,6 +153,42 @@ register_prepared_queries(Opts) ->
" AND id = ? AND direction = ?"
" ORDER BY id DESC ", LimitSQL/binary>>).

prepare_remove_domain(#{delete_domain_limit := infinity}) ->
mongoose_rdbms:prepare(mam_remove_domain, mam_message, ['mam_server_user.server'],
<<"DELETE FROM mam_message "
"WHERE user_id IN "
"(SELECT id from mam_server_user WHERE server = ?)">>),
mongoose_rdbms:prepare(mam_remove_domain_prefs, mam_config, ['mam_server_user.server'],
<<"DELETE FROM mam_config "
"WHERE user_id IN "
"(SELECT id from mam_server_user WHERE server = ?)">>),
mongoose_rdbms:prepare(mam_remove_domain_users, mam_server_user, [server],
<<"DELETE FROM mam_server_user WHERE server = ?">>);
prepare_remove_domain(#{delete_domain_limit := Limit}) ->
{MaybeLimitSQL, MaybeLimitMSSQL} = rdbms_queries:get_db_specific_limits_binaries(Limit),
IdTable = <<"(SELECT * FROM (SELECT ", MaybeLimitMSSQL/binary,
" msg.user_id, msg.id FROM mam_message msg",
" INNER JOIN mam_server_user msu ON msu.id=msg.user_id",
" WHERE msu.server = ?", MaybeLimitSQL/binary, ") AS T)">>,
mongoose_rdbms:prepare(mam_incr_remove_domain, mam_message, ['mam_server_user.server'],
<<"DELETE FROM mam_message "
"WHERE (user_id, id) IN ", IdTable/binary>>),

CfgTable = <<"(SELECT * FROM (SELECT ", MaybeLimitMSSQL/binary,
" cfg.user_id, cfg.remote_jid FROM mam_config cfg",
" INNER JOIN mam_server_user msu ON msu.id=cfg.user_id",
" WHERE msu.server = ?", MaybeLimitSQL/binary, ") AS T)">>,
mongoose_rdbms:prepare(mam_incr_remove_domain_prefs, mam_config, ['mam_server_user.server'],
<<"DELETE FROM mam_config "
"WHERE (user_id, remote_jid) IN ", CfgTable/binary>>),

ServerTable = <<"(SELECT * FROM (SELECT ", MaybeLimitMSSQL/binary,
" id FROM mam_server_user WHERE server = ? ", MaybeLimitSQL/binary, ") as t)">>,
mongoose_rdbms:prepare(mam_incr_remove_domain_users, mam_server_user, [server],
<<"DELETE ", MaybeLimitMSSQL/binary,
" FROM mam_server_user WHERE id IN ", ServerTable/binary>>).


%% ----------------------------------------------------------------------
%% Declarative logic

Expand Down Expand Up @@ -370,7 +393,7 @@ remove_domain_all(HostType, Domain) ->

-spec remove_domain_batch(host_type(), jid:lserver(), non_neg_integer()) -> any().
remove_domain_batch(HostType, Domain, Limit) ->
DeleteQueries = [mam_remove_domain, mam_remove_domain_prefs, mam_remove_domain_users],
DeleteQueries = [mam_incr_remove_domain, mam_incr_remove_domain_prefs, mam_incr_remove_domain_users],
TotalDeleted = mod_mam_utils:incremental_delete_domain(HostType, Domain, Limit, DeleteQueries, 0),
?LOG_INFO(#{what => mam_domain_removal_completed, total_records_deleted => TotalDeleted,
domain => Domain, host_type => HostType}).
Expand Down
9 changes: 1 addition & 8 deletions src/mam/mod_mam_utils.erl
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
%% Shared logic
-export([check_result_for_policy_violation/2,
lookup/3,
batch_delete_limits/1, incremental_delete_domain/5,
incremental_delete_domain/5,
db_message_codec/2, db_jid_codec/2]).

-callback extra_fin_element(mongooseim:host_type(),
Expand Down Expand Up @@ -1226,13 +1226,6 @@ db_jid_codec(HostType, Module) ->
db_message_codec(HostType, Module) ->
gen_mod:get_module_opt(HostType, Module, db_message_format).

-spec batch_delete_limits(#{delete_domain_limit := infinity | non_neg_integer(), _ => _}) ->
{binary(), binary()}.
batch_delete_limits(#{delete_domain_limit := infinity}) ->
{<<>>, <<>>};
batch_delete_limits(#{delete_domain_limit := Limit}) ->
rdbms_queries:get_db_specific_limits_binaries(Limit).

-spec incremental_delete_domain(
mongooseim:host_type(), jid:lserver(), non_neg_integer(), [atom()], non_neg_integer()) ->
non_neg_integer().
Expand Down

0 comments on commit 9feb307

Please sign in to comment.