Skip to content

Commit

Permalink
Fix query that creates inbox and mam delete domain statements
Browse files Browse the repository at this point in the history
  • Loading branch information
NelsonVides committed Jan 9, 2023
1 parent 759b64a commit 414a617
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 41 deletions.
15 changes: 10 additions & 5 deletions src/inbox/mod_inbox_rdbms.erl
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,16 @@ init(HostType, Opts) ->
UniqueKeyFields, <<"timestamp">>),
ok.

prepare_remove_domain(Opts) ->
{MaybeLimitSQL, MaybeLimitMSSQL} = mod_mam_utils:batch_delete_limits(Opts),
prepare_remove_domain(#{delete_domain_limit := infinity}) ->
mongoose_rdbms:prepare(
inbox_delete_domain, inbox, [lserver],
<<"DELETE ", MaybeLimitMSSQL/binary, "FROM inbox WHERE lserver = ? ", MaybeLimitSQL/binary>>).
inbox_delete_domain, inbox, [lserver], <<"DELETE FROM inbox WHERE lserver = ?">>);
prepare_remove_domain(#{delete_domain_limit := Limit}) ->
{MaybeLimitSQL, MaybeLimitMSSQL} = rdbms_queries:get_db_specific_limits_binaries(Limit),
ServerTable = <<"(SELECT * FROM (SELECT", MaybeLimitMSSQL/binary,
" lserver FROM inbox WHERE lserver = ? ", MaybeLimitSQL/binary, ") as t)">>,
mongoose_rdbms:prepare(
inbox_incr_delete_domain, inbox, [lserver],
<<"DELETE FROM inbox WHERE lserver IN ", ServerTable/binary>>).

-spec get_inbox(HostType :: mongooseim:host_type(),
LUser :: jid:luser(),
Expand Down Expand Up @@ -481,7 +486,7 @@ execute_delete(HostType, LUser, LServer) ->
execute_delete_domain(HostType, LServer, infinity) ->
mongoose_rdbms:execute_successfully(HostType, inbox_delete_domain, [LServer]);
execute_delete_domain(HostType, LServer, Limit) ->
mod_mam_utils:incremental_delete_domain(HostType, LServer, Limit, [inbox_delete_domain], 0).
mod_mam_utils:incremental_delete_domain(HostType, LServer, Limit, [inbox_incr_delete_domain], 0).

%% DB result processing
-type db_return() :: {RemBareJID :: jid:luser(),
Expand Down
33 changes: 21 additions & 12 deletions src/mam/mod_mam_muc_rdbms_arch.erl
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,7 @@ register_prepared_queries(Opts) ->
"WHERE room_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_muc_remove_domain, mam_muc_message, ['mam_server_user.server'],
<<"DELETE FROM mam_muc_message "
"WHERE room_id IN ", IdTable/binary>>),
mongoose_rdbms:prepare(mam_muc_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_muc_make_tombstone, mam_muc_message, [message, room_id, id],
<<"UPDATE mam_muc_message SET message = ?, search_body = '' "
Expand All @@ -148,6 +138,25 @@ register_prepared_queries(Opts) ->
<<"SELECT id, message FROM mam_muc_message "
" WHERE sender_id = ? ORDER BY id">>).

prepare_remove_domain(#{delete_domain_limit := infinity}) ->
mongoose_rdbms:prepare(mam_muc_remove_domain, mam_muc_message, ['mam_server_user.server'],
<<"DELETE FROM mam_muc_message "
"WHERE room_id IN (SELECT id FROM mam_server_user where server = ?)">>),
mongoose_rdbms:prepare(mam_muc_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 ", 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_muc_incr_remove_domain, mam_muc_message, ['mam_server_user.server'],
<<"DELETE FROM mam_muc_message "
"WHERE room_id IN ", IdTable/binary>>),
mongoose_rdbms:prepare(mam_muc_incr_remove_domain_users, mam_server_user, [server],
<<"DELETE ", MaybeLimitMSSQL/binary,
" FROM mam_server_user WHERE server IN", ServerTable/binary>>).

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

Expand Down Expand Up @@ -341,7 +350,7 @@ remove_domain_all(HostType, Domain) ->
-spec remove_domain_batch(host_type(), jid:lserver(), non_neg_integer()) -> any().
remove_domain_batch(HostType, Domain, Limit) ->
SubHosts = get_subhosts(HostType, Domain),
DeleteQueries = [mam_muc_remove_domain, mam_muc_remove_domain_users],
DeleteQueries = [mam_muc_incr_remove_domain, mam_muc_incr_remove_domain_users],
DelSubHost = [ mod_mam_utils:incremental_delete_domain(HostType, SubHost, Limit, DeleteQueries, 0)
|| SubHost <- SubHosts],
TotalDeleted = lists:sum(DelSubHost),
Expand Down
46 changes: 30 additions & 16 deletions src/mam/mod_mam_rdbms_arch.erl
Original file line number Diff line number Diff line change
Expand Up @@ -132,21 +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 = '' "
"WHERE user_id = ? AND id = ?">>),
Expand All @@ -166,6 +152,34 @@ 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 ", 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_incr_remove_domain, mam_message, ['mam_server_user.server'],
<<"DELETE FROM mam_message "
"WHERE user_id IN ", IdTable/binary>>),
mongoose_rdbms:prepare(mam_incr_remove_domain_prefs, mam_config, ['mam_server_user.server'],
<<"DELETE FROM mam_config "
"WHERE user_id IN ", IdTable/binary>>),
mongoose_rdbms:prepare(mam_incr_remove_domain_users, mam_server_user, [server],
<<"DELETE ", MaybeLimitMSSQL/binary,
" FROM mam_server_user WHERE server IN ", ServerTable/binary>>).


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

Expand Down Expand Up @@ -370,7 +384,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 414a617

Please sign in to comment.