Skip to main content

systemprompt_runtime/
schema_currency.rs

1//! Boot-time refusal of a database the migration step has not reached.
2//!
3//! When a node boots without migrating, [`assert_schema_current`] runs while
4//! the application context is built, before the domain services start, and
5//! fails with the pending work and the migrate command, instead of letting
6//! the first query against a missing column fail mid-request.
7//!
8//! Copyright (c) systemprompt.io — Business Source License 1.1.
9//! See <https://systemprompt.io> for licensing details.
10
11use systemprompt_database::{DatabaseProvider, schema_currency};
12use systemprompt_extension::ExtensionRegistry;
13use systemprompt_manifest::Profile;
14
15use crate::error::{RuntimeError, RuntimeResult};
16
17pub async fn assert_schema_current(
18    registry: &ExtensionRegistry,
19    db: &dyn DatabaseProvider,
20    profile: &Profile,
21) -> RuntimeResult<()> {
22    let currency = schema_currency(db, registry, &[]).await?;
23    if currency.is_current() {
24        return Ok(());
25    }
26    Err(RuntimeError::SchemaBehind {
27        profile: profile.name.clone(),
28        fresh: currency
29            .fresh_extensions
30            .iter()
31            .map(ToString::to_string)
32            .collect(),
33        pending: currency
34            .pending
35            .iter()
36            .map(|m| format!("{}:{:03} {}", m.extension_id, m.version, m.name))
37            .collect(),
38        drift: currency
39            .drift
40            .iter()
41            .map(|d| format!("{}:{:03} {}", d.extension_id, d.version, d.name))
42            .collect(),
43    })
44}