mithril_aggregator/database/query/epoch_settings/
delete_epoch_settings.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
use sqlite::Value;

use mithril_common::entities::Epoch;
use mithril_persistence::sqlite::{Query, SourceAlias, SqLiteEntity, WhereCondition};

use crate::database::record::EpochSettingsRecord;

/// Query to delete old [EpochSettingsRecord] from the sqlite database
pub struct DeleteEpochSettingsQuery {
    condition: WhereCondition,
}

impl Query for DeleteEpochSettingsQuery {
    type Entity = EpochSettingsRecord;

    fn filters(&self) -> WhereCondition {
        self.condition.clone()
    }

    fn get_definition(&self, condition: &str) -> String {
        // it is important to alias the fields with the same name as the table
        // since the table cannot be aliased in a RETURNING statement in SQLite.
        let projection = Self::Entity::get_projection()
            .expand(SourceAlias::new(&[("{:epoch_setting:}", "epoch_setting")]));

        format!("delete from epoch_setting where {condition} returning {projection}")
    }
}

impl DeleteEpochSettingsQuery {
    #[cfg(test)]
    /// Create the SQL condition to delete a record given the Epoch.
    pub fn by_epoch(epoch: Epoch) -> Self {
        let epoch_settings_id_value = Value::Integer(epoch.try_into().unwrap());

        Self {
            condition: WhereCondition::new("epoch_setting_id = ?*", vec![epoch_settings_id_value]),
        }
    }

    /// Create the SQL condition to prune data older than the given Epoch.
    pub fn below_epoch_threshold(epoch_threshold: Epoch) -> Self {
        let epoch_settings_id_value = Value::Integer(epoch_threshold.try_into().unwrap());

        Self {
            condition: WhereCondition::new("epoch_setting_id < ?*", vec![epoch_settings_id_value]),
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::database::query::GetEpochSettingsQuery;
    use crate::database::test_helper::{insert_epoch_settings, main_db_connection};
    use mithril_persistence::sqlite::ConnectionExtensions;

    use super::*;

    #[test]
    fn test_delete_by_epoch() {
        let connection = main_db_connection().unwrap();
        insert_epoch_settings(&connection, &[1, 2]).unwrap();

        let cursor = connection
            .fetch(DeleteEpochSettingsQuery::by_epoch(Epoch(2)))
            .unwrap();

        assert_eq!(1, cursor.count());

        let cursor = connection
            .fetch(GetEpochSettingsQuery::by_epoch(Epoch(1)).unwrap())
            .unwrap();

        assert_eq!(1, cursor.count());

        let cursor = connection
            .fetch(GetEpochSettingsQuery::by_epoch(Epoch(2)).unwrap())
            .unwrap();

        assert_eq!(0, cursor.count());
    }

    #[test]
    fn test_delete_below_threshold() {
        let connection = main_db_connection().unwrap();
        insert_epoch_settings(&connection, &[1, 2]).unwrap();

        let cursor = connection
            .fetch(DeleteEpochSettingsQuery::below_epoch_threshold(Epoch(2)))
            .unwrap();

        assert_eq!(1, cursor.count());

        let cursor = connection
            .fetch(GetEpochSettingsQuery::by_epoch(Epoch(1)).unwrap())
            .unwrap();

        assert_eq!(0, cursor.count());

        let cursor = connection
            .fetch(GetEpochSettingsQuery::by_epoch(Epoch(2)).unwrap())
            .unwrap();

        assert_eq!(1, cursor.count());
    }
}