add db migrations and restore config on unlock

This commit is contained in:
jfmonty2 committed 2026-11-18 05:49:45 -05:00
1 parent a10510ed6e
commit 0798124811
12 files changed
+365 -41

No files matched your search

Generated
+20
View File
@@ -1373,6 +1373,7 @@ dependencies = [
"getrandom 0.4.3", "getrandom 0.4.3",
"gpui-kit", "gpui-kit",
"gpui-omarchy", "gpui-omarchy",
"include_dir",
"serde", "serde",
"serde_json", "serde_json",
"tempfile", "tempfile",
@@ -3456,6 +3457,25 @@ version = "1.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6e44b0a4eaa4c82f441d50a963f2d5f05a787240aeee097597033e72accfd22f" checksum = "6e44b0a4eaa4c82f441d50a963f2d5f05a787240aeee097597033e72accfd22f"
[[package]]
name = "include_dir"
version = "0.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "923d117408f1e49d914f1a379a309cffe4f18c05cf4e3d12e613a15fc81bd0dd"
dependencies = [
"include_dir_macros",
]
[[package]]
name = "include_dir_macros"
version = "0.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7cab85a7ed0bd5f0e76d93846e0147172bed2e2d3f859bcc33a8d9699cad1a75"
dependencies = [
"proc-macro2",
"quote",
]
[[package]] [[package]]
name = "indexmap" name = "indexmap"
version = "2.14.2" version = "2.14.2"
+1
View File
@@ -16,6 +16,7 @@ duckdb = { version = "1.20000.1", features = ["bundled"] }
getrandom = { version = "0.4.3", features = ["sys_rng"] } getrandom = { version = "0.4.3", features = ["sys_rng"] }
gpui-kit = "0.7.0" gpui-kit = "0.7.0"
gpui-omarchy = "0.2.1" gpui-omarchy = "0.2.1"
include_dir = "0.7.4"
serde = "1.0.229" serde = "1.0.229"
serde_json = "1.0.154" serde_json = "1.0.154"
testresult = "0.4.2" testresult = "0.4.2"
+4
View File
@@ -0,0 +1,4 @@
fn main() {
// include_dir! only tracks files that already exist, so rebuild when one is added
println!("cargo:rerun-if-changed=migrations");
}
View File
Whitespace-only changes.
+4
View File
@@ -0,0 +1,4 @@
-- config will only ever hold a single row with a single value
CREATE TABLE config (value JSON);
-- seed it with an empty object, which will be populated with default values on deserialization
INSERT INTO config VALUES ('{}');
+57 -15
View File
@@ -1,4 +1,4 @@
use std::env::{self, VarError}; use std::env;
use std::ffi::OsString; use std::ffi::OsString;
use std::fs::File; use std::fs::File;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
@@ -28,7 +28,7 @@ impl PublicConfig {
pub fn load(path: &Path) -> Result<PublicConfig, CreddyError> { pub fn load(path: &Path) -> Result<PublicConfig, CreddyError> {
if path.exists() { if path.exists() {
let f = File::open(path)?; let f = File::open(path)?;
serde_json::from_reader(f).map_err(|e| CreddyError::ConfigDecode(e)) serde_json::from_reader(f).map_err(|e| CreddyError::InvalidJSON(e))
} }
else { else {
Ok(PublicConfig::default()) Ok(PublicConfig::default())
@@ -42,10 +42,26 @@ impl PublicConfig {
} }
} }
/// Config that can be stored in the encrypted db
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct PrivateConfig {
rehide_ms: u64,
}
impl Default for PrivateConfig {
fn default() -> PrivateConfig {
PrivateConfig {
rehide_ms: 1000,
}
}
}
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct AppConfig { pub struct AppConfig {
pub data_dir: PathBuf, pub data_dir: PathBuf,
pub db_salt: [u8; 32], pub db_salt: [u8; 32],
pub rehide_ms: u64,
} }
impl AppConfig { impl AppConfig {
@@ -59,12 +75,10 @@ impl AppConfig {
AppConfig::load_from(data_dir) AppConfig::load_from(data_dir)
} }
fn load_from(data_dir: PathBuf) -> Result<AppConfig, CreddyError> { pub fn load_from(data_dir: PathBuf) -> Result<AppConfig, CreddyError> {
let public_config = PublicConfig::load(&data_dir.join("public_config.json"))?; let public_config = PublicConfig::load(&data_dir.join("public_config.json"))?;
Ok(AppConfig { let private_config = PrivateConfig::default();
data_dir, Ok(AppConfig::from_parts(data_dir, public_config, private_config))
db_salt: public_config.db_salt,
})
} }
fn resolve_data_dir(creddy_data_dir: Option<OsString>, platform_data_dir: Option<PathBuf>) -> Result<PathBuf, CreddyError> { fn resolve_data_dir(creddy_data_dir: Option<OsString>, platform_data_dir: Option<PathBuf>) -> Result<PathBuf, CreddyError> {
@@ -74,6 +88,19 @@ impl AppConfig {
.ok_or(CreddyError::DataDirNotFound) .ok_or(CreddyError::DataDirNotFound)
} }
/// Pull the appropriate fields from instances of PublicConfig and PrivateConfig (plus data_dir since that comes from neither)
fn from_parts(data_dir: PathBuf, public: PublicConfig, private: PrivateConfig) -> AppConfig {
AppConfig {
data_dir,
db_salt: public.db_salt,
rehide_ms: private.rehide_ms,
}
}
pub fn merge_private(&mut self, private: PrivateConfig) {
self.rehide_ms = private.rehide_ms;
}
pub fn public_config_path(&self) -> PathBuf { pub fn public_config_path(&self) -> PathBuf {
self.data_dir.join("public_config.json") self.data_dir.join("public_config.json")
} }
@@ -87,6 +114,25 @@ impl AppConfig {
db_salt: self.db_salt, db_salt: self.db_salt,
} }
} }
pub fn private_config(&self) -> PrivateConfig {
PrivateConfig {
rehide_ms: self.rehide_ms,
}
}
}
impl Default for AppConfig {
fn default() -> AppConfig {
// AppConfig::default will only be called for testing or to fill in
// remaining defaults when constructing otherwise, and data_dir is
// always specified otherwise
AppConfig::from_parts(
PathBuf::from(""),
PublicConfig::default(),
PrivateConfig::default(),
)
}
} }
impl Global for AppConfig {} impl Global for AppConfig {}
@@ -171,10 +217,8 @@ mod tests {
#[test] #[test]
fn test_config_paths() { fn test_config_paths() {
let config = AppConfig { let mut config = AppConfig::default();
data_dir: PathBuf::from("/tmp/creddy"), config.data_dir = PathBuf::from("/tmp/creddy");
db_salt: PublicConfig::default().db_salt,
};
assert!(config.db_path() == PathBuf::from("/tmp/creddy/creddy.duckdb")); assert!(config.db_path() == PathBuf::from("/tmp/creddy/creddy.duckdb"));
assert!(config.public_config_path() == PathBuf::from("/tmp/creddy/public_config.json")); assert!(config.public_config_path() == PathBuf::from("/tmp/creddy/public_config.json"));
@@ -182,10 +226,8 @@ mod tests {
#[test] #[test]
fn test_public_config_from_app_config() { fn test_public_config_from_app_config() {
let config = AppConfig { let mut config = AppConfig::default();
data_dir: PathBuf::from("/tmp/creddy"), config.data_dir = PathBuf::from("/tmp/creddy");
db_salt: PublicConfig::default().db_salt,
};
let public = config.public_config(); let public = config.public_config();
assert!(public.db_salt == config.db_salt); assert!(public.db_salt == config.db_salt);
+48 -3
View File
@@ -1,9 +1,54 @@
use crate::config::AppConfig; use crate::config::PrivateConfig;
use crate::error::CreddyError; use crate::error::CreddyError;
use super::Db; use super::Db;
impl Db { impl Db {
pub fn save_config(&self, config: &AppConfig) -> Result<(), CreddyError> { pub fn load_private_config(&self) -> Result<PrivateConfig, CreddyError> {
todo!() 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(())
} }
} }
+174
View File
@@ -0,0 +1,174 @@
use duckdb::Connection;
use include_dir::{Dir, include_dir};
use crate::error::CreddyError;
static MIGRATIONS_DIR: Dir = include_dir!("$CARGO_MANIFEST_DIR/migrations");
struct Migration {
id: &'static str,
sql: &'static str,
}
/// Run all unapplied database migrations.
pub fn migrate(conn: &mut Connection) -> Result<(), CreddyError> {
apply_migrations(conn, &embedded_migrations())
}
/// Sorted list of migration files, checked for utf-8-ness at compile time.
fn embedded_migrations() -> Vec<Migration> {
let mut migrations: Vec<Migration> = MIGRATIONS_DIR
.files()
.filter(|f| f.path().extension().is_some_and(|ext| ext == "sql"))
.map(|f| Migration {
// these are baked in at compile time, so a bad file is a build problem, not a runtime one
id: f.path().file_name().and_then(|n| n.to_str()).expect("migration filename is not UTF-8"),
sql: f.contents_utf8().expect("migration file is not UTF-8"),
})
.collect();
migrations.sort_by_key(|m| m.id);
migrations
}
/// Run a specific list of migrations. (Makes testing easier than always hard-coding)
fn apply_migrations(conn: &mut Connection, known: &[Migration]) -> Result<(), CreddyError> {
create_migrations_table(conn)?;
let applied = list_applied_migrations(conn)?;
// the applied migrations must be exactly the first N known ones
for (i, id) in applied.iter().enumerate() {
if known.get(i).is_some_and(|m| m.id == id) {
continue;
}
// at this point, either known[i] doesn't exist or doesn't match `id`
if known.iter().any(|m| m.id == id) {
return Err(CreddyError::MigrationOutOfOrder(id.clone()));
}
//
return Err(CreddyError::DbTooNew(id.clone()));
}
for migration in &known[applied.len()..] {
let tx = conn.transaction()?;
tx.execute_batch(migration.sql)?;
tx.execute("INSERT INTO _migrations (id) VALUES (?);", [migration.id])?;
tx.commit()?;
}
Ok(())
}
/// Create the migrations table if it doesn't exist already.
fn create_migrations_table(conn: &Connection) -> Result<(), CreddyError> {
conn.execute_batch(
"CREATE TABLE IF NOT EXISTS _migrations (
id VARCHAR PRIMARY KEY,
applied_at TIMESTAMPTZ NOT NULL DEFAULT current_timestamp
);",
)?;
Ok(())
}
/// List migrations that have been applied to this database, in the order they were applied.
fn list_applied_migrations(conn: &Connection) -> Result<Vec<String>, CreddyError> {
let mut stmt = conn.prepare("SELECT id FROM _migrations ORDER BY id;")?;
let ids = stmt
.query_map([], |row| row.get(0))?
.collect::<Result<Vec<String>, _>>()?;
Ok(ids)
}
#[cfg(test)]
mod tests {
use testresult::TestResult;
use super::*;
const FIRST: Migration = Migration {
id: "0001_first.sql",
sql: "CREATE TABLE a (x INTEGER);",
};
const SECOND: Migration = Migration {
id: "0002_second.sql",
sql: "CREATE TABLE b (x INTEGER); INSERT INTO b VALUES (1);",
};
fn table_exists(conn: &Connection, name: &str) -> duckdb::Result<bool> {
conn.query_row(
"SELECT count(*) > 0 FROM duckdb_tables() WHERE table_name = ?;",
[name],
|row| row.get(0),
)
}
#[test]
fn test_embedded_migrations_apply() -> TestResult {
let mut conn = Connection::open_in_memory()?;
migrate(&mut conn)?;
let applied = list_applied_migrations(&conn)?;
let expected: Vec<_> = embedded_migrations().iter().map(|m| m.id.to_string()).collect();
assert_eq!(applied, expected);
Ok(())
}
#[test]
fn test_apply_incrementally() -> TestResult {
let mut conn = Connection::open_in_memory()?;
apply_migrations(&mut conn, &[FIRST])?;
assert_eq!(list_applied_migrations(&conn)?, ["0001_first.sql"]);
// already-applied migrations are skipped, so re-running FIRST would fail if it weren't
apply_migrations(&mut conn, &[FIRST, SECOND])?;
assert_eq!(list_applied_migrations(&conn)?, ["0001_first.sql", "0002_second.sql"]);
assert!(table_exists(&conn, "b")?);
Ok(())
}
#[test]
fn test_failed_migration_rolls_back() -> TestResult {
let mut conn = Connection::open_in_memory()?;
let broken = Migration {
id: "0002_broken.sql",
sql: "CREATE TABLE b (x INTEGER); this is not sql;",
};
let res = apply_migrations(&mut conn, &[FIRST, broken]);
assert!(matches!(res, Err(CreddyError::Db(_))));
// FIRST stays applied, but nothing from the broken migration survives
assert_eq!(list_applied_migrations(&conn)?, ["0001_first.sql"]);
assert!(table_exists(&conn, "a")?);
assert!(!table_exists(&conn, "b")?);
Ok(())
}
#[test]
fn test_db_too_new() -> TestResult {
let mut conn = Connection::open_in_memory()?;
apply_migrations(&mut conn, &[FIRST, SECOND])?;
// simulates running an older version of the app, which doesn't know about SECOND
match apply_migrations(&mut conn, &[FIRST]) {
Err(CreddyError::DbTooNew(id)) => assert_eq!(id, "0002_second.sql"),
res => panic!("Expected DbTooNew, got {res:?}"),
}
Ok(())
}
#[test]
fn test_migration_out_of_order() -> TestResult {
let mut conn = Connection::open_in_memory()?;
apply_migrations(&mut conn, &[SECOND])?;
// FIRST sorts before an already-applied migration, so it was added after the fact
match apply_migrations(&mut conn, &[FIRST, SECOND]) {
Err(CreddyError::MigrationOutOfOrder(id)) => assert_eq!(id, "0002_second.sql"),
res => panic!("Expected MigrationOutOfOrder, got {res:?}"),
}
Ok(())
}
}
+33 -16
View File
@@ -5,13 +5,13 @@ use base64ct::{Base64, Encoding};
use duckdb::{ use duckdb::{
Connection, Connection,
params, params,
Error,
}; };
use crate::error::CreddyError; use crate::error::CreddyError;
use crate::config::AppConfig; use crate::config::AppConfig;
mod config; mod config;
mod migrations;
// tests take forever with production parameters, so we turn them down in dev // tests take forever with production parameters, so we turn them down in dev
#[cfg(not(debug_assertions))] #[cfg(not(debug_assertions))]
@@ -46,14 +46,16 @@ impl Db {
.into_string() .into_string()
.map_err(|s| CreddyError::InvalidPath(s))?; .map_err(|s| CreddyError::InvalidPath(s))?;
let key = Db::derive_key(passphrase, &config.db_salt)?; let key = Db::derive_key(passphrase, &config.db_salt)?;
dbg!(&key);
let conn = Connection::open_in_memory()?; let mut conn = Connection::open_in_memory()?;
// hard-code the path for now, until we get proper config set up
conn.execute( conn.execute(
"ATTACH ? AS creddy (ENCRYPTION_KEY ?);", "ATTACH ? AS creddy (ENCRYPTION_KEY ?);",
params![path, key], params![path, key],
)?; )?;
// must come before migrating, or the tables end up in the in-memory db
conn.execute("USE creddy;", [])?; conn.execute("USE creddy;", [])?;
migrations::migrate(&mut conn)?;
Ok(Self{ connection: Mutex::new(conn) }) Ok(Self{ connection: Mutex::new(conn) })
} }
@@ -79,6 +81,7 @@ impl Db {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use testresult::TestResult; use testresult::TestResult;
use duckdb::Error;
use super::*; use super::*;
const TEST_PASSPHRASE: &'static str = "correct horse battery staple"; const TEST_PASSPHRASE: &'static str = "correct horse battery staple";
@@ -89,10 +92,9 @@ mod tests {
#[test] #[test]
fn test_connect_new() -> TestResult { fn test_connect_new() -> TestResult {
let data_dir = tempfile::tempdir()?; let data_dir = tempfile::tempdir()?;
let config = AppConfig { let mut config = AppConfig::default();
data_dir: data_dir.path().to_path_buf(), config.data_dir = data_dir.path().to_path_buf();
db_salt: [0u8; 32],
};
let res = Db::connect(&config, TEST_PASSPHRASE); let res = Db::connect(&config, TEST_PASSPHRASE);
assert!(res.is_ok()); assert!(res.is_ok());
@@ -124,11 +126,11 @@ mod tests {
#[test] #[test]
fn test_connect_existing() -> TestResult { fn test_connect_existing() -> TestResult {
let data_dir = tempfile::tempdir()?; let data_dir = tempfile::tempdir()?;
let config = AppConfig { let mut config = AppConfig::default();
data_dir: data_dir.path().to_path_buf(), config.data_dir = data_dir.path().to_path_buf();
db_salt: [0u8; 32], config.db_salt = TEST_SALT;
};
dbg!(TEST_KEY);
let conn = Connection::open_in_memory()?; let conn = Connection::open_in_memory()?;
conn.execute( conn.execute(
"ATTACH ? AS creddy (ENCRYPTION_KEY ?);", "ATTACH ? AS creddy (ENCRYPTION_KEY ?);",
@@ -142,18 +144,33 @@ mod tests {
#[test] #[test]
fn test_connect_create_dir() -> TestResult { fn test_connect_create_dir() -> TestResult {
let tmp_dir = tempfile::tempdir()?; let tmp_dir = tempfile::tempdir()?;
let data_dir = tmp_dir.path().join("creddy");
let mut config = AppConfig::default();
config.data_dir = tmp_dir.path().join("creddy");
// data_dir does not exist at this point // data_dir does not exist at this point
let config = AppConfig {
data_dir,
db_salt: TEST_SALT,
};
Db::connect(&config, TEST_PASSPHRASE)?; Db::connect(&config, TEST_PASSPHRASE)?;
Ok(()) Ok(())
} }
#[test]
fn test_connect_migrates_attached_db() -> TestResult {
let data_dir = tempfile::tempdir()?;
let mut config = AppConfig::default();
config.data_dir = data_dir.path().to_path_buf();
let db = Db::connect(&config, TEST_PASSPHRASE)?;
// the migrations table should live in the encrypted db, not the in-memory one
let database: String = db.connection.lock().unwrap().query_row(
"SELECT database_name FROM duckdb_tables() WHERE table_name = '_migrations';",
[],
|row| row.get(0),
)?;
assert_eq!(database, "creddy");
Ok(())
}
#[test] #[test]
fn test_derive_key() -> TestResult { fn test_derive_key() -> TestResult {
let key = Db::derive_key(TEST_PASSPHRASE, &TEST_SALT)?; let key = Db::derive_key(TEST_PASSPHRASE, &TEST_SALT)?;
+9 -3
View File
@@ -16,12 +16,18 @@ pub enum CreddyError {
#[error("I/O error: {0}")] #[error("I/O error: {0}")]
Io(#[from] std::io::Error), Io(#[from] std::io::Error),
#[error("Unable to load saved configuration: {0}")]
ConfigDecode(#[from] serde_json::Error),
#[error("Unable to hash password: {0}")] #[error("Unable to hash password: {0}")]
PasswordHash(#[from] argon2::Error), PasswordHash(#[from] argon2::Error),
#[error("Unable to get random data: {0}")] #[error("Unable to get random data: {0}")]
Entropy(#[from] getrandom::Error), Entropy(#[from] getrandom::Error),
#[error("This database was created by a newer version of Creddy (unknown migration {0})")]
DbTooNew(String),
#[error("Migration {0} was applied before an earlier migration that hasn't been")]
MigrationOutOfOrder(String),
#[error("Invalid JSON: {0}")]
InvalidJSON(#[from] serde_json::Error),
} }
+3 -1
View File
@@ -10,6 +10,9 @@ mod error;
mod screens; mod screens;
mod session; mod session;
#[cfg(test)]
mod test_data;
use config::AppConfig; use config::AppConfig;
use session::AppSession; use session::AppSession;
use screens::{HomeScreen, UnlockScreen, WelcomeScreen}; use screens::{HomeScreen, UnlockScreen, WelcomeScreen};
@@ -47,7 +50,6 @@ fn main() {
// todo: replace this with proper error handling // todo: replace this with proper error handling
let app_config = AppConfig::load().expect("failed to load config"); let app_config = AppConfig::load().expect("failed to load config");
let app_session = AppSession::load(&app_config); let app_session = AppSession::load(&app_config);
dbg!(&app_session);
// Icons are read through the asset source, so register the bundle. // Icons are read through the asset source, so register the bundle.
gpui_kit::application() gpui_kit::application()
+12 -3
View File
@@ -1,5 +1,5 @@
use gpui_kit::{ use gpui_kit::{
AppContext, Context, Entity, FontWeight, IntoElement, ParentElement, Render, SharedString, AppContext, BorrowAppContext, Context, Entity, FontWeight, IntoElement, ParentElement, Render, SharedString,
Styled, Subscription, Task, Window, div, rems, Styled, Subscription, Task, Window, div, rems,
}; };
use gpui_omarchy::{ButtonVariant, Status, alert, button, focus_scope}; use gpui_omarchy::{ButtonVariant, Status, alert, button, focus_scope};
@@ -58,13 +58,22 @@ impl UnlockScreen {
self.pending = Some(cx.spawn(async move |this, cx| { self.pending = Some(cx.spawn(async move |this, cx| {
// argon2 is deliberately slow, so keep it off the UI thread // argon2 is deliberately slow, so keep it off the UI thread
let result = cx let result = cx
.background_spawn(async move { Db::connect(&config, passphrase.as_str()) }) .background_spawn(async move {
let db = Db::connect(&config, passphrase.as_str())?;
// Some of AppConfig comes from the db, but we couldn't see it until now
let private_config = db.load_private_config()?;
Ok((db, private_config))
})
.await; .await;
// db connection has been established or not, so now we handle the result
let _ = this.update(cx, |this, cx| { let _ = this.update(cx, |this, cx| {
this.pending = None; this.pending = None;
match result { match result {
Ok(db) => cx.set_global(AppSession::Unlocked(db)), Ok((db, private_config)) => {
cx.update_global::<AppConfig, _>(|config, _cx| config.merge_private(private_config));
cx.set_global(AppSession::Unlocked(db));
},
Err(e) => this.error = Some(error_message(&e)), Err(e) => this.error = Some(error_message(&e)),
} }
cx.notify(); cx.notify();