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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
use anyhow::anyhow;
use chrono::{DateTime, NaiveDateTime, Utc};
use mithril_common::StdResult;
use sqlite::{Row, Value};
use std::{
    cmp::Ordering,
    fmt::{Debug, Display},
};

use crate::sqlite::{HydrationError, Projection, Query, SourceAlias, SqLiteEntity, WhereCondition};

use super::DbVersion;

/// Application using a database
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ApplicationNodeType {
    /// Aggregator node type
    Aggregator,

    /// Signer node type
    Signer,
}

impl ApplicationNodeType {
    /// [ApplicationNodeType] constructor.
    pub fn new(node_type: &str) -> StdResult<Self> {
        match node_type {
            "aggregator" => Ok(Self::Aggregator),
            "signer" => Ok(Self::Signer),
            _ => Err(anyhow!("unknown node type '{node_type}'")),
        }
    }
}

impl Display for ApplicationNodeType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Aggregator => write!(f, "aggregator"),
            Self::Signer => write!(f, "signer"),
        }
    }
}

/// Entity related to the `db_version` database table.
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct DatabaseVersion {
    /// Version of the database structure.
    pub version: DbVersion,

    /// Name of the application.
    pub application_type: ApplicationNodeType,

    /// Date of the last version upgrade
    pub updated_at: DateTime<Utc>,
}

impl SqLiteEntity for DatabaseVersion {
    fn hydrate(row: Row) -> Result<Self, HydrationError> {
        let version = row.read::<i64, _>(0);
        let application_type = row.read::<&str, _>(1);
        let updated_at = row.read::<&str, _>(2);

        Ok(Self {
            version,
            application_type: ApplicationNodeType::new(application_type)
                .map_err(|e| HydrationError::InvalidData(format!("{e}")))?,
            updated_at: match DateTime::parse_from_rfc3339(updated_at) {
                Ok(date) => Ok(date.with_timezone(&Utc)),
                // todo: remove this fallback when aggregators & signers have been migrated
                // Fallback to previous date format for compatibility
                Err(_) => NaiveDateTime::parse_from_str(updated_at, "%Y-%m-%d %H:%M:%S")
                    .map_err(|e| {
                        HydrationError::InvalidData(format!(
                            "Could not turn string '{updated_at}' to rfc3339 Datetime. Error: {e}"
                        ))
                    })
                    .map(|d| d.and_utc()),
            }?,
        })
    }

    fn get_projection() -> Projection {
        let mut projection = Projection::default();
        projection.add_field("version", "{:db_version:}.version", "text");
        projection.add_field(
            "application_type",
            "{:db_version:}.application_type",
            "text",
        );
        projection.add_field("updated_at", "{:db_version:}.updated_at", "timestamp");

        projection
    }
}

impl PartialOrd for DatabaseVersion {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        if self.application_type != other.application_type {
            None
        } else {
            self.version.partial_cmp(&other.version)
        }
    }
}

/// Query to get [DatabaseVersion] entities.
pub struct GetDatabaseVersionQuery {
    condition: WhereCondition,
}

impl GetDatabaseVersionQuery {
    /// Query to read the application version from the database.
    pub fn get_application_version(application_type: &ApplicationNodeType) -> Self {
        let filters = WhereCondition::new(
            "application_type = ?*",
            vec![Value::String(format!("{application_type}"))],
        );
        Self { condition: filters }
    }
}

impl Query for GetDatabaseVersionQuery {
    type Entity = DatabaseVersion;

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

    fn get_definition(&self, condition: &str) -> String {
        let aliases = SourceAlias::new(&[("{:db_version:}", "db_version")]);
        let projection = Self::Entity::get_projection().expand(aliases);

        format!(
            r#"
select {projection}
from db_version
where {condition}
"#
        )
    }
}

/// Query to UPSERT [DatabaseVersion] entities.
pub struct UpdateDatabaseVersionQuery {
    condition: WhereCondition,
}

impl UpdateDatabaseVersionQuery {
    /// Define a query that will UPSERT the given version.
    pub fn one(version: DatabaseVersion) -> Self {
        let filters = WhereCondition::new(
            "",
            vec![
                Value::String(format!("{}", version.application_type)),
                Value::Integer(version.version),
                Value::String(version.updated_at.to_rfc3339()),
            ],
        );

        Self { condition: filters }
    }
}

impl Query for UpdateDatabaseVersionQuery {
    type Entity = DatabaseVersion;

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

    fn get_definition(&self, _condition: &str) -> String {
        let aliases = SourceAlias::new(&[("{:db_version:}", "db_version")]);
        let projection = Self::Entity::get_projection().expand(aliases);

        format!(
            r#"
insert into db_version (application_type, version, updated_at) values (?, ?, ?)
  on conflict (application_type) do update set version = excluded.version, updated_at = excluded.updated_at
returning {projection}
"#
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_projection() {
        let projection = DatabaseVersion::get_projection();
        let aliases = SourceAlias::new(&[("{:db_version:}", "whatever")]);

        assert_eq!(
            "whatever.version as version, whatever.application_type as application_type, whatever.updated_at as updated_at"
                .to_string(),
            projection.expand(aliases)
        );
    }

    #[test]
    fn test_definition() {
        let query =
            GetDatabaseVersionQuery::get_application_version(&ApplicationNodeType::Aggregator);

        assert_eq!(
            r#"
select db_version.version as version, db_version.application_type as application_type, db_version.updated_at as updated_at
from db_version
where true
"#,
            query.get_definition("true")
        )
    }

    #[test]
    fn test_updated_entity() {
        let query = UpdateDatabaseVersionQuery::one(DatabaseVersion {
            version: 0,
            application_type: ApplicationNodeType::Aggregator,
            updated_at: Default::default(),
        });

        assert_eq!(
            r#"
insert into db_version (application_type, version, updated_at) values (?, ?, ?)
  on conflict (application_type) do update set version = excluded.version, updated_at = excluded.updated_at
returning db_version.version as version, db_version.application_type as application_type, db_version.updated_at as updated_at
"#,
            query.get_definition("true")
        )
    }
}