use std::future::Future;
use chrono::{Duration, Utc};
use sea_orm::{
ColumnTrait, Condition, ConnectionTrait, DbErr, EntityTrait, FromQueryResult, PaginatorTrait,
QueryFilter, QuerySelect, Select,
};
use crate::{
query::TrashedQuery,
row::{primary_key_condition, set_deleted_at},
};
const PRUNE_CHUNK: u64 = 500;
pub trait SoftDelete: EntityTrait<Model: FromQueryResult + Sync> {
fn deleted_at_column() -> Self::Column;
#[must_use]
fn updated_at_column() -> Option<Self::Column> {
None
}
fn before_soft_delete<C: ConnectionTrait>(
_model: &Self::Model,
_db: &C,
) -> impl Future<Output = Result<(), DbErr>> + Send {
async { Ok(()) }
}
fn after_soft_delete<C: ConnectionTrait>(
_model: &Self::Model,
_db: &C,
) -> impl Future<Output = Result<(), DbErr>> + Send {
async { Ok(()) }
}
fn before_restore<C: ConnectionTrait>(
_model: &Self::Model,
_db: &C,
) -> impl Future<Output = Result<(), DbErr>> + Send {
async { Ok(()) }
}
fn after_restore<C: ConnectionTrait>(
_model: &Self::Model,
_db: &C,
) -> impl Future<Output = Result<(), DbErr>> + Send {
async { Ok(()) }
}
fn before_force_delete<C: ConnectionTrait>(
_model: &Self::Model,
_db: &C,
) -> impl Future<Output = Result<(), DbErr>> + Send {
async { Ok(()) }
}
fn after_force_delete<C: ConnectionTrait>(
_model: &Self::Model,
_db: &C,
) -> impl Future<Output = Result<(), DbErr>> + Send {
async { Ok(()) }
}
#[must_use]
fn find_without_trashed() -> Select<Self> {
Self::find().without_trashed()
}
#[must_use]
fn find_only_trashed() -> Select<Self> {
Self::find().only_trashed()
}
fn soft_delete_many<C: ConnectionTrait>(
db: &C,
condition: Condition,
) -> impl Future<Output = Result<u64, DbErr>> + Send {
async move {
let result = set_deleted_at(Self::update_many(), Utc::now(), true)
.filter(condition)
.filter(Self::deleted_at_column().is_null())
.exec(db)
.await?;
Ok(result.rows_affected)
}
}
fn restore_many<C: ConnectionTrait>(
db: &C,
condition: Condition,
) -> impl Future<Output = Result<u64, DbErr>> + Send {
async move {
let result = set_deleted_at(Self::update_many(), Utc::now(), false)
.filter(condition)
.filter(Self::deleted_at_column().is_not_null())
.exec(db)
.await?;
Ok(result.rows_affected)
}
}
fn count_prunable<C: ConnectionTrait>(
db: &C,
older_than: Duration,
) -> impl Future<Output = Result<u64, DbErr>> + Send {
async move { prunable::<Self>(older_than).count(db).await }
}
fn prune_trashed<C: ConnectionTrait>(
db: &C,
older_than: Duration,
) -> impl Future<Output = Result<u64, DbErr>> + Send {
async move {
let cutoff = older_than;
let mut removed = 0;
loop {
let rows = prunable::<Self>(cutoff).limit(PRUNE_CHUNK).all(db).await?;
if rows.is_empty() {
return Ok(removed);
}
for row in rows {
Self::before_force_delete(&row, db).await?;
Self::delete_many()
.filter(primary_key_condition(&row))
.exec(db)
.await?;
Self::after_force_delete(&row, db).await?;
removed += 1;
}
}
}
}
fn mass_prune_trashed<C: ConnectionTrait>(
db: &C,
older_than: Duration,
) -> impl Future<Output = Result<u64, DbErr>> + Send {
async move {
let result = Self::delete_many()
.filter(Self::deleted_at_column().lt(Utc::now() - older_than))
.exec(db)
.await?;
Ok(result.rows_affected)
}
}
}
fn prunable<E: SoftDelete>(older_than: Duration) -> Select<E> {
E::find().filter(E::deleted_at_column().lt(Utc::now() - older_than))
}