use azoth_core::error::{AzothError, Result};
use rusqlite::Connection;
pub fn migrate(conn: &Connection, target_version: u32) -> Result<()> {
let current_version: u32 = conn
.query_row(
"SELECT schema_version FROM projection_meta WHERE id = 0",
[],
|row| {
let v: i64 = row.get(0)?;
Ok(v as u32)
},
)
.map_err(|e| AzothError::Projection(e.to_string()))?;
if target_version < current_version {
return Err(AzothError::InvalidState(format!(
"Cannot downgrade schema version from {} to {}. Use MigrationManager::rollback_last() instead.",
current_version, target_version
)));
}
if current_version == target_version {
return Ok(());
}
update_schema_version(conn, target_version)?;
Ok(())
}
fn update_schema_version(conn: &Connection, version: u32) -> Result<()> {
conn.execute(
"UPDATE projection_meta SET schema_version = ?1, updated_at = datetime('now') WHERE id = 0",
[version as i64],
)
.map_err(|e| AzothError::Projection(e.to_string()))?;
Ok(())
}