use engram_storage::Database;
use crate::error::CoreError;
pub const EMBEDDING_MODEL_KEY: &str = "embedding_model";
pub fn check(database: &Database, configured_model: &str) -> Result<(), CoreError> {
match super::read_meta(database, EMBEDDING_MODEL_KEY)? {
Some(stored) if stored == configured_model => Ok(()),
Some(stored) => Err(CoreError::EmbeddingModelMismatch {
stored,
configured: configured_model.to_string(),
}),
None => bootstrap(database, configured_model),
}
}
pub fn record(database: &Database, model: &str) -> Result<(), CoreError> {
super::write_meta(database, EMBEDDING_MODEL_KEY, model)
}
fn bootstrap(database: &Database, configured_model: &str) -> Result<(), CoreError> {
if memories_count(database)? > 0 {
eprintln!(
"warning: schema_meta.embedding_model bootstrap on non-empty database — \
if existing embeddings were computed with a model other than `{configured_model}`, \
run `engram reembed` before further use"
);
}
super::write_meta(database, EMBEDDING_MODEL_KEY, configured_model)
}
fn memories_count(database: &Database) -> Result<i64, CoreError> {
database
.connection()
.query_row("SELECT COUNT(*) FROM memories", [], |row| row.get(0))
.map_err(|error| CoreError::MigrationFailed(format!("memories count: {error}")))
}