diff --git a/server/db/db.rs b/server/db/db.rs index 5f28c8a..00e117e 100644 --- a/server/db/db.rs +++ b/server/db/db.rs @@ -2107,17 +2107,26 @@ impl LockedDatabase { } } -/// Sets pragmas for full database integrity. -pub(crate) fn set_integrity_pragmas(conn: &mut rusqlite::Connection) -> Result<(), Error> { +/// Pragmas for full database integrity. +/// +/// These are `pub` so that the `moonfire-nvr sql` command can pass to the SQLite3 binary with +/// `-cmd`. +pub static INTEGRITY_PRAGMAS: [&'static str; 3] = [ // Enforce foreign keys. This is on by default with --features=bundled (as rusqlite // compiles the SQLite3 amalgamation with -DSQLITE_DEFAULT_FOREIGN_KEYS=1). Ensure it's // always on. Note that our foreign keys are immediate rather than deferred, so we have to // be careful about the order of operations during the upgrade. - conn.execute("pragma foreign_keys = on", params![])?; - + "pragma foreign_keys = on", // Make the database actually durable. - conn.execute("pragma fullfsync = on", params![])?; - conn.execute("pragma synchronous = 3", params![])?; + "pragma fullfsync = on", + "pragma synchronous = 3", +]; + +/// Sets pragmas for full database integrity. +pub(crate) fn set_integrity_pragmas(conn: &mut rusqlite::Connection) -> Result<(), Error> { + for pragma in INTEGRITY_PRAGMAS { + conn.execute(pragma, params![])?; + } Ok(()) } diff --git a/server/src/cmds/sql.rs b/server/src/cmds/sql.rs index 3242c38..4a42e4b 100644 --- a/server/src/cmds/sql.rs +++ b/server/src/cmds/sql.rs @@ -52,6 +52,12 @@ pub fn run(args: Args) -> Result { db.push("?mode=ro"); } Err(Command::new("sqlite3") + .args( + db::db::INTEGRITY_PRAGMAS + .iter() + .map(|p| ["-cmd", p]) + .flatten(), + ) .arg(&db) .args(&args.arg) .exec()