From 0798124811f5263919da9ac6ed2093d775ad7182 Mon Sep 17 00:00:00 2001 From: Joseph Montanaro Date: Wed, 18 Nov 2026 05:48:49 -0500 Subject: [PATCH] add db migrations and restore config on unlock --- Cargo.lock | 20 +++++ Cargo.toml | 1 + build.rs | 4 + migrations/.gitkeep | 0 migrations/001_initial.sql | 4 + src/config.rs | 72 +++++++++++---- src/db/config.rs | 51 ++++++++++- src/db/migrations.rs | 174 +++++++++++++++++++++++++++++++++++++ src/db/mod.rs | 49 +++++++---- src/error.rs | 12 ++- src/main.rs | 4 +- src/screens/unlock.rs | 15 +++- 12 files changed, 365 insertions(+), 41 deletions(-) create mode 100644 build.rs create mode 100644 migrations/.gitkeep create mode 100644 migrations/001_initial.sql create mode 100644 src/db/migrations.rs diff --git a/Cargo.lock b/Cargo.lock index 6269b7a..dc66661 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1373,6 +1373,7 @@ dependencies = [ "getrandom 0.4.3", "gpui-kit", "gpui-omarchy", + "include_dir", "serde", "serde_json", "tempfile", @@ -3456,6 +3457,25 @@ version = "1.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" 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]] name = "indexmap" version = "2.14.2" diff --git a/Cargo.toml b/Cargo.toml index 2b8c734..97465a2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,6 +16,7 @@ duckdb = { version = "1.20000.1", features = ["bundled"] } getrandom = { version = "0.4.3", features = ["sys_rng"] } gpui-kit = "0.7.0" gpui-omarchy = "0.2.1" +include_dir = "0.7.4" serde = "1.0.229" serde_json = "1.0.154" testresult = "0.4.2" diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..bc235ea --- /dev/null +++ b/build.rs @@ -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"); +} diff --git a/migrations/.gitkeep b/migrations/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/migrations/001_initial.sql b/migrations/001_initial.sql new file mode 100644 index 0000000..090f393 --- /dev/null +++ b/migrations/001_initial.sql @@ -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 ('{}'); diff --git a/src/config.rs b/src/config.rs index 7836b0b..7cf9c00 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,4 +1,4 @@ -use std::env::{self, VarError}; +use std::env; use std::ffi::OsString; use std::fs::File; use std::path::{Path, PathBuf}; @@ -28,7 +28,7 @@ impl PublicConfig { pub fn load(path: &Path) -> Result { if path.exists() { 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 { 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)] pub struct AppConfig { pub data_dir: PathBuf, pub db_salt: [u8; 32], + pub rehide_ms: u64, } impl AppConfig { @@ -59,12 +75,10 @@ impl AppConfig { AppConfig::load_from(data_dir) } - fn load_from(data_dir: PathBuf) -> Result { + pub fn load_from(data_dir: PathBuf) -> Result { let public_config = PublicConfig::load(&data_dir.join("public_config.json"))?; - Ok(AppConfig { - data_dir, - db_salt: public_config.db_salt, - }) + let private_config = PrivateConfig::default(); + Ok(AppConfig::from_parts(data_dir, public_config, private_config)) } fn resolve_data_dir(creddy_data_dir: Option, platform_data_dir: Option) -> Result { @@ -74,6 +88,19 @@ impl AppConfig { .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 { self.data_dir.join("public_config.json") } @@ -87,6 +114,25 @@ impl AppConfig { 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 {} @@ -171,10 +217,8 @@ mod tests { #[test] fn test_config_paths() { - let config = AppConfig { - data_dir: PathBuf::from("/tmp/creddy"), - db_salt: PublicConfig::default().db_salt, - }; + let mut config = AppConfig::default(); + config.data_dir = PathBuf::from("/tmp/creddy"); assert!(config.db_path() == PathBuf::from("/tmp/creddy/creddy.duckdb")); assert!(config.public_config_path() == PathBuf::from("/tmp/creddy/public_config.json")); @@ -182,10 +226,8 @@ mod tests { #[test] fn test_public_config_from_app_config() { - let config = AppConfig { - data_dir: PathBuf::from("/tmp/creddy"), - db_salt: PublicConfig::default().db_salt, - }; + let mut config = AppConfig::default(); + config.data_dir = PathBuf::from("/tmp/creddy"); let public = config.public_config(); assert!(public.db_salt == config.db_salt); diff --git a/src/db/config.rs b/src/db/config.rs index f10d075..1321880 100644 --- a/src/db/config.rs +++ b/src/db/config.rs @@ -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 { + 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(()) } } diff --git a/src/db/migrations.rs b/src/db/migrations.rs new file mode 100644 index 0000000..4916299 --- /dev/null +++ b/src/db/migrations.rs @@ -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 { + let mut migrations: Vec = 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, CreddyError> { + let mut stmt = conn.prepare("SELECT id FROM _migrations ORDER BY id;")?; + let ids = stmt + .query_map([], |row| row.get(0))? + .collect::, _>>()?; + 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 { + 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(()) + } +} diff --git a/src/db/mod.rs b/src/db/mod.rs index 6bdb202..8deeef7 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -5,13 +5,13 @@ use base64ct::{Base64, Encoding}; use duckdb::{ Connection, params, - Error, }; use crate::error::CreddyError; use crate::config::AppConfig; mod config; +mod migrations; // tests take forever with production parameters, so we turn them down in dev #[cfg(not(debug_assertions))] @@ -46,14 +46,16 @@ impl Db { .into_string() .map_err(|s| CreddyError::InvalidPath(s))?; let key = Db::derive_key(passphrase, &config.db_salt)?; + dbg!(&key); - let conn = Connection::open_in_memory()?; - // hard-code the path for now, until we get proper config set up + let mut conn = Connection::open_in_memory()?; conn.execute( "ATTACH ? AS creddy (ENCRYPTION_KEY ?);", params![path, key], )?; + // must come before migrating, or the tables end up in the in-memory db conn.execute("USE creddy;", [])?; + migrations::migrate(&mut conn)?; Ok(Self{ connection: Mutex::new(conn) }) } @@ -79,6 +81,7 @@ impl Db { #[cfg(test)] mod tests { use testresult::TestResult; + use duckdb::Error; use super::*; const TEST_PASSPHRASE: &'static str = "correct horse battery staple"; @@ -89,10 +92,9 @@ mod tests { #[test] fn test_connect_new() -> TestResult { let data_dir = tempfile::tempdir()?; - let config = AppConfig { - data_dir: data_dir.path().to_path_buf(), - db_salt: [0u8; 32], - }; + let mut config = AppConfig::default(); + config.data_dir = data_dir.path().to_path_buf(); + let res = Db::connect(&config, TEST_PASSPHRASE); assert!(res.is_ok()); @@ -124,11 +126,11 @@ mod tests { #[test] fn test_connect_existing() -> TestResult { let data_dir = tempfile::tempdir()?; - let config = AppConfig { - data_dir: data_dir.path().to_path_buf(), - db_salt: [0u8; 32], - }; + let mut config = AppConfig::default(); + config.data_dir = data_dir.path().to_path_buf(); + config.db_salt = TEST_SALT; + dbg!(TEST_KEY); let conn = Connection::open_in_memory()?; conn.execute( "ATTACH ? AS creddy (ENCRYPTION_KEY ?);", @@ -142,18 +144,33 @@ mod tests { #[test] fn test_connect_create_dir() -> TestResult { 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 - let config = AppConfig { - data_dir, - db_salt: TEST_SALT, - }; Db::connect(&config, TEST_PASSPHRASE)?; 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] fn test_derive_key() -> TestResult { let key = Db::derive_key(TEST_PASSPHRASE, &TEST_SALT)?; diff --git a/src/error.rs b/src/error.rs index f683e7a..e419696 100644 --- a/src/error.rs +++ b/src/error.rs @@ -16,12 +16,18 @@ pub enum CreddyError { #[error("I/O error: {0}")] Io(#[from] std::io::Error), - #[error("Unable to load saved configuration: {0}")] - ConfigDecode(#[from] serde_json::Error), - #[error("Unable to hash password: {0}")] PasswordHash(#[from] argon2::Error), #[error("Unable to get random data: {0}")] 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), } diff --git a/src/main.rs b/src/main.rs index fcda3c1..6e60be3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,6 +10,9 @@ mod error; mod screens; mod session; +#[cfg(test)] +mod test_data; + use config::AppConfig; use session::AppSession; use screens::{HomeScreen, UnlockScreen, WelcomeScreen}; @@ -47,7 +50,6 @@ fn main() { // todo: replace this with proper error handling let app_config = AppConfig::load().expect("failed to load config"); let app_session = AppSession::load(&app_config); - dbg!(&app_session); // Icons are read through the asset source, so register the bundle. gpui_kit::application() diff --git a/src/screens/unlock.rs b/src/screens/unlock.rs index cfa651d..f234751 100644 --- a/src/screens/unlock.rs +++ b/src/screens/unlock.rs @@ -1,5 +1,5 @@ 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, }; use gpui_omarchy::{ButtonVariant, Status, alert, button, focus_scope}; @@ -58,13 +58,22 @@ impl UnlockScreen { self.pending = Some(cx.spawn(async move |this, cx| { // argon2 is deliberately slow, so keep it off the UI thread 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; + // db connection has been established or not, so now we handle the result let _ = this.update(cx, |this, cx| { this.pending = None; match result { - Ok(db) => cx.set_global(AppSession::Unlocked(db)), + Ok((db, private_config)) => { + cx.update_global::(|config, _cx| config.merge_private(private_config)); + cx.set_global(AppSession::Unlocked(db)); + }, Err(e) => this.error = Some(error_message(&e)), } cx.notify();