Replies: 5 comments 5 replies
-
I have a similiar question: Is it possible to use an encrypted db with SqlitePool? |
Beta Was this translation helpful? Give feedback.
-
Hopefully the ability to use SqliteConnectOptions is added then, I find it quite surprising how little attention has been given to connecting to encrypted databases, id have thought it was a quite important detail but apparently not, no examples in Rocket or Sqlx. I only started using Rust a few days ago specifically to use Rocket as a server framework, so digging into generated docs for this has been a bit of a nightmare given my lack of Rust knowledge. |
Beta Was this translation helpful? Give feedback.
-
@wezm @joenano let options = SqliteConnectOptions::from_str(&url)?
.pragma("key", "password")
// other options, like synchronous land here
.foreign_keys(true);
let pool = SqlitePoolOptions::new().connect_with(options).await?; |
Beta Was this translation helpful? Give feedback.
-
I have the same question. I'm using Any ideas?
|
Beta Was this translation helpful? Give feedback.
-
I don't know if it is the "right" way... but you can still use the Example: use rocket_db_pools::{sqlx::{self, ConnectOptions}, Database, Pool};
use rocket::config::LogLevel;
use rocket::figment::Figment;
use std::time::Duration;
type Options<D> = <<D as sqlx::Database>::Connection as sqlx::Connection>::Options;
fn specialize(__options: &mut dyn std::any::Any, __config: &rocket_db_pools::Config) {
if let Some(o) = __options.downcast_mut::<sqlx::sqlite::SqliteConnectOptions>() {
*o = std::mem::take(o)
.busy_timeout(Duration::from_secs(__config.connect_timeout))
.create_if_missing(true);
}
}
// So we can set our own pragma
// I mean I don't need to but I want to make sure I can...
pub struct MyPool(sqlx::SqlitePool);
#[rocket::async_trait]
impl Pool for MyPool {
// Like everything(really everything(I mean not quite everything(but close))) relating to this is just yoinked from:
// https://api.rocket.rs/v0.5/src/rocket_db_pools/pool.rs#278
type Error = rocket_db_pools::Error<sqlx::Error>;
type Connection = sqlx::pool::PoolConnection<sqlx::Sqlite>;
async fn init(figment: &Figment) -> Result<Self, Self::Error> {
let config = figment.extract::<rocket_db_pools::Config>()?;
let mut opts = config
.url
.parse::<Options<sqlx::Sqlite>>()
.map_err(rocket_db_pools::Error::Init)?;
specialize(&mut opts, &config);
opts = opts.disable_statement_logging();
if let Ok(level) = figment.extract_inner::<LogLevel>(rocket::Config::LOG_LEVEL) {
if !matches!(level, LogLevel::Normal | LogLevel::Off) {
opts = opts
.log_statements(level.into())
.log_slow_statements(level.into(), Duration::default());
}
}
// custom options
let custom_opts = opts.pragma("foreign_keys", "OFF");
// again I have no use for this right now but meh
Ok(MyPool(
sqlx::pool::PoolOptions::new()
.max_connections(config.max_connections as u32)
.acquire_timeout(Duration::from_secs(config.connect_timeout))
.idle_timeout(config.idle_timeout.map(Duration::from_secs))
.min_connections(config.min_connections.unwrap_or_default())
.connect_with(custom_opts)
.await
.map_err(rocket_db_pools::Error::Init)?,
))
}
async fn get(&self) -> Result<Self::Connection, Self::Error> {
self.0.get().await
}
async fn close(&self) {
self.0.close().await;
}
}
#[derive(Database)]
#[database("db")]
pub struct MyDatabase(MyPool); |
Beta Was this translation helpful? Give feedback.
-
Specifically I'd like to be able to change the synchronous value.
Beta Was this translation helpful? Give feedback.
All reactions