make moonfire-nvr sql enforce integrity

https://github.com/scottlamb/moonfire-nvr/issues/175#issuecomment-1008096087
This commit is contained in:
Scott Lamb 2022-01-08 10:49:50 -08:00
parent 0406e09ca4
commit 77775a82e5
2 changed files with 21 additions and 6 deletions

View File

@ -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(())
}

View File

@ -52,6 +52,12 @@ pub fn run(args: Args) -> Result<i32, Error> {
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()