use std::fmt;
use std::future::Future;
use navree_db::Db;
use navree_events::{Event, EventBus};
use navree_kernel::{AppContext, BoxError, BoxFuture};
use serde::{Deserialize, Serialize};
use crate::error::{OrmError, Result};
use crate::exec::{Access, ExecContext, Executor};
use crate::ir::{Direction, Operator};
use crate::model::{Model, PrimaryKey};
use crate::query::{Builder, key_expr};
use crate::record::Record;
use crate::scope::Trashed;
use crate::value::Field;
#[diagnostic::on_unimplemented(
message = "`{Self}` is marked prunable but does not implement `Prunable`",
note = "add `impl navree_orm::prune::Prunable for {Self} {{ fn prunable(query: Builder<Self>) -> Builder<Self> {{ .. }} }}`"
)]
pub trait Prunable: Model {
const CHUNK_SIZE: Option<u64> = None;
fn prunable(query: Builder<Self>) -> Builder<Self>;
fn pruning(&self, ctx: &ExecContext) -> impl Future<Output = Result<()>> + Send {
let _ = ctx;
async { Ok(()) }
}
fn prune_all<E: Executor + ?Sized>(
exec: &E,
chunk_size: u64,
) -> impl Future<Output = Result<u64>> + Send {
prune_each::<Self, E>(exec, chunk_size)
}
}
#[diagnostic::on_unimplemented(
message = "`{Self}` is marked mass_prunable but does not implement `MassPrunable`",
note = "add `impl navree_orm::prune::MassPrunable for {Self} {{ fn prunable(query: Builder<Self>) -> Builder<Self> {{ .. }} }}`"
)]
pub trait MassPrunable: Model {
const CHUNK_SIZE: Option<u64> = None;
fn prunable(query: Builder<Self>) -> Builder<Self>;
fn prune_all<E: Executor + ?Sized>(
exec: &E,
chunk_size: u64,
) -> impl Future<Output = Result<u64>> + Send {
prune_mass::<Self, E>(exec, chunk_size)
}
}
impl<M: Model> Builder<M> {
pub(crate) fn including_trashed(mut self) -> Self {
if self.scopes.trashed == Trashed::Hidden {
self.scopes.trashed = Trashed::Included;
}
self
}
}
async fn prune_each<M: Prunable, E: Executor + ?Sized>(exec: &E, chunk_size: u64) -> Result<u64> {
let chunk = chunk_size.max(1);
let ctx = exec.target(Access::Write).await?.context();
let mut total = 0;
let mut after: Option<M::Key> = None;
loop {
let mut query = M::prunable(M::query())
.including_trashed()
.reorder_by(key_expr::<M>(), Direction::Asc)
.limit(chunk);
if let Some(after) = &after {
query = query.where_(crate::column::Filter::compare(
key_expr::<M>(),
Operator::Gt,
crate::ir::Expr::Value(after.to_value()),
));
}
let records = query.get(exec).await?;
let fetched = records.len() as u64;
let Some(last) = records.last().map(Record::key) else {
break;
};
for mut record in records {
match prune_record(exec, &ctx, &mut record).await {
Ok(true) => total += 1,
Ok(false) => {}
Err(error) => PruneFailed::of(&record, error).report(&ctx).await,
}
}
announce(&ctx, ModelsPruned::new(M::meta().name(), total)).await;
if fetched < chunk {
break;
}
after = Some(last);
}
Ok(total)
}
async fn prune_record<M: Prunable, E: Executor + ?Sized>(
exec: &E,
ctx: &ExecContext,
record: &mut Record<M>,
) -> Result<bool> {
record.model.pruning(ctx).await?;
if M::meta().soft_deletes_enabled() {
record.force_delete(exec).await
} else {
record.delete(exec).await
}
}
async fn prune_mass<M: MassPrunable, E: Executor + ?Sized>(
exec: &E,
chunk_size: u64,
) -> Result<u64> {
let mut query = M::prunable(M::query());
if query.ir.limit.is_none() {
query = query.limit(chunk_size.max(1));
}
let ctx = exec.target(Access::Write).await?.context();
let mut total = 0;
loop {
let deleted = query.clone().force_delete(exec).await?;
if deleted == 0 {
break;
}
total += deleted;
announce(&ctx, ModelsPruned::new(M::meta().name(), total)).await;
}
Ok(total)
}
async fn announce<E: Event>(ctx: &ExecContext, event: E) {
let Some(app) = ctx.app() else {
return;
};
announce_in(app, event).await;
}
async fn announce_in<E: Event>(app: &AppContext, event: E) {
let Some(bus) = app.try_get::<EventBus>().await else {
return;
};
if let Err(error) = bus.emit(event).await {
tracing::warn!(target: "navree_orm::prune", event = E::NAME, %error, "a pruning event listener failed");
}
}
#[derive(Debug, thiserror::Error)]
#[error("cannot prune {model} {key}: {source}")]
struct PruneFailed {
model: &'static str,
key: String,
#[source]
source: OrmError,
}
impl PruneFailed {
fn of<M: Model>(record: &Record<M>, source: OrmError) -> Self {
PruneFailed {
model: M::meta().name(),
key: record.key().to_key_string(),
source,
}
}
async fn report(&self, ctx: &ExecContext) {
tracing::error!(target: "navree_orm::prune", model = self.model, key = %self.key, error = %self.source, "a record could not be pruned");
if let Some(app) = ctx.app() {
navree_telemetry::Telemetry::from_app(app)
.await
.report_exception(self);
}
}
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct ModelsPruned {
pub model: String,
pub count: u64,
}
impl ModelsPruned {
fn new(model: &str, count: u64) -> Self {
ModelsPruned {
model: model.to_owned(),
count,
}
}
}
impl Event for ModelsPruned {
const NAME: &'static str = "orm.models_pruned";
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct ModelPruningStarting {
pub models: Vec<String>,
}
impl Event for ModelPruningStarting {
const NAME: &'static str = "orm.model_pruning_starting";
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct ModelPruningFinished {
pub models: Vec<String>,
}
impl Event for ModelPruningFinished {
const NAME: &'static str = "orm.model_pruning_finished";
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum PruneKind {
EachRecord,
MassDelete,
}
#[derive(Clone, Copy)]
pub struct Pruning {
kind: PruneKind,
chunk_size: Option<u64>,
run: fn(Db, u64) -> BoxFuture<'static, Result<u64>>,
count: fn(Db) -> BoxFuture<'static, Result<u64>>,
}
impl Pruning {
pub const fn each<M: Prunable>() -> Self {
Pruning {
kind: PruneKind::EachRecord,
chunk_size: <M as Prunable>::CHUNK_SIZE,
run: run_each::<M>,
count: count_each::<M>,
}
}
pub const fn mass<M: MassPrunable>() -> Self {
Pruning {
kind: PruneKind::MassDelete,
chunk_size: <M as MassPrunable>::CHUNK_SIZE,
run: run_mass::<M>,
count: count_mass::<M>,
}
}
pub fn kind(&self) -> PruneKind {
self.kind
}
pub fn chunk_size(&self) -> Option<u64> {
self.chunk_size
}
pub async fn run(&self, db: Db, chunk_size: u64) -> Result<u64> {
(self.run)(db, chunk_size).await
}
pub async fn count(&self, db: Db) -> Result<u64> {
(self.count)(db).await
}
}
impl fmt::Debug for Pruning {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Pruning")
.field("kind", &self.kind)
.field("chunk_size", &self.chunk_size)
.finish_non_exhaustive()
}
}
fn run_each<M: Prunable>(db: Db, chunk_size: u64) -> BoxFuture<'static, Result<u64>> {
Box::pin(async move { M::prune_all(&db, chunk_size).await })
}
fn count_each<M: Prunable>(db: Db) -> BoxFuture<'static, Result<u64>> {
Box::pin(async move { M::prunable(M::query()).including_trashed().count(&db).await })
}
fn run_mass<M: MassPrunable>(db: Db, chunk_size: u64) -> BoxFuture<'static, Result<u64>> {
Box::pin(async move { M::prune_all(&db, chunk_size).await })
}
fn count_mass<M: MassPrunable>(db: Db) -> BoxFuture<'static, Result<u64>> {
Box::pin(async move {
let mut query = M::prunable(M::query()).including_trashed();
query.ir.limit = None;
query.count(&db).await
})
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct PruneRun {
pub models: Vec<String>,
pub except: Vec<String>,
pub chunk: Option<u64>,
pub pretend: bool,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Pruned {
pub model: &'static str,
pub count: u64,
}
pub async fn prune(app: &AppContext, run: &PruneRun) -> std::result::Result<Vec<Pruned>, BoxError> {
if !run.models.is_empty() && !run.except.is_empty() {
return Err("--model and --except cannot be combined".into());
}
let prunable: Vec<(&'static str, Pruning)> = crate::module::registered_models(app)
.into_iter()
.filter_map(|model| {
model
.meta()
.pruning()
.map(|pruning| (model.name(), *pruning))
})
.collect();
for name in &run.models {
if !prunable.iter().any(|(known, _)| known == name) {
let known: Vec<&str> = prunable.iter().map(|(name, _)| *name).collect();
return Err(format!(
"no prunable model is named `{name}` (prunable: {}; register models with m.model::<M>() and mark them #[model(prunable)] or #[model(mass_prunable)])",
if known.is_empty() { "none".to_owned() } else { known.join(", ") }
)
.into());
}
}
let chosen: Vec<(&'static str, Pruning)> = prunable
.into_iter()
.filter(|(name, _)| run.models.is_empty() || run.models.iter().any(|m| m == name))
.filter(|(name, _)| !run.except.iter().any(|m| m == name))
.collect();
if chosen.is_empty() {
return Ok(Vec::new());
}
let db = Db::clone(&*app.get::<Db>().await?);
let default_chunk = match run.chunk {
Some(chunk) => chunk,
None => match app.try_get::<crate::OrmRuntime>().await {
Some(runtime) => runtime.options().prune_chunk,
None => crate::DEFAULT_PRUNE_CHUNK,
},
};
let names: Vec<String> = chosen.iter().map(|(name, _)| (*name).to_owned()).collect();
let mut done = Vec::with_capacity(chosen.len());
if run.pretend {
for (model, pruning) in chosen {
done.push(Pruned {
model,
count: pruning.count(db.clone()).await?,
});
}
return Ok(done);
}
announce_in(
app,
ModelPruningStarting {
models: names.clone(),
},
)
.await;
for (model, pruning) in chosen {
let chunk = pruning.chunk_size().unwrap_or(default_chunk);
done.push(Pruned {
model,
count: pruning.run(db.clone(), chunk).await?,
});
}
announce_in(app, ModelPruningFinished { models: names }).await;
Ok(done)
}
pub(crate) async fn prune_command(
app: AppContext,
args: Vec<String>,
) -> std::result::Result<(), BoxError> {
let run = parse_args(&args).map_err(|message| format!("model:prune: {message}"))?;
let pruned = prune(&app, &run)
.await
.map_err(|error| format!("model:prune: {error}"))?;
if pruned.is_empty() {
println!("No prunable models found.");
}
for Pruned { model, count } in pruned {
match (count, run.pretend) {
(0, _) => println!("No prunable {model} records found."),
(count, true) => println!("{count} {model} records will be pruned."),
(count, false) => println!("Pruned {count} {model} records."),
}
}
Ok(())
}
fn parse_args(args: &[String]) -> std::result::Result<PruneRun, String> {
let mut run = PruneRun::default();
let mut args = args.iter();
while let Some(arg) = args.next() {
let (name, inline) = match arg.split_once('=') {
Some((name, value)) => (name, Some(value.to_owned())),
None => (arg.as_str(), None),
};
let mut value = |option: &str| {
inline
.clone()
.or_else(|| args.next().cloned())
.ok_or_else(|| format!("{option} needs a value"))
};
match name {
"--model" => run.models.push(value("--model")?),
"--except" => run.except.push(value("--except")?),
"--chunk" => {
let text = value("--chunk")?;
match text.parse::<u64>() {
Ok(chunk) if chunk > 0 => run.chunk = Some(chunk),
_ => {
return Err(format!(
"--chunk must be a whole number of at least 1, not `{text}`"
));
}
}
}
"--pretend" if inline.is_none() => run.pretend = true,
other => {
return Err(format!(
"unknown option `{other}` (expected --model, --except, --chunk or --pretend)"
));
}
}
}
Ok(run)
}
#[cfg(test)]
mod tests {
use super::*;
fn args(list: &[&str]) -> Vec<String> {
list.iter().map(|arg| (*arg).to_owned()).collect()
}
#[test]
fn options_parse_in_both_spellings() {
let run = parse_args(&args(&[
"--model",
"Deal",
"--model=Note",
"--chunk=50",
"--pretend",
]))
.unwrap();
assert_eq!(run.models, ["Deal", "Note"]);
assert_eq!(run.chunk, Some(50));
assert!(run.pretend);
assert_eq!(
parse_args(&args(&["--chunk"])).unwrap_err(),
"--chunk needs a value"
);
assert_eq!(
parse_args(&args(&["--chunk", "x"])).unwrap_err(),
"--chunk must be a whole number of at least 1, not `x`"
);
assert!(parse_args(&args(&["--pretend=yes"])).is_err());
}
}