/ src / database / pool.rs
pool.rs
 1  use anyhow::Result;
 2  use gpui::{App, Global};
 3  use sqlx::{
 4    SqlitePool,
 5    sqlite::{SqliteConnectOptions, SqliteJournalMode, SqliteSynchronous},
 6  };
 7  use std::path::PathBuf;
 8  
 9  pub struct Pool(pub SqlitePool);
10  impl Global for Pool {}
11  
12  /// Create a new sqlite pool and initialize the database.
13  pub async fn create(data_dir: PathBuf) -> Result<SqlitePool> {
14    let options = SqliteConnectOptions::new()
15      .filename(data_dir.join("app.db"))
16      .optimize_on_close(true, None)
17      .synchronous(SqliteSynchronous::Normal)
18      .journal_mode(SqliteJournalMode::Wal)
19      .statement_cache_capacity(0)
20      .create_if_missing(true);
21  
22    let pool = SqlitePool::connect_with(options).await?;
23    sqlx::migrate!().run(&pool).await?;
24    Ok(pool)
25  }
26  
27  pub fn init(cx: &mut App, pool: SqlitePool) {
28    cx.set_global(Pool(pool));
29  }