Skip to content

Commit f1284fc

Browse files
committed
Use WAL mode for sqlite
Hopefully this should help with performance
1 parent e286ec5 commit f1284fc

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/target
2-
/db.sqlite
2+
/db.sqlite*
33
/Rocket.toml
44
/.env
55
/apworlds

src/main.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::path::PathBuf;
22

33
use db::{DbInstrumentation, QUERY_HISTOGRAM};
4+
use diesel::connection::SimpleConnection;
45
use diesel::r2d2::Pool;
56
use diesel::sqlite::Sqlite;
67
use diesel::{r2d2::ConnectionManager, SqliteConnection};
@@ -114,6 +115,19 @@ impl<R: Handler + Clone> From<AdminOnlyRoute<R>> for Vec<Route> {
114115
struct AdminToken(String);
115116
struct APWorldPath(PathBuf);
116117

118+
#[derive(Debug)]
119+
struct SqliteCustomizer;
120+
121+
impl diesel::r2d2::CustomizeConnection<SqliteConnection, diesel::r2d2::Error> for SqliteCustomizer {
122+
fn on_acquire(&self, conn: &mut SqliteConnection) -> Result<(), diesel::r2d2::Error> {
123+
conn.batch_execute(
124+
"PRAGMA journal_mode = WAL; PRAGMA synchronous = NORMAL; PRAGMA busy_timeout = 1000;",
125+
)
126+
.map_err(diesel::r2d2::Error::QueryError)?;
127+
Ok(())
128+
}
129+
}
130+
117131
#[rocket::main]
118132
async fn main() -> anyhow::Result<()> {
119133
dotenv().ok();
@@ -127,7 +141,10 @@ async fn main() -> anyhow::Result<()> {
127141
.expect("Failed to set diesel instrumentation");
128142

129143
let manager = ConnectionManager::<SqliteConnection>::new(db_url);
130-
let db_pool = Pool::new(manager).expect("Failed to create database pool, aborting");
144+
let db_pool = Pool::builder()
145+
.connection_customizer(Box::new(SqliteCustomizer))
146+
.build(manager)
147+
.expect("Failed to create database pool, aborting");
131148
{
132149
let mut connection = db_pool
133150
.get()

0 commit comments

Comments
 (0)