add db migrations and restore config on unlock
This commit is contained in:
1 parent
a10510ed6e
commit
0798124811
12 files changed
+365
-41
No files matched your search
+48
-3
@@ -1,9 +1,54 @@
|
||||
use crate::config::AppConfig;
|
||||
use crate::config::PrivateConfig;
|
||||
use crate::error::CreddyError;
|
||||
use super::Db;
|
||||
|
||||
impl Db {
|
||||
pub fn save_config(&self, config: &AppConfig) -> Result<(), CreddyError> {
|
||||
todo!()
|
||||
pub fn load_private_config(&self) -> Result<PrivateConfig, CreddyError> {
|
||||
let conn = self.connection.lock().unwrap();
|
||||
let json: String = conn.query_row("SELECT value FROM config", [], |row| row.get(0))?;
|
||||
let config: PrivateConfig = serde_json::from_str(&json)?;
|
||||
Ok(config)
|
||||
}
|
||||
|
||||
pub fn save_private_config(&self, config: &PrivateConfig) -> Result<(), CreddyError> {
|
||||
let json = serde_json::to_string(&config)?;
|
||||
let conn = self.connection.lock().unwrap();
|
||||
conn.execute("INSERT INTO config VALUES (?);", [&json])?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use testresult::TestResult;
|
||||
use tempfile::TempDir;
|
||||
use crate::config::AppConfig;
|
||||
use super::*;
|
||||
|
||||
|
||||
fn test_db() -> Result<(Db, TempDir), CreddyError> {
|
||||
let data_dir = tempfile::tempdir()?;
|
||||
let mut config = AppConfig::default();
|
||||
config.data_dir = data_dir.path().to_path_buf();
|
||||
let db = Db::connect(&config, "correct horse battery staple")?;
|
||||
Ok((db, data_dir))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_save_private_config() -> TestResult {
|
||||
let (db, _data_dir) = test_db()?;
|
||||
let private_config = PrivateConfig::default();
|
||||
db.save_private_config(&private_config)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_load_priate_config() -> TestResult {
|
||||
let (db, _data_dir) = test_db()?;
|
||||
let private_config = PrivateConfig::default();
|
||||
db.save_private_config(&private_config)?;
|
||||
let loaded = db.load_private_config()?;
|
||||
assert_eq!(loaded, private_config);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user