systemprompt_database/lifecycle/installation/
report.rs1use 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#[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#[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}