mithril_aggregator/database/query/single_signature/
update_single_signature.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
use sqlite::Value;

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

use crate::database::record::SingleSignatureRecord;

/// Query to update [SingleSignatureRecord] in the sqlite database
pub struct UpdateSingleSignatureRecordQuery {
    condition: WhereCondition,
}

impl UpdateSingleSignatureRecordQuery {
    pub fn one(single_signature_record: SingleSignatureRecord) -> Self {
        let condition =
        WhereCondition::new(
            "(open_message_id, signer_id, registration_epoch_setting_id, lottery_indexes, signature, created_at) values (?*, ?*, ?*, ?*, ?*, ?*)",
            vec![
                Value::String(single_signature_record.open_message_id.to_string()),
                Value::String(single_signature_record.signer_id),
                Value::Integer(
                    single_signature_record.registration_epoch_settings_id.try_into().unwrap(),
                ),
                Value::String(serde_json::to_string(&single_signature_record.lottery_indexes).unwrap()),
                Value::String(single_signature_record.signature),
                Value::String(single_signature_record.created_at.to_rfc3339()),
            ],
        );

        Self { condition }
    }
}

impl Query for UpdateSingleSignatureRecordQuery {
    type Entity = SingleSignatureRecord;

    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(&[(
            "{:single_signature:}",
            "single_signature",
        )]));

        format!("insert or replace into single_signature {condition} returning {projection}")
    }
}

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

    use super::*;

    #[test]
    fn test_update_single_signature_record() {
        let single_signature_records = setup_single_signature_records(2, 3, 4);

        let connection = main_db_connection().unwrap();

        for single_signature_record in single_signature_records.clone() {
            let single_signature_record_saved = connection
                .fetch_first(UpdateSingleSignatureRecordQuery::one(
                    single_signature_record.clone(),
                ))
                .unwrap();
            assert_eq!(Some(single_signature_record), single_signature_record_saved);
        }

        for mut single_signature_record in single_signature_records {
            single_signature_record.lottery_indexes.push(5);
            let single_signature_record_saved = connection
                .fetch_first(UpdateSingleSignatureRecordQuery::one(
                    single_signature_record.clone(),
                ))
                .unwrap();
            assert_eq!(Some(single_signature_record), single_signature_record_saved);
        }
    }
}