diff options
author | Jamie Murphy <hello@itsjamie.dev> | 2023-02-12 02:30:30 -0800 |
---|---|---|
committer | Jamie Murphy <hello@itsjamie.dev> | 2023-02-12 02:30:30 -0800 |
commit | 31ab804370a7b143e57235cdcd910767ce34b6e2 (patch) | |
tree | b0e022a6e2fc48a88452c46cb269ba3db9ae1c13 | |
parent | b76c02168272f48a042a438bb8429bbdf667dfb1 (diff) | |
download | gnome-todo-31ab804370a7b143e57235cdcd910767ce34b6e2.tar.gz |
local_provider: Setup database if needed
When running the database for the first time, or if I ever make a chagne
to the database, we should be able to run all the migrations by default
-rw-r--r-- | src/engine/providers/local.rs | 15 |
1 files changed, 11 insertions, 4 deletions
diff --git a/src/engine/providers/local.rs b/src/engine/providers/local.rs index 80a91538..ef86b587 100644 --- a/src/engine/providers/local.rs +++ b/src/engine/providers/local.rs @@ -9,6 +9,7 @@ use diesel::{ r2d2::{self, ConnectionManager}, SqliteConnection, }; +use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness}; use gtk::{ glib::{self, Object}, prelude::*, @@ -18,9 +19,11 @@ use once_cell::sync::Lazy; pub use crate::{config, provider::prelude::*}; +type Pool = r2d2::Pool<ConnectionManager<SqliteConnection>>; + static DB_PATH: Lazy<PathBuf> = Lazy::new(|| PathBuf::new().join(config::DATADIR).join("endeavour")); -type Pool = r2d2::Pool<ConnectionManager<SqliteConnection>>; +const MIGRATIONS: EmbeddedMigrations = embed_migrations!("migrations/"); fn create_pool<P: AsRef<Path>>(path: P) -> anyhow::Result<Pool> where @@ -35,6 +38,11 @@ where let manager = ConnectionManager::<SqliteConnection>::new(db_path.to_str().unwrap_or_default()); let pool = r2d2::Pool::builder().build(manager)?; + + let mut db = pool.get()?; + db.run_pending_migrations(MIGRATIONS) + .expect("Failed to setup database"); + Ok(pool) } @@ -128,9 +136,8 @@ mod tests { use super::*; #[test] - fn save_task() { + fn test_save_task() { let provider = LocalProvider::default(); - let task = block_on(provider.imp().create_task("Test Task")); - println!("{}", task.priority()); + block_on(provider.imp().create_task("Test Task")); } } |