Skip to main content

systemprompt_database/lifecycle/installation/
report.rs

1//! Outcome of a schema installation that succeeded but left typed drift
2//! behind: a declared foreign key an established database could not create.
3//! Callers surface the report — health endpoints, `infra db migrate` — rather
4//! than reading it from the log.
5//!
6//! Copyright (c) systemprompt.io — Business Source License 1.1.
7//! See <https://systemprompt.io> for licensing details.
8
9use serde::Serialize;
10use systemprompt_identifiers::ExtensionId;
11use tracing::{info, warn};
12
13use super::undeclared::SchemaResidue;
14
15#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
16pub struct ForeignKeyDrift {
17    pub extension: ExtensionId,
18    pub table: String,
19    pub constraint: String,
20    pub sql: String,
21    pub cause: String,
22}
23
24/// What `install_extension_schemas*` applied and what it could not.
25///
26/// `foreign_key_drift` is non-empty only for an established extension whose
27/// declarative foreign key cannot be created; the install still committed
28/// every other statement. A fresh database never reports drift — the same
29/// condition fails the install there. `residue` is what the database holds
30/// that no registered extension declares (tables and migration ledgers a
31/// deleted crate left behind); it never fails an install and is reported so
32/// a drop migration can be written.
33#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize)]
34pub struct SchemaInstallReport {
35    pub foreign_key_drift: Vec<ForeignKeyDrift>,
36    pub residue: SchemaResidue,
37    pub ahead: Vec<SchemaAhead>,
38}
39
40impl SchemaInstallReport {
41    pub(crate) fn log_outcome(&self) {
42        let report = self;
43        info!(
44            foreign_key_drift = report.foreign_key_drift.len(),
45            undeclared_tables = report.residue.undeclared_tables.len(),
46            orphan_migration_ledgers = report.residue.orphan_migration_ledgers.len(),
47            "Extension schema installation complete"
48        );
49        for table in &report.residue.undeclared_tables {
50            warn!(
51                schema = table.schema,
52                table = table.table,
53                live_rows = table.live_rows,
54                "live table declared by no registered extension — add a DROP TABLE migration"
55            );
56        }
57        for ledger in &report.residue.orphan_migration_ledgers {
58            warn!(
59                extension = %ledger.extension_id,
60                rows = ledger.rows,
61                "extension_migrations ledger for an extension that no longer exists"
62            );
63        }
64    }
65}
66
67impl SchemaAhead {
68    #[must_use]
69    pub fn from_currency(currency: &super::extension::current::SchemaCurrency) -> Vec<Self> {
70        currency
71            .ahead
72            .iter()
73            .map(|m| Self {
74                extension: m.extension_id.clone(),
75                version: m.version,
76                name: m.name.clone(),
77                breaks_older: currency
78                    .breaking
79                    .iter()
80                    .any(|b| b.extension_id == m.extension_id && b.version == m.version),
81            })
82            .collect()
83    }
84}
85
86/// A ledger row a newer release wrote: the database is ahead of this binary.
87#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
88pub struct SchemaAhead {
89    pub extension: ExtensionId,
90    pub version: u32,
91    pub name: String,
92    pub breaks_older: bool,
93}
94
95impl SchemaInstallReport {
96    #[must_use]
97    pub const fn is_clean(&self) -> bool {
98        self.foreign_key_drift.is_empty()
99    }
100
101    #[must_use]
102    pub const fn is_tidy(&self) -> bool {
103        self.residue.is_empty()
104    }
105
106    #[must_use]
107    pub const fn is_ahead(&self) -> bool {
108        !self.ahead.is_empty()
109    }
110
111    #[must_use]
112    pub fn breaks_this_release(&self) -> bool {
113        self.ahead.iter().any(|m| m.breaks_older)
114    }
115}