add Meta queries, check db version on start
parent
768a608b02
commit
466f870d6f
|
@ -1,12 +1,13 @@
|
|||
CREATE TABLE meta
|
||||
(
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
|
||||
key VARCHAR NOT NULL,
|
||||
value VARCHAR NOT NULL
|
||||
value VARCHAR NOT NULL,
|
||||
UNIQUE(key)
|
||||
);
|
||||
|
||||
INSERT INTO meta (key, value)
|
||||
VALUES ('version', '0.1.0');
|
||||
VALUES ('VERSION', '0');
|
||||
|
||||
CREATE TABLE files
|
||||
(
|
||||
|
|
|
@ -3,7 +3,7 @@ use std::path::PathBuf;
|
|||
use chrono::NaiveDateTime;
|
||||
use serde::Serialize;
|
||||
|
||||
use super::schema::{data, files};
|
||||
use super::schema::{data, files, meta};
|
||||
use crate::util::hash::Hash;
|
||||
|
||||
#[derive(Queryable, Serialize, Clone, Debug)]
|
||||
|
@ -49,3 +49,11 @@ pub struct Entry {
|
|||
pub value_num: Option<f64>,
|
||||
pub immutable: bool,
|
||||
}
|
||||
|
||||
#[derive(Queryable, Insertable, Serialize, Clone, Debug)]
|
||||
#[table_name = "meta"]
|
||||
pub struct MetaValue {
|
||||
pub id: i32,
|
||||
pub key: String,
|
||||
pub value: String,
|
||||
}
|
||||
|
|
|
@ -23,7 +23,7 @@ table! {
|
|||
|
||||
table! {
|
||||
meta (id) {
|
||||
id -> Nullable<Integer>,
|
||||
id -> Integer,
|
||||
key -> Text,
|
||||
value -> Text,
|
||||
}
|
||||
|
|
|
@ -10,7 +10,9 @@ pub mod inner;
|
|||
pub mod lang;
|
||||
|
||||
use crate::addressing::{Address, Addressable};
|
||||
use crate::database::constants::{IS_OF_TYPE_ATTR, TYPE_ADDR, TYPE_HAS_ATTR, TYPE_INVARIANT, LABEL_ATTR};
|
||||
use crate::database::constants::{
|
||||
IS_OF_TYPE_ATTR, LABEL_ATTR, TYPE_ADDR, TYPE_HAS_ATTR, TYPE_INVARIANT,
|
||||
};
|
||||
use crate::database::entry::{Entry, EntryValue, ImmutableEntry};
|
||||
use crate::database::inner::models;
|
||||
use crate::database::inner::schema::data;
|
||||
|
@ -112,6 +114,7 @@ impl UpEndDatabase {
|
|||
busy_timeout: Some(Duration::from_secs(30)),
|
||||
}))
|
||||
.build(manager)?;
|
||||
trace!("Pool created.");
|
||||
|
||||
let db = UpEndDatabase {
|
||||
pool,
|
||||
|
@ -120,6 +123,14 @@ impl UpEndDatabase {
|
|||
};
|
||||
let connection = db.connection().unwrap();
|
||||
|
||||
if !new {
|
||||
let db_major: u64 = connection.get_meta("VERSION")?.parse()?;
|
||||
if db_major > crate::common::PKG_VERSION_MAJOR.parse().unwrap() {
|
||||
return Err(anyhow!("Incompatible database! Found version "));
|
||||
}
|
||||
}
|
||||
|
||||
trace!("Running initial config.");
|
||||
let enable_wal_mode = true;
|
||||
connection.execute(if enable_wal_mode {
|
||||
trace!("Enabling WAL journal mode & truncating WAL log...");
|
||||
|
@ -129,7 +140,7 @@ impl UpEndDatabase {
|
|||
"PRAGMA journal_mode = TRUNCATE;"
|
||||
})?;
|
||||
|
||||
trace!("Pool created, running migrations...");
|
||||
trace!("Running migrations...");
|
||||
|
||||
embedded_migrations::run_with_output(
|
||||
&db.pool.get()?,
|
||||
|
@ -175,6 +186,20 @@ impl UpEndConnection {
|
|||
self.conn.transaction(f)
|
||||
}
|
||||
|
||||
pub fn get_meta<S: AsRef<str>>(&self, key: S) -> Result<String> {
|
||||
use crate::database::inner::schema::meta::dsl;
|
||||
let key = key.as_ref();
|
||||
|
||||
debug!("Querying META:{key}");
|
||||
|
||||
dsl::meta
|
||||
.filter(dsl::key.eq(key))
|
||||
.load::<models::MetaValue>(&self.conn)?
|
||||
.first()
|
||||
.ok_or(anyhow!("No META \"{key}\" value found."))
|
||||
.map(|mv| mv.value.clone())
|
||||
}
|
||||
|
||||
pub fn insert_file(&self, file: models::NewFile) -> Result<usize> {
|
||||
use crate::database::inner::schema::files;
|
||||
|
||||
|
@ -251,7 +276,10 @@ impl UpEndConnection {
|
|||
}
|
||||
|
||||
pub fn normalize_path(&self, path: &Path) -> Result<PathBuf> {
|
||||
Ok(path.canonicalize()?.strip_prefix(self.vault_path.as_path())?.to_path_buf())
|
||||
Ok(path
|
||||
.canonicalize()?
|
||||
.strip_prefix(self.vault_path.as_path())?
|
||||
.to_path_buf())
|
||||
}
|
||||
|
||||
pub fn retrieve_entry(&self, hash: Hash) -> Result<Option<Entry>> {
|
||||
|
|
Loading…
Reference in New Issue