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

Optionally use unsafe SQLite optimisations during initial loading #63

Merged
merged 1 commit into from
Nov 3, 2023
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
10 changes: 9 additions & 1 deletion src/api_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,15 @@ use sqlx::{
use std::str::FromStr;

pub async fn run_api_tests(table: &str, database: &str) -> Result<(), sqlx::Error> {
let config = valve(table, database, &ValveCommand::Config, false, "table").await?;
let config = valve(
table,
database,
&ValveCommand::Config,
false,
false,
"table",
)
.await?;
let config: SerdeValue = serde_json::from_str(config.as_str()).unwrap();
let config = config.as_object().unwrap();

Expand Down
20 changes: 19 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -982,12 +982,16 @@ pub enum ValveCommand {
/// optionally create and/or load it according to the value of `command` (see [ValveCommand]).
/// If the `verbose` flag is set to true, output status messages while loading. If `config_table`
/// is given and `table_table` indicates a database, query the table called `config_table` for the
/// table table information. Returns the configuration map as a String.
/// table table information. Returns the configuration map as a String. If `initial_load` is set to
/// true, then (SQLite only) the database settings will be tuned for initial loading. Note that
/// these settings are unsafe and should be used for initial loading only, as data integrity will
/// not be guaranteed in the case of an interrupted transaction.
pub async fn valve(
table_table: &str,
database: &str,
command: &ValveCommand,
verbose: bool,
initial_load: bool,
config_table: &str,
) -> Result<String, sqlx::Error> {
let parser = StartParser::new();
Expand Down Expand Up @@ -1025,6 +1029,20 @@ pub async fn valve(
sqlx_query("PRAGMA foreign_keys = ON")
.execute(&pool)
.await?;
if initial_load {
// These pragmas are unsafe but they are used during initial loading since data
// integrity is not a priority in this case.
sqlx_query("PRAGMA journal_mode = OFF")
.execute(&pool)
.await?;
sqlx_query("PRAGMA synchronous = 0").execute(&pool).await?;
sqlx_query("PRAGMA cache_size = 1000000")
.execute(&pool)
.await?;
sqlx_query("PRAGMA temp_store = MEMORY")
.execute(&pool)
.await?;
}
}

let (sorted_table_list, constraints_config) = configure_db(
Expand Down
12 changes: 12 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ async fn main() -> Result<(), sqlx::Error> {
let mut create_only = false;
let mut config_table = String::new();
let mut verbose = false;
let mut initial_load = false;
let mut source = String::new();
let mut destination = String::new();

Expand Down Expand Up @@ -65,6 +66,14 @@ async fn main() -> Result<(), sqlx::Error> {
r#"Write the SQL used to create the database to stdout after configuring it, and then
while loading the database, write progress messages to stderr."#,
);
ap.refer(&mut initial_load).add_option(
&["--initial_load"],
StoreTrue,
r#"(SQLite only) When this flag is set, the database settings will be tuned for initial
loading. Note that these settings are unsafe and should be used for initial loading
only, as data integrity will not be guaranteed in the case of an interrupted
transaction."#,
);
ap.refer(&mut source).add_argument(
"SOURCE",
Store,
Expand Down Expand Up @@ -109,6 +118,7 @@ async fn main() -> Result<(), sqlx::Error> {
&String::from(":memory:"),
&ValveCommand::Config,
false,
false,
&config_table,
)
.await?;
Expand Down Expand Up @@ -141,6 +151,7 @@ async fn main() -> Result<(), sqlx::Error> {
&destination,
&ValveCommand::Create,
verbose,
false,
&config_table,
)
.await?;
Expand All @@ -150,6 +161,7 @@ async fn main() -> Result<(), sqlx::Error> {
&destination,
&ValveCommand::Load,
verbose,
initial_load,
&config_table,
)
.await?;
Expand Down