mirror of
https://github.com/chatmail/core.git
synced 2026-05-16 21:36:30 +03:00
refactor: Remove deduplicate_peerstates()
There's the `UNIQUE (acpeerstates.addr)` constraint since db v94.
This commit is contained in:
@@ -811,30 +811,6 @@ enum PeerstateChange {
|
|||||||
Aeap(String),
|
Aeap(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Removes duplicate peerstates from `acpeerstates` database table.
|
|
||||||
///
|
|
||||||
/// Normally there should be no more than one peerstate per address.
|
|
||||||
/// However, the database does not enforce this condition.
|
|
||||||
///
|
|
||||||
/// Previously there were bugs that caused creation of additional
|
|
||||||
/// peerstates when existing peerstate could not be read due to a
|
|
||||||
/// temporary database error or a failure to parse stored data. This
|
|
||||||
/// procedure fixes the problem by removing duplicate records.
|
|
||||||
pub(crate) async fn deduplicate_peerstates(sql: &Sql) -> Result<()> {
|
|
||||||
sql.execute(
|
|
||||||
"DELETE FROM acpeerstates
|
|
||||||
WHERE id NOT IN (
|
|
||||||
SELECT MIN(id)
|
|
||||||
FROM acpeerstates
|
|
||||||
GROUP BY addr
|
|
||||||
)",
|
|
||||||
(),
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ use crate::imex::BLOBS_BACKUP_NAME;
|
|||||||
use crate::log::LogExt;
|
use crate::log::LogExt;
|
||||||
use crate::message::{Message, MsgId, Viewtype};
|
use crate::message::{Message, MsgId, Viewtype};
|
||||||
use crate::param::{Param, Params};
|
use crate::param::{Param, Params};
|
||||||
use crate::peerstate::{deduplicate_peerstates, Peerstate};
|
use crate::peerstate::Peerstate;
|
||||||
use crate::stock_str;
|
use crate::stock_str;
|
||||||
use crate::tools::{delete_file, time, SystemTime};
|
use crate::tools::{delete_file, time, SystemTime};
|
||||||
|
|
||||||
@@ -730,10 +730,6 @@ pub async fn housekeeping(context: &Context) -> Result<()> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Err(err) = deduplicate_peerstates(&context.sql).await {
|
|
||||||
warn!(context, "Failed to deduplicate peerstates: {:#}.", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Try to clear the freelist to free some space on the disk. This
|
// Try to clear the freelist to free some space on the disk. This
|
||||||
// only works if auto_vacuum is enabled.
|
// only works if auto_vacuum is enabled.
|
||||||
match context
|
match context
|
||||||
|
|||||||
Reference in New Issue
Block a user