Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support the immutable option on SQLite connections #1289

Merged
merged 2 commits into from
Sep 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions sqlx-core/src/sqlite/connection/establish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ pub(crate) async fn establish(options: &SqliteConnectOptions) -> Result<SqliteCo
})?
.to_owned();

filename.push('\0');

// By default, we connect to an in-memory database.
// [SQLITE_OPEN_NOMUTEX] will instruct [sqlite3_open_v2] to return an error if it
// cannot satisfy our wish for a thread-safe, lock-free connection object
Expand All @@ -55,6 +53,13 @@ pub(crate) async fn establish(options: &SqliteConnectOptions) -> Result<SqliteCo
SQLITE_OPEN_PRIVATECACHE
};

if options.immutable {
filename = format!("file:{}?immutable=true", filename);
flags |= libsqlite3_sys::SQLITE_OPEN_URI;
}

filename.push('\0');

let busy_timeout = options.busy_timeout;

let handle = blocking!({
Expand Down
7 changes: 7 additions & 0 deletions sqlx-core/src/sqlite/options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ pub struct SqliteConnectOptions {
pub(crate) synchronous: SqliteSynchronous,
pub(crate) auto_vacuum: SqliteAutoVacuum,
pub(crate) page_size: u32,
pub(crate) immutable: bool,
}

impl Default for SqliteConnectOptions {
Expand All @@ -88,6 +89,7 @@ impl SqliteConnectOptions {
synchronous: SqliteSynchronous::Full,
auto_vacuum: Default::default(),
page_size: 4096,
immutable: false,
}
}

Expand Down Expand Up @@ -191,6 +193,11 @@ impl SqliteConnectOptions {
self
}

pub fn immutable(mut self, immutable: bool) -> Self {
self.immutable = immutable;
self
}

/// Sets the log settings.
///
/// # Example
Expand Down
14 changes: 14 additions & 0 deletions sqlx-core/src/sqlite/options/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,20 @@ impl FromStr for SqliteConnectOptions {
}
},

"immutable" => match &*value {
"true" | "1" => {
options.immutable = true;
}
"false" | "0" => {
options.immutable = false;
}
_ => {
return Err(Error::Configuration(
format!("unknown value {:?} for `immutable`", value).into(),
));
}
},

_ => {
return Err(Error::Configuration(
format!(
Expand Down