Skip to content

Commit

Permalink
Use WAL mode for sqlite
Browse files Browse the repository at this point in the history
Hopefully this should help with performance
  • Loading branch information
Eijebong committed May 21, 2024
1 parent e286ec5 commit f1284fc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/target
/db.sqlite
/db.sqlite*
/Rocket.toml
/.env
/apworlds
19 changes: 18 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::path::PathBuf;

use db::{DbInstrumentation, QUERY_HISTOGRAM};
use diesel::connection::SimpleConnection;
use diesel::r2d2::Pool;
use diesel::sqlite::Sqlite;
use diesel::{r2d2::ConnectionManager, SqliteConnection};
Expand Down Expand Up @@ -114,6 +115,19 @@ impl<R: Handler + Clone> From<AdminOnlyRoute<R>> for Vec<Route> {
struct AdminToken(String);
struct APWorldPath(PathBuf);

#[derive(Debug)]
struct SqliteCustomizer;

impl diesel::r2d2::CustomizeConnection<SqliteConnection, diesel::r2d2::Error> for SqliteCustomizer {
fn on_acquire(&self, conn: &mut SqliteConnection) -> Result<(), diesel::r2d2::Error> {
conn.batch_execute(
"PRAGMA journal_mode = WAL; PRAGMA synchronous = NORMAL; PRAGMA busy_timeout = 1000;",
)
.map_err(diesel::r2d2::Error::QueryError)?;
Ok(())
}
}

#[rocket::main]
async fn main() -> anyhow::Result<()> {
dotenv().ok();
Expand All @@ -127,7 +141,10 @@ async fn main() -> anyhow::Result<()> {
.expect("Failed to set diesel instrumentation");

let manager = ConnectionManager::<SqliteConnection>::new(db_url);
let db_pool = Pool::new(manager).expect("Failed to create database pool, aborting");
let db_pool = Pool::builder()
.connection_customizer(Box::new(SqliteCustomizer))
.build(manager)
.expect("Failed to create database pool, aborting");
{
let mut connection = db_pool
.get()
Expand Down

0 comments on commit f1284fc

Please sign in to comment.