Skip to content

Commit a811a18

Browse files
committed
Support sqlx_sqlite extensions in db_pools.
Resolves rwf2#2762.
1 parent fb39c02 commit a811a18

File tree

4 files changed

+26
-5
lines changed

4 files changed

+26
-5
lines changed

contrib/db_pools/lib/src/config.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@ use rocket::serde::{Deserialize, Serialize};
1515
/// [default.databases.db_name]
1616
/// url = "/path/to/db.sqlite"
1717
///
18-
/// # only `url` is required. `Initializer` provides defaults for the rest.
18+
/// # Only `url` is required. These have sane defaults and are optional.
1919
/// min_connections = 64
2020
/// max_connections = 1024
2121
/// connect_timeout = 5
2222
/// idle_timeout = 120
23+
///
24+
/// # This option is only supported by the `sqlx_sqlite` driver.
25+
/// extensions = ["memvfs", "rot13"]
2326
/// ```
2427
///
2528
/// Alternatively, a custom provider can be used. For example, a custom `Figment`
@@ -36,6 +39,7 @@ use rocket::serde::{Deserialize, Serialize};
3639
/// max_connections: 1024,
3740
/// connect_timeout: 3,
3841
/// idle_timeout: None,
42+
/// extensions: None,
3943
/// }));
4044
///
4145
/// rocket::custom(figment)
@@ -45,7 +49,8 @@ use rocket::serde::{Deserialize, Serialize};
4549
/// For general information on configuration in Rocket, see [`rocket::config`].
4650
/// For higher-level details on configuring a database, see the [crate-level
4751
/// docs](crate#configuration).
48-
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
52+
// NOTE: Defaults provided by the figment created in the `Initializer` fairing.
53+
#[derive(Default, Serialize, Deserialize, Debug, Clone, PartialEq)]
4954
#[serde(crate = "rocket::serde")]
5055
pub struct Config {
5156
/// Database-specific connection and configuration URL.
@@ -80,4 +85,11 @@ pub struct Config {
8085
///
8186
/// _Default:_ `None`.
8287
pub idle_timeout: Option<u64>,
88+
/// A list of database extensions to load at run-time.
89+
///
90+
/// **Note:** Only the `sqlx_sqlite` driver supports this option (for SQLite
91+
/// extensions) at this time. All other drivers ignore this option.
92+
///
93+
/// _Default:_ `None`.
94+
pub extensions: Option<Vec<String>>,
8395
}

contrib/db_pools/lib/src/database.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,8 @@ impl<D: Database> Fairing for Initializer<D> {
261261

262262
let figment = rocket.figment()
263263
.focus(&format!("databases.{}", D::NAME))
264-
.merge(Serialized::default("max_connections", workers * 4))
265-
.merge(Serialized::default("connect_timeout", 5));
264+
.join(Serialized::default("max_connections", workers * 4))
265+
.join(Serialized::default("connect_timeout", 5));
266266

267267
match <D::Pool>::init(&figment).await {
268268
Ok(pool) => Ok(rocket.manage(D::from(pool))),

contrib/db_pools/lib/src/lib.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -180,11 +180,14 @@
180180
//! [default.databases.db_name]
181181
//! url = "db.sqlite"
182182
//!
183-
//! # only `url` is required. the rest have defaults and are thus optional
183+
//! # Only `url` is required. These have sane defaults and are optional.
184184
//! min_connections = 64
185185
//! max_connections = 1024
186186
//! connect_timeout = 5
187187
//! idle_timeout = 120
188+
//!
189+
//! # This option is only supported by the `sqlx_sqlite` driver.
190+
//! extensions = ["memvfs", "rot13"]
188191
//! ```
189192
//!
190193
//! Or via environment variables:

contrib/db_pools/lib/src/pool.rs

+6
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,12 @@ mod sqlx {
281281
*o = std::mem::take(o)
282282
.busy_timeout(Duration::from_secs(__config.connect_timeout))
283283
.create_if_missing(true);
284+
285+
if let Some(ref exts) = __config.extensions {
286+
for ext in exts {
287+
*o = std::mem::take(o).extension(ext.clone());
288+
}
289+
}
284290
}
285291
}
286292

0 commit comments

Comments
 (0)