Merge pull request #55 from PR0M3TH3AN/codex/define-schema_version-and-add-migration-verification

Add schema version tracking
This commit is contained in:
thePR0M3TH3AN
2025-05-22 08:18:05 -04:00
committed by GitHub
2 changed files with 27 additions and 0 deletions

View File

@@ -36,6 +36,7 @@ All foreign keys use `ON DELETE CASCADE` so deleting a file, tag, etc. automatic
3. **0003\_create\_links\_collections\_views.sql** introduce `links`, `collections`, `collection_files`, and `views` tables.
4. **0004\_fix\_hierarchical\_tags\_fts.sql** refine FTS triggers to index full hierarchical tag-paths via a recursive CTE.
3. Expose this schema through our library (`libmarlin::db::open`) so any client sees a v1.1 store.
4. Track the version in code via `SCHEMA_VERSION` and provide `current_schema_version()` to query the DB.
## 3. ER Diagram

View File

@@ -18,6 +18,11 @@ use rusqlite::{
use std::result::Result as StdResult;
use tracing::{debug, info, warn};
/* ─── schema version ───────────────────────────────────────────────── */
/// Current library schema version.
pub const SCHEMA_VERSION: i32 = 1_1;
/* ─── embedded migrations ─────────────────────────────────────────── */
const MIGRATIONS: &[(&str, &str)] = &[
@@ -47,6 +52,18 @@ const MIGRATIONS: &[(&str, &str)] = &[
),
];
/* ─── schema helpers ─────────────────────────────────────────────── */
/// Fetch the highest version recorded in the `schema_version` table.
pub fn current_schema_version(conn: &Connection) -> Result<i32> {
let version: i32 = conn.query_row(
"SELECT IFNULL(MAX(version), 0) FROM schema_version",
[],
|r| r.get(0),
)?;
Ok(version)
}
/* ─── connection bootstrap ────────────────────────────────────────── */
pub fn open<P: AsRef<Path>>(db_path: P) -> Result<Connection> {
@@ -133,6 +150,15 @@ pub(crate) fn apply_migrations(conn: &mut Connection) -> Result<()> {
warn!("migrations not applied: {:?}", missing);
}
let current = current_schema_version(conn)?;
if current != SCHEMA_VERSION {
anyhow::bail!(
"database schema version {} does not match library version {}",
current,
SCHEMA_VERSION
);
}
Ok(())
}