mithril_signer/database/query/stake_pool/
delete_stake_pool.rsuse sqlite::Value;
use mithril_common::{entities::Epoch, StdResult};
use mithril_persistence::sqlite::{Query, SourceAlias, SqLiteEntity, WhereCondition};
use crate::database::record::StakePool;
pub struct DeleteStakePoolQuery {
condition: WhereCondition,
}
impl Query for DeleteStakePoolQuery {
type Entity = StakePool;
fn filters(&self) -> WhereCondition {
self.condition.clone()
}
fn get_definition(&self, condition: &str) -> String {
let projection = Self::Entity::get_projection()
.expand(SourceAlias::new(&[("{:stake_pool:}", "stake_pool")]));
format!("delete from stake_pool where {condition} returning {projection}")
}
}
impl DeleteStakePoolQuery {
pub fn below_epoch_threshold(epoch_threshold: Epoch) -> Self {
let condition = WhereCondition::new(
"epoch < ?*",
vec![Value::Integer(epoch_threshold.try_into().unwrap())],
);
Self { condition }
}
pub fn by_epoch(epoch: Epoch) -> StdResult<Self> {
let condition = WhereCondition::new("epoch = ?*", vec![Value::Integer(epoch.try_into()?)]);
Ok(Self { condition })
}
}
#[cfg(test)]
mod tests {
use crate::database::query::GetStakePoolQuery;
use crate::database::test_helper::{insert_stake_pool, main_db_connection};
use mithril_persistence::sqlite::ConnectionExtensions;
use super::*;
#[test]
fn test_prune_below_epoch_threshold() {
let connection = main_db_connection().unwrap();
insert_stake_pool(&connection, &[1, 2]).unwrap();
let cursor = connection
.fetch(DeleteStakePoolQuery::below_epoch_threshold(Epoch(2)))
.unwrap();
assert_eq!(3, cursor.count());
let cursor = connection
.fetch(GetStakePoolQuery::by_epoch(Epoch(1)).unwrap())
.unwrap();
assert_eq!(0, cursor.count());
let cursor = connection
.fetch(GetStakePoolQuery::by_epoch(Epoch(2)).unwrap())
.unwrap();
assert_eq!(3, cursor.count());
}
}