mithril_aggregator/database/query/buffered_single_signature/
delete_buffered_single_signature.rs1use sqlite::Value;
2
3use mithril_common::entities::{PartyId, SignedEntityTypeDiscriminants};
4use mithril_persistence::sqlite::{Query, SourceAlias, SqLiteEntity, WhereCondition};
5
6use crate::database::record::BufferedSingleSignatureRecord;
7
8pub struct DeleteBufferedSingleSignatureQuery {
10 condition: WhereCondition,
11}
12
13impl DeleteBufferedSingleSignatureQuery {
14 pub fn by_discriminant_and_party_ids(
15 signed_entity_type_discriminant: SignedEntityTypeDiscriminants,
16 party_ids: Vec<PartyId>,
17 ) -> Self {
18 let ids_values = party_ids.into_iter().map(Value::String).collect();
19
20 Self {
21 condition: WhereCondition::new(
22 "signed_entity_type_id = ?*",
23 vec![Value::Integer(
24 signed_entity_type_discriminant.index() as i64
25 )],
26 )
27 .and_where(WhereCondition::where_in("party_id", ids_values)),
28 }
29 }
30}
31
32impl Query for DeleteBufferedSingleSignatureQuery {
33 type Entity = BufferedSingleSignatureRecord;
34
35 fn filters(&self) -> WhereCondition {
36 self.condition.clone()
37 }
38
39 fn get_definition(&self, condition: &str) -> String {
40 let projection = Self::Entity::get_projection().expand(SourceAlias::new(&[(
43 "{:buffered_single_signature:}",
44 "buffered_single_signature",
45 )]));
46
47 format!("delete from buffered_single_signature where {condition} returning {projection}")
48 }
49}
50
51#[cfg(test)]
52mod tests {
53 use mithril_common::entities::SignedEntityTypeDiscriminants::{
54 CardanoTransactions, MithrilStakeDistribution,
55 };
56 use mithril_persistence::sqlite::ConnectionExtensions;
57
58 use crate::database::query::GetBufferedSingleSignatureQuery;
59 use crate::database::record::strip_buffered_sigs_date;
60 use crate::database::test_helper::{insert_buffered_single_signatures, main_db_connection};
61
62 use super::*;
63
64 #[test]
65 fn test_delete_buffered_single_signature_records_by_discriminant_and_party_ids() {
66 let connection = main_db_connection().unwrap();
67 let records = BufferedSingleSignatureRecord::fakes(&[
68 ("party_1", MithrilStakeDistribution),
69 ("party_2", MithrilStakeDistribution),
70 ("party_3", MithrilStakeDistribution),
71 ("party_1", CardanoTransactions),
72 ("party_2", CardanoTransactions),
73 ]);
74 insert_buffered_single_signatures(&connection, records.clone()).unwrap();
75
76 let cursor = connection
77 .fetch(
78 DeleteBufferedSingleSignatureQuery::by_discriminant_and_party_ids(
79 MithrilStakeDistribution,
80 vec!["party_1".into(), "party_3".into()],
81 ),
82 )
83 .unwrap();
84 assert_eq!(2, cursor.count());
85
86 let remaining_records: Vec<BufferedSingleSignatureRecord> = connection
87 .fetch_collect(GetBufferedSingleSignatureQuery::all())
88 .unwrap();
89 assert_eq!(
90 strip_buffered_sigs_date(&BufferedSingleSignatureRecord::fakes(&[
91 ("party_2", CardanoTransactions),
92 ("party_1", CardanoTransactions),
93 ("party_2", MithrilStakeDistribution),
94 ])),
95 strip_buffered_sigs_date(&remaining_records)
96 );
97
98 let cursor = connection
99 .fetch(
100 DeleteBufferedSingleSignatureQuery::by_discriminant_and_party_ids(
101 CardanoTransactions,
102 vec!["party_1".into(), "party_2".into()],
103 ),
104 )
105 .unwrap();
106 assert_eq!(2, cursor.count());
107
108 let remaining_records: Vec<BufferedSingleSignatureRecord> = connection
109 .fetch_collect(GetBufferedSingleSignatureQuery::all())
110 .unwrap();
111 assert_eq!(
112 strip_buffered_sigs_date(&BufferedSingleSignatureRecord::fakes(&[(
113 "party_2",
114 MithrilStakeDistribution
115 ),])),
116 strip_buffered_sigs_date(&remaining_records)
117 );
118 }
119}