Skip to content

Commit

Permalink
Support sqlx_sqlite extensions in db_pools.
Browse files Browse the repository at this point in the history
Resolves #2762.
  • Loading branch information
SergioBenitez committed May 2, 2024
1 parent e6aaea0 commit 7203762
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
16 changes: 14 additions & 2 deletions contrib/db_pools/lib/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ use rocket::serde::{Deserialize, Serialize};
/// [default.databases.db_name]
/// url = "/path/to/db.sqlite"
///
/// # only `url` is required. `Initializer` provides defaults for the rest.
/// # Only `url` is required. You do not need to configure these.
/// min_connections = 64
/// max_connections = 1024
/// connect_timeout = 5
/// idle_timeout = 120
///
/// # this option is only supported by the `sqlx_sqlite` driver
/// extensions = ["memvfs", "rot13"]
/// ```
///
/// Alternatively, a custom provider can be used. For example, a custom `Figment`
Expand All @@ -36,6 +39,7 @@ use rocket::serde::{Deserialize, Serialize};
/// max_connections: 1024,
/// connect_timeout: 3,
/// idle_timeout: None,
/// extensions: None,
/// }));
///
/// rocket::custom(figment)
Expand All @@ -45,7 +49,8 @@ use rocket::serde::{Deserialize, Serialize};
/// For general information on configuration in Rocket, see [`rocket::config`].
/// For higher-level details on configuring a database, see the [crate-level
/// docs](crate#configuration).
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
// NOTE: Defaults provided by the figment created in the `Initializer` fairing.
#[derive(Default, Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(crate = "rocket::serde")]
pub struct Config {
/// Database-specific connection and configuration URL.
Expand Down Expand Up @@ -80,4 +85,11 @@ pub struct Config {
///
/// _Default:_ `None`.
pub idle_timeout: Option<u64>,
/// A list of database extensions to load at run-time.
///
/// **Note:** Only the `sqlx_sqlite` driver supports this option (for SQLite
/// extensions) at this time. All other drivers ignore this option.
///
/// _Default:_ `None`.
pub extensions: Option<Vec<String>>,
}
4 changes: 2 additions & 2 deletions contrib/db_pools/lib/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,8 @@ impl<D: Database> Fairing for Initializer<D> {

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

match <D::Pool>::init(&figment).await {
Ok(pool) => Ok(rocket.manage(D::from(pool))),
Expand Down
6 changes: 6 additions & 0 deletions contrib/db_pools/lib/src/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,12 @@ mod sqlx {
*o = std::mem::take(o)
.busy_timeout(Duration::from_secs(__config.connect_timeout))
.create_if_missing(true);

if let Some(ref exts) = __config.extensions {
for ext in exts {
*o = std::mem::take(o).extension(ext.clone());
}
}
}
}

Expand Down

0 comments on commit 7203762

Please sign in to comment.