Skip to main content

schedr_db/
status.rs

1//! Whether a database's schema fits this build of schedr.
2//!
3//! `schedr db migrate` applies the migrations; the master refuses to start against an outdated
4//! schema (see archive/PROJECT.md, State storage). A database migrated by a **newer** schedr is
5//! still usable: within a major version schema changes are additive, so an older master or
6//! webserver keeps working during a rolling upgrade (see Upgrades and compatibility).
7
8#[cfg(test)]
9mod tests;
10
11/// How the applied migrations compare with the ones built into this binary.
12#[derive(Debug, Clone, PartialEq, Eq)]
13pub enum SchemaStatus {
14    /// No migration has been applied: run `schedr db migrate`.
15    Uninitialized,
16    /// Exactly the migrations of this binary are applied.
17    UpToDate,
18    /// Migrations of this binary are missing: run `schedr db migrate`.
19    Outdated {
20        /// The versions not applied yet.
21        missing: Vec<i64>,
22    },
23    /// Migrations applied under a version this binary has, but with other contents: an earlier
24    /// schedr (0.x, whose migrations 1.0 rewrote) created the database. Not usable, and
25    /// `schedr db migrate` refuses it too: 1.0 needs a fresh database (see CHANGELOG).
26    Modified {
27        /// The versions whose applied checksum differs from this binary's.
28        changed: Vec<i64>,
29    },
30    /// The database has migrations this binary doesn't know (a newer schedr migrated it).
31    Newer {
32        /// The versions this binary doesn't know.
33        unknown: Vec<i64>,
34    },
35}
36
37impl SchemaStatus {
38    /// Compares the versions built into the binary with the versions applied to the database.
39    #[must_use]
40    pub fn compare(embedded: &[i64], applied: &[i64]) -> Self {
41        if applied.is_empty() && !embedded.is_empty() {
42            return Self::Uninitialized;
43        }
44        let missing: Vec<i64> = embedded
45            .iter()
46            .copied()
47            .filter(|v| !applied.contains(v))
48            .collect();
49        if !missing.is_empty() {
50            return Self::Outdated { missing };
51        }
52        let unknown: Vec<i64> = applied
53            .iter()
54            .copied()
55            .filter(|v| !embedded.contains(v))
56            .collect();
57        if unknown.is_empty() {
58            Self::UpToDate
59        } else {
60            Self::Newer { unknown }
61        }
62    }
63
64    /// Compares the migrations built into the binary with the applied ones, version and
65    /// checksum: a version applied with another checksum makes the schema
66    /// [`Modified`](Self::Modified), whatever else holds.
67    #[must_use]
68    pub fn compare_checksums(embedded: &[(i64, &[u8])], applied: &[(i64, &[u8])]) -> Self {
69        let changed: Vec<i64> = applied
70            .iter()
71            .filter(|(version, checksum)| {
72                embedded.iter().any(|(v, c)| v == version && c != checksum)
73            })
74            .map(|(version, _)| *version)
75            .collect();
76        if !changed.is_empty() {
77            return Self::Modified { changed };
78        }
79        let versions = |list: &[(i64, &[u8])]| list.iter().map(|(v, _)| *v).collect::<Vec<_>>();
80        Self::compare(&versions(embedded), &versions(applied))
81    }
82
83    /// Whether schedr can run against this schema: up to date, or migrated by a newer schedr.
84    #[must_use]
85    pub const fn is_usable(&self) -> bool {
86        matches!(self, Self::UpToDate | Self::Newer { .. })
87    }
88}