ockam_node/storage/database/migrations/migration_support/
migration_status.rs

1use crate::database::MigrationFailure;
2use crate::storage::database::migrations::migration_support::migrator::Version;
3use core::fmt::{Display, Formatter};
4use serde::Serialize;
5
6/// This enum models the state of a database with respect to migrations
7#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
8pub enum MigrationStatus {
9    /// The database is up to date with the latest version.
10    UpToDate(Version),
11    /// The database needs to be updated.
12    Todo(Option<Version>, Version),
13    /// A migration was attempted but failed
14    Failed(Version, MigrationFailure),
15}
16
17impl Display for MigrationStatus {
18    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
19        match self {
20            MigrationStatus::UpToDate(version) => {
21                write!(f, "✅ The database is up to date (version: {})", version)?
22            }
23            MigrationStatus::Todo(current_version, next_version) => write!(
24                f,
25                "⚙️ The database needs to be updated ({}next version: {})",
26                current_version
27                    .map(|v| format!("current version: {v}, "))
28                    .unwrap_or("".to_string()),
29                next_version
30            )?,
31            MigrationStatus::Failed(version, reason) => write!(
32                f,
33                "❌ The database failed to be updated at version: {}.\nReason: {reason}",
34                version
35            )?,
36        };
37        Ok(())
38    }
39}
40
41impl MigrationStatus {
42    /// Return true if the database is up to date
43    pub fn up_to_date(&self) -> bool {
44        matches!(self, MigrationStatus::UpToDate(_))
45    }
46}