mirror of
https://github.com/scottlamb/moonfire-nvr.git
synced 2025-12-02 06:07:51 -05:00
rustfmt everything
I want to make the project more accessible by not expecting folks to match my idiosyncratic style. Now almost [1] everything is written in the "standard" style. CI enforces this. [1] "Almost": I used #[rustfmt::skip] in a few sections where I felt aligning things in columns significantly improves readability.
This commit is contained in:
@@ -31,14 +31,13 @@
|
||||
/// Upgrades the database schema.
|
||||
///
|
||||
/// See `guide/schema.md` for more information.
|
||||
|
||||
use crate::db;
|
||||
use failure::{Error, bail};
|
||||
use failure::{bail, Error};
|
||||
use log::info;
|
||||
use std::ffi::CStr;
|
||||
use std::io::Write;
|
||||
use nix::NixPath;
|
||||
use rusqlite::params;
|
||||
use std::ffi::CStr;
|
||||
use std::io::Write;
|
||||
use uuid::Uuid;
|
||||
|
||||
mod v0_to_v1;
|
||||
@@ -59,10 +58,16 @@ pub struct Args<'a> {
|
||||
}
|
||||
|
||||
fn set_journal_mode(conn: &rusqlite::Connection, requested: &str) -> Result<(), Error> {
|
||||
assert!(!requested.contains(';')); // quick check for accidental sql injection.
|
||||
let actual = conn.query_row(&format!("pragma journal_mode = {}", requested), params![],
|
||||
|row| row.get::<_, String>(0))?;
|
||||
info!("...database now in journal_mode {} (requested {}).", actual, requested);
|
||||
assert!(!requested.contains(';')); // quick check for accidental sql injection.
|
||||
let actual = conn.query_row(
|
||||
&format!("pragma journal_mode = {}", requested),
|
||||
params![],
|
||||
|row| row.get::<_, String>(0),
|
||||
)?;
|
||||
info!(
|
||||
"...database now in journal_mode {} (requested {}).",
|
||||
actual, requested
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -78,24 +83,31 @@ fn upgrade(args: &Args, target_ver: i32, conn: &mut rusqlite::Connection) -> Res
|
||||
|
||||
{
|
||||
assert_eq!(upgraders.len(), db::EXPECTED_VERSION as usize);
|
||||
let old_ver =
|
||||
conn.query_row("select max(id) from version", params![],
|
||||
|row| row.get(0))?;
|
||||
let old_ver = conn.query_row("select max(id) from version", params![], |row| row.get(0))?;
|
||||
if old_ver > db::EXPECTED_VERSION {
|
||||
bail!("Database is at version {}, later than expected {}",
|
||||
old_ver, db::EXPECTED_VERSION);
|
||||
bail!(
|
||||
"Database is at version {}, later than expected {}",
|
||||
old_ver,
|
||||
db::EXPECTED_VERSION
|
||||
);
|
||||
} else if old_ver < 0 {
|
||||
bail!("Database is at negative version {}!", old_ver);
|
||||
}
|
||||
info!("Upgrading database from version {} to version {}...", old_ver, target_ver);
|
||||
for ver in old_ver .. target_ver {
|
||||
info!(
|
||||
"Upgrading database from version {} to version {}...",
|
||||
old_ver, target_ver
|
||||
);
|
||||
for ver in old_ver..target_ver {
|
||||
info!("...from version {} to version {}", ver, ver + 1);
|
||||
let tx = conn.transaction()?;
|
||||
upgraders[ver as usize](&args, &tx)?;
|
||||
tx.execute(r#"
|
||||
tx.execute(
|
||||
r#"
|
||||
insert into version (id, unix_time, notes)
|
||||
values (?, cast(strftime('%s', 'now') as int32), ?)
|
||||
"#, params![ver + 1, UPGRADE_NOTES])?;
|
||||
"#,
|
||||
params![ver + 1, UPGRADE_NOTES],
|
||||
)?;
|
||||
tx.commit()?;
|
||||
}
|
||||
}
|
||||
@@ -117,10 +129,12 @@ pub fn run(args: &Args, conn: &mut rusqlite::Connection) -> Result<(), Error> {
|
||||
// non-WAL mode. https://www.sqlite.org/wal.html
|
||||
if !args.no_vacuum {
|
||||
info!("...vacuuming database after upgrade.");
|
||||
conn.execute_batch(r#"
|
||||
conn.execute_batch(
|
||||
r#"
|
||||
pragma page_size = 16384;
|
||||
vacuum;
|
||||
"#)?;
|
||||
"#,
|
||||
)?;
|
||||
}
|
||||
|
||||
set_journal_mode(&conn, "wal")?;
|
||||
@@ -142,11 +156,17 @@ impl UuidPath {
|
||||
}
|
||||
|
||||
impl NixPath for UuidPath {
|
||||
fn is_empty(&self) -> bool { false }
|
||||
fn len(&self) -> usize { 36 }
|
||||
fn is_empty(&self) -> bool {
|
||||
false
|
||||
}
|
||||
fn len(&self) -> usize {
|
||||
36
|
||||
}
|
||||
|
||||
fn with_nix_path<T, F>(&self, f: F) -> Result<T, nix::Error>
|
||||
where F: FnOnce(&CStr) -> T {
|
||||
where
|
||||
F: FnOnce(&CStr) -> T,
|
||||
{
|
||||
let p = CStr::from_bytes_with_nul(&self.0[..]).expect("no interior nuls");
|
||||
Ok(f(p))
|
||||
}
|
||||
@@ -154,14 +174,13 @@ impl NixPath for UuidPath {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::compare;
|
||||
use crate::testutil;
|
||||
use failure::ResultExt;
|
||||
use fnv::FnvHashMap;
|
||||
use super::*;
|
||||
|
||||
const BAD_ANAMORPHIC_VIDEO_SAMPLE_ENTRY: &[u8] =
|
||||
b"\x00\x00\x00\x84\x61\x76\x63\x31\x00\x00\
|
||||
const BAD_ANAMORPHIC_VIDEO_SAMPLE_ENTRY: &[u8] = b"\x00\x00\x00\x84\x61\x76\x63\x31\x00\x00\
|
||||
\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\
|
||||
\x00\x00\x00\x00\x00\x00\x01\x40\x00\xf0\x00\x48\x00\x00\x00\x48\
|
||||
\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\
|
||||
@@ -209,29 +228,44 @@ mod tests {
|
||||
//let path = tmpdir.path().to_str().ok_or_else(|| format_err!("invalid UTF-8"))?.to_owned();
|
||||
let mut upgraded = new_conn()?;
|
||||
upgraded.execute_batch(include_str!("v0.sql"))?;
|
||||
upgraded.execute_batch(r#"
|
||||
upgraded.execute_batch(
|
||||
r#"
|
||||
insert into camera (id, uuid, short_name, description, host, username, password,
|
||||
main_rtsp_path, sub_rtsp_path, retain_bytes)
|
||||
values (1, zeroblob(16), 'test camera', 'desc', 'host', 'user', 'pass',
|
||||
'main', 'sub', 42);
|
||||
"#)?;
|
||||
upgraded.execute(r#"
|
||||
"#,
|
||||
)?;
|
||||
upgraded.execute(
|
||||
r#"
|
||||
insert into video_sample_entry (id, sha1, width, height, data)
|
||||
values (1, X'0000000000000000000000000000000000000000', 1920, 1080, ?);
|
||||
"#, params![testutil::TEST_VIDEO_SAMPLE_ENTRY_DATA])?;
|
||||
upgraded.execute(r#"
|
||||
"#,
|
||||
params![testutil::TEST_VIDEO_SAMPLE_ENTRY_DATA],
|
||||
)?;
|
||||
upgraded.execute(
|
||||
r#"
|
||||
insert into video_sample_entry (id, sha1, width, height, data)
|
||||
values (2, X'0000000000000000000000000000000000000001', 320, 240, ?);
|
||||
"#, params![BAD_ANAMORPHIC_VIDEO_SAMPLE_ENTRY])?;
|
||||
upgraded.execute(r#"
|
||||
"#,
|
||||
params![BAD_ANAMORPHIC_VIDEO_SAMPLE_ENTRY],
|
||||
)?;
|
||||
upgraded.execute(
|
||||
r#"
|
||||
insert into video_sample_entry (id, sha1, width, height, data)
|
||||
values (3, X'0000000000000000000000000000000000000002', 704, 480, ?);
|
||||
"#, params![GOOD_ANAMORPHIC_VIDEO_SAMPLE_ENTRY])?;
|
||||
upgraded.execute(r#"
|
||||
"#,
|
||||
params![GOOD_ANAMORPHIC_VIDEO_SAMPLE_ENTRY],
|
||||
)?;
|
||||
upgraded.execute(
|
||||
r#"
|
||||
insert into video_sample_entry (id, sha1, width, height, data)
|
||||
values (4, X'0000000000000000000000000000000000000003', 704, 480, ?);
|
||||
"#, params![GOOD_ANAMORPHIC_VIDEO_SAMPLE_ENTRY])?;
|
||||
upgraded.execute_batch(r#"
|
||||
"#,
|
||||
params![GOOD_ANAMORPHIC_VIDEO_SAMPLE_ENTRY],
|
||||
)?;
|
||||
upgraded.execute_batch(
|
||||
r#"
|
||||
insert into recording (id, camera_id, sample_file_bytes, start_time_90k, duration_90k,
|
||||
local_time_delta_90k, video_samples, video_sync_samples,
|
||||
video_sample_entry_id, sample_file_uuid, sample_file_sha1,
|
||||
@@ -244,7 +278,8 @@ mod tests {
|
||||
X'C94D4D0B533746059CD40B29039E641E', zeroblob(20), X'00');
|
||||
insert into reserved_sample_files values (X'51EF700C933E4197AAE4EE8161E94221', 0),
|
||||
(X'E69D45E8CBA64DC1BA2ECB1585983A10', 1);
|
||||
"#)?;
|
||||
"#,
|
||||
)?;
|
||||
let rec1 = tmpdir.path().join("e69d45e8-cba6-4dc1-ba2e-cb1585983a10");
|
||||
let rec2 = tmpdir.path().join("94de8484-ff87-4a52-95d4-88c8038a0312");
|
||||
let rec3 = tmpdir.path().join("c94d4d0b-5337-4605-9cd4-0b29039e641e");
|
||||
@@ -254,17 +289,24 @@ mod tests {
|
||||
std::fs::File::create(&rec3)?;
|
||||
std::fs::File::create(&garbage)?;
|
||||
|
||||
for (ver, fresh_sql) in &[(1, Some(include_str!("v1.sql"))),
|
||||
(2, None), // transitional; don't compare schemas.
|
||||
(3, Some(include_str!("v3.sql"))),
|
||||
(4, None), // transitional; don't compare schemas.
|
||||
(5, Some(include_str!("v5.sql"))),
|
||||
(6, Some(include_str!("../schema.sql")))] {
|
||||
upgrade(&Args {
|
||||
sample_file_dir: Some(&tmpdir.path()),
|
||||
preset_journal: "delete",
|
||||
no_vacuum: false,
|
||||
}, *ver, &mut upgraded).context(format!("upgrading to version {}", ver))?;
|
||||
for (ver, fresh_sql) in &[
|
||||
(1, Some(include_str!("v1.sql"))),
|
||||
(2, None), // transitional; don't compare schemas.
|
||||
(3, Some(include_str!("v3.sql"))),
|
||||
(4, None), // transitional; don't compare schemas.
|
||||
(5, Some(include_str!("v5.sql"))),
|
||||
(6, Some(include_str!("../schema.sql"))),
|
||||
] {
|
||||
upgrade(
|
||||
&Args {
|
||||
sample_file_dir: Some(&tmpdir.path()),
|
||||
preset_journal: "delete",
|
||||
no_vacuum: false,
|
||||
},
|
||||
*ver,
|
||||
&mut upgraded,
|
||||
)
|
||||
.context(format!("upgrading to version {}", ver))?;
|
||||
if let Some(f) = fresh_sql {
|
||||
compare(&upgraded, *ver, f)?;
|
||||
}
|
||||
@@ -277,14 +319,16 @@ mod tests {
|
||||
}
|
||||
if *ver == 6 {
|
||||
// Check that the pasp was set properly.
|
||||
let mut stmt = upgraded.prepare(r#"
|
||||
let mut stmt = upgraded.prepare(
|
||||
r#"
|
||||
select
|
||||
id,
|
||||
pasp_h_spacing,
|
||||
pasp_v_spacing
|
||||
from
|
||||
video_sample_entry
|
||||
"#)?;
|
||||
"#,
|
||||
)?;
|
||||
let mut rows = stmt.query(params![])?;
|
||||
let mut pasp_by_id = FnvHashMap::default();
|
||||
while let Some(row) = rows.next()? {
|
||||
|
||||
Reference in New Issue
Block a user