2020-03-02 01:53:41 -05:00
|
|
|
// This file is part of Moonfire NVR, a security camera network video recorder.
|
2021-02-17 16:28:48 -05:00
|
|
|
// Copyright (C) 2020 The Moonfire NVR Authors; see AUTHORS and LICENSE.txt.
|
|
|
|
// SPDX-License-Identifier: GPL-v3.0-or-later WITH GPL-3.0-linking-exception.
|
2018-03-01 00:21:47 -05:00
|
|
|
|
2021-04-10 20:34:52 -04:00
|
|
|
//! Upgrades the database schema.
|
|
|
|
//!
|
|
|
|
//! See `guide/schema.md` for more information.
|
|
|
|
|
2018-12-28 13:21:49 -05:00
|
|
|
use crate::db;
|
2021-02-17 01:15:54 -05:00
|
|
|
use failure::{bail, Error};
|
2018-12-28 22:53:29 -05:00
|
|
|
use log::info;
|
2019-07-22 01:40:01 -04:00
|
|
|
use nix::NixPath;
|
2019-07-11 11:07:40 -04:00
|
|
|
use rusqlite::params;
|
2021-02-17 01:15:54 -05:00
|
|
|
use std::ffi::CStr;
|
|
|
|
use std::io::Write;
|
2019-07-22 01:40:01 -04:00
|
|
|
use uuid::Uuid;
|
2018-03-01 00:21:47 -05:00
|
|
|
|
|
|
|
mod v0_to_v1;
|
|
|
|
mod v1_to_v2;
|
|
|
|
mod v2_to_v3;
|
2019-06-06 19:18:13 -04:00
|
|
|
mod v3_to_v4;
|
2019-07-05 00:22:45 -04:00
|
|
|
mod v4_to_v5;
|
2020-03-20 00:35:42 -04:00
|
|
|
mod v5_to_v6;
|
2018-03-01 00:21:47 -05:00
|
|
|
|
|
|
|
const UPGRADE_NOTES: &'static str =
|
|
|
|
concat!("upgraded using moonfire-db ", env!("CARGO_PKG_VERSION"));
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Args<'a> {
|
2020-04-18 01:41:55 -04:00
|
|
|
pub sample_file_dir: Option<&'a std::path::Path>,
|
|
|
|
pub preset_journal: &'a str,
|
|
|
|
pub no_vacuum: bool,
|
2018-03-01 00:21:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn set_journal_mode(conn: &rusqlite::Connection, requested: &str) -> Result<(), Error> {
|
2021-02-17 01:15:54 -05:00
|
|
|
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
|
|
|
|
);
|
2018-03-01 00:21:47 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-07-11 11:07:40 -04:00
|
|
|
fn upgrade(args: &Args, target_ver: i32, conn: &mut rusqlite::Connection) -> Result<(), Error> {
|
2018-03-01 00:21:47 -05:00
|
|
|
let upgraders = [
|
2018-03-01 12:26:03 -05:00
|
|
|
v0_to_v1::run,
|
|
|
|
v1_to_v2::run,
|
|
|
|
v2_to_v3::run,
|
2019-06-06 19:18:13 -04:00
|
|
|
v3_to_v4::run,
|
2019-07-05 00:22:45 -04:00
|
|
|
v4_to_v5::run,
|
2020-03-20 00:35:42 -04:00
|
|
|
v5_to_v6::run,
|
2018-03-01 00:21:47 -05:00
|
|
|
];
|
|
|
|
|
|
|
|
{
|
|
|
|
assert_eq!(upgraders.len(), db::EXPECTED_VERSION as usize);
|
2021-02-17 01:15:54 -05:00
|
|
|
let old_ver = conn.query_row("select max(id) from version", params![], |row| row.get(0))?;
|
2018-03-01 00:21:47 -05:00
|
|
|
if old_ver > db::EXPECTED_VERSION {
|
2021-02-17 01:15:54 -05:00
|
|
|
bail!(
|
|
|
|
"Database is at version {}, later than expected {}",
|
|
|
|
old_ver,
|
|
|
|
db::EXPECTED_VERSION
|
|
|
|
);
|
2018-03-01 00:21:47 -05:00
|
|
|
} else if old_ver < 0 {
|
|
|
|
bail!("Database is at negative version {}!", old_ver);
|
|
|
|
}
|
2021-02-17 01:15:54 -05:00
|
|
|
info!(
|
|
|
|
"Upgrading database from version {} to version {}...",
|
|
|
|
old_ver, target_ver
|
|
|
|
);
|
|
|
|
for ver in old_ver..target_ver {
|
2018-03-01 00:21:47 -05:00
|
|
|
info!("...from version {} to version {}", ver, ver + 1);
|
|
|
|
let tx = conn.transaction()?;
|
2018-03-01 12:26:03 -05:00
|
|
|
upgraders[ver as usize](&args, &tx)?;
|
2021-02-17 01:15:54 -05:00
|
|
|
tx.execute(
|
|
|
|
r#"
|
2018-03-01 00:21:47 -05:00
|
|
|
insert into version (id, unix_time, notes)
|
|
|
|
values (?, cast(strftime('%s', 'now') as int32), ?)
|
2021-02-17 01:15:54 -05:00
|
|
|
"#,
|
|
|
|
params![ver + 1, UPGRADE_NOTES],
|
|
|
|
)?;
|
2018-03-01 00:21:47 -05:00
|
|
|
tx.commit()?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-11 11:07:40 -04:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn run(args: &Args, conn: &mut rusqlite::Connection) -> Result<(), Error> {
|
2020-06-08 16:35:45 -04:00
|
|
|
db::set_integrity_pragmas(conn)?;
|
2021-02-11 00:44:06 -05:00
|
|
|
set_journal_mode(&conn, args.preset_journal)?;
|
2019-07-11 11:07:40 -04:00
|
|
|
upgrade(args, db::EXPECTED_VERSION, conn)?;
|
2019-07-10 04:09:52 -04:00
|
|
|
|
2021-02-11 00:44:06 -05:00
|
|
|
// As in "moonfire-nvr init": try for page_size=16384 and wal for the reasons explained there.
|
|
|
|
//
|
|
|
|
// Do the vacuum prior to switching back to WAL for two reasons:
|
|
|
|
// * page_size only takes effect on a vacuum in non-WAL mode.
|
|
|
|
// https://www.sqlite.org/pragma.html#pragma_page_size
|
|
|
|
// * vacuum is a huge transaction, and on old versions of SQLite3, that's best done in
|
|
|
|
// non-WAL mode. https://www.sqlite.org/wal.html
|
2020-04-18 01:41:55 -04:00
|
|
|
if !args.no_vacuum {
|
2018-03-01 00:21:47 -05:00
|
|
|
info!("...vacuuming database after upgrade.");
|
2021-02-17 01:15:54 -05:00
|
|
|
conn.execute_batch(
|
|
|
|
r#"
|
2018-03-01 00:21:47 -05:00
|
|
|
pragma page_size = 16384;
|
|
|
|
vacuum;
|
2021-02-17 01:15:54 -05:00
|
|
|
"#,
|
|
|
|
)?;
|
2018-03-01 00:21:47 -05:00
|
|
|
}
|
2021-02-11 00:44:06 -05:00
|
|
|
|
|
|
|
set_journal_mode(&conn, "wal")?;
|
2018-03-01 00:21:47 -05:00
|
|
|
info!("...done.");
|
2019-07-11 11:07:40 -04:00
|
|
|
|
2018-03-01 00:21:47 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
2019-07-11 11:07:40 -04:00
|
|
|
|
2019-07-22 01:40:01 -04:00
|
|
|
/// A uuid-based path, as used in version 0 and version 1 schemas.
|
|
|
|
struct UuidPath([u8; 37]);
|
|
|
|
|
|
|
|
impl UuidPath {
|
|
|
|
pub(crate) fn from(uuid: Uuid) -> Self {
|
|
|
|
let mut buf = [0u8; 37];
|
|
|
|
write!(&mut buf[..36], "{}", uuid.to_hyphenated_ref())
|
|
|
|
.expect("can't format uuid to pathname buf");
|
|
|
|
UuidPath(buf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl NixPath for UuidPath {
|
2021-02-17 01:15:54 -05:00
|
|
|
fn is_empty(&self) -> bool {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
fn len(&self) -> usize {
|
|
|
|
36
|
|
|
|
}
|
2019-07-22 01:40:01 -04:00
|
|
|
|
|
|
|
fn with_nix_path<T, F>(&self, f: F) -> Result<T, nix::Error>
|
2021-02-17 01:15:54 -05:00
|
|
|
where
|
|
|
|
F: FnOnce(&CStr) -> T,
|
|
|
|
{
|
2019-07-22 01:40:01 -04:00
|
|
|
let p = CStr::from_bytes_with_nul(&self.0[..]).expect("no interior nuls");
|
|
|
|
Ok(f(p))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-11 11:07:40 -04:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2021-02-17 01:15:54 -05:00
|
|
|
use super::*;
|
2019-07-11 11:07:40 -04:00
|
|
|
use crate::compare;
|
2019-07-22 01:40:01 -04:00
|
|
|
use crate::testutil;
|
2020-04-18 01:41:55 -04:00
|
|
|
use failure::ResultExt;
|
2020-03-20 00:35:42 -04:00
|
|
|
use fnv::FnvHashMap;
|
2019-07-11 11:07:40 -04:00
|
|
|
|
2021-02-17 01:15:54 -05:00
|
|
|
const BAD_ANAMORPHIC_VIDEO_SAMPLE_ENTRY: &[u8] = b"\x00\x00\x00\x84\x61\x76\x63\x31\x00\x00\
|
2020-03-20 00:35:42 -04:00
|
|
|
\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\
|
|
|
|
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\
|
|
|
|
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x18\xff\xff\x00\x00\x00\x2e\
|
|
|
|
\x61\x76\x63\x43\x01\x4d\x40\x1e\xff\xe1\x00\x17\x67\x4d\x40\x1e\
|
|
|
|
\x9a\x66\x0a\x0f\xff\x35\x01\x01\x01\x40\x00\x00\xfa\x00\x00\x03\
|
|
|
|
\x01\xf4\x01\x01\x00\x04\x68\xee\x3c\x80";
|
|
|
|
|
|
|
|
const GOOD_ANAMORPHIC_VIDEO_SAMPLE_ENTRY: &[u8] =
|
|
|
|
b"\x00\x00\x00\x9f\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\
|
|
|
|
\x02\xc0\x01\xe0\x00\x48\x00\x00\x00\x48\x00\x00\x00\x00\x00\x00\
|
|
|
|
\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\
|
|
|
|
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\
|
|
|
|
\x00\x00\x00\x18\xff\xff\x00\x00\x00\x49\x61\x76\x63\x43\x01\x64\
|
|
|
|
\x00\x16\xff\xe1\x00\x31\x67\x64\x00\x16\xac\x1b\x1a\x80\xb0\x3d\
|
|
|
|
\xff\xff\x00\x28\x00\x21\x6e\x0c\x0c\x0c\x80\x00\x01\xf4\x00\x00\
|
|
|
|
\x27\x10\x74\x30\x07\xd0\x00\x07\xa1\x25\xde\x5c\x68\x60\x0f\xa0\
|
|
|
|
\x00\x0f\x42\x4b\xbc\xb8\x50\x01\x00\x05\x68\xee\x38\x30\x00";
|
|
|
|
|
2019-07-11 11:07:40 -04:00
|
|
|
fn new_conn() -> Result<rusqlite::Connection, Error> {
|
|
|
|
let conn = rusqlite::Connection::open_in_memory()?;
|
|
|
|
conn.execute("pragma foreign_keys = on", params![])?;
|
|
|
|
conn.execute("pragma fullfsync = on", params![])?;
|
|
|
|
conn.execute("pragma synchronous = 2", params![])?;
|
|
|
|
Ok(conn)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn compare(c: &rusqlite::Connection, ver: i32, fresh_sql: &str) -> Result<(), Error> {
|
|
|
|
let fresh = new_conn()?;
|
|
|
|
fresh.execute_batch(fresh_sql)?;
|
|
|
|
if let Some(diffs) = compare::get_diffs("upgraded", &c, "fresh", &fresh)? {
|
|
|
|
panic!("Version {}: differences found:\n{}", ver, diffs);
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Upgrades and compares schemas.
|
|
|
|
/// Doesn't (yet) compare any actual data.
|
|
|
|
#[test]
|
|
|
|
fn upgrade_and_compare() -> Result<(), Error> {
|
2019-07-22 01:40:01 -04:00
|
|
|
testutil::init();
|
|
|
|
let tmpdir = tempdir::TempDir::new("moonfire-nvr-test")?;
|
2020-04-18 01:41:55 -04:00
|
|
|
//let path = tmpdir.path().to_str().ok_or_else(|| format_err!("invalid UTF-8"))?.to_owned();
|
2019-07-11 11:07:40 -04:00
|
|
|
let mut upgraded = new_conn()?;
|
|
|
|
upgraded.execute_batch(include_str!("v0.sql"))?;
|
2021-02-17 01:15:54 -05:00
|
|
|
upgraded.execute_batch(
|
|
|
|
r#"
|
2019-07-22 01:40:01 -04:00
|
|
|
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);
|
2021-02-17 01:15:54 -05:00
|
|
|
"#,
|
|
|
|
)?;
|
|
|
|
upgraded.execute(
|
|
|
|
r#"
|
2019-07-22 01:40:01 -04:00
|
|
|
insert into video_sample_entry (id, sha1, width, height, data)
|
2020-03-20 23:52:30 -04:00
|
|
|
values (1, X'0000000000000000000000000000000000000000', 1920, 1080, ?);
|
2021-02-17 01:15:54 -05:00
|
|
|
"#,
|
|
|
|
params![testutil::TEST_VIDEO_SAMPLE_ENTRY_DATA],
|
|
|
|
)?;
|
|
|
|
upgraded.execute(
|
|
|
|
r#"
|
2020-03-20 00:35:42 -04:00
|
|
|
insert into video_sample_entry (id, sha1, width, height, data)
|
2020-03-20 23:52:30 -04:00
|
|
|
values (2, X'0000000000000000000000000000000000000001', 320, 240, ?);
|
2021-02-17 01:15:54 -05:00
|
|
|
"#,
|
|
|
|
params![BAD_ANAMORPHIC_VIDEO_SAMPLE_ENTRY],
|
|
|
|
)?;
|
|
|
|
upgraded.execute(
|
|
|
|
r#"
|
2020-03-20 00:35:42 -04:00
|
|
|
insert into video_sample_entry (id, sha1, width, height, data)
|
2020-03-20 23:52:30 -04:00
|
|
|
values (3, X'0000000000000000000000000000000000000002', 704, 480, ?);
|
2021-02-17 01:15:54 -05:00
|
|
|
"#,
|
|
|
|
params![GOOD_ANAMORPHIC_VIDEO_SAMPLE_ENTRY],
|
|
|
|
)?;
|
|
|
|
upgraded.execute(
|
|
|
|
r#"
|
2020-06-10 22:35:52 -04:00
|
|
|
insert into video_sample_entry (id, sha1, width, height, data)
|
|
|
|
values (4, X'0000000000000000000000000000000000000003', 704, 480, ?);
|
2021-02-17 01:15:54 -05:00
|
|
|
"#,
|
|
|
|
params![GOOD_ANAMORPHIC_VIDEO_SAMPLE_ENTRY],
|
|
|
|
)?;
|
|
|
|
upgraded.execute_batch(
|
|
|
|
r#"
|
2019-07-22 01:40:01 -04:00
|
|
|
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,
|
|
|
|
video_index)
|
|
|
|
values (1, 1, 42, 140063580000000, 90000, 0, 1, 1, 1,
|
2020-06-10 22:35:52 -04:00
|
|
|
X'E69D45E8CBA64DC1BA2ECB1585983A10', zeroblob(20), X'00'),
|
|
|
|
(2, 1, 42, 140063580090000, 90000, 0, 1, 1, 2,
|
|
|
|
X'94DE8484FF874A5295D488C8038A0312', zeroblob(20), X'00'),
|
|
|
|
(3, 1, 42, 140063580180000, 90000, 0, 1, 1, 3,
|
|
|
|
X'C94D4D0B533746059CD40B29039E641E', zeroblob(20), X'00');
|
2019-07-22 01:40:01 -04:00
|
|
|
insert into reserved_sample_files values (X'51EF700C933E4197AAE4EE8161E94221', 0),
|
|
|
|
(X'E69D45E8CBA64DC1BA2ECB1585983A10', 1);
|
2021-02-17 01:15:54 -05:00
|
|
|
"#,
|
|
|
|
)?;
|
2019-07-22 01:40:01 -04:00
|
|
|
let rec1 = tmpdir.path().join("e69d45e8-cba6-4dc1-ba2e-cb1585983a10");
|
2020-06-10 22:35:52 -04:00
|
|
|
let rec2 = tmpdir.path().join("94de8484-ff87-4a52-95d4-88c8038a0312");
|
|
|
|
let rec3 = tmpdir.path().join("c94d4d0b-5337-4605-9cd4-0b29039e641e");
|
2019-07-22 01:40:01 -04:00
|
|
|
let garbage = tmpdir.path().join("51ef700c-933e-4197-aae4-ee8161e94221");
|
|
|
|
std::fs::File::create(&rec1)?;
|
2020-06-10 22:35:52 -04:00
|
|
|
std::fs::File::create(&rec2)?;
|
|
|
|
std::fs::File::create(&rec3)?;
|
2019-07-22 01:40:01 -04:00
|
|
|
std::fs::File::create(&garbage)?;
|
2019-07-11 11:07:40 -04:00
|
|
|
|
2021-02-17 01:15:54 -05:00
|
|
|
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))?;
|
2019-07-11 11:07:40 -04:00
|
|
|
if let Some(f) = fresh_sql {
|
|
|
|
compare(&upgraded, *ver, f)?;
|
|
|
|
}
|
2019-07-25 01:18:44 -04:00
|
|
|
if *ver == 3 {
|
|
|
|
// Check that the garbage files is cleaned up properly, but also add it back
|
|
|
|
// to simulate a bug prior to 433be217. The v5 upgrade should take care of
|
|
|
|
// anything left over.
|
|
|
|
assert!(!garbage.exists());
|
|
|
|
std::fs::File::create(&garbage)?;
|
|
|
|
}
|
2020-03-20 00:35:42 -04:00
|
|
|
if *ver == 6 {
|
|
|
|
// Check that the pasp was set properly.
|
2021-02-17 01:15:54 -05:00
|
|
|
let mut stmt = upgraded.prepare(
|
|
|
|
r#"
|
2020-03-20 00:35:42 -04:00
|
|
|
select
|
|
|
|
id,
|
|
|
|
pasp_h_spacing,
|
|
|
|
pasp_v_spacing
|
|
|
|
from
|
|
|
|
video_sample_entry
|
2021-02-17 01:15:54 -05:00
|
|
|
"#,
|
|
|
|
)?;
|
2020-03-20 00:35:42 -04:00
|
|
|
let mut rows = stmt.query(params![])?;
|
|
|
|
let mut pasp_by_id = FnvHashMap::default();
|
|
|
|
while let Some(row) = rows.next()? {
|
|
|
|
let id: i32 = row.get(0)?;
|
|
|
|
let pasp_h_spacing: i32 = row.get(1)?;
|
|
|
|
let pasp_v_spacing: i32 = row.get(2)?;
|
|
|
|
pasp_by_id.insert(id, (pasp_h_spacing, pasp_v_spacing));
|
|
|
|
}
|
|
|
|
assert_eq!(pasp_by_id.get(&1), Some(&(1, 1)));
|
|
|
|
assert_eq!(pasp_by_id.get(&2), Some(&(4, 3)));
|
|
|
|
assert_eq!(pasp_by_id.get(&3), Some(&(40, 33)));
|
2020-06-10 22:35:52 -04:00
|
|
|
|
|
|
|
// No recording references this video_sample_entry, so it gets dropped on upgrade.
|
|
|
|
assert_eq!(pasp_by_id.get(&4), None);
|
2020-03-20 00:35:42 -04:00
|
|
|
}
|
2019-07-11 11:07:40 -04:00
|
|
|
}
|
2019-07-22 01:40:01 -04:00
|
|
|
|
|
|
|
// Check that recording files get renamed.
|
|
|
|
assert!(!rec1.exists());
|
|
|
|
assert!(tmpdir.path().join("0000000100000001").exists());
|
|
|
|
|
|
|
|
// Check that garbage files get cleaned up.
|
|
|
|
assert!(!garbage.exists());
|
|
|
|
|
2019-07-11 11:07:40 -04:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|