Skip to content

Commit 4042fa3

Browse files
committed
fix: run_on_start command configuring SQLite pragmas
1 parent 5e0d66b commit 4042fa3

File tree

3 files changed

+20
-13
lines changed

3 files changed

+20
-13
lines changed

src/config.rs

+16-3
Original file line numberDiff line numberDiff line change
@@ -219,9 +219,22 @@ pub struct Database {
219219
#[serde(default)]
220220
pub dangerously_recreate: bool,
221221

222-
// sqlite configuration
223-
/// set the foreign key pragma to be enabled or disabled
224-
pub enable_foreign_keys: Option<bool>,
222+
// sqlite run on start commands
223+
/// this can be used to confiure PRAGMAs for SQLite where you can pass all values as a string.
224+
/// Default values are:
225+
///
226+
/// PRAGMA foreign_keys = ON;
227+
///
228+
/// PRAGMA journal_mode = WAL;
229+
///
230+
/// PRAGMA synchronous = NORMAL;
231+
///
232+
/// PRAGMA mmap_size = 134217728;
233+
///
234+
/// PRAGMA journal_size_limit = 67108864;
235+
///
236+
/// PRAGMA cache_size = 2000;
237+
pub run_on_start: Option<String>,
225238
}
226239

227240
#[derive(Debug, Clone, Deserialize, Serialize)]

src/db.rs

+3-9
Original file line numberDiff line numberDiff line change
@@ -150,24 +150,18 @@ pub async fn connect(config: &config::Database) -> Result<DbConn, sea_orm::DbErr
150150
let db = Database::connect(opt).await?;
151151

152152
if db.get_database_backend() == DatabaseBackend::Sqlite {
153-
// default set to "ON" to prevent breaking changes
154-
let foreign_keys = config
155-
.enable_foreign_keys
156-
.map(|fk| if fk { "ON" } else { "OFF" })
157-
.unwrap_or("ON");
158-
159153
db.execute(Statement::from_string(
160154
DatabaseBackend::Sqlite,
161-
format!(
155+
config.run_on_start.clone().unwrap_or(
162156
"
163-
PRAGMA foreign_keys = {foreign_keys};
157+
PRAGMA foreign_keys = ON;
164158
PRAGMA journal_mode = WAL;
165159
PRAGMA synchronous = NORMAL;
166160
PRAGMA mmap_size = 134217728;
167161
PRAGMA journal_size_limit = 67108864;
168162
PRAGMA cache_size = 2000;
169-
PRAGMA busy_timeout = 5000;
170163
"
164+
.to_string(),
171165
),
172166
))
173167
.await?;

src/tests_cfg/config.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ pub fn test_config() -> Config {
3636
auto_migrate: false,
3737
dangerously_truncate: false,
3838
dangerously_recreate: false,
39-
enable_foreign_keys: None,
39+
run_on_start: None,
4040
},
4141
queue: None,
4242
auth: None,

0 commit comments

Comments
 (0)