Skip to content

Commit

Permalink
Fix query that creates mam muc delete domain statements
Browse files Browse the repository at this point in the history
  • Loading branch information
NelsonVides committed Jan 9, 2023
1 parent efe3671 commit 6cae0e5
Showing 1 changed file with 21 additions and 12 deletions.
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 * FROM (SELECT ", MaybeLimitMSSQL/binary,
" id from mam_server_user WHERE server = ? ", MaybeLimitSQL/binary, ") as t)">>,
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

0 comments on commit 6cae0e5

Please sign in to comment.