cat_gateway/db/event/schema_check/
mod.rs

1//! Check if the schema is up-to-date.
2
3use crate::db::event::{EventDB, DATABASE_SCHEMA_VERSION};
4
5/// Schema in database does not match schema supported by the Crate.
6#[derive(thiserror::Error, Debug, PartialEq, Eq)]
7#[error(" Schema in database does not match schema supported by the Crate. The current schema version: {was}, the schema version we expected: {expected}")]
8pub(crate) struct MismatchedSchemaError {
9    /// The current DB schema version.
10    was: i32,
11    /// The expected DB schema version.
12    expected: i32,
13}
14
15/// `select_max_version.sql`
16const SELECT_MAX_VERSION_SQL: &str = include_str!("select_max_version.sql");
17
18impl EventDB {
19    /// Check the schema version.
20    /// return the current schema version if its current.
21    /// Otherwise return an error.
22    pub(crate) async fn schema_version_check() -> anyhow::Result<i32> {
23        let schema_check = Self::query_one(SELECT_MAX_VERSION_SQL, &[]).await?;
24
25        let current_ver = schema_check.try_get("max")?;
26
27        if current_ver == DATABASE_SCHEMA_VERSION {
28            Ok(current_ver)
29        } else {
30            Err(MismatchedSchemaError {
31                was: current_ver,
32                expected: DATABASE_SCHEMA_VERSION,
33            }
34            .into())
35        }
36    }
37}