mod schema; pub mod users; use diesel::connection::SimpleConnection; use diesel::r2d2::{ConnectionManager, Pool}; use diesel::sqlite::{Sqlite, SqliteConnection}; use std::error::Error; use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness}; pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!("migrations"); type DbError = Box; fn run_migrations(connection: &mut impl MigrationHarness) -> Result<(), DbError> { // This will run the necessary migrations. // // See the documentation for `MigrationHarness` for // all available methods. connection.run_pending_migrations(MIGRATIONS)?; Ok(()) } fn initialize_db(conn: &mut SqliteConnection) -> Result<(), DbError> { // Enable WAL mode and enforce foreign keys conn.batch_execute( "PRAGMA journal_mode = WAL; PRAGMA synchronous = NORMAL; PRAGMA foreign_keys = ON;", )?; run_migrations(conn)?; Ok(()) } pub fn initialize_pool(url: &str) -> Result>, DbError> { let manager = ConnectionManager::new(url); let pool = Pool::builder() .test_on_check_out(true) .build(manager) .expect("oops"); let mut conn = pool.get()?; initialize_db(&mut conn)?; Ok(pool) }