From 45e7d8426d94090bf5bb2a2b8482d12502123e97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Chmielowski?= Date: Thu, 17 Apr 2025 14:21:25 +0200 Subject: [PATCH] Make delete_old_mam_messages_batch work with sqlite --- src/mod_mam_sql.erl | 46 ++++++++++++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/src/mod_mam_sql.erl b/src/mod_mam_sql.erl index de97eaaa5..2747254ac 100644 --- a/src/mod_mam_sql.erl +++ b/src/mod_mam_sql.erl @@ -213,21 +213,37 @@ count_messages_to_delete(ServerHost, TimeStamp, Type) -> delete_old_messages_batch(ServerHost, TimeStamp, Type, Batch) -> TS = misc:now_to_usec(TimeStamp), Res = - case Type of - all -> - ejabberd_sql:sql_query( - ServerHost, - ?SQL("delete from archive" - " where timestamp < %(TS)d and %(ServerHost)H limit %(Batch)d")); - _ -> - SType = misc:atom_to_binary(Type), - ejabberd_sql:sql_query( - ServerHost, - ?SQL("delete from archive" - " where timestamp < %(TS)d" - " and kind=%(SType)s" - " and %(ServerHost)H limit %(Batch)d")) - end, + case Type of + all -> + ejabberd_sql:sql_query( + ServerHost, + fun(sqlite, _) -> + ejabberd_sql:sql_query_t( + ?SQL("delete from archive where rowid in " + "(select rowid from archive where timestamp < %(TS)d and %(ServerHost)H limit %(Batch)d)")); + (_, _) -> + ejabberd_sql:sql_query_t( + ?SQL("delete from archive" + " where timestamp < %(TS)d and %(ServerHost)H limit %(Batch)d")) + end); + _ -> + SType = misc:atom_to_binary(Type), + ejabberd_sql:sql_query( + ServerHost, + fun(sqlire,_)-> + ejabberd_sql:sql_query_t( + ?SQL("delete from archive where rowid in (" + " select rowid from archive where timestamp < %(TS)d" + " and kind=%(SType)s" + " and %(ServerHost)H limit %(Batch)d)")); + (_,_)-> + ejabberd_sql:sql_query_t( + ?SQL("delete from archive" + " where timestamp < %(TS)d" + " and kind=%(SType)s" + " and %(ServerHost)H limit %(Batch)d")) + end) + end, case Res of {updated, Count} -> {ok, Count};